blob: 98711cc8656ac73eaaa810ac294e2f05bb1477b9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#if defined(POLARSSL_PLATFORM_C)
38#include "polarssl/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020039#else
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdlib.h>
45#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#include <basetsd.h>
49typedef UINT32 uint32_t;
50#else
51#include <inttypes.h>
52#endif
53
54#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakker34617722014-06-13 17:20:13 +020058#if defined(POLARSSL_SSL_SESSION_TICKETS)
59/* Implementation that should never be optimized out by the compiler */
60static void polarssl_zeroize( void *v, size_t n ) {
61 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
62}
63#endif
64
Paul Bakker0be444a2013-08-27 21:55:01 +020065#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010066static void ssl_write_hostname_ext( ssl_context *ssl,
67 unsigned char *buf,
68 size_t *olen )
69{
70 unsigned char *p = buf;
71
72 *olen = 0;
73
Paul Bakker66d5d072014-06-17 16:39:18 +020074 if( ssl->hostname == NULL )
Paul Bakkerd3edc862013-03-20 16:07:17 +010075 return;
76
77 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
78 ssl->hostname ) );
79
80 /*
81 * struct {
82 * NameType name_type;
83 * select (name_type) {
84 * case host_name: HostName;
85 * } name;
86 * } ServerName;
87 *
88 * enum {
89 * host_name(0), (255)
90 * } NameType;
91 *
92 * opaque HostName<1..2^16-1>;
93 *
94 * struct {
95 * ServerName server_name_list<1..2^16-1>
96 * } ServerNameList;
97 */
98 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
99 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
100
101 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
102 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
103
104 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
105 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
106
107 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
108 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
109 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
110
111 memcpy( p, ssl->hostname, ssl->hostname_len );
112
113 *olen = ssl->hostname_len + 9;
114}
Paul Bakker0be444a2013-08-27 21:55:01 +0200115#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100117#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118static void ssl_write_renegotiation_ext( ssl_context *ssl,
119 unsigned char *buf,
120 size_t *olen )
121{
122 unsigned char *p = buf;
123
124 *olen = 0;
125
126 if( ssl->renegotiation != SSL_RENEGOTIATION )
127 return;
128
129 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
130
131 /*
132 * Secure renegotiation
133 */
134 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
135 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
136
137 *p++ = 0x00;
138 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
139 *p++ = ssl->verify_data_len & 0xFF;
140
141 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
142
143 *olen = 5 + ssl->verify_data_len;
144}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100145#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100146
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100147/*
148 * Only if we handle at least one key exchange that needs signatures.
149 */
150#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
151 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100152static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
153 unsigned char *buf,
154 size_t *olen )
155{
156 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100157 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200158#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
159 unsigned char *sig_alg_list = buf + 6;
160#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100161
162 *olen = 0;
163
164 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
165 return;
166
167 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
168
169 /*
170 * Prepare signature_algorithms extension (TLS 1.2)
171 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200172#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200173#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100174 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
175 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
176 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
177 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
178#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200179#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100180 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
181 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
182 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
183 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
184#endif
185#if defined(POLARSSL_SHA1_C)
186 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
187 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
188#endif
189#if defined(POLARSSL_MD5_C)
190 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
191 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
192#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200193#endif /* POLARSSL_RSA_C */
194#if defined(POLARSSL_ECDSA_C)
195#if defined(POLARSSL_SHA512_C)
196 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
197 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
198 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
199 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
200#endif
201#if defined(POLARSSL_SHA256_C)
202 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
203 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
204 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
205 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
206#endif
207#if defined(POLARSSL_SHA1_C)
208 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
209 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
210#endif
211#if defined(POLARSSL_MD5_C)
212 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
213 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
214#endif
215#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100216
217 /*
218 * enum {
219 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
220 * sha512(6), (255)
221 * } HashAlgorithm;
222 *
223 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
224 * SignatureAlgorithm;
225 *
226 * struct {
227 * HashAlgorithm hash;
228 * SignatureAlgorithm signature;
229 * } SignatureAndHashAlgorithm;
230 *
231 * SignatureAndHashAlgorithm
232 * supported_signature_algorithms<2..2^16-2>;
233 */
234 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
235 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
236
237 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
238 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
239
240 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
241 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
242
Paul Bakkerd3edc862013-03-20 16:07:17 +0100243 *olen = 6 + sig_alg_len;
244}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100245#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
246 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100247
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200248#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100249static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
250 unsigned char *buf,
251 size_t *olen )
252{
253 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100254 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100255 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100256 const ecp_curve_info *info;
257#if defined(POLARSSL_SSL_SET_CURVES)
258 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100259#else
260 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100261#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100262
263 *olen = 0;
264
265 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
266
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100267#if defined(POLARSSL_SSL_SET_CURVES)
268 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200269 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100270 info = ecp_curve_info_from_grp_id( *grp_id );
271#else
272 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
273 {
274#endif
275
276 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
277 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200278 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200279
280 if( elliptic_curve_len == 0 )
281 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100282
283 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
284 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
285
286 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
287 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
288
289 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
290 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
291
Paul Bakkerd3edc862013-03-20 16:07:17 +0100292 *olen = 6 + elliptic_curve_len;
293}
294
295static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
296 unsigned char *buf,
297 size_t *olen )
298{
299 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200300 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100301
302 *olen = 0;
303
304 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
305
306 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
307 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
308
309 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100310 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200311
312 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100313 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
314
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200315 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100316}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200317#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100318
Paul Bakker05decb22013-08-15 13:33:48 +0200319#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200320static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
321 unsigned char *buf,
322 size_t *olen )
323{
324 unsigned char *p = buf;
325
326 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
327 *olen = 0;
328 return;
329 }
330
331 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
332
333 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
334 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
335
336 *p++ = 0x00;
337 *p++ = 1;
338
339 *p++ = ssl->mfl_code;
340
341 *olen = 5;
342}
Paul Bakker05decb22013-08-15 13:33:48 +0200343#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200344
Paul Bakker1f2bc622013-08-15 13:45:55 +0200345#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200346static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
347 unsigned char *buf, size_t *olen )
348{
349 unsigned char *p = buf;
350
351 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
352 {
353 *olen = 0;
354 return;
355 }
356
357 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
358
359 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
360 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
361
362 *p++ = 0x00;
363 *p++ = 0x00;
364
365 *olen = 4;
366}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200367#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200368
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100369#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
370static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
371 unsigned char *buf, size_t *olen )
372{
373 unsigned char *p = buf;
374
375 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
376 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
377 {
378 *olen = 0;
379 return;
380 }
381
382 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
383 "extension" ) );
384
385 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
386 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
387
388 *p++ = 0x00;
389 *p++ = 0x00;
390
391 *olen = 4;
392}
393#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
394
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200395#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
396static void ssl_write_extended_ms_ext( ssl_context *ssl,
397 unsigned char *buf, size_t *olen )
398{
399 unsigned char *p = buf;
400
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200401 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
402 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200403 {
404 *olen = 0;
405 return;
406 }
407
408 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
409 "extension" ) );
410
411 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
412 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
413
414 *p++ = 0x00;
415 *p++ = 0x00;
416
417 *olen = 4;
418}
419#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
420
Paul Bakkera503a632013-08-14 13:48:06 +0200421#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200422static void ssl_write_session_ticket_ext( ssl_context *ssl,
423 unsigned char *buf, size_t *olen )
424{
425 unsigned char *p = buf;
426 size_t tlen = ssl->session_negotiate->ticket_len;
427
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200428 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
429 {
430 *olen = 0;
431 return;
432 }
433
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200434 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
435
436 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
437 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
438
439 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
440 *p++ = (unsigned char)( ( tlen ) & 0xFF );
441
442 *olen = 4;
443
444 if( ssl->session_negotiate->ticket == NULL ||
445 ssl->session_negotiate->ticket_len == 0 )
446 {
447 return;
448 }
449
450 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
451
452 memcpy( p, ssl->session_negotiate->ticket, tlen );
453
454 *olen += tlen;
455}
Paul Bakkera503a632013-08-14 13:48:06 +0200456#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200457
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200458#if defined(POLARSSL_SSL_ALPN)
459static void ssl_write_alpn_ext( ssl_context *ssl,
460 unsigned char *buf, size_t *olen )
461{
462 unsigned char *p = buf;
463 const char **cur;
464
465 if( ssl->alpn_list == NULL )
466 {
467 *olen = 0;
468 return;
469 }
470
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200471 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200472
473 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
474 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
475
476 /*
477 * opaque ProtocolName<1..2^8-1>;
478 *
479 * struct {
480 * ProtocolName protocol_name_list<2..2^16-1>
481 * } ProtocolNameList;
482 */
483
484 /* Skip writing extension and list length for now */
485 p += 4;
486
487 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
488 {
489 *p = (unsigned char)( strlen( *cur ) & 0xFF );
490 memcpy( p + 1, *cur, *p );
491 p += 1 + *p;
492 }
493
494 *olen = p - buf;
495
496 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
497 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
498 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
499
500 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
501 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
502 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
503}
504#endif /* POLARSSL_SSL_ALPN */
505
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200506/*
507 * Generate random bytes for ClientHello
508 */
509static int ssl_generate_random( ssl_context *ssl )
510{
511 int ret;
512 unsigned char *p = ssl->handshake->randbytes;
513#if defined(POLARSSL_HAVE_TIME)
514 time_t t;
515#endif
516
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200517 /*
518 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
519 */
520#if defined(POLARSSL_SSL_PROTO_DTLS)
521 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
522 ssl->handshake->verify_cookie != NULL )
523 {
524 return( 0 );
525 }
526#endif
527
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200528#if defined(POLARSSL_HAVE_TIME)
529 t = time( NULL );
530 *p++ = (unsigned char)( t >> 24 );
531 *p++ = (unsigned char)( t >> 16 );
532 *p++ = (unsigned char)( t >> 8 );
533 *p++ = (unsigned char)( t );
534
535 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
536#else
537 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
538 return( ret );
539
540 p += 4;
541#endif /* POLARSSL_HAVE_TIME */
542
543 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
544 return( ret );
545
546 return( 0 );
547}
548
Paul Bakker5121ce52009-01-03 21:22:43 +0000549static int ssl_write_client_hello( ssl_context *ssl )
550{
Paul Bakker23986e52011-04-24 08:57:21 +0000551 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100552 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200554 unsigned char *p, *q;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200555 unsigned char offer_compress;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200556 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200557 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
560
Paul Bakkera9a028e2013-11-21 17:31:06 +0100561 if( ssl->f_rng == NULL )
562 {
563 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
564 return( POLARSSL_ERR_SSL_NO_RNG );
565 }
566
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100567#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000568 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100569#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000570 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000571 ssl->major_ver = ssl->min_major_ver;
572 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000573 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Paul Bakker490ecc82011-10-06 13:04:09 +0000575 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
576 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200577 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
578 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000579 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 /*
582 * 0 . 0 handshake type
583 * 1 . 3 handshake length
584 * 4 . 5 highest version supported
585 * 6 . 9 current UNIX time
586 * 10 . 37 random bytes
587 */
588 buf = ssl->out_msg;
589 p = buf + 4;
590
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100591 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
592 ssl->transport, p );
593 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
596 buf[4], buf[5] ) );
597
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200598 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
599 {
600 SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200601 return( ret );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200602 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200603
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200604 memcpy( p, ssl->handshake->randbytes, 32 );
605 SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
606 p += 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
608 /*
609 * 38 . 38 session id length
610 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100611 * 39+n . 39+n DTLS only: cookie length (1 byte)
612 * 40+n . .. DTSL only: cookie
613 * .. . .. ciphersuitelist length (2 bytes)
614 * .. . .. ciphersuitelist
615 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000616 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100617 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000618 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 */
Paul Bakker48916f92012-09-16 19:57:18 +0000620 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100622 if( n < 16 || n > 32 ||
623#if defined(POLARSSL_SSL_RENEGOTIATION)
624 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
625#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000626 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200627 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200629 }
630
Paul Bakkera503a632013-08-14 13:48:06 +0200631#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200632 /*
633 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
634 * generate and include a Session ID in the TLS ClientHello."
635 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100636#if defined(POLARSSL_SSL_RENEGOTIATION)
637 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
638#endif
639 if( ssl->session_negotiate->ticket != NULL &&
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200640 ssl->session_negotiate->ticket_len != 0 )
641 {
642 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
643
644 if( ret != 0 )
645 return( ret );
646
647 ssl->session_negotiate->length = n = 32;
648 }
Paul Bakkera503a632013-08-14 13:48:06 +0200649#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
651 *p++ = (unsigned char) n;
652
653 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000654 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
656 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
657 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
658
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100659 /*
660 * DTLS cookie
661 */
662#if defined(POLARSSL_SSL_PROTO_DTLS)
663 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
664 {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200665 if( ssl->handshake->verify_cookie == NULL )
666 {
667 SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
668 *p++ = 0;
669 }
670 else
671 {
672 SSL_DEBUG_BUF( 3, "client hello, cookie",
673 ssl->handshake->verify_cookie,
674 ssl->handshake->verify_cookie_len );
675
676 *p++ = ssl->handshake->verify_cookie_len;
677 memcpy( p, ssl->handshake->verify_cookie,
678 ssl->handshake->verify_cookie_len );
679 p += ssl->handshake->verify_cookie_len;
680 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100681 }
682#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Paul Bakker48916f92012-09-16 19:57:18 +0000684 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100685 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000686 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100687 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
688
689 /* Skip writing ciphersuite length for now */
690 n = 0;
691 q = p;
692 p += 2;
693
Paul Bakker48916f92012-09-16 19:57:18 +0000694 /*
695 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
696 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100697#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000698 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100699#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000700 {
701 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
702 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200703 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000704 }
705
Paul Bakker2fbefde2013-06-29 16:01:15 +0200706 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200708 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
709
710 if( ciphersuite_info == NULL )
711 continue;
712
713 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
714 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
715 continue;
716
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100717#if defined(POLARSSL_SSL_PROTO_DTLS)
718 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
719 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
720 continue;
721#endif
722
Paul Bakkere3166ce2011-01-27 17:40:50 +0000723 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200724 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
Paul Bakker2fbefde2013-06-29 16:01:15 +0200726 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200727 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
728 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 }
730
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200731 /* Some versions of OpenSSL don't handle it correctly if not at end */
732#if defined(POLARSSL_SSL_FALLBACK_SCSV)
733 if( ssl->fallback == SSL_IS_FALLBACK )
734 {
735 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
736 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
737 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
738 n++;
739 }
740#endif
741
Paul Bakker2fbefde2013-06-29 16:01:15 +0200742 *q++ = (unsigned char)( n >> 7 );
743 *q++ = (unsigned char)( n << 1 );
744
745 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
746
Paul Bakker2770fbd2012-07-03 13:30:23 +0000747#if defined(POLARSSL_ZLIB_SUPPORT)
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200748 offer_compress = 1;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000749#else
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200750 offer_compress = 0;
751#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200753 /*
754 * We don't support compression with DTLS right now: is many records come
755 * in the same datagram, uncompressing one could overwrite the next one.
756 * We don't want to add complexity for handling that case unless there is
757 * an actual need for it.
758 */
759#if defined(POLARSSL_SSL_PROTO_DTLS)
760 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
761 offer_compress = 0;
762#endif
763
764 if( offer_compress )
765 {
766 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
767 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
768 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
769
770 *p++ = 2;
771 *p++ = SSL_COMPRESS_DEFLATE;
772 *p++ = SSL_COMPRESS_NULL;
773 }
774 else
775 {
776 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
777 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
778 SSL_COMPRESS_NULL ) );
779
780 *p++ = 1;
781 *p++ = SSL_COMPRESS_NULL;
782 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000783
Paul Bakkerd3edc862013-03-20 16:07:17 +0100784 // First write extensions, then the total length
785 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200786#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100787 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
788 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200789#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100791#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100792 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
793 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100794#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000795
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100796#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
797 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100798 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
799 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200800#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000801
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200802#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100803 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
804 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100805
Paul Bakkerd3edc862013-03-20 16:07:17 +0100806 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
807 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100808#endif
809
Paul Bakker05decb22013-08-15 13:33:48 +0200810#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200811 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
812 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200813#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200814
Paul Bakker1f2bc622013-08-15 13:45:55 +0200815#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200816 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
817 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200818#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200819
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100820#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
821 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
822 ext_len += olen;
823#endif
824
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200825#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
826 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
827 ext_len += olen;
828#endif
829
Paul Bakkera503a632013-08-14 13:48:06 +0200830#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200831 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
832 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200833#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200834
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200835#if defined(POLARSSL_SSL_ALPN)
836 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
837 ext_len += olen;
838#endif
839
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100840 /* olen unused if all extensions are disabled */
841 ((void) olen);
842
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000843 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
844 ext_len ) );
845
Paul Bakkera7036632014-04-30 10:15:38 +0200846 if( ext_len > 0 )
847 {
848 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
849 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
850 p += ext_len;
851 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100852
Paul Bakker5121ce52009-01-03 21:22:43 +0000853 ssl->out_msglen = p - buf;
854 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
855 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
856
857 ssl->state++;
858
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +0200859#if defined(POLARSSL_SSL_PROTO_DTLS)
860 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
861 ssl_send_flight_completed( ssl );
862#endif
863
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 if( ( ret = ssl_write_record( ssl ) ) != 0 )
865 {
866 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
867 return( ret );
868 }
869
870 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
871
872 return( 0 );
873}
874
Paul Bakker48916f92012-09-16 19:57:18 +0000875static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200876 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000877 size_t len )
878{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000879 int ret;
880
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100881#if defined(POLARSSL_SSL_RENEGOTIATION)
882 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000883 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100884 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000885 if( len != 1 + ssl->verify_data_len * 2 ||
886 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100887 safer_memcmp( buf + 1,
888 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
889 safer_memcmp( buf + 1 + ssl->verify_data_len,
890 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000891 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100892 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000893
894 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
895 return( ret );
896
Paul Bakker48916f92012-09-16 19:57:18 +0000897 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
898 }
899 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100900 else
901#endif /* POLARSSL_SSL_RENEGOTIATION */
902 {
903 if( len != 1 || buf[0] != 0x00 )
904 {
905 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
906
907 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
908 return( ret );
909
910 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
911 }
912
913 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
914 }
Paul Bakker48916f92012-09-16 19:57:18 +0000915
916 return( 0 );
917}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200918
Paul Bakker05decb22013-08-15 13:33:48 +0200919#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200920static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200921 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200922 size_t len )
923{
924 /*
925 * server should use the extension only if we did,
926 * and if so the server's value should match ours (and len is always 1)
927 */
928 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
929 len != 1 ||
930 buf[0] != ssl->mfl_code )
931 {
932 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
933 }
934
935 return( 0 );
936}
Paul Bakker05decb22013-08-15 13:33:48 +0200937#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000938
Paul Bakker1f2bc622013-08-15 13:45:55 +0200939#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200940static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
941 const unsigned char *buf,
942 size_t len )
943{
944 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
945 len != 0 )
946 {
947 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
948 }
949
950 ((void) buf);
951
952 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
953
954 return( 0 );
955}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200956#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200957
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100958#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
959static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
960 const unsigned char *buf,
961 size_t len )
962{
963 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
964 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
965 len != 0 )
966 {
967 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
968 }
969
970 ((void) buf);
971
972 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
973
974 return( 0 );
975}
976#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
977
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200978#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
979static int ssl_parse_extended_ms_ext( ssl_context *ssl,
980 const unsigned char *buf,
981 size_t len )
982{
983 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200984 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200985 len != 0 )
986 {
987 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
988 }
989
990 ((void) buf);
991
992 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
993
994 return( 0 );
995}
996#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
997
Paul Bakkera503a632013-08-14 13:48:06 +0200998#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200999static int ssl_parse_session_ticket_ext( ssl_context *ssl,
1000 const unsigned char *buf,
1001 size_t len )
1002{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001003 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
1004 len != 0 )
1005 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001006 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001007 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001008
1009 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001010
1011 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001012
1013 return( 0 );
1014}
Paul Bakkera503a632013-08-14 13:48:06 +02001015#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001016
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001017#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001018static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
1019 const unsigned char *buf,
1020 size_t len )
1021{
1022 size_t list_size;
1023 const unsigned char *p;
1024
1025 list_size = buf[0];
1026 if( list_size + 1 != len )
1027 {
1028 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1029 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1030 }
1031
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001032 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001033 while( list_size > 0 )
1034 {
1035 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
1036 p[0] == POLARSSL_ECP_PF_COMPRESSED )
1037 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001038 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001039 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1040 return( 0 );
1041 }
1042
1043 list_size--;
1044 p++;
1045 }
1046
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +02001047 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1048 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001049}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001050#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001051
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001052#if defined(POLARSSL_SSL_ALPN)
1053static int ssl_parse_alpn_ext( ssl_context *ssl,
1054 const unsigned char *buf, size_t len )
1055{
1056 size_t list_len, name_len;
1057 const char **p;
1058
1059 /* If we didn't send it, the server shouldn't send it */
1060 if( ssl->alpn_list == NULL )
1061 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1062
1063 /*
1064 * opaque ProtocolName<1..2^8-1>;
1065 *
1066 * struct {
1067 * ProtocolName protocol_name_list<2..2^16-1>
1068 * } ProtocolNameList;
1069 *
1070 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1071 */
1072
1073 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1074 if( len < 4 )
1075 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1076
1077 list_len = ( buf[0] << 8 ) | buf[1];
1078 if( list_len != len - 2 )
1079 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1080
1081 name_len = buf[2];
1082 if( name_len != list_len - 1 )
1083 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1084
1085 /* Check that the server chosen protocol was in our list and save it */
1086 for( p = ssl->alpn_list; *p != NULL; p++ )
1087 {
1088 if( name_len == strlen( *p ) &&
1089 memcmp( buf + 3, *p, name_len ) == 0 )
1090 {
1091 ssl->alpn_chosen = *p;
1092 return( 0 );
1093 }
1094 }
1095
1096 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1097}
1098#endif /* POLARSSL_SSL_ALPN */
1099
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001100/*
1101 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1102 */
1103#if defined(POLARSSL_SSL_PROTO_DTLS)
1104static int ssl_parse_hello_verify_request( ssl_context *ssl )
1105{
Manuel Pégourié-Gonnard069eb792014-09-10 20:08:29 +02001106 const unsigned char *p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001107 int major_ver, minor_ver;
1108 unsigned char cookie_len;
1109
1110 SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
1111
1112 /*
1113 * struct {
1114 * ProtocolVersion server_version;
1115 * opaque cookie<0..2^8-1>;
1116 * } HelloVerifyRequest;
1117 */
1118 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
1119 ssl_read_version( &major_ver, &minor_ver, ssl->transport, p );
1120 p += 2;
1121
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02001122 /*
1123 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1124 * even is lower than our min version.
1125 */
1126 if( major_ver < SSL_MAJOR_VERSION_3 ||
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001127 minor_ver < SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02001128 major_ver > ssl->max_major_ver ||
1129 minor_ver > ssl->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001130 {
1131 SSL_DEBUG_MSG( 1, ( "bad server version" ) );
1132
1133 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1134 SSL_ALERT_MSG_PROTOCOL_VERSION );
1135
1136 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1137 }
1138
1139 cookie_len = *p++;
1140 SSL_DEBUG_BUF( 3, "cookie", (unsigned char *) p, cookie_len );
1141
1142 polarssl_free( ssl->handshake->verify_cookie );
1143
1144 ssl->handshake->verify_cookie = polarssl_malloc( cookie_len );
1145 if( ssl->handshake->verify_cookie == NULL )
1146 {
1147 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", cookie_len ) );
1148 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1149 }
1150
1151 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
1152 ssl->handshake->verify_cookie_len = cookie_len;
1153
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001154 /* Start over at ClientHello */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001155 ssl->state = SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001156 ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001157
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001158 ssl_recv_flight_completed( ssl );
1159
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001160 SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
1161
1162 return( 0 );
1163}
1164#endif /* POLARSSL_SSL_PROTO_DTLS */
1165
Paul Bakker5121ce52009-01-03 21:22:43 +00001166static int ssl_parse_server_hello( ssl_context *ssl )
1167{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001168 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001169 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001170 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001171 unsigned char *buf, *ext;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001172 unsigned char comp, accept_comp;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001173#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001174 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001175#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001176 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001177#if defined(POLARSSL_DEBUG_C)
1178 uint32_t t;
1179#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001180
1181 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1182
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 buf = ssl->in_msg;
1184
1185 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1186 {
1187 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1188 return( ret );
1189 }
1190
1191 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1192 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001193#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001194 if( ssl->renegotiation == SSL_RENEGOTIATION )
1195 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001196 ssl->renego_records_seen++;
1197
1198 if( ssl->renego_max_records >= 0 &&
1199 ssl->renego_records_seen > ssl->renego_max_records )
1200 {
1201 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1202 "but not honored by server" ) );
1203 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1204 }
1205
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001206 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1207 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1208 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001209#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001210
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001212 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 }
1214
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001215#if defined(POLARSSL_SSL_PROTO_DTLS)
1216 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1217 {
1218 if( buf[0] == SSL_HS_HELLO_VERIFY_REQUEST )
1219 {
1220 SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1221 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1222 return( ssl_parse_hello_verify_request( ssl ) );
1223 }
1224 else
1225 {
1226 /* We made it through the verification process */
1227 polarssl_free( ssl->handshake->verify_cookie );
1228 ssl->handshake->verify_cookie = NULL;
1229 ssl->handshake->verify_cookie_len = 0;
1230 }
1231 }
1232#endif /* POLARSSL_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001233
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001234 if( ssl->in_hslen < 38 + ssl_hs_hdr_len( ssl ) ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001235 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 {
1237 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001238 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 }
1240
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001241 /*
1242 * 0 . 1 server_version
1243 * 2 . 33 random (maybe including 4 bytes of Unix time)
1244 * 34 . 34 session_id length = n
1245 * 35 . 34+n session_id
1246 * 35+n . 36+n cipher_suite
1247 * 37+n . 37+n compression_method
1248 *
1249 * 38+n . 39+n extensions length (optional)
1250 * 40+n . .. extensions
1251 */
1252 buf += ssl_hs_hdr_len( ssl );
1253
1254 SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001255 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001256 ssl->transport, buf + 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001258 if( ssl->major_ver < ssl->min_major_ver ||
1259 ssl->minor_ver < ssl->min_minor_ver ||
1260 ssl->major_ver > ssl->max_major_ver ||
1261 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001262 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001263 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
1264 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1265 ssl->min_major_ver, ssl->min_minor_ver,
1266 ssl->major_ver, ssl->minor_ver,
1267 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001268
1269 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1270 SSL_ALERT_MSG_PROTOCOL_VERSION );
1271
1272 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1273 }
1274
Paul Bakker1504af52012-02-11 16:17:43 +00001275#if defined(POLARSSL_DEBUG_C)
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001276 t = ( (uint32_t) buf[2] << 24 )
1277 | ( (uint32_t) buf[3] << 16 )
1278 | ( (uint32_t) buf[4] << 8 )
1279 | ( (uint32_t) buf[5] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001280 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001281#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001282
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001283 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001285 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001287 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001288
Paul Bakker48916f92012-09-16 19:57:18 +00001289 if( n > 32 )
1290 {
1291 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1292 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1293 }
1294
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001295 if( ssl->in_hslen > 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001297 ext_len = ( ( buf[38 + n] << 8 )
1298 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Paul Bakker48916f92012-09-16 19:57:18 +00001300 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001301 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001302 {
1303 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1304 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 }
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001307 else if( ssl->in_hslen == 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001308 {
1309 ext_len = 0;
1310 }
1311 else
1312 {
1313 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1314 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001317 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001318 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001319
1320 /*
1321 * Read and check compression
1322 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001323 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001325#if defined(POLARSSL_ZLIB_SUPPORT)
1326 accept_comp = 1;
1327#else
1328 accept_comp = 0;
1329#endif
1330
1331 /* See comments in ssl_write_client_hello() */
1332#if defined(POLARSSL_SSL_PROTO_DTLS)
1333 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1334 accept_comp = 0;
1335#endif
1336
1337 if( ( accept_comp == 0 && comp != SSL_COMPRESS_NULL ) ||
1338 ( comp != SSL_COMPRESS_NULL && comp != SSL_COMPRESS_DEFLATE ) )
1339 {
1340 SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
1341 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1342 }
1343
Paul Bakker380da532012-04-18 16:10:25 +00001344 /*
1345 * Initialize update checksum functions
1346 */
Paul Bakker68884e32013-01-07 18:20:04 +01001347 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1348
1349 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1350 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001351 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001352 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1353 }
Paul Bakker380da532012-04-18 16:10:25 +00001354
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001355 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1356
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001358 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
1360 /*
1361 * Check if the session can be resumed
1362 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001363 if( ssl->handshake->resume == 0 || n == 0 ||
1364#if defined(POLARSSL_SSL_RENEGOTIATION)
1365 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1366#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001367 ssl->session_negotiate->ciphersuite != i ||
1368 ssl->session_negotiate->compression != comp ||
1369 ssl->session_negotiate->length != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001370 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 {
1372 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001373 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001374#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001375 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001376#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001377 ssl->session_negotiate->ciphersuite = i;
1378 ssl->session_negotiate->compression = comp;
1379 ssl->session_negotiate->length = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001380 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 }
1382 else
1383 {
1384 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001385
1386 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1387 {
1388 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1389 return( ret );
1390 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 }
1392
1393 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001394 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001395
Paul Bakkere3166ce2011-01-27 17:40:50 +00001396 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001397 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
1399 i = 0;
1400 while( 1 )
1401 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001402 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 {
1404 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001405 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 }
1407
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001408 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1409 ssl->session_negotiate->ciphersuite )
1410 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001412 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 }
1414
Paul Bakker2770fbd2012-07-03 13:30:23 +00001415 if( comp != SSL_COMPRESS_NULL
1416#if defined(POLARSSL_ZLIB_SUPPORT)
1417 && comp != SSL_COMPRESS_DEFLATE
1418#endif
1419 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 {
1421 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001422 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 }
Paul Bakker48916f92012-09-16 19:57:18 +00001424 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001426 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001427
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001428 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1429
Paul Bakker48916f92012-09-16 19:57:18 +00001430 while( ext_len )
1431 {
1432 unsigned int ext_id = ( ( ext[0] << 8 )
1433 | ( ext[1] ) );
1434 unsigned int ext_size = ( ( ext[2] << 8 )
1435 | ( ext[3] ) );
1436
1437 if( ext_size + 4 > ext_len )
1438 {
1439 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1440 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1441 }
1442
1443 switch( ext_id )
1444 {
1445 case TLS_EXT_RENEGOTIATION_INFO:
1446 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001447#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001448 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001449#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001450
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001451 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1452 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001453 return( ret );
1454
1455 break;
1456
Paul Bakker05decb22013-08-15 13:33:48 +02001457#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001458 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1459 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1460
1461 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1462 ext + 4, ext_size ) ) != 0 )
1463 {
1464 return( ret );
1465 }
1466
1467 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001468#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001469
Paul Bakker1f2bc622013-08-15 13:45:55 +02001470#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001471 case TLS_EXT_TRUNCATED_HMAC:
1472 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1473
1474 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1475 ext + 4, ext_size ) ) != 0 )
1476 {
1477 return( ret );
1478 }
1479
1480 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001481#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001482
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001483#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1484 case TLS_EXT_ENCRYPT_THEN_MAC:
1485 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1486
1487 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1488 ext + 4, ext_size ) ) != 0 )
1489 {
1490 return( ret );
1491 }
1492
1493 break;
1494#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1495
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001496#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1497 case TLS_EXT_EXTENDED_MASTER_SECRET:
1498 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1499
1500 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1501 ext + 4, ext_size ) ) != 0 )
1502 {
1503 return( ret );
1504 }
1505
1506 break;
1507#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1508
Paul Bakkera503a632013-08-14 13:48:06 +02001509#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001510 case TLS_EXT_SESSION_TICKET:
1511 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1512
1513 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1514 ext + 4, ext_size ) ) != 0 )
1515 {
1516 return( ret );
1517 }
1518
1519 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001520#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001521
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001522#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001523 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1524 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1525
1526 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1527 ext + 4, ext_size ) ) != 0 )
1528 {
1529 return( ret );
1530 }
1531
1532 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001533#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001534
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001535#if defined(POLARSSL_SSL_ALPN)
1536 case TLS_EXT_ALPN:
1537 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1538
1539 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1540 return( ret );
1541
1542 break;
1543#endif /* POLARSSL_SSL_ALPN */
1544
Paul Bakker48916f92012-09-16 19:57:18 +00001545 default:
1546 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1547 ext_id ) );
1548 }
1549
1550 ext_len -= 4 + ext_size;
1551 ext += 4 + ext_size;
1552
1553 if( ext_len > 0 && ext_len < 4 )
1554 {
1555 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1556 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1557 }
1558 }
1559
1560 /*
1561 * Renegotiation security checks
1562 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001563 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1564 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001565 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001566 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1567 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001568 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001569#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001570 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1571 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1572 renegotiation_info_seen == 0 )
1573 {
1574 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1575 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001576 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001577 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1578 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1579 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001580 {
1581 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001582 handshake_failure = 1;
1583 }
1584 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1585 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1586 renegotiation_info_seen == 1 )
1587 {
1588 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1589 handshake_failure = 1;
1590 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001591#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001592
1593 if( handshake_failure == 1 )
1594 {
1595 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1596 return( ret );
1597
Paul Bakker48916f92012-09-16 19:57:18 +00001598 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001600
1601 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1602
1603 return( 0 );
1604}
1605
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001606#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1607 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001608static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1609 unsigned char *end )
1610{
1611 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1612
Paul Bakker29e1f122013-04-16 13:07:56 +02001613 /*
1614 * Ephemeral DH parameters:
1615 *
1616 * struct {
1617 * opaque dh_p<1..2^16-1>;
1618 * opaque dh_g<1..2^16-1>;
1619 * opaque dh_Ys<1..2^16-1>;
1620 * } ServerDHParams;
1621 */
1622 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1623 {
1624 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1625 return( ret );
1626 }
1627
1628 if( ssl->handshake->dhm_ctx.len < 64 ||
1629 ssl->handshake->dhm_ctx.len > 512 )
1630 {
1631 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1632 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1633 }
1634
1635 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1636 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1637 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001638
1639 return( ret );
1640}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001641#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1642 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001643
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001644#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001645 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001646 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1647 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1648 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1649static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1650{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001651 const ecp_curve_info *curve_info;
1652
1653 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1654 if( curve_info == NULL )
1655 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001656 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1657 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001658 }
1659
1660 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001661
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001662#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1663 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1664#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001665 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1666 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001667#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001668 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001669
1670 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1671
1672 return( 0 );
1673}
Paul Bakker9af723c2014-05-01 13:03:14 +02001674#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1675 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1676 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1677 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1678 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001679
1680#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1681 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001682 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001683static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1684 unsigned char **p,
1685 unsigned char *end )
1686{
1687 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1688
Paul Bakker29e1f122013-04-16 13:07:56 +02001689 /*
1690 * Ephemeral ECDH parameters:
1691 *
1692 * struct {
1693 * ECParameters curve_params;
1694 * ECPoint public;
1695 * } ServerECDHParams;
1696 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001697 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1698 (const unsigned char **) p, end ) ) != 0 )
1699 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001700 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001701 return( ret );
1702 }
1703
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001704 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001705 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001706 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001707 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1708 }
1709
Paul Bakker29e1f122013-04-16 13:07:56 +02001710 return( ret );
1711}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001712#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001713 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1714 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001715
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001716#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001717static int ssl_parse_server_psk_hint( ssl_context *ssl,
1718 unsigned char **p,
1719 unsigned char *end )
1720{
1721 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001722 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001723 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001724
1725 /*
1726 * PSK parameters:
1727 *
1728 * opaque psk_identity_hint<0..2^16-1>;
1729 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001730 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001731 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001732
1733 if( (*p) + len > end )
1734 {
1735 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1736 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1737 }
1738
1739 // TODO: Retrieve PSK identity hint and callback to app
1740 //
1741 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001742 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001743
1744 return( ret );
1745}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001746#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001747
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001748#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1749 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1750/*
1751 * Generate a pre-master secret and encrypt it with the server's RSA key
1752 */
1753static int ssl_write_encrypted_pms( ssl_context *ssl,
1754 size_t offset, size_t *olen,
1755 size_t pms_offset )
1756{
1757 int ret;
1758 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1759 unsigned char *p = ssl->handshake->premaster + pms_offset;
1760
1761 /*
1762 * Generate (part of) the pre-master as
1763 * struct {
1764 * ProtocolVersion client_version;
1765 * opaque random[46];
1766 * } PreMasterSecret;
1767 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001768 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1769 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001770
1771 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1772 {
1773 SSL_DEBUG_RET( 1, "f_rng", ret );
1774 return( ret );
1775 }
1776
1777 ssl->handshake->pmslen = 48;
1778
1779 /*
1780 * Now write it out, encrypted
1781 */
1782 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1783 POLARSSL_PK_RSA ) )
1784 {
1785 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1786 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1787 }
1788
1789 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1790 p, ssl->handshake->pmslen,
1791 ssl->out_msg + offset + len_bytes, olen,
1792 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1793 ssl->f_rng, ssl->p_rng ) ) != 0 )
1794 {
1795 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1796 return( ret );
1797 }
1798
1799#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1800 defined(POLARSSL_SSL_PROTO_TLS1_2)
1801 if( len_bytes == 2 )
1802 {
1803 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1804 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1805 *olen += 2;
1806 }
1807#endif
1808
1809 return( 0 );
1810}
1811#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1812 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001813
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001814#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001815#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001816 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1817 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001818static int ssl_parse_signature_algorithm( ssl_context *ssl,
1819 unsigned char **p,
1820 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001821 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001822 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001823{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001824 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001825 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001826 *pk_alg = POLARSSL_PK_NONE;
1827
1828 /* Only in TLS 1.2 */
1829 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1830 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001831 return( 0 );
1832 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001833
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001834 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001835 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1836
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001837 /*
1838 * Get hash algorithm
1839 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001840 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001841 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001842 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1843 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001844 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1845 }
1846
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001847 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001848 * Get signature algorithm
1849 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001850 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001851 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001852 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1853 "SignatureAlgorithm %d", (*p)[1] ) );
1854 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001855 }
1856
1857 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1858 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1859 *p += 2;
1860
1861 return( 0 );
1862}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001863#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001864 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1865 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001866#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001867
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001868
1869#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1870 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1871static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1872{
1873 int ret;
1874 const ecp_keypair *peer_key;
1875
1876 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1877 POLARSSL_PK_ECKEY ) )
1878 {
1879 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1880 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1881 }
1882
1883 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1884
1885 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1886 POLARSSL_ECDH_THEIRS ) ) != 0 )
1887 {
1888 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1889 return( ret );
1890 }
1891
1892 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1893 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001894 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001895 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1896 }
1897
1898 return( ret );
1899}
1900#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1901 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1902
Paul Bakker41c83d32013-03-20 14:39:14 +01001903static int ssl_parse_server_key_exchange( ssl_context *ssl )
1904{
Paul Bakker23986e52011-04-24 08:57:21 +00001905 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001906 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001907 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001908
1909 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1910
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001911#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001912 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 {
1914 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1915 ssl->state++;
1916 return( 0 );
1917 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001918 ((void) p);
1919 ((void) end);
1920#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001921
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001922#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1923 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1924 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1925 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1926 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001927 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1928 {
1929 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1930 return( ret );
1931 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001932
1933 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1934 ssl->state++;
1935 return( 0 );
1936 }
1937 ((void) p);
1938 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001939#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1940 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001941
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1943 {
1944 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1945 return( ret );
1946 }
1947
1948 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1949 {
1950 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001951 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 }
1953
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001954 /*
1955 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1956 * doesn't use a psk_identity_hint
1957 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1959 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001960 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1961 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001962 {
1963 ssl->record_read = 1;
1964 goto exit;
1965 }
1966
1967 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1968 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 }
1970
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001971 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001972 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001973 SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001974
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001975#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1976 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1977 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1978 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1979 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1980 {
1981 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1982 {
1983 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1984 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1985 }
1986 } /* FALLTROUGH */
1987#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1988
1989#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1990 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1991 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1992 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1993 ; /* nothing more to do */
1994 else
1995#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1996 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1997#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1998 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1999 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2000 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002002 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002003 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002004 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002005 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2006 }
2007 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002008 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002009#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2010 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002011#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002012 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002013 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2014 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002015 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002016 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002017 {
2018 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2019 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002020 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2021 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2022 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002023 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002024 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002025#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002026 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002027 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002028 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002029 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002030 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002031 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002032
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002033#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002034 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2035 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02002036 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002037 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2038 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002039 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002040 size_t sig_len, hashlen;
2041 unsigned char hash[64];
2042 md_type_t md_alg = POLARSSL_MD_NONE;
2043 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002044 unsigned char *params = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002045 size_t params_len = p - params;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002046
Paul Bakker29e1f122013-04-16 13:07:56 +02002047 /*
2048 * Handle the digitally-signed structure
2049 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002050#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2051 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002052 {
Paul Bakker9659dae2013-08-28 16:21:34 +02002053 if( ssl_parse_signature_algorithm( ssl, &p, end,
2054 &md_alg, &pk_alg ) != 0 )
2055 {
2056 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2057 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2058 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002059
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002060 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002061 {
2062 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2063 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2064 }
2065 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002066 else
Paul Bakker9af723c2014-05-01 13:03:14 +02002067#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002068#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2069 defined(POLARSSL_SSL_PROTO_TLS1_1)
2070 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002071 {
2072 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002073
Paul Bakker9659dae2013-08-28 16:21:34 +02002074 /* Default hash for ECDSA is SHA-1 */
2075 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
2076 md_alg = POLARSSL_MD_SHA1;
2077 }
2078 else
2079#endif
2080 {
2081 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002082 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02002083 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002084
2085 /*
2086 * Read signature
2087 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002088 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002089 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002090
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002091 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002092 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002093 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01002094 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002097 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002098
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002099 /*
2100 * Compute the hash that has been signed
2101 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002102#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2103 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002104 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002105 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002106 md5_context md5;
2107 sha1_context sha1;
2108
Paul Bakker5b4af392014-06-26 12:09:34 +02002109 md5_init( &md5 );
2110 sha1_init( &sha1 );
2111
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002112 hashlen = 36;
2113
Paul Bakker29e1f122013-04-16 13:07:56 +02002114 /*
2115 * digitally-signed struct {
2116 * opaque md5_hash[16];
2117 * opaque sha_hash[20];
2118 * };
2119 *
2120 * md5_hash
2121 * MD5(ClientHello.random + ServerHello.random
2122 * + ServerParams);
2123 * sha_hash
2124 * SHA(ClientHello.random + ServerHello.random
2125 * + ServerParams);
2126 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002127 md5_starts( &md5 );
2128 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002129 md5_update( &md5, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002130 md5_finish( &md5, hash );
2131
2132 sha1_starts( &sha1 );
2133 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002134 sha1_update( &sha1, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002135 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02002136
2137 md5_free( &md5 );
2138 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02002139 }
2140 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002141#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2142 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002143#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2144 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002145 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002146 {
2147 md_context_t ctx;
2148
Paul Bakker84bbeb52014-07-01 14:53:22 +02002149 md_init( &ctx );
2150
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002151 /* Info from md_alg will be used instead */
2152 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02002153
2154 /*
2155 * digitally-signed struct {
2156 * opaque client_random[32];
2157 * opaque server_random[32];
2158 * ServerDHParams params;
2159 * };
2160 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002161 if( ( ret = md_init_ctx( &ctx,
2162 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002163 {
2164 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2165 return( ret );
2166 }
2167
2168 md_starts( &ctx );
2169 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002170 md_update( &ctx, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002171 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002172 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002173 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002174 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002175#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2176 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002177 {
Paul Bakker577e0062013-08-28 11:57:20 +02002178 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002179 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002180 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002181
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002182 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2183 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002184
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002185 /*
2186 * Verify signature
2187 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002188 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002189 {
2190 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2191 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2192 }
2193
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002194 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2195 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002196 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002197 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002198 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002201#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002202 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2203 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002205exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 ssl->state++;
2207
2208 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2209
2210 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002211}
2212
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002213#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2214 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2215 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2216 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2217static int ssl_parse_certificate_request( ssl_context *ssl )
2218{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002219 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2220
2221 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2222
2223 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2224 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2225 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2226 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2227 {
2228 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2229 ssl->state++;
2230 return( 0 );
2231 }
2232
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002233 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2234 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002235}
2236#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002237static int ssl_parse_certificate_request( ssl_context *ssl )
2238{
2239 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002240 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002241 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002242 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002243 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002244
2245 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2246
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002247 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2248 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2249 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2250 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2251 {
2252 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2253 ssl->state++;
2254 return( 0 );
2255 }
2256
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002257 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002259 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2260 {
2261 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2262 return( ret );
2263 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002264
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002265 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2266 {
2267 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2268 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2269 }
2270
2271 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 }
2273
2274 ssl->client_auth = 0;
2275 ssl->state++;
2276
2277 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2278 ssl->client_auth++;
2279
2280 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2281 ssl->client_auth ? "a" : "no" ) );
2282
Paul Bakker926af752012-11-23 13:38:07 +01002283 if( ssl->client_auth == 0 )
2284 goto exit;
2285
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002286 ssl->record_read = 0;
2287
Paul Bakker926af752012-11-23 13:38:07 +01002288 // TODO: handshake_failure alert for an anonymous server to request
2289 // client authentication
2290
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002291 /*
2292 * struct {
2293 * ClientCertificateType certificate_types<1..2^8-1>;
2294 * SignatureAndHashAlgorithm
2295 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2296 * DistinguishedName certificate_authorities<0..2^16-1>;
2297 * } CertificateRequest;
2298 */
Paul Bakker926af752012-11-23 13:38:07 +01002299 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002300
Paul Bakker926af752012-11-23 13:38:07 +01002301 // Retrieve cert types
2302 //
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002303 cert_type_len = buf[ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002304 n = cert_type_len;
2305
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002306 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002307 {
2308 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2309 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2310 }
2311
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002312 p = buf + ssl_hs_hdr_len( ssl ) + 1;
Paul Bakker926af752012-11-23 13:38:07 +01002313 while( cert_type_len > 0 )
2314 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002315#if defined(POLARSSL_RSA_C)
2316 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002317 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002318 {
2319 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2320 break;
2321 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002322 else
2323#endif
2324#if defined(POLARSSL_ECDSA_C)
2325 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002326 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002327 {
2328 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2329 break;
2330 }
2331 else
2332#endif
2333 {
2334 ; /* Unsupported cert type, ignore */
2335 }
Paul Bakker926af752012-11-23 13:38:07 +01002336
2337 cert_type_len--;
2338 p++;
2339 }
2340
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002341#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002342 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2343 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002344 /* Ignored, see comments about hash in write_certificate_verify */
2345 // TODO: should check the signature part against our pk_key though
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002346 size_t sig_alg_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2347 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002348
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002349 p = buf + ssl_hs_hdr_len( ssl ) + 3 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002350 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002351 n += sig_alg_len;
2352
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002353 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002354 {
2355 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2356 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2357 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002358 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002359#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002360
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002361 /* Ignore certificate_authorities, we only have one cert anyway */
2362 // TODO: should not send cert if no CA matches
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002363 dn_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + m + n] << 8 )
2364 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002365
2366 n += dn_len;
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002367 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 3 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002368 {
2369 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2370 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2371 }
2372
2373exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2375
2376 return( 0 );
2377}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002378#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2379 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2380 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2381 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
2383static int ssl_parse_server_hello_done( ssl_context *ssl )
2384{
2385 int ret;
2386
2387 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2388
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002389 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 {
2391 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2392 {
2393 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2394 return( ret );
2395 }
2396
2397 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2398 {
2399 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002400 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 }
2402 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002403 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002404
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002405 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) ||
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2407 {
2408 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002409 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 }
2411
2412 ssl->state++;
2413
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002414#if defined(POLARSSL_SSL_PROTO_DTLS)
2415 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2416 ssl_recv_flight_completed( ssl );
2417#endif
2418
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2420
2421 return( 0 );
2422}
2423
2424static int ssl_write_client_key_exchange( ssl_context *ssl )
2425{
Paul Bakker23986e52011-04-24 08:57:21 +00002426 int ret;
2427 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002428 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
2430 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2431
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002432#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002433 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 /*
2436 * DHM key exchange -- send G^X mod P
2437 */
Paul Bakker48916f92012-09-16 19:57:18 +00002438 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002439
2440 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2441 ssl->out_msg[5] = (unsigned char)( n );
2442 i = 6;
2443
Paul Bakker29b64762012-09-25 09:36:44 +00002444 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002445 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 &ssl->out_msg[i], n,
2447 ssl->f_rng, ssl->p_rng );
2448 if( ret != 0 )
2449 {
2450 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2451 return( ret );
2452 }
2453
Paul Bakker48916f92012-09-16 19:57:18 +00002454 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2455 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002457 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
Paul Bakker48916f92012-09-16 19:57:18 +00002459 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2460 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002461 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002462 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 {
2464 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2465 return( ret );
2466 }
2467
Paul Bakker48916f92012-09-16 19:57:18 +00002468 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 }
2470 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002471#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002472#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002473 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2474 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2475 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002476 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002477 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2478 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2479 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002480 {
2481 /*
2482 * ECDH key exchange -- send client public value
2483 */
2484 i = 4;
2485
2486 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2487 &n,
2488 &ssl->out_msg[i], 1000,
2489 ssl->f_rng, ssl->p_rng );
2490 if( ret != 0 )
2491 {
2492 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2493 return( ret );
2494 }
2495
2496 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2497
2498 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2499 &ssl->handshake->pmslen,
2500 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002501 POLARSSL_MPI_MAX_SIZE,
2502 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002503 {
2504 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2505 return( ret );
2506 }
2507
2508 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2509 }
2510 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002511#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002512 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2513 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2514 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002515#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002516 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002517 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002518 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2519 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002520 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002521 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002522 * opaque psk_identity<0..2^16-1>;
2523 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002524 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002525 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2526
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002527 i = 4;
2528 n = ssl->psk_identity_len;
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;
2557 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2558 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002559
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002560 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002561 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002562 &ssl->out_msg[i], n,
2563 ssl->f_rng, ssl->p_rng );
2564 if( ret != 0 )
2565 {
2566 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2567 return( ret );
2568 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002569 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002570 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002571#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002572#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002573 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002574 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002575 /*
2576 * ClientECDiffieHellmanPublic public;
2577 */
2578 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2579 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2580 ssl->f_rng, ssl->p_rng );
2581 if( ret != 0 )
2582 {
2583 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2584 return( ret );
2585 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002586
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002587 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2588 }
2589 else
2590#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2591 {
2592 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002593 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002594 }
2595
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002596 if( ( ret = ssl_psk_derive_premaster( ssl,
2597 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002598 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002599 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002600 return( ret );
2601 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002602 }
2603 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002604#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002605#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002606 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002607 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002608 i = 4;
2609 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002610 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 }
Paul Bakkered27a042013-04-18 22:46:23 +02002612 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002613#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002614 {
2615 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002616 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002617 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002618 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 ssl->out_msglen = i + n;
2621 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2622 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2623
2624 ssl->state++;
2625
2626 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2627 {
2628 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2629 return( ret );
2630 }
2631
2632 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2633
2634 return( 0 );
2635}
2636
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002637#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2638 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002639 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2640 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002641static int ssl_write_certificate_verify( ssl_context *ssl )
2642{
Paul Bakkered27a042013-04-18 22:46:23 +02002643 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002644 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
2646 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2647
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002648 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2649 {
2650 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2651 return( ret );
2652 }
2653
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002654 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002655 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002656 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002657 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002658 {
2659 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2660 ssl->state++;
2661 return( 0 );
2662 }
2663
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002664 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2665 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002666}
2667#else
2668static int ssl_write_certificate_verify( ssl_context *ssl )
2669{
2670 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2671 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2672 size_t n = 0, offset = 0;
2673 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002674 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002675 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002676 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002677
2678 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2679
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002680 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2681 {
2682 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2683 return( ret );
2684 }
2685
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002686 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002687 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002688 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002689 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2690 {
2691 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2692 ssl->state++;
2693 return( 0 );
2694 }
2695
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002696 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 {
2698 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2699 ssl->state++;
2700 return( 0 );
2701 }
2702
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002703 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002705 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2706 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 }
2708
2709 /*
2710 * Make an RSA signature of the handshake digests
2711 */
Paul Bakker48916f92012-09-16 19:57:18 +00002712 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002714#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2715 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002716 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002717 {
Paul Bakker926af752012-11-23 13:38:07 +01002718 /*
2719 * digitally-signed struct {
2720 * opaque md5_hash[16];
2721 * opaque sha_hash[20];
2722 * };
2723 *
2724 * md5_hash
2725 * MD5(handshake_messages);
2726 *
2727 * sha_hash
2728 * SHA(handshake_messages);
2729 */
2730 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002731 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002732
2733 /*
2734 * For ECDSA, default hash is SHA-1 only
2735 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002736 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002737 {
2738 hash_start += 16;
2739 hashlen -= 16;
2740 md_alg = POLARSSL_MD_SHA1;
2741 }
Paul Bakker926af752012-11-23 13:38:07 +01002742 }
2743 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002744#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2745 POLARSSL_SSL_PROTO_TLS1_1 */
2746#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2747 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002748 {
2749 /*
2750 * digitally-signed struct {
2751 * opaque handshake_messages[handshake_messages_length];
2752 * };
2753 *
2754 * Taking shortcut here. We assume that the server always allows the
2755 * PRF Hash function and has sent it in the allowed signature
2756 * algorithms list received in the Certificate Request message.
2757 *
2758 * Until we encounter a server that does not, we will take this
2759 * shortcut.
2760 *
2761 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2762 * in order to satisfy 'weird' needs from the server side.
2763 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002764 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2765 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002766 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002767 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002768 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002769 }
2770 else
2771 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002772 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002773 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002774 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002775 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002776
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002777 /* Info from md_alg will be used instead */
2778 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002779 offset = 2;
2780 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002781 else
2782#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002783 {
2784 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002785 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002786 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002787
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002788 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002789 ssl->out_msg + 6 + offset, &n,
2790 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002791 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002792 SSL_DEBUG_RET( 1, "pk_sign", ret );
2793 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002794 }
Paul Bakker926af752012-11-23 13:38:07 +01002795
Paul Bakker1ef83d62012-04-11 12:09:53 +00002796 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2797 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002798
Paul Bakker1ef83d62012-04-11 12:09:53 +00002799 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2801 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2802
2803 ssl->state++;
2804
2805 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2806 {
2807 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2808 return( ret );
2809 }
2810
2811 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2812
Paul Bakkered27a042013-04-18 22:46:23 +02002813 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002814}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002815#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2816 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2817 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002818
Paul Bakkera503a632013-08-14 13:48:06 +02002819#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002820static int ssl_parse_new_session_ticket( ssl_context *ssl )
2821{
2822 int ret;
2823 uint32_t lifetime;
2824 size_t ticket_len;
2825 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002826 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002827
2828 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2829
2830 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2831 {
2832 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2833 return( ret );
2834 }
2835
2836 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2837 {
2838 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2839 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2840 }
2841
2842 /*
2843 * struct {
2844 * uint32 ticket_lifetime_hint;
2845 * opaque ticket<0..2^16-1>;
2846 * } NewSessionTicket;
2847 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002848 * 0 . 3 ticket_lifetime_hint
2849 * 4 . 5 ticket_len (n)
2850 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002851 */
2852 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002853 ssl->in_hslen < 6 + ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002854 {
2855 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2856 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2857 }
2858
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002859 msg = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002860
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002861 lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
2862 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002863
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002864 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
2865
2866 if( ticket_len + 6 + ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002867 {
2868 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2869 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2870 }
2871
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002872 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2873
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002874 /* We're not waiting for a NewSessionTicket message any more */
2875 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002876 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002877
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002878 /*
2879 * Zero-length ticket means the server changed his mind and doesn't want
2880 * to send a ticket after all, so just forget it
2881 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002882 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002883 return( 0 );
2884
Paul Bakker34617722014-06-13 17:20:13 +02002885 polarssl_zeroize( ssl->session_negotiate->ticket,
2886 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002887 polarssl_free( ssl->session_negotiate->ticket );
2888 ssl->session_negotiate->ticket = NULL;
2889 ssl->session_negotiate->ticket_len = 0;
2890
2891 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2892 {
2893 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2894 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2895 }
2896
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002897 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002898
2899 ssl->session_negotiate->ticket = ticket;
2900 ssl->session_negotiate->ticket_len = ticket_len;
2901 ssl->session_negotiate->ticket_lifetime = lifetime;
2902
2903 /*
2904 * RFC 5077 section 3.4:
2905 * "If the client receives a session ticket from the server, then it
2906 * discards any Session ID that was sent in the ServerHello."
2907 */
2908 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2909 ssl->session_negotiate->length = 0;
2910
2911 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2912
2913 return( 0 );
2914}
Paul Bakkera503a632013-08-14 13:48:06 +02002915#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002916
Paul Bakker5121ce52009-01-03 21:22:43 +00002917/*
Paul Bakker1961b702013-01-25 14:49:24 +01002918 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 */
Paul Bakker1961b702013-01-25 14:49:24 +01002920int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002921{
2922 int ret = 0;
2923
Paul Bakker1961b702013-01-25 14:49:24 +01002924 if( ssl->state == SSL_HANDSHAKE_OVER )
2925 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Paul Bakker1961b702013-01-25 14:49:24 +01002927 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2928
2929 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2930 return( ret );
2931
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002932#if defined(POLARSSL_SSL_PROTO_DTLS)
2933 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2934 ssl->handshake != NULL &&
2935 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2936 {
2937 if( ( ret = ssl_resend( ssl ) ) != 0 )
2938 return( ret );
2939 }
2940#endif
2941
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002942 /* Change state now, so that it is right in ssl_read_record(), used
2943 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
2944#if defined(POLARSSL_SSL_SESSION_TICKETS)
2945 if( ssl->state == SSL_SERVER_CHANGE_CIPHER_SPEC &&
2946 ssl->handshake->new_session_ticket != 0 )
2947 {
2948 ssl->state = SSL_SERVER_NEW_SESSION_TICKET;
2949 }
2950#endif
2951
Paul Bakker1961b702013-01-25 14:49:24 +01002952 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 {
Paul Bakker1961b702013-01-25 14:49:24 +01002954 case SSL_HELLO_REQUEST:
2955 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002956 break;
2957
Paul Bakker1961b702013-01-25 14:49:24 +01002958 /*
2959 * ==> ClientHello
2960 */
2961 case SSL_CLIENT_HELLO:
2962 ret = ssl_write_client_hello( ssl );
2963 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
Paul Bakker1961b702013-01-25 14:49:24 +01002965 /*
2966 * <== ServerHello
2967 * Certificate
2968 * ( ServerKeyExchange )
2969 * ( CertificateRequest )
2970 * ServerHelloDone
2971 */
2972 case SSL_SERVER_HELLO:
2973 ret = ssl_parse_server_hello( ssl );
2974 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Paul Bakker1961b702013-01-25 14:49:24 +01002976 case SSL_SERVER_CERTIFICATE:
2977 ret = ssl_parse_certificate( ssl );
2978 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
Paul Bakker1961b702013-01-25 14:49:24 +01002980 case SSL_SERVER_KEY_EXCHANGE:
2981 ret = ssl_parse_server_key_exchange( ssl );
2982 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002983
Paul Bakker1961b702013-01-25 14:49:24 +01002984 case SSL_CERTIFICATE_REQUEST:
2985 ret = ssl_parse_certificate_request( ssl );
2986 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002987
Paul Bakker1961b702013-01-25 14:49:24 +01002988 case SSL_SERVER_HELLO_DONE:
2989 ret = ssl_parse_server_hello_done( ssl );
2990 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002991
Paul Bakker1961b702013-01-25 14:49:24 +01002992 /*
2993 * ==> ( Certificate/Alert )
2994 * ClientKeyExchange
2995 * ( CertificateVerify )
2996 * ChangeCipherSpec
2997 * Finished
2998 */
2999 case SSL_CLIENT_CERTIFICATE:
3000 ret = ssl_write_certificate( ssl );
3001 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003002
Paul Bakker1961b702013-01-25 14:49:24 +01003003 case SSL_CLIENT_KEY_EXCHANGE:
3004 ret = ssl_write_client_key_exchange( ssl );
3005 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Paul Bakker1961b702013-01-25 14:49:24 +01003007 case SSL_CERTIFICATE_VERIFY:
3008 ret = ssl_write_certificate_verify( ssl );
3009 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003010
Paul Bakker1961b702013-01-25 14:49:24 +01003011 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3012 ret = ssl_write_change_cipher_spec( ssl );
3013 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Paul Bakker1961b702013-01-25 14:49:24 +01003015 case SSL_CLIENT_FINISHED:
3016 ret = ssl_write_finished( ssl );
3017 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
Paul Bakker1961b702013-01-25 14:49:24 +01003019 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003020 * <== ( NewSessionTicket )
3021 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003022 * Finished
3023 */
Paul Bakkera503a632013-08-14 13:48:06 +02003024#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003025 case SSL_SERVER_NEW_SESSION_TICKET:
3026 ret = ssl_parse_new_session_ticket( ssl );
3027 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003028#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003029
3030 case SSL_SERVER_CHANGE_CIPHER_SPEC:
3031 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003032 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003033
Paul Bakker1961b702013-01-25 14:49:24 +01003034 case SSL_SERVER_FINISHED:
3035 ret = ssl_parse_finished( ssl );
3036 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003037
Paul Bakker1961b702013-01-25 14:49:24 +01003038 case SSL_FLUSH_BUFFERS:
3039 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3040 ssl->state = SSL_HANDSHAKE_WRAPUP;
3041 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
Paul Bakker1961b702013-01-25 14:49:24 +01003043 case SSL_HANDSHAKE_WRAPUP:
3044 ssl_handshake_wrapup( ssl );
3045 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003046
Paul Bakker1961b702013-01-25 14:49:24 +01003047 default:
3048 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3049 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3050 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003051
3052 return( ret );
3053}
Paul Bakker9af723c2014-05-01 13:03:14 +02003054#endif /* POLARSSL_SSL_CLI_C */