blob: 774cce4ffa3485f8c49152e82f425429c852dd90 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#if defined(POLARSSL_PLATFORM_C)
38#include "polarssl/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020039#else
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdlib.h>
45#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#include <basetsd.h>
49typedef UINT32 uint32_t;
50#else
51#include <inttypes.h>
52#endif
53
54#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakker34617722014-06-13 17:20:13 +020058#if defined(POLARSSL_SSL_SESSION_TICKETS)
59/* Implementation that should never be optimized out by the compiler */
60static void polarssl_zeroize( void *v, size_t n ) {
61 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
62}
63#endif
64
Paul Bakker0be444a2013-08-27 21:55:01 +020065#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010066static void ssl_write_hostname_ext( ssl_context *ssl,
67 unsigned char *buf,
68 size_t *olen )
69{
70 unsigned char *p = buf;
71
72 *olen = 0;
73
Paul Bakker66d5d072014-06-17 16:39:18 +020074 if( ssl->hostname == NULL )
Paul Bakkerd3edc862013-03-20 16:07:17 +010075 return;
76
77 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
78 ssl->hostname ) );
79
80 /*
81 * struct {
82 * NameType name_type;
83 * select (name_type) {
84 * case host_name: HostName;
85 * } name;
86 * } ServerName;
87 *
88 * enum {
89 * host_name(0), (255)
90 * } NameType;
91 *
92 * opaque HostName<1..2^16-1>;
93 *
94 * struct {
95 * ServerName server_name_list<1..2^16-1>
96 * } ServerNameList;
97 */
98 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
99 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
100
101 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
102 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
103
104 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
105 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
106
107 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
108 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
109 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
110
111 memcpy( p, ssl->hostname, ssl->hostname_len );
112
113 *olen = ssl->hostname_len + 9;
114}
Paul Bakker0be444a2013-08-27 21:55:01 +0200115#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100117#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118static void ssl_write_renegotiation_ext( ssl_context *ssl,
119 unsigned char *buf,
120 size_t *olen )
121{
122 unsigned char *p = buf;
123
124 *olen = 0;
125
126 if( ssl->renegotiation != SSL_RENEGOTIATION )
127 return;
128
129 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
130
131 /*
132 * Secure renegotiation
133 */
134 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
135 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
136
137 *p++ = 0x00;
138 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
139 *p++ = ssl->verify_data_len & 0xFF;
140
141 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
142
143 *olen = 5 + ssl->verify_data_len;
144}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100145#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100146
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100147/*
148 * Only if we handle at least one key exchange that needs signatures.
149 */
150#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
151 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100152static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
153 unsigned char *buf,
154 size_t *olen )
155{
156 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100157 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200158#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
159 unsigned char *sig_alg_list = buf + 6;
160#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100161
162 *olen = 0;
163
164 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
165 return;
166
167 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
168
169 /*
170 * Prepare signature_algorithms extension (TLS 1.2)
171 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200172#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200173#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100174 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
175 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
176 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
177 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
178#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200179#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100180 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
181 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
182 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
183 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
184#endif
185#if defined(POLARSSL_SHA1_C)
186 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
187 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
188#endif
189#if defined(POLARSSL_MD5_C)
190 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
191 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
192#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200193#endif /* POLARSSL_RSA_C */
194#if defined(POLARSSL_ECDSA_C)
195#if defined(POLARSSL_SHA512_C)
196 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
197 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
198 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
199 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
200#endif
201#if defined(POLARSSL_SHA256_C)
202 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
203 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
204 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
205 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
206#endif
207#if defined(POLARSSL_SHA1_C)
208 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
209 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
210#endif
211#if defined(POLARSSL_MD5_C)
212 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
213 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
214#endif
215#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100216
217 /*
218 * enum {
219 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
220 * sha512(6), (255)
221 * } HashAlgorithm;
222 *
223 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
224 * SignatureAlgorithm;
225 *
226 * struct {
227 * HashAlgorithm hash;
228 * SignatureAlgorithm signature;
229 * } SignatureAndHashAlgorithm;
230 *
231 * SignatureAndHashAlgorithm
232 * supported_signature_algorithms<2..2^16-2>;
233 */
234 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
235 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
236
237 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
238 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
239
240 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
241 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
242
Paul Bakkerd3edc862013-03-20 16:07:17 +0100243 *olen = 6 + sig_alg_len;
244}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100245#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
246 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100247
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200248#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100249static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
250 unsigned char *buf,
251 size_t *olen )
252{
253 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100254 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100255 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100256 const ecp_curve_info *info;
257#if defined(POLARSSL_SSL_SET_CURVES)
258 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100259#else
260 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100261#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100262
263 *olen = 0;
264
265 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
266
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100267#if defined(POLARSSL_SSL_SET_CURVES)
268 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200269 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100270 info = ecp_curve_info_from_grp_id( *grp_id );
271#else
272 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
273 {
274#endif
275
276 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
277 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200278 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200279
280 if( elliptic_curve_len == 0 )
281 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100282
283 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
284 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
285
286 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
287 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
288
289 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
290 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
291
Paul Bakkerd3edc862013-03-20 16:07:17 +0100292 *olen = 6 + elliptic_curve_len;
293}
294
295static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
296 unsigned char *buf,
297 size_t *olen )
298{
299 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200300 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100301
302 *olen = 0;
303
304 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
305
306 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
307 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
308
309 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100310 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200311
312 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100313 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
314
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200315 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100316}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200317#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100318
Paul Bakker05decb22013-08-15 13:33:48 +0200319#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200320static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
321 unsigned char *buf,
322 size_t *olen )
323{
324 unsigned char *p = buf;
325
326 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
327 *olen = 0;
328 return;
329 }
330
331 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
332
333 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
334 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
335
336 *p++ = 0x00;
337 *p++ = 1;
338
339 *p++ = ssl->mfl_code;
340
341 *olen = 5;
342}
Paul Bakker05decb22013-08-15 13:33:48 +0200343#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200344
Paul Bakker1f2bc622013-08-15 13:45:55 +0200345#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200346static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
347 unsigned char *buf, size_t *olen )
348{
349 unsigned char *p = buf;
350
351 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
352 {
353 *olen = 0;
354 return;
355 }
356
357 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
358
359 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
360 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
361
362 *p++ = 0x00;
363 *p++ = 0x00;
364
365 *olen = 4;
366}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200367#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200368
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100369#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
370static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
371 unsigned char *buf, size_t *olen )
372{
373 unsigned char *p = buf;
374
375 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
376 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
377 {
378 *olen = 0;
379 return;
380 }
381
382 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
383 "extension" ) );
384
385 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
386 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
387
388 *p++ = 0x00;
389 *p++ = 0x00;
390
391 *olen = 4;
392}
393#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
394
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200395#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
396static void ssl_write_extended_ms_ext( ssl_context *ssl,
397 unsigned char *buf, size_t *olen )
398{
399 unsigned char *p = buf;
400
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200401 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
402 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200403 {
404 *olen = 0;
405 return;
406 }
407
408 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
409 "extension" ) );
410
411 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
412 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
413
414 *p++ = 0x00;
415 *p++ = 0x00;
416
417 *olen = 4;
418}
419#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
420
Paul Bakkera503a632013-08-14 13:48:06 +0200421#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200422static void ssl_write_session_ticket_ext( ssl_context *ssl,
423 unsigned char *buf, size_t *olen )
424{
425 unsigned char *p = buf;
426 size_t tlen = ssl->session_negotiate->ticket_len;
427
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200428 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
429 {
430 *olen = 0;
431 return;
432 }
433
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200434 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
435
436 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
437 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
438
439 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
440 *p++ = (unsigned char)( ( tlen ) & 0xFF );
441
442 *olen = 4;
443
444 if( ssl->session_negotiate->ticket == NULL ||
445 ssl->session_negotiate->ticket_len == 0 )
446 {
447 return;
448 }
449
450 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
451
452 memcpy( p, ssl->session_negotiate->ticket, tlen );
453
454 *olen += tlen;
455}
Paul Bakkera503a632013-08-14 13:48:06 +0200456#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200457
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200458#if defined(POLARSSL_SSL_ALPN)
459static void ssl_write_alpn_ext( ssl_context *ssl,
460 unsigned char *buf, size_t *olen )
461{
462 unsigned char *p = buf;
463 const char **cur;
464
465 if( ssl->alpn_list == NULL )
466 {
467 *olen = 0;
468 return;
469 }
470
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200471 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200472
473 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
474 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
475
476 /*
477 * opaque ProtocolName<1..2^8-1>;
478 *
479 * struct {
480 * ProtocolName protocol_name_list<2..2^16-1>
481 * } ProtocolNameList;
482 */
483
484 /* Skip writing extension and list length for now */
485 p += 4;
486
487 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
488 {
489 *p = (unsigned char)( strlen( *cur ) & 0xFF );
490 memcpy( p + 1, *cur, *p );
491 p += 1 + *p;
492 }
493
494 *olen = p - buf;
495
496 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
497 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
498 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
499
500 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
501 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
502 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
503}
504#endif /* POLARSSL_SSL_ALPN */
505
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200506/*
507 * Generate random bytes for ClientHello
508 */
509static int ssl_generate_random( ssl_context *ssl )
510{
511 int ret;
512 unsigned char *p = ssl->handshake->randbytes;
513#if defined(POLARSSL_HAVE_TIME)
514 time_t t;
515#endif
516
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200517 /*
518 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
519 */
520#if defined(POLARSSL_SSL_PROTO_DTLS)
521 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
522 ssl->handshake->verify_cookie != NULL )
523 {
524 return( 0 );
525 }
526#endif
527
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200528#if defined(POLARSSL_HAVE_TIME)
529 t = time( NULL );
530 *p++ = (unsigned char)( t >> 24 );
531 *p++ = (unsigned char)( t >> 16 );
532 *p++ = (unsigned char)( t >> 8 );
533 *p++ = (unsigned char)( t );
534
535 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
536#else
537 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
538 return( ret );
539
540 p += 4;
541#endif /* POLARSSL_HAVE_TIME */
542
543 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
544 return( ret );
545
546 return( 0 );
547}
548
Paul Bakker5121ce52009-01-03 21:22:43 +0000549static int ssl_write_client_hello( ssl_context *ssl )
550{
Paul Bakker23986e52011-04-24 08:57:21 +0000551 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100552 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200554 unsigned char *p, *q;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200555 unsigned char offer_compress;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200556 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200557 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
560
Paul Bakkera9a028e2013-11-21 17:31:06 +0100561 if( ssl->f_rng == NULL )
562 {
563 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
564 return( POLARSSL_ERR_SSL_NO_RNG );
565 }
566
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100567#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000568 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100569#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000570 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000571 ssl->major_ver = ssl->min_major_ver;
572 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000573 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Paul Bakker490ecc82011-10-06 13:04:09 +0000575 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
576 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200577 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
578 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000579 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 /*
582 * 0 . 0 handshake type
583 * 1 . 3 handshake length
584 * 4 . 5 highest version supported
585 * 6 . 9 current UNIX time
586 * 10 . 37 random bytes
587 */
588 buf = ssl->out_msg;
589 p = buf + 4;
590
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100591 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
592 ssl->transport, p );
593 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
596 buf[4], buf[5] ) );
597
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200598 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
599 {
600 SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200601 return( ret );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200602 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200603
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200604 memcpy( p, ssl->handshake->randbytes, 32 );
605 SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
606 p += 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
608 /*
609 * 38 . 38 session id length
610 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100611 * 39+n . 39+n DTLS only: cookie length (1 byte)
612 * 40+n . .. DTSL only: cookie
613 * .. . .. ciphersuitelist length (2 bytes)
614 * .. . .. ciphersuitelist
615 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000616 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100617 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000618 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 */
Paul Bakker48916f92012-09-16 19:57:18 +0000620 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100622 if( n < 16 || n > 32 ||
623#if defined(POLARSSL_SSL_RENEGOTIATION)
624 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
625#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000626 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200627 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200629 }
630
Paul Bakkera503a632013-08-14 13:48:06 +0200631#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200632 /*
633 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
634 * generate and include a Session ID in the TLS ClientHello."
635 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100636#if defined(POLARSSL_SSL_RENEGOTIATION)
637 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
638#endif
639 if( ssl->session_negotiate->ticket != NULL &&
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200640 ssl->session_negotiate->ticket_len != 0 )
641 {
642 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
643
644 if( ret != 0 )
645 return( ret );
646
647 ssl->session_negotiate->length = n = 32;
648 }
Paul Bakkera503a632013-08-14 13:48:06 +0200649#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
651 *p++ = (unsigned char) n;
652
653 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000654 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
656 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
657 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
658
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100659 /*
660 * DTLS cookie
661 */
662#if defined(POLARSSL_SSL_PROTO_DTLS)
663 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
664 {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200665 if( ssl->handshake->verify_cookie == NULL )
666 {
667 SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
668 *p++ = 0;
669 }
670 else
671 {
672 SSL_DEBUG_BUF( 3, "client hello, cookie",
673 ssl->handshake->verify_cookie,
674 ssl->handshake->verify_cookie_len );
675
676 *p++ = ssl->handshake->verify_cookie_len;
677 memcpy( p, ssl->handshake->verify_cookie,
678 ssl->handshake->verify_cookie_len );
679 p += ssl->handshake->verify_cookie_len;
680 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100681 }
682#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Paul Bakker48916f92012-09-16 19:57:18 +0000684 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100685 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000686 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100687 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
688
689 /* Skip writing ciphersuite length for now */
690 n = 0;
691 q = p;
692 p += 2;
693
Paul Bakker48916f92012-09-16 19:57:18 +0000694 /*
695 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
696 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100697#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000698 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100699#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000700 {
701 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
702 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200703 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000704 }
705
Paul Bakker2fbefde2013-06-29 16:01:15 +0200706 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200708 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
709
710 if( ciphersuite_info == NULL )
711 continue;
712
713 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
714 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
715 continue;
716
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100717#if defined(POLARSSL_SSL_PROTO_DTLS)
718 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
719 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
720 continue;
721#endif
722
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100723 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
724 ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
725 continue;
726
Paul Bakkere3166ce2011-01-27 17:40:50 +0000727 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200728 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Paul Bakker2fbefde2013-06-29 16:01:15 +0200730 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200731 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
732 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 }
734
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200735 /* Some versions of OpenSSL don't handle it correctly if not at end */
736#if defined(POLARSSL_SSL_FALLBACK_SCSV)
737 if( ssl->fallback == SSL_IS_FALLBACK )
738 {
739 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
740 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
741 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
742 n++;
743 }
744#endif
745
Paul Bakker2fbefde2013-06-29 16:01:15 +0200746 *q++ = (unsigned char)( n >> 7 );
747 *q++ = (unsigned char)( n << 1 );
748
749 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
750
Paul Bakker2770fbd2012-07-03 13:30:23 +0000751#if defined(POLARSSL_ZLIB_SUPPORT)
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200752 offer_compress = 1;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000753#else
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200754 offer_compress = 0;
755#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200757 /*
758 * We don't support compression with DTLS right now: is many records come
759 * in the same datagram, uncompressing one could overwrite the next one.
760 * We don't want to add complexity for handling that case unless there is
761 * an actual need for it.
762 */
763#if defined(POLARSSL_SSL_PROTO_DTLS)
764 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
765 offer_compress = 0;
766#endif
767
768 if( offer_compress )
769 {
770 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
771 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
772 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
773
774 *p++ = 2;
775 *p++ = SSL_COMPRESS_DEFLATE;
776 *p++ = SSL_COMPRESS_NULL;
777 }
778 else
779 {
780 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
781 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
782 SSL_COMPRESS_NULL ) );
783
784 *p++ = 1;
785 *p++ = SSL_COMPRESS_NULL;
786 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000787
Paul Bakkerd3edc862013-03-20 16:07:17 +0100788 // First write extensions, then the total length
789 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200790#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100791 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
792 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200793#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100795#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100796 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
797 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100798#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000799
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100800#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
801 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100802 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
803 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200804#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000805
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200806#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100807 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
808 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100809
Paul Bakkerd3edc862013-03-20 16:07:17 +0100810 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
811 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100812#endif
813
Paul Bakker05decb22013-08-15 13:33:48 +0200814#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200815 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
816 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200817#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200818
Paul Bakker1f2bc622013-08-15 13:45:55 +0200819#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200820 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
821 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200822#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200823
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100824#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
825 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
826 ext_len += olen;
827#endif
828
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200829#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
830 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
831 ext_len += olen;
832#endif
833
Paul Bakkera503a632013-08-14 13:48:06 +0200834#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200835 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
836 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200837#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200838
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200839#if defined(POLARSSL_SSL_ALPN)
840 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
841 ext_len += olen;
842#endif
843
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100844 /* olen unused if all extensions are disabled */
845 ((void) olen);
846
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000847 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
848 ext_len ) );
849
Paul Bakkera7036632014-04-30 10:15:38 +0200850 if( ext_len > 0 )
851 {
852 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
853 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
854 p += ext_len;
855 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100856
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 ssl->out_msglen = p - buf;
858 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
859 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
860
861 ssl->state++;
862
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +0200863#if defined(POLARSSL_SSL_PROTO_DTLS)
864 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
865 ssl_send_flight_completed( ssl );
866#endif
867
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 if( ( ret = ssl_write_record( ssl ) ) != 0 )
869 {
870 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
871 return( ret );
872 }
873
874 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
875
876 return( 0 );
877}
878
Paul Bakker48916f92012-09-16 19:57:18 +0000879static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200880 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000881 size_t len )
882{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000883 int ret;
884
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100885#if defined(POLARSSL_SSL_RENEGOTIATION)
886 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000887 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100888 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000889 if( len != 1 + ssl->verify_data_len * 2 ||
890 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100891 safer_memcmp( buf + 1,
892 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
893 safer_memcmp( buf + 1 + ssl->verify_data_len,
894 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000895 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100896 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000897
898 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
899 return( ret );
900
Paul Bakker48916f92012-09-16 19:57:18 +0000901 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
902 }
903 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100904 else
905#endif /* POLARSSL_SSL_RENEGOTIATION */
906 {
907 if( len != 1 || buf[0] != 0x00 )
908 {
909 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
910
911 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
912 return( ret );
913
914 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
915 }
916
917 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
918 }
Paul Bakker48916f92012-09-16 19:57:18 +0000919
920 return( 0 );
921}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200922
Paul Bakker05decb22013-08-15 13:33:48 +0200923#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200924static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200925 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200926 size_t len )
927{
928 /*
929 * server should use the extension only if we did,
930 * and if so the server's value should match ours (and len is always 1)
931 */
932 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
933 len != 1 ||
934 buf[0] != ssl->mfl_code )
935 {
936 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
937 }
938
939 return( 0 );
940}
Paul Bakker05decb22013-08-15 13:33:48 +0200941#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000942
Paul Bakker1f2bc622013-08-15 13:45:55 +0200943#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200944static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
945 const unsigned char *buf,
946 size_t len )
947{
948 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
949 len != 0 )
950 {
951 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
952 }
953
954 ((void) buf);
955
956 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
957
958 return( 0 );
959}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200960#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200961
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100962#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
963static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
964 const unsigned char *buf,
965 size_t len )
966{
967 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
968 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
969 len != 0 )
970 {
971 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
972 }
973
974 ((void) buf);
975
976 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
977
978 return( 0 );
979}
980#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
981
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200982#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
983static int ssl_parse_extended_ms_ext( ssl_context *ssl,
984 const unsigned char *buf,
985 size_t len )
986{
987 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200988 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200989 len != 0 )
990 {
991 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
992 }
993
994 ((void) buf);
995
996 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
997
998 return( 0 );
999}
1000#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1001
Paul Bakkera503a632013-08-14 13:48:06 +02001002#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001003static int ssl_parse_session_ticket_ext( ssl_context *ssl,
1004 const unsigned char *buf,
1005 size_t len )
1006{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001007 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
1008 len != 0 )
1009 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001010 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001011 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001012
1013 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001014
1015 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001016
1017 return( 0 );
1018}
Paul Bakkera503a632013-08-14 13:48:06 +02001019#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001020
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001021#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001022static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
1023 const unsigned char *buf,
1024 size_t len )
1025{
1026 size_t list_size;
1027 const unsigned char *p;
1028
1029 list_size = buf[0];
1030 if( list_size + 1 != len )
1031 {
1032 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1033 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1034 }
1035
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001036 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001037 while( list_size > 0 )
1038 {
1039 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
1040 p[0] == POLARSSL_ECP_PF_COMPRESSED )
1041 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001042 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001043 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1044 return( 0 );
1045 }
1046
1047 list_size--;
1048 p++;
1049 }
1050
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +02001051 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1052 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001053}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001054#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001055
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001056#if defined(POLARSSL_SSL_ALPN)
1057static int ssl_parse_alpn_ext( ssl_context *ssl,
1058 const unsigned char *buf, size_t len )
1059{
1060 size_t list_len, name_len;
1061 const char **p;
1062
1063 /* If we didn't send it, the server shouldn't send it */
1064 if( ssl->alpn_list == NULL )
1065 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1066
1067 /*
1068 * opaque ProtocolName<1..2^8-1>;
1069 *
1070 * struct {
1071 * ProtocolName protocol_name_list<2..2^16-1>
1072 * } ProtocolNameList;
1073 *
1074 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1075 */
1076
1077 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1078 if( len < 4 )
1079 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1080
1081 list_len = ( buf[0] << 8 ) | buf[1];
1082 if( list_len != len - 2 )
1083 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1084
1085 name_len = buf[2];
1086 if( name_len != list_len - 1 )
1087 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1088
1089 /* Check that the server chosen protocol was in our list and save it */
1090 for( p = ssl->alpn_list; *p != NULL; p++ )
1091 {
1092 if( name_len == strlen( *p ) &&
1093 memcmp( buf + 3, *p, name_len ) == 0 )
1094 {
1095 ssl->alpn_chosen = *p;
1096 return( 0 );
1097 }
1098 }
1099
1100 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1101}
1102#endif /* POLARSSL_SSL_ALPN */
1103
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001104/*
1105 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1106 */
1107#if defined(POLARSSL_SSL_PROTO_DTLS)
1108static int ssl_parse_hello_verify_request( ssl_context *ssl )
1109{
Manuel Pégourié-Gonnard069eb792014-09-10 20:08:29 +02001110 const unsigned char *p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001111 int major_ver, minor_ver;
1112 unsigned char cookie_len;
1113
1114 SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
1115
1116 /*
1117 * struct {
1118 * ProtocolVersion server_version;
1119 * opaque cookie<0..2^8-1>;
1120 * } HelloVerifyRequest;
1121 */
1122 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
1123 ssl_read_version( &major_ver, &minor_ver, ssl->transport, p );
1124 p += 2;
1125
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02001126 /*
1127 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1128 * even is lower than our min version.
1129 */
1130 if( major_ver < SSL_MAJOR_VERSION_3 ||
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001131 minor_ver < SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02001132 major_ver > ssl->max_major_ver ||
1133 minor_ver > ssl->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001134 {
1135 SSL_DEBUG_MSG( 1, ( "bad server version" ) );
1136
1137 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1138 SSL_ALERT_MSG_PROTOCOL_VERSION );
1139
1140 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1141 }
1142
1143 cookie_len = *p++;
1144 SSL_DEBUG_BUF( 3, "cookie", (unsigned char *) p, cookie_len );
1145
1146 polarssl_free( ssl->handshake->verify_cookie );
1147
1148 ssl->handshake->verify_cookie = polarssl_malloc( cookie_len );
1149 if( ssl->handshake->verify_cookie == NULL )
1150 {
1151 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", cookie_len ) );
1152 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1153 }
1154
1155 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
1156 ssl->handshake->verify_cookie_len = cookie_len;
1157
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001158 /* Start over at ClientHello */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001159 ssl->state = SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001160 ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001161
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001162 ssl_recv_flight_completed( ssl );
1163
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001164 SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
1165
1166 return( 0 );
1167}
1168#endif /* POLARSSL_SSL_PROTO_DTLS */
1169
Paul Bakker5121ce52009-01-03 21:22:43 +00001170static int ssl_parse_server_hello( ssl_context *ssl )
1171{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001172 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001173 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001174 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001175 unsigned char *buf, *ext;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001176 unsigned char comp, accept_comp;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001177#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001178 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001179#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001180 int handshake_failure = 0;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001181 const ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001182#if defined(POLARSSL_DEBUG_C)
1183 uint32_t t;
1184#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001185
1186 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1187
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 buf = ssl->in_msg;
1189
1190 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1191 {
1192 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1193 return( ret );
1194 }
1195
1196 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1197 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001198#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001199 if( ssl->renegotiation == SSL_RENEGOTIATION )
1200 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001201 ssl->renego_records_seen++;
1202
1203 if( ssl->renego_max_records >= 0 &&
1204 ssl->renego_records_seen > ssl->renego_max_records )
1205 {
1206 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1207 "but not honored by server" ) );
1208 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1209 }
1210
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001211 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1212 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1213 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001214#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001215
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001217 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 }
1219
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001220#if defined(POLARSSL_SSL_PROTO_DTLS)
1221 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1222 {
1223 if( buf[0] == SSL_HS_HELLO_VERIFY_REQUEST )
1224 {
1225 SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1226 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1227 return( ssl_parse_hello_verify_request( ssl ) );
1228 }
1229 else
1230 {
1231 /* We made it through the verification process */
1232 polarssl_free( ssl->handshake->verify_cookie );
1233 ssl->handshake->verify_cookie = NULL;
1234 ssl->handshake->verify_cookie_len = 0;
1235 }
1236 }
1237#endif /* POLARSSL_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001239 if( ssl->in_hslen < 38 + ssl_hs_hdr_len( ssl ) ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001240 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 {
1242 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001243 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 }
1245
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001246 /*
1247 * 0 . 1 server_version
1248 * 2 . 33 random (maybe including 4 bytes of Unix time)
1249 * 34 . 34 session_id length = n
1250 * 35 . 34+n session_id
1251 * 35+n . 36+n cipher_suite
1252 * 37+n . 37+n compression_method
1253 *
1254 * 38+n . 39+n extensions length (optional)
1255 * 40+n . .. extensions
1256 */
1257 buf += ssl_hs_hdr_len( ssl );
1258
1259 SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001260 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001261 ssl->transport, buf + 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001263 if( ssl->major_ver < ssl->min_major_ver ||
1264 ssl->minor_ver < ssl->min_minor_ver ||
1265 ssl->major_ver > ssl->max_major_ver ||
1266 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001267 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001268 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
1269 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1270 ssl->min_major_ver, ssl->min_minor_ver,
1271 ssl->major_ver, ssl->minor_ver,
1272 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001273
1274 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1275 SSL_ALERT_MSG_PROTOCOL_VERSION );
1276
1277 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1278 }
1279
Paul Bakker1504af52012-02-11 16:17:43 +00001280#if defined(POLARSSL_DEBUG_C)
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001281 t = ( (uint32_t) buf[2] << 24 )
1282 | ( (uint32_t) buf[3] << 16 )
1283 | ( (uint32_t) buf[4] << 8 )
1284 | ( (uint32_t) buf[5] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001285 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001286#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001288 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001290 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001292 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Paul Bakker48916f92012-09-16 19:57:18 +00001294 if( n > 32 )
1295 {
1296 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1297 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1298 }
1299
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001300 if( ssl->in_hslen > 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001301 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001302 ext_len = ( ( buf[38 + n] << 8 )
1303 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001304
Paul Bakker48916f92012-09-16 19:57:18 +00001305 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001306 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001307 {
1308 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1309 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 }
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001312 else if( ssl->in_hslen == 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001313 {
1314 ext_len = 0;
1315 }
1316 else
1317 {
1318 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1319 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001322 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001323 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001324
1325 /*
1326 * Read and check compression
1327 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001328 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001330#if defined(POLARSSL_ZLIB_SUPPORT)
1331 accept_comp = 1;
1332#else
1333 accept_comp = 0;
1334#endif
1335
1336 /* See comments in ssl_write_client_hello() */
1337#if defined(POLARSSL_SSL_PROTO_DTLS)
1338 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1339 accept_comp = 0;
1340#endif
1341
1342 if( ( accept_comp == 0 && comp != SSL_COMPRESS_NULL ) ||
1343 ( comp != SSL_COMPRESS_NULL && comp != SSL_COMPRESS_DEFLATE ) )
1344 {
1345 SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
1346 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1347 }
1348
Paul Bakker380da532012-04-18 16:10:25 +00001349 /*
1350 * Initialize update checksum functions
1351 */
Paul Bakker68884e32013-01-07 18:20:04 +01001352 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1353
1354 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1355 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001356 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001357 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1358 }
Paul Bakker380da532012-04-18 16:10:25 +00001359
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001360 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1361
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001363 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
1365 /*
1366 * Check if the session can be resumed
1367 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001368 if( ssl->handshake->resume == 0 || n == 0 ||
1369#if defined(POLARSSL_SSL_RENEGOTIATION)
1370 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1371#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001372 ssl->session_negotiate->ciphersuite != i ||
1373 ssl->session_negotiate->compression != comp ||
1374 ssl->session_negotiate->length != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001375 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 {
1377 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001378 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001379#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001380 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001381#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001382 ssl->session_negotiate->ciphersuite = i;
1383 ssl->session_negotiate->compression = comp;
1384 ssl->session_negotiate->length = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001385 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 }
1387 else
1388 {
1389 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001390
1391 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1392 {
1393 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1394 return( ret );
1395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 }
1397
1398 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001399 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Paul Bakkere3166ce2011-01-27 17:40:50 +00001401 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001402 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001404 suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1405 if( suite_info == NULL ||
1406 ( ssl->arc4_disabled &&
1407 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) )
1408 {
1409 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1410 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1411 }
1412
1413
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 i = 0;
1415 while( 1 )
1416 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001417 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 {
1419 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001420 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 }
1422
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001423 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1424 ssl->session_negotiate->ciphersuite )
1425 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001427 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 }
1429
Paul Bakker2770fbd2012-07-03 13:30:23 +00001430 if( comp != SSL_COMPRESS_NULL
1431#if defined(POLARSSL_ZLIB_SUPPORT)
1432 && comp != SSL_COMPRESS_DEFLATE
1433#endif
1434 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 {
1436 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001437 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 }
Paul Bakker48916f92012-09-16 19:57:18 +00001439 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001441 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001442
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001443 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1444
Paul Bakker48916f92012-09-16 19:57:18 +00001445 while( ext_len )
1446 {
1447 unsigned int ext_id = ( ( ext[0] << 8 )
1448 | ( ext[1] ) );
1449 unsigned int ext_size = ( ( ext[2] << 8 )
1450 | ( ext[3] ) );
1451
1452 if( ext_size + 4 > ext_len )
1453 {
1454 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1455 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1456 }
1457
1458 switch( ext_id )
1459 {
1460 case TLS_EXT_RENEGOTIATION_INFO:
1461 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001462#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001463 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001464#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001465
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001466 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1467 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001468 return( ret );
1469
1470 break;
1471
Paul Bakker05decb22013-08-15 13:33:48 +02001472#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001473 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1474 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1475
1476 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1477 ext + 4, ext_size ) ) != 0 )
1478 {
1479 return( ret );
1480 }
1481
1482 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001483#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001484
Paul Bakker1f2bc622013-08-15 13:45:55 +02001485#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001486 case TLS_EXT_TRUNCATED_HMAC:
1487 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1488
1489 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1490 ext + 4, ext_size ) ) != 0 )
1491 {
1492 return( ret );
1493 }
1494
1495 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001496#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001497
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001498#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1499 case TLS_EXT_ENCRYPT_THEN_MAC:
1500 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1501
1502 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1503 ext + 4, ext_size ) ) != 0 )
1504 {
1505 return( ret );
1506 }
1507
1508 break;
1509#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1510
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001511#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1512 case TLS_EXT_EXTENDED_MASTER_SECRET:
1513 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1514
1515 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1516 ext + 4, ext_size ) ) != 0 )
1517 {
1518 return( ret );
1519 }
1520
1521 break;
1522#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1523
Paul Bakkera503a632013-08-14 13:48:06 +02001524#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001525 case TLS_EXT_SESSION_TICKET:
1526 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1527
1528 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1529 ext + 4, ext_size ) ) != 0 )
1530 {
1531 return( ret );
1532 }
1533
1534 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001535#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001536
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001537#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001538 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1539 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1540
1541 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1542 ext + 4, ext_size ) ) != 0 )
1543 {
1544 return( ret );
1545 }
1546
1547 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001548#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001549
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001550#if defined(POLARSSL_SSL_ALPN)
1551 case TLS_EXT_ALPN:
1552 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1553
1554 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1555 return( ret );
1556
1557 break;
1558#endif /* POLARSSL_SSL_ALPN */
1559
Paul Bakker48916f92012-09-16 19:57:18 +00001560 default:
1561 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1562 ext_id ) );
1563 }
1564
1565 ext_len -= 4 + ext_size;
1566 ext += 4 + ext_size;
1567
1568 if( ext_len > 0 && ext_len < 4 )
1569 {
1570 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1571 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1572 }
1573 }
1574
1575 /*
1576 * Renegotiation security checks
1577 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001578 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1579 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001580 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001581 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1582 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001583 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001584#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001585 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1586 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1587 renegotiation_info_seen == 0 )
1588 {
1589 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1590 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001591 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001592 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1593 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1594 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001595 {
1596 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001597 handshake_failure = 1;
1598 }
1599 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1600 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1601 renegotiation_info_seen == 1 )
1602 {
1603 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1604 handshake_failure = 1;
1605 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001606#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001607
1608 if( handshake_failure == 1 )
1609 {
1610 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1611 return( ret );
1612
Paul Bakker48916f92012-09-16 19:57:18 +00001613 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
1616 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1617
1618 return( 0 );
1619}
1620
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001621#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1622 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001623static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1624 unsigned char *end )
1625{
1626 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1627
Paul Bakker29e1f122013-04-16 13:07:56 +02001628 /*
1629 * Ephemeral DH parameters:
1630 *
1631 * struct {
1632 * opaque dh_p<1..2^16-1>;
1633 * opaque dh_g<1..2^16-1>;
1634 * opaque dh_Ys<1..2^16-1>;
1635 * } ServerDHParams;
1636 */
1637 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1638 {
1639 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1640 return( ret );
1641 }
1642
1643 if( ssl->handshake->dhm_ctx.len < 64 ||
1644 ssl->handshake->dhm_ctx.len > 512 )
1645 {
1646 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1647 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1648 }
1649
1650 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1651 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1652 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001653
1654 return( ret );
1655}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001656#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1657 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001658
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001659#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001660 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001661 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1662 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1663 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1664static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1665{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001666 const ecp_curve_info *curve_info;
1667
1668 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1669 if( curve_info == NULL )
1670 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001671 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1672 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001673 }
1674
1675 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001676
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001677#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1678 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1679#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001680 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1681 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001682#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001683 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001684
1685 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1686
1687 return( 0 );
1688}
Paul Bakker9af723c2014-05-01 13:03:14 +02001689#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1690 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1691 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1692 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1693 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001694
1695#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1696 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001697 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001698static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1699 unsigned char **p,
1700 unsigned char *end )
1701{
1702 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1703
Paul Bakker29e1f122013-04-16 13:07:56 +02001704 /*
1705 * Ephemeral ECDH parameters:
1706 *
1707 * struct {
1708 * ECParameters curve_params;
1709 * ECPoint public;
1710 * } ServerECDHParams;
1711 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001712 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1713 (const unsigned char **) p, end ) ) != 0 )
1714 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001715 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001716 return( ret );
1717 }
1718
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001719 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001720 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001721 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001722 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1723 }
1724
Paul Bakker29e1f122013-04-16 13:07:56 +02001725 return( ret );
1726}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001727#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001728 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1729 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001730
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001731#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001732static int ssl_parse_server_psk_hint( ssl_context *ssl,
1733 unsigned char **p,
1734 unsigned char *end )
1735{
1736 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001737 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001738 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001739
1740 /*
1741 * PSK parameters:
1742 *
1743 * opaque psk_identity_hint<0..2^16-1>;
1744 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001745 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001746 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001747
1748 if( (*p) + len > end )
1749 {
1750 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1751 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1752 }
1753
1754 // TODO: Retrieve PSK identity hint and callback to app
1755 //
1756 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001757 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001758
1759 return( ret );
1760}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001761#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001762
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001763#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1764 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1765/*
1766 * Generate a pre-master secret and encrypt it with the server's RSA key
1767 */
1768static int ssl_write_encrypted_pms( ssl_context *ssl,
1769 size_t offset, size_t *olen,
1770 size_t pms_offset )
1771{
1772 int ret;
1773 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1774 unsigned char *p = ssl->handshake->premaster + pms_offset;
1775
1776 /*
1777 * Generate (part of) the pre-master as
1778 * struct {
1779 * ProtocolVersion client_version;
1780 * opaque random[46];
1781 * } PreMasterSecret;
1782 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001783 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1784 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001785
1786 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1787 {
1788 SSL_DEBUG_RET( 1, "f_rng", ret );
1789 return( ret );
1790 }
1791
1792 ssl->handshake->pmslen = 48;
1793
1794 /*
1795 * Now write it out, encrypted
1796 */
1797 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1798 POLARSSL_PK_RSA ) )
1799 {
1800 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1801 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1802 }
1803
1804 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1805 p, ssl->handshake->pmslen,
1806 ssl->out_msg + offset + len_bytes, olen,
1807 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1808 ssl->f_rng, ssl->p_rng ) ) != 0 )
1809 {
1810 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1811 return( ret );
1812 }
1813
1814#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1815 defined(POLARSSL_SSL_PROTO_TLS1_2)
1816 if( len_bytes == 2 )
1817 {
1818 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1819 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1820 *olen += 2;
1821 }
1822#endif
1823
1824 return( 0 );
1825}
1826#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1827 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001828
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001829#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001830#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001831 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1832 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001833static int ssl_parse_signature_algorithm( ssl_context *ssl,
1834 unsigned char **p,
1835 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001836 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001837 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001838{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001839 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001840 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001841 *pk_alg = POLARSSL_PK_NONE;
1842
1843 /* Only in TLS 1.2 */
1844 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1845 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001846 return( 0 );
1847 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001848
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001849 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001850 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1851
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001852 /*
1853 * Get hash algorithm
1854 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001855 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001856 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001857 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1858 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001859 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1860 }
1861
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001862 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001863 * Get signature algorithm
1864 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001865 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001866 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001867 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1868 "SignatureAlgorithm %d", (*p)[1] ) );
1869 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001870 }
1871
1872 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1873 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1874 *p += 2;
1875
1876 return( 0 );
1877}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001878#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001879 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1880 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001881#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001882
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001883
1884#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1885 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1886static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1887{
1888 int ret;
1889 const ecp_keypair *peer_key;
1890
1891 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1892 POLARSSL_PK_ECKEY ) )
1893 {
1894 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1895 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1896 }
1897
1898 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1899
1900 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1901 POLARSSL_ECDH_THEIRS ) ) != 0 )
1902 {
1903 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1904 return( ret );
1905 }
1906
1907 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1908 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001909 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001910 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1911 }
1912
1913 return( ret );
1914}
1915#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1916 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1917
Paul Bakker41c83d32013-03-20 14:39:14 +01001918static int ssl_parse_server_key_exchange( ssl_context *ssl )
1919{
Paul Bakker23986e52011-04-24 08:57:21 +00001920 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001921 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001922 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001923
1924 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1925
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001926#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001927 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 {
1929 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1930 ssl->state++;
1931 return( 0 );
1932 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001933 ((void) p);
1934 ((void) end);
1935#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001937#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1938 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1939 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1940 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1941 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001942 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1943 {
1944 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1945 return( ret );
1946 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001947
1948 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1949 ssl->state++;
1950 return( 0 );
1951 }
1952 ((void) p);
1953 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001954#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1955 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001956
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1958 {
1959 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1960 return( ret );
1961 }
1962
1963 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1964 {
1965 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001966 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 }
1968
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001969 /*
1970 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1971 * doesn't use a psk_identity_hint
1972 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1974 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001975 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1976 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001977 {
1978 ssl->record_read = 1;
1979 goto exit;
1980 }
1981
1982 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1983 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 }
1985
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001986 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001987 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001988 SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001989
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001990#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1991 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1992 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1993 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1994 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1995 {
1996 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1997 {
1998 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1999 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2000 }
2001 } /* FALLTROUGH */
2002#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
2003
2004#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2005 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2006 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2007 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2008 ; /* nothing more to do */
2009 else
2010#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2011 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
2012#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2013 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2014 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2015 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002016 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002017 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002018 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002019 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002020 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2021 }
2022 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002023 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002024#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2025 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002026#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002027 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002028 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2029 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002030 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002031 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002032 {
2033 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2034 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002035 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2036 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2037 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002038 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002039 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002040#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002041 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002042 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002043 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002044 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002045 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002046 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002047
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002048#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002049 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2050 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02002051 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002052 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2053 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002054 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002055 size_t sig_len, hashlen;
2056 unsigned char hash[64];
2057 md_type_t md_alg = POLARSSL_MD_NONE;
2058 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002059 unsigned char *params = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002060 size_t params_len = p - params;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002061
Paul Bakker29e1f122013-04-16 13:07:56 +02002062 /*
2063 * Handle the digitally-signed structure
2064 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002065#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2066 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002067 {
Paul Bakker9659dae2013-08-28 16:21:34 +02002068 if( ssl_parse_signature_algorithm( ssl, &p, end,
2069 &md_alg, &pk_alg ) != 0 )
2070 {
2071 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2072 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2073 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002074
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002075 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002076 {
2077 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2078 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2079 }
2080 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002081 else
Paul Bakker9af723c2014-05-01 13:03:14 +02002082#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002083#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2084 defined(POLARSSL_SSL_PROTO_TLS1_1)
2085 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002086 {
2087 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002088
Paul Bakker9659dae2013-08-28 16:21:34 +02002089 /* Default hash for ECDSA is SHA-1 */
2090 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
2091 md_alg = POLARSSL_MD_SHA1;
2092 }
2093 else
2094#endif
2095 {
2096 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002097 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02002098 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002099
2100 /*
2101 * Read signature
2102 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002103 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002104 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002105
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002106 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002107 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002108 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01002109 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2110 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002111
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002112 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002113
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002114 /*
2115 * Compute the hash that has been signed
2116 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002117#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2118 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002119 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002120 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002121 md5_context md5;
2122 sha1_context sha1;
2123
Paul Bakker5b4af392014-06-26 12:09:34 +02002124 md5_init( &md5 );
2125 sha1_init( &sha1 );
2126
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002127 hashlen = 36;
2128
Paul Bakker29e1f122013-04-16 13:07:56 +02002129 /*
2130 * digitally-signed struct {
2131 * opaque md5_hash[16];
2132 * opaque sha_hash[20];
2133 * };
2134 *
2135 * md5_hash
2136 * MD5(ClientHello.random + ServerHello.random
2137 * + ServerParams);
2138 * sha_hash
2139 * SHA(ClientHello.random + ServerHello.random
2140 * + ServerParams);
2141 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002142 md5_starts( &md5 );
2143 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002144 md5_update( &md5, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002145 md5_finish( &md5, hash );
2146
2147 sha1_starts( &sha1 );
2148 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002149 sha1_update( &sha1, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002150 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02002151
2152 md5_free( &md5 );
2153 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02002154 }
2155 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002156#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2157 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002158#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2159 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002160 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002161 {
2162 md_context_t ctx;
2163
Paul Bakker84bbeb52014-07-01 14:53:22 +02002164 md_init( &ctx );
2165
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002166 /* Info from md_alg will be used instead */
2167 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02002168
2169 /*
2170 * digitally-signed struct {
2171 * opaque client_random[32];
2172 * opaque server_random[32];
2173 * ServerDHParams params;
2174 * };
2175 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002176 if( ( ret = md_init_ctx( &ctx,
2177 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002178 {
2179 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2180 return( ret );
2181 }
2182
2183 md_starts( &ctx );
2184 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002185 md_update( &ctx, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002186 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002187 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002188 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002189 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002190#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2191 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002192 {
Paul Bakker577e0062013-08-28 11:57:20 +02002193 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002194 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002195 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002196
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002197 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2198 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002199
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002200 /*
2201 * Verify signature
2202 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002203 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002204 {
2205 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2206 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2207 }
2208
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002209 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2210 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002211 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002212 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002213 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002214 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002216#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002217 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2218 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002219
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002220exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 ssl->state++;
2222
2223 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2224
2225 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002226}
2227
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002228#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2229 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2230 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2231 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2232static int ssl_parse_certificate_request( ssl_context *ssl )
2233{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002234 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2235
2236 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2237
2238 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2239 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2240 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2241 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2242 {
2243 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2244 ssl->state++;
2245 return( 0 );
2246 }
2247
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002248 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2249 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002250}
2251#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002252static int ssl_parse_certificate_request( ssl_context *ssl )
2253{
2254 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002255 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002256 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002257 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002258 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002259
2260 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2261
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002262 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2263 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2264 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2265 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2266 {
2267 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2268 ssl->state++;
2269 return( 0 );
2270 }
2271
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002272 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002274 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2275 {
2276 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2277 return( ret );
2278 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002280 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2281 {
2282 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2283 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2284 }
2285
2286 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
2288
2289 ssl->client_auth = 0;
2290 ssl->state++;
2291
2292 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2293 ssl->client_auth++;
2294
2295 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2296 ssl->client_auth ? "a" : "no" ) );
2297
Paul Bakker926af752012-11-23 13:38:07 +01002298 if( ssl->client_auth == 0 )
2299 goto exit;
2300
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002301 ssl->record_read = 0;
2302
Paul Bakker926af752012-11-23 13:38:07 +01002303 // TODO: handshake_failure alert for an anonymous server to request
2304 // client authentication
2305
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002306 /*
2307 * struct {
2308 * ClientCertificateType certificate_types<1..2^8-1>;
2309 * SignatureAndHashAlgorithm
2310 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2311 * DistinguishedName certificate_authorities<0..2^16-1>;
2312 * } CertificateRequest;
2313 */
Paul Bakker926af752012-11-23 13:38:07 +01002314 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002315
Paul Bakker926af752012-11-23 13:38:07 +01002316 // Retrieve cert types
2317 //
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002318 cert_type_len = buf[ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002319 n = cert_type_len;
2320
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002321 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002322 {
2323 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2324 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2325 }
2326
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002327 p = buf + ssl_hs_hdr_len( ssl ) + 1;
Paul Bakker926af752012-11-23 13:38:07 +01002328 while( cert_type_len > 0 )
2329 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002330#if defined(POLARSSL_RSA_C)
2331 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002332 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002333 {
2334 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2335 break;
2336 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002337 else
2338#endif
2339#if defined(POLARSSL_ECDSA_C)
2340 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002341 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002342 {
2343 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2344 break;
2345 }
2346 else
2347#endif
2348 {
2349 ; /* Unsupported cert type, ignore */
2350 }
Paul Bakker926af752012-11-23 13:38:07 +01002351
2352 cert_type_len--;
2353 p++;
2354 }
2355
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002356#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002357 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2358 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002359 /* Ignored, see comments about hash in write_certificate_verify */
2360 // TODO: should check the signature part against our pk_key though
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002361 size_t sig_alg_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2362 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002363
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002364 p = buf + ssl_hs_hdr_len( ssl ) + 3 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002365 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002366 n += sig_alg_len;
2367
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002368 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002369 {
2370 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2371 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2372 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002373 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002375
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002376 /* Ignore certificate_authorities, we only have one cert anyway */
2377 // TODO: should not send cert if no CA matches
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002378 dn_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + m + n] << 8 )
2379 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002380
2381 n += dn_len;
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002382 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 3 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002383 {
2384 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2385 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2386 }
2387
2388exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2390
2391 return( 0 );
2392}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002393#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2394 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2395 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2396 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
2398static int ssl_parse_server_hello_done( ssl_context *ssl )
2399{
2400 int ret;
2401
2402 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2403
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002404 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 {
2406 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2407 {
2408 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2409 return( ret );
2410 }
2411
2412 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2413 {
2414 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002415 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 }
2417 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002418 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002420 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) ||
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2422 {
2423 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002424 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 }
2426
2427 ssl->state++;
2428
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002429#if defined(POLARSSL_SSL_PROTO_DTLS)
2430 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2431 ssl_recv_flight_completed( ssl );
2432#endif
2433
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2435
2436 return( 0 );
2437}
2438
2439static int ssl_write_client_key_exchange( ssl_context *ssl )
2440{
Paul Bakker23986e52011-04-24 08:57:21 +00002441 int ret;
2442 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002443 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2446
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002447#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002448 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 /*
2451 * DHM key exchange -- send G^X mod P
2452 */
Paul Bakker48916f92012-09-16 19:57:18 +00002453 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
2455 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2456 ssl->out_msg[5] = (unsigned char)( n );
2457 i = 6;
2458
Paul Bakker29b64762012-09-25 09:36:44 +00002459 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002460 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 &ssl->out_msg[i], n,
2462 ssl->f_rng, ssl->p_rng );
2463 if( ret != 0 )
2464 {
2465 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2466 return( ret );
2467 }
2468
Paul Bakker48916f92012-09-16 19:57:18 +00002469 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2470 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002472 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Paul Bakker48916f92012-09-16 19:57:18 +00002474 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2475 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002476 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002477 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 {
2479 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2480 return( ret );
2481 }
2482
Paul Bakker48916f92012-09-16 19:57:18 +00002483 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 }
2485 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002486#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002487#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002488 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2489 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2490 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002491 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002492 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2493 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2494 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002495 {
2496 /*
2497 * ECDH key exchange -- send client public value
2498 */
2499 i = 4;
2500
2501 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2502 &n,
2503 &ssl->out_msg[i], 1000,
2504 ssl->f_rng, ssl->p_rng );
2505 if( ret != 0 )
2506 {
2507 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2508 return( ret );
2509 }
2510
2511 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2512
2513 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2514 &ssl->handshake->pmslen,
2515 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002516 POLARSSL_MPI_MAX_SIZE,
2517 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002518 {
2519 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2520 return( ret );
2521 }
2522
2523 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2524 }
2525 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002526#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002527 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2528 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2529 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002530#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002531 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002532 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002533 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2534 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002535 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002536 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002537 * opaque psk_identity<0..2^16-1>;
2538 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002539 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002540 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2541
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002542 i = 4;
2543 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002544 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2545 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002546
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002547 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2548 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002549
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002550#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002551 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002552 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002553 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002554 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002555 else
2556#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002557#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2558 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2559 {
2560 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2561 return( ret );
2562 }
2563 else
2564#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002565#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002566 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002567 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002568 /*
2569 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2570 */
2571 n = ssl->handshake->dhm_ctx.len;
2572 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2573 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002574
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002575 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002576 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002577 &ssl->out_msg[i], n,
2578 ssl->f_rng, ssl->p_rng );
2579 if( ret != 0 )
2580 {
2581 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2582 return( ret );
2583 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002584 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002585 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002586#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002587#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002588 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002589 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002590 /*
2591 * ClientECDiffieHellmanPublic public;
2592 */
2593 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2594 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2595 ssl->f_rng, ssl->p_rng );
2596 if( ret != 0 )
2597 {
2598 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2599 return( ret );
2600 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002601
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002602 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2603 }
2604 else
2605#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2606 {
2607 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002608 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002609 }
2610
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002611 if( ( ret = ssl_psk_derive_premaster( ssl,
2612 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002613 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002614 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002615 return( ret );
2616 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002617 }
2618 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002619#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002620#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002621 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002623 i = 4;
2624 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002625 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 }
Paul Bakkered27a042013-04-18 22:46:23 +02002627 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002628#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002629 {
2630 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002631 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002632 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002633 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 ssl->out_msglen = i + n;
2636 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2637 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2638
2639 ssl->state++;
2640
2641 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2642 {
2643 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2644 return( ret );
2645 }
2646
2647 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2648
2649 return( 0 );
2650}
2651
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002652#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2653 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002654 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2655 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002656static int ssl_write_certificate_verify( ssl_context *ssl )
2657{
Paul Bakkered27a042013-04-18 22:46:23 +02002658 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002659 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
2661 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2662
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002663 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2664 {
2665 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2666 return( ret );
2667 }
2668
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002669 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002670 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002671 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002672 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002673 {
2674 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2675 ssl->state++;
2676 return( 0 );
2677 }
2678
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002679 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2680 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002681}
2682#else
2683static int ssl_write_certificate_verify( ssl_context *ssl )
2684{
2685 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2686 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2687 size_t n = 0, offset = 0;
2688 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002689 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002690 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002691 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002692
2693 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2694
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002695 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2696 {
2697 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2698 return( ret );
2699 }
2700
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002701 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002702 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002703 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002704 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2705 {
2706 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2707 ssl->state++;
2708 return( 0 );
2709 }
2710
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002711 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 {
2713 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2714 ssl->state++;
2715 return( 0 );
2716 }
2717
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002718 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002720 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2721 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 }
2723
2724 /*
2725 * Make an RSA signature of the handshake digests
2726 */
Paul Bakker48916f92012-09-16 19:57:18 +00002727 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002729#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2730 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002731 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002732 {
Paul Bakker926af752012-11-23 13:38:07 +01002733 /*
2734 * digitally-signed struct {
2735 * opaque md5_hash[16];
2736 * opaque sha_hash[20];
2737 * };
2738 *
2739 * md5_hash
2740 * MD5(handshake_messages);
2741 *
2742 * sha_hash
2743 * SHA(handshake_messages);
2744 */
2745 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002746 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002747
2748 /*
2749 * For ECDSA, default hash is SHA-1 only
2750 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002751 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002752 {
2753 hash_start += 16;
2754 hashlen -= 16;
2755 md_alg = POLARSSL_MD_SHA1;
2756 }
Paul Bakker926af752012-11-23 13:38:07 +01002757 }
2758 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002759#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2760 POLARSSL_SSL_PROTO_TLS1_1 */
2761#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2762 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002763 {
2764 /*
2765 * digitally-signed struct {
2766 * opaque handshake_messages[handshake_messages_length];
2767 * };
2768 *
2769 * Taking shortcut here. We assume that the server always allows the
2770 * PRF Hash function and has sent it in the allowed signature
2771 * algorithms list received in the Certificate Request message.
2772 *
2773 * Until we encounter a server that does not, we will take this
2774 * shortcut.
2775 *
2776 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2777 * in order to satisfy 'weird' needs from the server side.
2778 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002779 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2780 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002781 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002782 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002783 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002784 }
2785 else
2786 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002787 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002788 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002789 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002790 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002791
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002792 /* Info from md_alg will be used instead */
2793 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002794 offset = 2;
2795 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002796 else
2797#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002798 {
2799 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002800 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002801 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002802
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002803 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002804 ssl->out_msg + 6 + offset, &n,
2805 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002806 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002807 SSL_DEBUG_RET( 1, "pk_sign", ret );
2808 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002809 }
Paul Bakker926af752012-11-23 13:38:07 +01002810
Paul Bakker1ef83d62012-04-11 12:09:53 +00002811 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2812 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Paul Bakker1ef83d62012-04-11 12:09:53 +00002814 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2816 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2817
2818 ssl->state++;
2819
2820 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2821 {
2822 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2823 return( ret );
2824 }
2825
2826 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2827
Paul Bakkered27a042013-04-18 22:46:23 +02002828 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002829}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002830#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2831 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2832 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Paul Bakkera503a632013-08-14 13:48:06 +02002834#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002835static int ssl_parse_new_session_ticket( ssl_context *ssl )
2836{
2837 int ret;
2838 uint32_t lifetime;
2839 size_t ticket_len;
2840 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002841 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002842
2843 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2844
2845 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2846 {
2847 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2848 return( ret );
2849 }
2850
2851 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2852 {
2853 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2854 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2855 }
2856
2857 /*
2858 * struct {
2859 * uint32 ticket_lifetime_hint;
2860 * opaque ticket<0..2^16-1>;
2861 * } NewSessionTicket;
2862 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002863 * 0 . 3 ticket_lifetime_hint
2864 * 4 . 5 ticket_len (n)
2865 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002866 */
2867 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002868 ssl->in_hslen < 6 + ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002869 {
2870 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2871 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2872 }
2873
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002874 msg = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002875
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002876 lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
2877 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002878
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002879 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
2880
2881 if( ticket_len + 6 + ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002882 {
2883 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2884 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2885 }
2886
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002887 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2888
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002889 /* We're not waiting for a NewSessionTicket message any more */
2890 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002891 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002892
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002893 /*
2894 * Zero-length ticket means the server changed his mind and doesn't want
2895 * to send a ticket after all, so just forget it
2896 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002897 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002898 return( 0 );
2899
Paul Bakker34617722014-06-13 17:20:13 +02002900 polarssl_zeroize( ssl->session_negotiate->ticket,
2901 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002902 polarssl_free( ssl->session_negotiate->ticket );
2903 ssl->session_negotiate->ticket = NULL;
2904 ssl->session_negotiate->ticket_len = 0;
2905
2906 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2907 {
2908 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2909 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2910 }
2911
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002912 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002913
2914 ssl->session_negotiate->ticket = ticket;
2915 ssl->session_negotiate->ticket_len = ticket_len;
2916 ssl->session_negotiate->ticket_lifetime = lifetime;
2917
2918 /*
2919 * RFC 5077 section 3.4:
2920 * "If the client receives a session ticket from the server, then it
2921 * discards any Session ID that was sent in the ServerHello."
2922 */
2923 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2924 ssl->session_negotiate->length = 0;
2925
2926 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2927
2928 return( 0 );
2929}
Paul Bakkera503a632013-08-14 13:48:06 +02002930#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002931
Paul Bakker5121ce52009-01-03 21:22:43 +00002932/*
Paul Bakker1961b702013-01-25 14:49:24 +01002933 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002934 */
Paul Bakker1961b702013-01-25 14:49:24 +01002935int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002936{
2937 int ret = 0;
2938
Paul Bakker1961b702013-01-25 14:49:24 +01002939 if( ssl->state == SSL_HANDSHAKE_OVER )
2940 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002941
Paul Bakker1961b702013-01-25 14:49:24 +01002942 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2943
2944 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2945 return( ret );
2946
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002947#if defined(POLARSSL_SSL_PROTO_DTLS)
2948 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2949 ssl->handshake != NULL &&
2950 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2951 {
2952 if( ( ret = ssl_resend( ssl ) ) != 0 )
2953 return( ret );
2954 }
2955#endif
2956
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002957 /* Change state now, so that it is right in ssl_read_record(), used
2958 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
2959#if defined(POLARSSL_SSL_SESSION_TICKETS)
2960 if( ssl->state == SSL_SERVER_CHANGE_CIPHER_SPEC &&
2961 ssl->handshake->new_session_ticket != 0 )
2962 {
2963 ssl->state = SSL_SERVER_NEW_SESSION_TICKET;
2964 }
2965#endif
2966
Paul Bakker1961b702013-01-25 14:49:24 +01002967 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002968 {
Paul Bakker1961b702013-01-25 14:49:24 +01002969 case SSL_HELLO_REQUEST:
2970 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002971 break;
2972
Paul Bakker1961b702013-01-25 14:49:24 +01002973 /*
2974 * ==> ClientHello
2975 */
2976 case SSL_CLIENT_HELLO:
2977 ret = ssl_write_client_hello( ssl );
2978 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
Paul Bakker1961b702013-01-25 14:49:24 +01002980 /*
2981 * <== ServerHello
2982 * Certificate
2983 * ( ServerKeyExchange )
2984 * ( CertificateRequest )
2985 * ServerHelloDone
2986 */
2987 case SSL_SERVER_HELLO:
2988 ret = ssl_parse_server_hello( ssl );
2989 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
Paul Bakker1961b702013-01-25 14:49:24 +01002991 case SSL_SERVER_CERTIFICATE:
2992 ret = ssl_parse_certificate( ssl );
2993 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002994
Paul Bakker1961b702013-01-25 14:49:24 +01002995 case SSL_SERVER_KEY_EXCHANGE:
2996 ret = ssl_parse_server_key_exchange( ssl );
2997 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
Paul Bakker1961b702013-01-25 14:49:24 +01002999 case SSL_CERTIFICATE_REQUEST:
3000 ret = ssl_parse_certificate_request( ssl );
3001 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003002
Paul Bakker1961b702013-01-25 14:49:24 +01003003 case SSL_SERVER_HELLO_DONE:
3004 ret = ssl_parse_server_hello_done( ssl );
3005 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Paul Bakker1961b702013-01-25 14:49:24 +01003007 /*
3008 * ==> ( Certificate/Alert )
3009 * ClientKeyExchange
3010 * ( CertificateVerify )
3011 * ChangeCipherSpec
3012 * Finished
3013 */
3014 case SSL_CLIENT_CERTIFICATE:
3015 ret = ssl_write_certificate( ssl );
3016 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
Paul Bakker1961b702013-01-25 14:49:24 +01003018 case SSL_CLIENT_KEY_EXCHANGE:
3019 ret = ssl_write_client_key_exchange( ssl );
3020 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003021
Paul Bakker1961b702013-01-25 14:49:24 +01003022 case SSL_CERTIFICATE_VERIFY:
3023 ret = ssl_write_certificate_verify( ssl );
3024 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003025
Paul Bakker1961b702013-01-25 14:49:24 +01003026 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3027 ret = ssl_write_change_cipher_spec( ssl );
3028 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Paul Bakker1961b702013-01-25 14:49:24 +01003030 case SSL_CLIENT_FINISHED:
3031 ret = ssl_write_finished( ssl );
3032 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003033
Paul Bakker1961b702013-01-25 14:49:24 +01003034 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003035 * <== ( NewSessionTicket )
3036 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003037 * Finished
3038 */
Paul Bakkera503a632013-08-14 13:48:06 +02003039#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003040 case SSL_SERVER_NEW_SESSION_TICKET:
3041 ret = ssl_parse_new_session_ticket( ssl );
3042 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003043#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003044
3045 case SSL_SERVER_CHANGE_CIPHER_SPEC:
3046 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003047 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003048
Paul Bakker1961b702013-01-25 14:49:24 +01003049 case SSL_SERVER_FINISHED:
3050 ret = ssl_parse_finished( ssl );
3051 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003052
Paul Bakker1961b702013-01-25 14:49:24 +01003053 case SSL_FLUSH_BUFFERS:
3054 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3055 ssl->state = SSL_HANDSHAKE_WRAPUP;
3056 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Paul Bakker1961b702013-01-25 14:49:24 +01003058 case SSL_HANDSHAKE_WRAPUP:
3059 ssl_handshake_wrapup( ssl );
3060 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003061
Paul Bakker1961b702013-01-25 14:49:24 +01003062 default:
3063 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3064 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3065 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
3067 return( ret );
3068}
Paul Bakker9af723c2014-05-01 13:03:14 +02003069#endif /* POLARSSL_SSL_CLI_C */