blob: c002cc5a5fb7e2fcac2d3c252d3ef10ed675f583 [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 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200638 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000639#endif
640 if( ssl->session_negotiate->ticket != NULL &&
641 ssl->session_negotiate->ticket_len != 0 )
642 {
643 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200644
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000645 if( ret != 0 )
646 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200647
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000648 ssl->session_negotiate->length = n = 32;
649 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200650 }
Paul Bakkera503a632013-08-14 13:48:06 +0200651#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
653 *p++ = (unsigned char) n;
654
655 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000656 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
658 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
659 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
660
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100661 /*
662 * DTLS cookie
663 */
664#if defined(POLARSSL_SSL_PROTO_DTLS)
665 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
666 {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200667 if( ssl->handshake->verify_cookie == NULL )
668 {
669 SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
670 *p++ = 0;
671 }
672 else
673 {
674 SSL_DEBUG_BUF( 3, "client hello, cookie",
675 ssl->handshake->verify_cookie,
676 ssl->handshake->verify_cookie_len );
677
678 *p++ = ssl->handshake->verify_cookie_len;
679 memcpy( p, ssl->handshake->verify_cookie,
680 ssl->handshake->verify_cookie_len );
681 p += ssl->handshake->verify_cookie_len;
682 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100683 }
684#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
Paul Bakker48916f92012-09-16 19:57:18 +0000686 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100687 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000688 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100689 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
690
691 /* Skip writing ciphersuite length for now */
692 n = 0;
693 q = p;
694 p += 2;
695
Paul Bakker2fbefde2013-06-29 16:01:15 +0200696 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200698 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
699
700 if( ciphersuite_info == NULL )
701 continue;
702
703 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
704 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
705 continue;
706
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100707#if defined(POLARSSL_SSL_PROTO_DTLS)
708 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
709 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
710 continue;
711#endif
712
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100713 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
714 ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
715 continue;
716
Paul Bakkere3166ce2011-01-27 17:40:50 +0000717 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200718 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
Paul Bakker2fbefde2013-06-29 16:01:15 +0200720 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200721 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
722 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 }
724
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +0000725 /*
726 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
727 */
728#if defined(POLARSSL_SSL_RENEGOTIATION)
729 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
730#endif
731 {
732 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
733 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
734 n++;
735 }
736
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200737 /* Some versions of OpenSSL don't handle it correctly if not at end */
738#if defined(POLARSSL_SSL_FALLBACK_SCSV)
739 if( ssl->fallback == SSL_IS_FALLBACK )
740 {
741 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
742 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
743 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
744 n++;
745 }
746#endif
747
Paul Bakker2fbefde2013-06-29 16:01:15 +0200748 *q++ = (unsigned char)( n >> 7 );
749 *q++ = (unsigned char)( n << 1 );
750
751 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
752
Paul Bakker2770fbd2012-07-03 13:30:23 +0000753#if defined(POLARSSL_ZLIB_SUPPORT)
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200754 offer_compress = 1;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000755#else
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200756 offer_compress = 0;
757#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200759 /*
760 * We don't support compression with DTLS right now: is many records come
761 * in the same datagram, uncompressing one could overwrite the next one.
762 * We don't want to add complexity for handling that case unless there is
763 * an actual need for it.
764 */
765#if defined(POLARSSL_SSL_PROTO_DTLS)
766 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
767 offer_compress = 0;
768#endif
769
770 if( offer_compress )
771 {
772 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
773 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
774 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
775
776 *p++ = 2;
777 *p++ = SSL_COMPRESS_DEFLATE;
778 *p++ = SSL_COMPRESS_NULL;
779 }
780 else
781 {
782 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
783 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
784 SSL_COMPRESS_NULL ) );
785
786 *p++ = 1;
787 *p++ = SSL_COMPRESS_NULL;
788 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000789
Paul Bakkerd3edc862013-03-20 16:07:17 +0100790 // First write extensions, then the total length
791 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200792#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100793 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
794 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200795#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000796
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100797#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100798 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
799 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100800#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000801
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100802#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
803 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100804 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
805 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200806#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000807
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200808#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100809 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
810 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100811
Paul Bakkerd3edc862013-03-20 16:07:17 +0100812 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
813 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100814#endif
815
Paul Bakker05decb22013-08-15 13:33:48 +0200816#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200817 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
818 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200819#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200820
Paul Bakker1f2bc622013-08-15 13:45:55 +0200821#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200822 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
823 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200824#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200825
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100826#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
827 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
828 ext_len += olen;
829#endif
830
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200831#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
832 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
833 ext_len += olen;
834#endif
835
Paul Bakkera503a632013-08-14 13:48:06 +0200836#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200837 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
838 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200839#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200840
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200841#if defined(POLARSSL_SSL_ALPN)
842 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
843 ext_len += olen;
844#endif
845
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100846 /* olen unused if all extensions are disabled */
847 ((void) olen);
848
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000849 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
850 ext_len ) );
851
Paul Bakkera7036632014-04-30 10:15:38 +0200852 if( ext_len > 0 )
853 {
854 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
855 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
856 p += ext_len;
857 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100858
Paul Bakker5121ce52009-01-03 21:22:43 +0000859 ssl->out_msglen = p - buf;
860 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
861 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
862
863 ssl->state++;
864
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +0200865#if defined(POLARSSL_SSL_PROTO_DTLS)
866 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
867 ssl_send_flight_completed( ssl );
868#endif
869
Paul Bakker5121ce52009-01-03 21:22:43 +0000870 if( ( ret = ssl_write_record( ssl ) ) != 0 )
871 {
872 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
873 return( ret );
874 }
875
876 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
877
878 return( 0 );
879}
880
Paul Bakker48916f92012-09-16 19:57:18 +0000881static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200882 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000883 size_t len )
884{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000885 int ret;
886
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100887#if defined(POLARSSL_SSL_RENEGOTIATION)
888 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000889 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100890 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000891 if( len != 1 + ssl->verify_data_len * 2 ||
892 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100893 safer_memcmp( buf + 1,
894 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
895 safer_memcmp( buf + 1 + ssl->verify_data_len,
896 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000897 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100898 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000899
900 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
901 return( ret );
902
Paul Bakker48916f92012-09-16 19:57:18 +0000903 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
904 }
905 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100906 else
907#endif /* POLARSSL_SSL_RENEGOTIATION */
908 {
909 if( len != 1 || buf[0] != 0x00 )
910 {
911 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
912
913 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
914 return( ret );
915
916 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
917 }
918
919 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
920 }
Paul Bakker48916f92012-09-16 19:57:18 +0000921
922 return( 0 );
923}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200924
Paul Bakker05decb22013-08-15 13:33:48 +0200925#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200926static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200927 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200928 size_t len )
929{
930 /*
931 * server should use the extension only if we did,
932 * and if so the server's value should match ours (and len is always 1)
933 */
934 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
935 len != 1 ||
936 buf[0] != ssl->mfl_code )
937 {
938 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
939 }
940
941 return( 0 );
942}
Paul Bakker05decb22013-08-15 13:33:48 +0200943#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000944
Paul Bakker1f2bc622013-08-15 13:45:55 +0200945#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200946static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
947 const unsigned char *buf,
948 size_t len )
949{
950 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
951 len != 0 )
952 {
953 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
954 }
955
956 ((void) buf);
957
958 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
959
960 return( 0 );
961}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200962#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200963
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100964#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
965static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
966 const unsigned char *buf,
967 size_t len )
968{
969 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
970 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
971 len != 0 )
972 {
973 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
974 }
975
976 ((void) buf);
977
978 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
979
980 return( 0 );
981}
982#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
983
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200984#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
985static int ssl_parse_extended_ms_ext( ssl_context *ssl,
986 const unsigned char *buf,
987 size_t len )
988{
989 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200990 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200991 len != 0 )
992 {
993 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
994 }
995
996 ((void) buf);
997
998 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
999
1000 return( 0 );
1001}
1002#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1003
Paul Bakkera503a632013-08-14 13:48:06 +02001004#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001005static int ssl_parse_session_ticket_ext( ssl_context *ssl,
1006 const unsigned char *buf,
1007 size_t len )
1008{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001009 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
1010 len != 0 )
1011 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001012 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001013 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001014
1015 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001016
1017 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001018
1019 return( 0 );
1020}
Paul Bakkera503a632013-08-14 13:48:06 +02001021#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001022
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001023#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001024static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
1025 const unsigned char *buf,
1026 size_t len )
1027{
1028 size_t list_size;
1029 const unsigned char *p;
1030
1031 list_size = buf[0];
1032 if( list_size + 1 != len )
1033 {
1034 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1035 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1036 }
1037
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001038 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001039 while( list_size > 0 )
1040 {
1041 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
1042 p[0] == POLARSSL_ECP_PF_COMPRESSED )
1043 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001044 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001045 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1046 return( 0 );
1047 }
1048
1049 list_size--;
1050 p++;
1051 }
1052
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +02001053 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1054 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001055}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001056#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001057
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001058#if defined(POLARSSL_SSL_ALPN)
1059static int ssl_parse_alpn_ext( ssl_context *ssl,
1060 const unsigned char *buf, size_t len )
1061{
1062 size_t list_len, name_len;
1063 const char **p;
1064
1065 /* If we didn't send it, the server shouldn't send it */
1066 if( ssl->alpn_list == NULL )
1067 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1068
1069 /*
1070 * opaque ProtocolName<1..2^8-1>;
1071 *
1072 * struct {
1073 * ProtocolName protocol_name_list<2..2^16-1>
1074 * } ProtocolNameList;
1075 *
1076 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1077 */
1078
1079 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1080 if( len < 4 )
1081 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1082
1083 list_len = ( buf[0] << 8 ) | buf[1];
1084 if( list_len != len - 2 )
1085 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1086
1087 name_len = buf[2];
1088 if( name_len != list_len - 1 )
1089 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1090
1091 /* Check that the server chosen protocol was in our list and save it */
1092 for( p = ssl->alpn_list; *p != NULL; p++ )
1093 {
1094 if( name_len == strlen( *p ) &&
1095 memcmp( buf + 3, *p, name_len ) == 0 )
1096 {
1097 ssl->alpn_chosen = *p;
1098 return( 0 );
1099 }
1100 }
1101
1102 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1103}
1104#endif /* POLARSSL_SSL_ALPN */
1105
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001106/*
1107 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1108 */
1109#if defined(POLARSSL_SSL_PROTO_DTLS)
1110static int ssl_parse_hello_verify_request( ssl_context *ssl )
1111{
Manuel Pégourié-Gonnard069eb792014-09-10 20:08:29 +02001112 const unsigned char *p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001113 int major_ver, minor_ver;
1114 unsigned char cookie_len;
1115
1116 SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
1117
1118 /*
1119 * struct {
1120 * ProtocolVersion server_version;
1121 * opaque cookie<0..2^8-1>;
1122 * } HelloVerifyRequest;
1123 */
1124 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
1125 ssl_read_version( &major_ver, &minor_ver, ssl->transport, p );
1126 p += 2;
1127
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02001128 /*
1129 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1130 * even is lower than our min version.
1131 */
1132 if( major_ver < SSL_MAJOR_VERSION_3 ||
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001133 minor_ver < SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02001134 major_ver > ssl->max_major_ver ||
1135 minor_ver > ssl->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001136 {
1137 SSL_DEBUG_MSG( 1, ( "bad server version" ) );
1138
1139 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1140 SSL_ALERT_MSG_PROTOCOL_VERSION );
1141
1142 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1143 }
1144
1145 cookie_len = *p++;
1146 SSL_DEBUG_BUF( 3, "cookie", (unsigned char *) p, cookie_len );
1147
1148 polarssl_free( ssl->handshake->verify_cookie );
1149
1150 ssl->handshake->verify_cookie = polarssl_malloc( cookie_len );
1151 if( ssl->handshake->verify_cookie == NULL )
1152 {
1153 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", cookie_len ) );
1154 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1155 }
1156
1157 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
1158 ssl->handshake->verify_cookie_len = cookie_len;
1159
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001160 /* Start over at ClientHello */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001161 ssl->state = SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001162 ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001163
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001164 ssl_recv_flight_completed( ssl );
1165
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001166 SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
1167
1168 return( 0 );
1169}
1170#endif /* POLARSSL_SSL_PROTO_DTLS */
1171
Paul Bakker5121ce52009-01-03 21:22:43 +00001172static int ssl_parse_server_hello( ssl_context *ssl )
1173{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001174 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001175 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001176 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001177 unsigned char *buf, *ext;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001178 unsigned char comp, accept_comp;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001179#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001180 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001181#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001182 int handshake_failure = 0;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001183 const ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001184#if defined(POLARSSL_DEBUG_C)
1185 uint32_t t;
1186#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001187
1188 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1189
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 buf = ssl->in_msg;
1191
1192 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1193 {
1194 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1195 return( ret );
1196 }
1197
1198 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1199 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001200#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001201 if( ssl->renegotiation == SSL_RENEGOTIATION )
1202 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001203 ssl->renego_records_seen++;
1204
1205 if( ssl->renego_max_records >= 0 &&
1206 ssl->renego_records_seen > ssl->renego_max_records )
1207 {
1208 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1209 "but not honored by server" ) );
1210 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1211 }
1212
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001213 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1214 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1215 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001216#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001217
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001219 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 }
1221
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001222#if defined(POLARSSL_SSL_PROTO_DTLS)
1223 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1224 {
1225 if( buf[0] == SSL_HS_HELLO_VERIFY_REQUEST )
1226 {
1227 SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1228 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1229 return( ssl_parse_hello_verify_request( ssl ) );
1230 }
1231 else
1232 {
1233 /* We made it through the verification process */
1234 polarssl_free( ssl->handshake->verify_cookie );
1235 ssl->handshake->verify_cookie = NULL;
1236 ssl->handshake->verify_cookie_len = 0;
1237 }
1238 }
1239#endif /* POLARSSL_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001241 if( ssl->in_hslen < 38 + ssl_hs_hdr_len( ssl ) ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001242 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 {
1244 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001245 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 }
1247
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001248 /*
1249 * 0 . 1 server_version
1250 * 2 . 33 random (maybe including 4 bytes of Unix time)
1251 * 34 . 34 session_id length = n
1252 * 35 . 34+n session_id
1253 * 35+n . 36+n cipher_suite
1254 * 37+n . 37+n compression_method
1255 *
1256 * 38+n . 39+n extensions length (optional)
1257 * 40+n . .. extensions
1258 */
1259 buf += ssl_hs_hdr_len( ssl );
1260
1261 SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001262 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001263 ssl->transport, buf + 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001265 if( ssl->major_ver < ssl->min_major_ver ||
1266 ssl->minor_ver < ssl->min_minor_ver ||
1267 ssl->major_ver > ssl->max_major_ver ||
1268 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001269 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001270 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
1271 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1272 ssl->min_major_ver, ssl->min_minor_ver,
1273 ssl->major_ver, ssl->minor_ver,
1274 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001275
1276 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1277 SSL_ALERT_MSG_PROTOCOL_VERSION );
1278
1279 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1280 }
1281
Paul Bakker1504af52012-02-11 16:17:43 +00001282#if defined(POLARSSL_DEBUG_C)
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001283 t = ( (uint32_t) buf[2] << 24 )
1284 | ( (uint32_t) buf[3] << 16 )
1285 | ( (uint32_t) buf[4] << 8 )
1286 | ( (uint32_t) buf[5] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001287 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001288#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001290 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001292 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001294 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001295
Paul Bakker48916f92012-09-16 19:57:18 +00001296 if( n > 32 )
1297 {
1298 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1299 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1300 }
1301
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001302 if( ssl->in_hslen > 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001304 ext_len = ( ( buf[38 + n] << 8 )
1305 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Paul Bakker48916f92012-09-16 19:57:18 +00001307 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001308 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001309 {
1310 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1311 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1312 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 }
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001314 else if( ssl->in_hslen == 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001315 {
1316 ext_len = 0;
1317 }
1318 else
1319 {
1320 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1321 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001323
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001324 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001325 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001326
1327 /*
1328 * Read and check compression
1329 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001330 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001332#if defined(POLARSSL_ZLIB_SUPPORT)
1333 accept_comp = 1;
1334#else
1335 accept_comp = 0;
1336#endif
1337
1338 /* See comments in ssl_write_client_hello() */
1339#if defined(POLARSSL_SSL_PROTO_DTLS)
1340 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1341 accept_comp = 0;
1342#endif
1343
1344 if( ( accept_comp == 0 && comp != SSL_COMPRESS_NULL ) ||
1345 ( comp != SSL_COMPRESS_NULL && comp != SSL_COMPRESS_DEFLATE ) )
1346 {
1347 SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
1348 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1349 }
1350
Paul Bakker380da532012-04-18 16:10:25 +00001351 /*
1352 * Initialize update checksum functions
1353 */
Paul Bakker68884e32013-01-07 18:20:04 +01001354 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1355
1356 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1357 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001358 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001359 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1360 }
Paul Bakker380da532012-04-18 16:10:25 +00001361
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001362 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1363
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001365 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
1367 /*
1368 * Check if the session can be resumed
1369 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001370 if( ssl->handshake->resume == 0 || n == 0 ||
1371#if defined(POLARSSL_SSL_RENEGOTIATION)
1372 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1373#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001374 ssl->session_negotiate->ciphersuite != i ||
1375 ssl->session_negotiate->compression != comp ||
1376 ssl->session_negotiate->length != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001377 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 {
1379 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001380 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001381#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001382 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001383#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001384 ssl->session_negotiate->ciphersuite = i;
1385 ssl->session_negotiate->compression = comp;
1386 ssl->session_negotiate->length = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001387 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 }
1389 else
1390 {
1391 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001392
1393 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1394 {
1395 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1396 return( ret );
1397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 }
1399
1400 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001401 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Paul Bakkere3166ce2011-01-27 17:40:50 +00001403 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001404 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001406 suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1407 if( suite_info == NULL ||
1408 ( ssl->arc4_disabled &&
1409 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) )
1410 {
1411 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1412 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1413 }
1414
1415
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 i = 0;
1417 while( 1 )
1418 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001419 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
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 }
1424
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001425 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1426 ssl->session_negotiate->ciphersuite )
1427 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001429 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 }
1431
Paul Bakker2770fbd2012-07-03 13:30:23 +00001432 if( comp != SSL_COMPRESS_NULL
1433#if defined(POLARSSL_ZLIB_SUPPORT)
1434 && comp != SSL_COMPRESS_DEFLATE
1435#endif
1436 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 {
1438 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001439 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
Paul Bakker48916f92012-09-16 19:57:18 +00001441 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001443 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001444
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001445 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1446
Paul Bakker48916f92012-09-16 19:57:18 +00001447 while( ext_len )
1448 {
1449 unsigned int ext_id = ( ( ext[0] << 8 )
1450 | ( ext[1] ) );
1451 unsigned int ext_size = ( ( ext[2] << 8 )
1452 | ( ext[3] ) );
1453
1454 if( ext_size + 4 > ext_len )
1455 {
1456 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1457 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1458 }
1459
1460 switch( ext_id )
1461 {
1462 case TLS_EXT_RENEGOTIATION_INFO:
1463 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001464#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001465 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001466#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001467
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001468 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1469 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001470 return( ret );
1471
1472 break;
1473
Paul Bakker05decb22013-08-15 13:33:48 +02001474#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001475 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1476 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1477
1478 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1479 ext + 4, ext_size ) ) != 0 )
1480 {
1481 return( ret );
1482 }
1483
1484 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001485#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001486
Paul Bakker1f2bc622013-08-15 13:45:55 +02001487#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001488 case TLS_EXT_TRUNCATED_HMAC:
1489 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1490
1491 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1492 ext + 4, ext_size ) ) != 0 )
1493 {
1494 return( ret );
1495 }
1496
1497 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001498#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001499
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001500#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1501 case TLS_EXT_ENCRYPT_THEN_MAC:
1502 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1503
1504 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1505 ext + 4, ext_size ) ) != 0 )
1506 {
1507 return( ret );
1508 }
1509
1510 break;
1511#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1512
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001513#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1514 case TLS_EXT_EXTENDED_MASTER_SECRET:
1515 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1516
1517 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1518 ext + 4, ext_size ) ) != 0 )
1519 {
1520 return( ret );
1521 }
1522
1523 break;
1524#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1525
Paul Bakkera503a632013-08-14 13:48:06 +02001526#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001527 case TLS_EXT_SESSION_TICKET:
1528 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1529
1530 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1531 ext + 4, ext_size ) ) != 0 )
1532 {
1533 return( ret );
1534 }
1535
1536 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001537#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001538
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001539#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001540 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1541 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1542
1543 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1544 ext + 4, ext_size ) ) != 0 )
1545 {
1546 return( ret );
1547 }
1548
1549 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001550#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001551
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001552#if defined(POLARSSL_SSL_ALPN)
1553 case TLS_EXT_ALPN:
1554 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1555
1556 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1557 return( ret );
1558
1559 break;
1560#endif /* POLARSSL_SSL_ALPN */
1561
Paul Bakker48916f92012-09-16 19:57:18 +00001562 default:
1563 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1564 ext_id ) );
1565 }
1566
1567 ext_len -= 4 + ext_size;
1568 ext += 4 + ext_size;
1569
1570 if( ext_len > 0 && ext_len < 4 )
1571 {
1572 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1573 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1574 }
1575 }
1576
1577 /*
1578 * Renegotiation security checks
1579 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001580 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1581 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001582 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001583 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1584 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001585 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001586#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001587 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1588 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1589 renegotiation_info_seen == 0 )
1590 {
1591 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1592 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001593 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001594 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1595 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1596 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001597 {
1598 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001599 handshake_failure = 1;
1600 }
1601 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1602 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1603 renegotiation_info_seen == 1 )
1604 {
1605 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1606 handshake_failure = 1;
1607 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001608#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001609
1610 if( handshake_failure == 1 )
1611 {
1612 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1613 return( ret );
1614
Paul Bakker48916f92012-09-16 19:57:18 +00001615 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
1618 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1619
1620 return( 0 );
1621}
1622
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001623#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1624 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001625static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1626 unsigned char *end )
1627{
1628 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1629
Paul Bakker29e1f122013-04-16 13:07:56 +02001630 /*
1631 * Ephemeral DH parameters:
1632 *
1633 * struct {
1634 * opaque dh_p<1..2^16-1>;
1635 * opaque dh_g<1..2^16-1>;
1636 * opaque dh_Ys<1..2^16-1>;
1637 * } ServerDHParams;
1638 */
1639 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1640 {
1641 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1642 return( ret );
1643 }
1644
1645 if( ssl->handshake->dhm_ctx.len < 64 ||
1646 ssl->handshake->dhm_ctx.len > 512 )
1647 {
1648 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1649 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1650 }
1651
1652 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1653 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1654 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001655
1656 return( ret );
1657}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001658#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1659 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001660
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001661#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001662 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001663 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1664 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1665 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1666static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1667{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001668 const ecp_curve_info *curve_info;
1669
1670 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1671 if( curve_info == NULL )
1672 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001673 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1674 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001675 }
1676
1677 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001678
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001679#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1680 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1681#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001682 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1683 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001684#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001685 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001686
1687 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1688
1689 return( 0 );
1690}
Paul Bakker9af723c2014-05-01 13:03:14 +02001691#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1692 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1693 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1694 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1695 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001696
1697#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1698 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001699 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001700static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1701 unsigned char **p,
1702 unsigned char *end )
1703{
1704 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1705
Paul Bakker29e1f122013-04-16 13:07:56 +02001706 /*
1707 * Ephemeral ECDH parameters:
1708 *
1709 * struct {
1710 * ECParameters curve_params;
1711 * ECPoint public;
1712 * } ServerECDHParams;
1713 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001714 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1715 (const unsigned char **) p, end ) ) != 0 )
1716 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001717 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001718 return( ret );
1719 }
1720
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001721 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001722 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001723 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001724 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1725 }
1726
Paul Bakker29e1f122013-04-16 13:07:56 +02001727 return( ret );
1728}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001729#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001730 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1731 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001732
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001733#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001734static int ssl_parse_server_psk_hint( ssl_context *ssl,
1735 unsigned char **p,
1736 unsigned char *end )
1737{
1738 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001739 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001740 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001741
1742 /*
1743 * PSK parameters:
1744 *
1745 * opaque psk_identity_hint<0..2^16-1>;
1746 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001747 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001748 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001749
1750 if( (*p) + len > end )
1751 {
1752 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1753 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1754 }
1755
1756 // TODO: Retrieve PSK identity hint and callback to app
1757 //
1758 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001759 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001760
1761 return( ret );
1762}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001763#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001764
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001765#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1766 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1767/*
1768 * Generate a pre-master secret and encrypt it with the server's RSA key
1769 */
1770static int ssl_write_encrypted_pms( ssl_context *ssl,
1771 size_t offset, size_t *olen,
1772 size_t pms_offset )
1773{
1774 int ret;
1775 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1776 unsigned char *p = ssl->handshake->premaster + pms_offset;
1777
1778 /*
1779 * Generate (part of) the pre-master as
1780 * struct {
1781 * ProtocolVersion client_version;
1782 * opaque random[46];
1783 * } PreMasterSecret;
1784 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001785 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1786 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001787
1788 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1789 {
1790 SSL_DEBUG_RET( 1, "f_rng", ret );
1791 return( ret );
1792 }
1793
1794 ssl->handshake->pmslen = 48;
1795
1796 /*
1797 * Now write it out, encrypted
1798 */
1799 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1800 POLARSSL_PK_RSA ) )
1801 {
1802 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1803 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1804 }
1805
1806 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1807 p, ssl->handshake->pmslen,
1808 ssl->out_msg + offset + len_bytes, olen,
1809 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1810 ssl->f_rng, ssl->p_rng ) ) != 0 )
1811 {
1812 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1813 return( ret );
1814 }
1815
1816#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1817 defined(POLARSSL_SSL_PROTO_TLS1_2)
1818 if( len_bytes == 2 )
1819 {
1820 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1821 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1822 *olen += 2;
1823 }
1824#endif
1825
1826 return( 0 );
1827}
1828#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1829 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001830
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001831#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001832#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001833 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1834 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001835static int ssl_parse_signature_algorithm( ssl_context *ssl,
1836 unsigned char **p,
1837 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001838 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001839 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001840{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001841 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001842 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001843 *pk_alg = POLARSSL_PK_NONE;
1844
1845 /* Only in TLS 1.2 */
1846 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1847 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001848 return( 0 );
1849 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001850
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001851 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001852 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1853
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001854 /*
1855 * Get hash algorithm
1856 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001857 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001858 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001859 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1860 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001861 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1862 }
1863
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001864 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001865 * Get signature algorithm
1866 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001867 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001868 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001869 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1870 "SignatureAlgorithm %d", (*p)[1] ) );
1871 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001872 }
1873
1874 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1875 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1876 *p += 2;
1877
1878 return( 0 );
1879}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001880#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001881 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1882 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001883#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001884
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001885
1886#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1887 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1888static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1889{
1890 int ret;
1891 const ecp_keypair *peer_key;
1892
1893 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1894 POLARSSL_PK_ECKEY ) )
1895 {
1896 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1897 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1898 }
1899
1900 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1901
1902 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1903 POLARSSL_ECDH_THEIRS ) ) != 0 )
1904 {
1905 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1906 return( ret );
1907 }
1908
1909 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1910 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001911 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001912 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1913 }
1914
1915 return( ret );
1916}
1917#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1918 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1919
Paul Bakker41c83d32013-03-20 14:39:14 +01001920static int ssl_parse_server_key_exchange( ssl_context *ssl )
1921{
Paul Bakker23986e52011-04-24 08:57:21 +00001922 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001923 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001924 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001925
1926 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1927
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001928#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001929 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 {
1931 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1932 ssl->state++;
1933 return( 0 );
1934 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001935 ((void) p);
1936 ((void) end);
1937#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001939#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1940 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1941 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1942 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1943 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001944 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1945 {
1946 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1947 return( ret );
1948 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001949
1950 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1951 ssl->state++;
1952 return( 0 );
1953 }
1954 ((void) p);
1955 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001956#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1957 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001958
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1960 {
1961 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1962 return( ret );
1963 }
1964
1965 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1966 {
1967 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001968 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 }
1970
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001971 /*
1972 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1973 * doesn't use a psk_identity_hint
1974 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1976 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001977 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1978 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001979 {
1980 ssl->record_read = 1;
1981 goto exit;
1982 }
1983
1984 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1985 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 }
1987
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001988 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001989 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001990 SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001991
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001992#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1993 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1994 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1995 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1996 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1997 {
1998 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1999 {
2000 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2001 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2002 }
2003 } /* FALLTROUGH */
2004#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
2005
2006#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2007 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2008 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2009 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2010 ; /* nothing more to do */
2011 else
2012#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2013 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
2014#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2015 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2016 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2017 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002019 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002020 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002021 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002022 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2023 }
2024 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002025 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002026#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2027 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002028#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002029 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002030 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2031 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002032 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002033 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002034 {
2035 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2036 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002037 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2038 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2039 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002040 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002041 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002042#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002043 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002044 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002045 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002046 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002047 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002048 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002049
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002050#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002051 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2052 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02002053 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002054 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2055 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002056 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002057 size_t sig_len, hashlen;
2058 unsigned char hash[64];
2059 md_type_t md_alg = POLARSSL_MD_NONE;
2060 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002061 unsigned char *params = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002062 size_t params_len = p - params;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002063
Paul Bakker29e1f122013-04-16 13:07:56 +02002064 /*
2065 * Handle the digitally-signed structure
2066 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002067#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2068 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002069 {
Paul Bakker9659dae2013-08-28 16:21:34 +02002070 if( ssl_parse_signature_algorithm( ssl, &p, end,
2071 &md_alg, &pk_alg ) != 0 )
2072 {
2073 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2074 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2075 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002076
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002077 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002078 {
2079 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2080 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2081 }
2082 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002083 else
Paul Bakker9af723c2014-05-01 13:03:14 +02002084#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002085#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2086 defined(POLARSSL_SSL_PROTO_TLS1_1)
2087 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002088 {
2089 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002090
Paul Bakker9659dae2013-08-28 16:21:34 +02002091 /* Default hash for ECDSA is SHA-1 */
2092 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
2093 md_alg = POLARSSL_MD_SHA1;
2094 }
2095 else
2096#endif
2097 {
2098 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002099 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02002100 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002101
2102 /*
2103 * Read signature
2104 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002105 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002106 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002107
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002108 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002109 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002110 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01002111 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2112 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002113
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002114 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002115
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002116 /*
2117 * Compute the hash that has been signed
2118 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002119#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2120 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002121 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002122 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002123 md5_context md5;
2124 sha1_context sha1;
2125
Paul Bakker5b4af392014-06-26 12:09:34 +02002126 md5_init( &md5 );
2127 sha1_init( &sha1 );
2128
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002129 hashlen = 36;
2130
Paul Bakker29e1f122013-04-16 13:07:56 +02002131 /*
2132 * digitally-signed struct {
2133 * opaque md5_hash[16];
2134 * opaque sha_hash[20];
2135 * };
2136 *
2137 * md5_hash
2138 * MD5(ClientHello.random + ServerHello.random
2139 * + ServerParams);
2140 * sha_hash
2141 * SHA(ClientHello.random + ServerHello.random
2142 * + ServerParams);
2143 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002144 md5_starts( &md5 );
2145 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002146 md5_update( &md5, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002147 md5_finish( &md5, hash );
2148
2149 sha1_starts( &sha1 );
2150 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002151 sha1_update( &sha1, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002152 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02002153
2154 md5_free( &md5 );
2155 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02002156 }
2157 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002158#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2159 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002160#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2161 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002162 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002163 {
2164 md_context_t ctx;
2165
Paul Bakker84bbeb52014-07-01 14:53:22 +02002166 md_init( &ctx );
2167
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002168 /* Info from md_alg will be used instead */
2169 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02002170
2171 /*
2172 * digitally-signed struct {
2173 * opaque client_random[32];
2174 * opaque server_random[32];
2175 * ServerDHParams params;
2176 * };
2177 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002178 if( ( ret = md_init_ctx( &ctx,
2179 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002180 {
2181 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2182 return( ret );
2183 }
2184
2185 md_starts( &ctx );
2186 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002187 md_update( &ctx, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002188 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002189 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002190 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002191 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002192#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2193 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002194 {
Paul Bakker577e0062013-08-28 11:57:20 +02002195 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002196 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002197 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002198
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002199 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2200 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002201
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002202 /*
2203 * Verify signature
2204 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002205 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002206 {
2207 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2208 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2209 }
2210
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002211 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2212 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002213 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002214 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002215 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002216 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002218#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002219 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2220 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002221
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002222exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 ssl->state++;
2224
2225 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2226
2227 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002228}
2229
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002230#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2231 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2232 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2233 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2234static int ssl_parse_certificate_request( ssl_context *ssl )
2235{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002236 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2237
2238 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2239
2240 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2241 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2242 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2243 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2244 {
2245 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2246 ssl->state++;
2247 return( 0 );
2248 }
2249
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002250 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2251 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002252}
2253#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002254static int ssl_parse_certificate_request( ssl_context *ssl )
2255{
2256 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002257 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002258 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002259 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002260 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
2262 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2263
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002264 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2265 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2266 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2267 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2268 {
2269 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2270 ssl->state++;
2271 return( 0 );
2272 }
2273
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002274 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002276 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2277 {
2278 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2279 return( ret );
2280 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002282 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2283 {
2284 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2285 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2286 }
2287
2288 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 }
2290
2291 ssl->client_auth = 0;
2292 ssl->state++;
2293
2294 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2295 ssl->client_auth++;
2296
2297 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2298 ssl->client_auth ? "a" : "no" ) );
2299
Paul Bakker926af752012-11-23 13:38:07 +01002300 if( ssl->client_auth == 0 )
2301 goto exit;
2302
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002303 ssl->record_read = 0;
2304
Paul Bakker926af752012-11-23 13:38:07 +01002305 // TODO: handshake_failure alert for an anonymous server to request
2306 // client authentication
2307
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002308 /*
2309 * struct {
2310 * ClientCertificateType certificate_types<1..2^8-1>;
2311 * SignatureAndHashAlgorithm
2312 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2313 * DistinguishedName certificate_authorities<0..2^16-1>;
2314 * } CertificateRequest;
2315 */
Paul Bakker926af752012-11-23 13:38:07 +01002316 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002317
Paul Bakker926af752012-11-23 13:38:07 +01002318 // Retrieve cert types
2319 //
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002320 cert_type_len = buf[ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002321 n = cert_type_len;
2322
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002323 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002324 {
2325 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2326 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2327 }
2328
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002329 p = buf + ssl_hs_hdr_len( ssl ) + 1;
Paul Bakker926af752012-11-23 13:38:07 +01002330 while( cert_type_len > 0 )
2331 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002332#if defined(POLARSSL_RSA_C)
2333 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002334 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002335 {
2336 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2337 break;
2338 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002339 else
2340#endif
2341#if defined(POLARSSL_ECDSA_C)
2342 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002343 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002344 {
2345 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2346 break;
2347 }
2348 else
2349#endif
2350 {
2351 ; /* Unsupported cert type, ignore */
2352 }
Paul Bakker926af752012-11-23 13:38:07 +01002353
2354 cert_type_len--;
2355 p++;
2356 }
2357
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002358#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002359 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2360 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002361 /* Ignored, see comments about hash in write_certificate_verify */
2362 // TODO: should check the signature part against our pk_key though
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002363 size_t sig_alg_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2364 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002365
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002366 p = buf + ssl_hs_hdr_len( ssl ) + 3 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002367 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002368 n += sig_alg_len;
2369
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002370 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002371 {
2372 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2373 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2374 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002375 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002376#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002377
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002378 /* Ignore certificate_authorities, we only have one cert anyway */
2379 // TODO: should not send cert if no CA matches
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002380 dn_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + m + n] << 8 )
2381 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002382
2383 n += dn_len;
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002384 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 3 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002385 {
2386 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2387 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2388 }
2389
2390exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2392
2393 return( 0 );
2394}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002395#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2396 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2397 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2398 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
2400static int ssl_parse_server_hello_done( ssl_context *ssl )
2401{
2402 int ret;
2403
2404 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2405
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002406 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 {
2408 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2409 {
2410 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2411 return( ret );
2412 }
2413
2414 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2415 {
2416 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002417 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 }
2419 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002420 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002422 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) ||
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2424 {
2425 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002426 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 }
2428
2429 ssl->state++;
2430
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002431#if defined(POLARSSL_SSL_PROTO_DTLS)
2432 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2433 ssl_recv_flight_completed( ssl );
2434#endif
2435
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2437
2438 return( 0 );
2439}
2440
2441static int ssl_write_client_key_exchange( ssl_context *ssl )
2442{
Paul Bakker23986e52011-04-24 08:57:21 +00002443 int ret;
2444 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002445 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
2447 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2448
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002449#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002450 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 /*
2453 * DHM key exchange -- send G^X mod P
2454 */
Paul Bakker48916f92012-09-16 19:57:18 +00002455 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
2457 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2458 ssl->out_msg[5] = (unsigned char)( n );
2459 i = 6;
2460
Paul Bakker29b64762012-09-25 09:36:44 +00002461 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002462 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 &ssl->out_msg[i], n,
2464 ssl->f_rng, ssl->p_rng );
2465 if( ret != 0 )
2466 {
2467 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2468 return( ret );
2469 }
2470
Paul Bakker48916f92012-09-16 19:57:18 +00002471 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2472 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002474 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002475
Paul Bakker48916f92012-09-16 19:57:18 +00002476 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2477 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002478 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002479 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 {
2481 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2482 return( ret );
2483 }
2484
Paul Bakker48916f92012-09-16 19:57:18 +00002485 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 }
2487 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002488#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002489#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002490 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2491 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2492 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002493 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002494 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2495 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2496 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002497 {
2498 /*
2499 * ECDH key exchange -- send client public value
2500 */
2501 i = 4;
2502
2503 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2504 &n,
2505 &ssl->out_msg[i], 1000,
2506 ssl->f_rng, ssl->p_rng );
2507 if( ret != 0 )
2508 {
2509 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2510 return( ret );
2511 }
2512
2513 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2514
2515 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2516 &ssl->handshake->pmslen,
2517 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002518 POLARSSL_MPI_MAX_SIZE,
2519 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002520 {
2521 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2522 return( ret );
2523 }
2524
2525 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2526 }
2527 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002528#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002529 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2530 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2531 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002532#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002533 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002534 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002535 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2536 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002537 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002538 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002539 * opaque psk_identity<0..2^16-1>;
2540 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002541 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002542 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2543
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002544 i = 4;
2545 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002546 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2547 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002548
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002549 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2550 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002551
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002552#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002553 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002554 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002555 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002556 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002557 else
2558#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002559#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2560 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2561 {
2562 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2563 return( ret );
2564 }
2565 else
2566#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002567#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002568 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002569 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002570 /*
2571 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2572 */
2573 n = ssl->handshake->dhm_ctx.len;
2574 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2575 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002576
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002577 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002578 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002579 &ssl->out_msg[i], n,
2580 ssl->f_rng, ssl->p_rng );
2581 if( ret != 0 )
2582 {
2583 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2584 return( ret );
2585 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002586 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002587 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002588#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002589#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002590 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002591 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002592 /*
2593 * ClientECDiffieHellmanPublic public;
2594 */
2595 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2596 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2597 ssl->f_rng, ssl->p_rng );
2598 if( ret != 0 )
2599 {
2600 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2601 return( ret );
2602 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002603
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002604 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2605 }
2606 else
2607#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2608 {
2609 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002610 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002611 }
2612
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002613 if( ( ret = ssl_psk_derive_premaster( ssl,
2614 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002615 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002616 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002617 return( ret );
2618 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002619 }
2620 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002621#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002622#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002623 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002625 i = 4;
2626 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002627 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 }
Paul Bakkered27a042013-04-18 22:46:23 +02002629 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002630#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002631 {
2632 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002633 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002634 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002635 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 ssl->out_msglen = i + n;
2638 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2639 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2640
2641 ssl->state++;
2642
2643 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2644 {
2645 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2646 return( ret );
2647 }
2648
2649 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2650
2651 return( 0 );
2652}
2653
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002654#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2655 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002656 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2657 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002658static int ssl_write_certificate_verify( ssl_context *ssl )
2659{
Paul Bakkered27a042013-04-18 22:46:23 +02002660 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002661 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
2663 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2664
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002665 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2666 {
2667 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2668 return( ret );
2669 }
2670
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002671 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002672 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002673 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002674 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002675 {
2676 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2677 ssl->state++;
2678 return( 0 );
2679 }
2680
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002681 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2682 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002683}
2684#else
2685static int ssl_write_certificate_verify( ssl_context *ssl )
2686{
2687 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2688 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2689 size_t n = 0, offset = 0;
2690 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002691 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002692 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002693 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002694
2695 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2696
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002697 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2698 {
2699 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2700 return( ret );
2701 }
2702
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002703 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002704 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002705 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002706 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2707 {
2708 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2709 ssl->state++;
2710 return( 0 );
2711 }
2712
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002713 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 {
2715 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2716 ssl->state++;
2717 return( 0 );
2718 }
2719
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002720 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002722 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2723 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 }
2725
2726 /*
2727 * Make an RSA signature of the handshake digests
2728 */
Paul Bakker48916f92012-09-16 19:57:18 +00002729 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002731#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2732 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002733 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002734 {
Paul Bakker926af752012-11-23 13:38:07 +01002735 /*
2736 * digitally-signed struct {
2737 * opaque md5_hash[16];
2738 * opaque sha_hash[20];
2739 * };
2740 *
2741 * md5_hash
2742 * MD5(handshake_messages);
2743 *
2744 * sha_hash
2745 * SHA(handshake_messages);
2746 */
2747 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002748 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002749
2750 /*
2751 * For ECDSA, default hash is SHA-1 only
2752 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002753 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002754 {
2755 hash_start += 16;
2756 hashlen -= 16;
2757 md_alg = POLARSSL_MD_SHA1;
2758 }
Paul Bakker926af752012-11-23 13:38:07 +01002759 }
2760 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002761#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2762 POLARSSL_SSL_PROTO_TLS1_1 */
2763#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2764 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002765 {
2766 /*
2767 * digitally-signed struct {
2768 * opaque handshake_messages[handshake_messages_length];
2769 * };
2770 *
2771 * Taking shortcut here. We assume that the server always allows the
2772 * PRF Hash function and has sent it in the allowed signature
2773 * algorithms list received in the Certificate Request message.
2774 *
2775 * Until we encounter a server that does not, we will take this
2776 * shortcut.
2777 *
2778 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2779 * in order to satisfy 'weird' needs from the server side.
2780 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002781 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2782 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002783 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002784 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002785 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002786 }
2787 else
2788 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002789 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002790 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002791 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002792 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002793
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002794 /* Info from md_alg will be used instead */
2795 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002796 offset = 2;
2797 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002798 else
2799#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002800 {
2801 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002802 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002803 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002804
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002805 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002806 ssl->out_msg + 6 + offset, &n,
2807 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002808 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002809 SSL_DEBUG_RET( 1, "pk_sign", ret );
2810 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002811 }
Paul Bakker926af752012-11-23 13:38:07 +01002812
Paul Bakker1ef83d62012-04-11 12:09:53 +00002813 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2814 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Paul Bakker1ef83d62012-04-11 12:09:53 +00002816 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2818 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2819
2820 ssl->state++;
2821
2822 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2823 {
2824 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2825 return( ret );
2826 }
2827
2828 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2829
Paul Bakkered27a042013-04-18 22:46:23 +02002830 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002832#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2833 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2834 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Paul Bakkera503a632013-08-14 13:48:06 +02002836#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002837static int ssl_parse_new_session_ticket( ssl_context *ssl )
2838{
2839 int ret;
2840 uint32_t lifetime;
2841 size_t ticket_len;
2842 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002843 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002844
2845 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2846
2847 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2848 {
2849 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2850 return( ret );
2851 }
2852
2853 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2854 {
2855 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2856 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2857 }
2858
2859 /*
2860 * struct {
2861 * uint32 ticket_lifetime_hint;
2862 * opaque ticket<0..2^16-1>;
2863 * } NewSessionTicket;
2864 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002865 * 0 . 3 ticket_lifetime_hint
2866 * 4 . 5 ticket_len (n)
2867 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002868 */
2869 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002870 ssl->in_hslen < 6 + ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002871 {
2872 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2873 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2874 }
2875
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002876 msg = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002877
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002878 lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
2879 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002880
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002881 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
2882
2883 if( ticket_len + 6 + ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002884 {
2885 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2886 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2887 }
2888
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002889 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2890
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002891 /* We're not waiting for a NewSessionTicket message any more */
2892 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002893 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002894
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002895 /*
2896 * Zero-length ticket means the server changed his mind and doesn't want
2897 * to send a ticket after all, so just forget it
2898 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002899 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002900 return( 0 );
2901
Paul Bakker34617722014-06-13 17:20:13 +02002902 polarssl_zeroize( ssl->session_negotiate->ticket,
2903 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002904 polarssl_free( ssl->session_negotiate->ticket );
2905 ssl->session_negotiate->ticket = NULL;
2906 ssl->session_negotiate->ticket_len = 0;
2907
2908 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2909 {
2910 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2911 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2912 }
2913
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002914 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002915
2916 ssl->session_negotiate->ticket = ticket;
2917 ssl->session_negotiate->ticket_len = ticket_len;
2918 ssl->session_negotiate->ticket_lifetime = lifetime;
2919
2920 /*
2921 * RFC 5077 section 3.4:
2922 * "If the client receives a session ticket from the server, then it
2923 * discards any Session ID that was sent in the ServerHello."
2924 */
2925 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2926 ssl->session_negotiate->length = 0;
2927
2928 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2929
2930 return( 0 );
2931}
Paul Bakkera503a632013-08-14 13:48:06 +02002932#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002933
Paul Bakker5121ce52009-01-03 21:22:43 +00002934/*
Paul Bakker1961b702013-01-25 14:49:24 +01002935 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 */
Paul Bakker1961b702013-01-25 14:49:24 +01002937int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002938{
2939 int ret = 0;
2940
Paul Bakker1961b702013-01-25 14:49:24 +01002941 if( ssl->state == SSL_HANDSHAKE_OVER )
2942 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Paul Bakker1961b702013-01-25 14:49:24 +01002944 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2945
2946 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2947 return( ret );
2948
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002949#if defined(POLARSSL_SSL_PROTO_DTLS)
2950 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2951 ssl->handshake != NULL &&
2952 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2953 {
2954 if( ( ret = ssl_resend( ssl ) ) != 0 )
2955 return( ret );
2956 }
2957#endif
2958
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002959 /* Change state now, so that it is right in ssl_read_record(), used
2960 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
2961#if defined(POLARSSL_SSL_SESSION_TICKETS)
2962 if( ssl->state == SSL_SERVER_CHANGE_CIPHER_SPEC &&
2963 ssl->handshake->new_session_ticket != 0 )
2964 {
2965 ssl->state = SSL_SERVER_NEW_SESSION_TICKET;
2966 }
2967#endif
2968
Paul Bakker1961b702013-01-25 14:49:24 +01002969 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002970 {
Paul Bakker1961b702013-01-25 14:49:24 +01002971 case SSL_HELLO_REQUEST:
2972 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002973 break;
2974
Paul Bakker1961b702013-01-25 14:49:24 +01002975 /*
2976 * ==> ClientHello
2977 */
2978 case SSL_CLIENT_HELLO:
2979 ret = ssl_write_client_hello( ssl );
2980 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002981
Paul Bakker1961b702013-01-25 14:49:24 +01002982 /*
2983 * <== ServerHello
2984 * Certificate
2985 * ( ServerKeyExchange )
2986 * ( CertificateRequest )
2987 * ServerHelloDone
2988 */
2989 case SSL_SERVER_HELLO:
2990 ret = ssl_parse_server_hello( ssl );
2991 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002992
Paul Bakker1961b702013-01-25 14:49:24 +01002993 case SSL_SERVER_CERTIFICATE:
2994 ret = ssl_parse_certificate( ssl );
2995 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002996
Paul Bakker1961b702013-01-25 14:49:24 +01002997 case SSL_SERVER_KEY_EXCHANGE:
2998 ret = ssl_parse_server_key_exchange( ssl );
2999 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003000
Paul Bakker1961b702013-01-25 14:49:24 +01003001 case SSL_CERTIFICATE_REQUEST:
3002 ret = ssl_parse_certificate_request( ssl );
3003 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003004
Paul Bakker1961b702013-01-25 14:49:24 +01003005 case SSL_SERVER_HELLO_DONE:
3006 ret = ssl_parse_server_hello_done( ssl );
3007 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Paul Bakker1961b702013-01-25 14:49:24 +01003009 /*
3010 * ==> ( Certificate/Alert )
3011 * ClientKeyExchange
3012 * ( CertificateVerify )
3013 * ChangeCipherSpec
3014 * Finished
3015 */
3016 case SSL_CLIENT_CERTIFICATE:
3017 ret = ssl_write_certificate( ssl );
3018 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003019
Paul Bakker1961b702013-01-25 14:49:24 +01003020 case SSL_CLIENT_KEY_EXCHANGE:
3021 ret = ssl_write_client_key_exchange( ssl );
3022 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Paul Bakker1961b702013-01-25 14:49:24 +01003024 case SSL_CERTIFICATE_VERIFY:
3025 ret = ssl_write_certificate_verify( ssl );
3026 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
Paul Bakker1961b702013-01-25 14:49:24 +01003028 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3029 ret = ssl_write_change_cipher_spec( ssl );
3030 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
Paul Bakker1961b702013-01-25 14:49:24 +01003032 case SSL_CLIENT_FINISHED:
3033 ret = ssl_write_finished( ssl );
3034 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Paul Bakker1961b702013-01-25 14:49:24 +01003036 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003037 * <== ( NewSessionTicket )
3038 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003039 * Finished
3040 */
Paul Bakkera503a632013-08-14 13:48:06 +02003041#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003042 case SSL_SERVER_NEW_SESSION_TICKET:
3043 ret = ssl_parse_new_session_ticket( ssl );
3044 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003045#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003046
3047 case SSL_SERVER_CHANGE_CIPHER_SPEC:
3048 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003049 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Paul Bakker1961b702013-01-25 14:49:24 +01003051 case SSL_SERVER_FINISHED:
3052 ret = ssl_parse_finished( ssl );
3053 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003054
Paul Bakker1961b702013-01-25 14:49:24 +01003055 case SSL_FLUSH_BUFFERS:
3056 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3057 ssl->state = SSL_HANDSHAKE_WRAPUP;
3058 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003059
Paul Bakker1961b702013-01-25 14:49:24 +01003060 case SSL_HANDSHAKE_WRAPUP:
3061 ssl_handshake_wrapup( ssl );
3062 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003063
Paul Bakker1961b702013-01-25 14:49:24 +01003064 default:
3065 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3066 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3067 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003068
3069 return( ret );
3070}
Paul Bakker9af723c2014-05-01 13:03:14 +02003071#endif /* POLARSSL_SSL_CLI_C */