blob: cf8e46c91a3d09d4305d785ca4c9baa13473ff88 [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
Paul Bakker5121ce52009-01-03 21:22:43 +0000506static int ssl_write_client_hello( ssl_context *ssl )
507{
Paul Bakker23986e52011-04-24 08:57:21 +0000508 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100509 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200511 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200512#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200514#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200515 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200516 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
519
Paul Bakkera9a028e2013-11-21 17:31:06 +0100520 if( ssl->f_rng == NULL )
521 {
522 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
523 return( POLARSSL_ERR_SSL_NO_RNG );
524 }
525
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100526#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000527 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100528#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000529 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000530 ssl->major_ver = ssl->min_major_ver;
531 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000532 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
Paul Bakker490ecc82011-10-06 13:04:09 +0000534 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
535 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200536 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
537 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000538 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540 /*
541 * 0 . 0 handshake type
542 * 1 . 3 handshake length
543 * 4 . 5 highest version supported
544 * 6 . 9 current UNIX time
545 * 10 . 37 random bytes
546 */
547 buf = ssl->out_msg;
548 p = buf + 4;
549
550 *p++ = (unsigned char) ssl->max_major_ver;
551 *p++ = (unsigned char) ssl->max_minor_ver;
552
553 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
554 buf[4], buf[5] ) );
555
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200556#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 t = time( NULL );
558 *p++ = (unsigned char)( t >> 24 );
559 *p++ = (unsigned char)( t >> 16 );
560 *p++ = (unsigned char)( t >> 8 );
561 *p++ = (unsigned char)( t );
562
563 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200564#else
565 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
566 return( ret );
567
568 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200569#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Paul Bakkera3d195c2011-11-27 21:07:34 +0000571 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
572 return( ret );
573
574 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Paul Bakker48916f92012-09-16 19:57:18 +0000576 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
579
580 /*
581 * 38 . 38 session id length
582 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000583 * 40+n . 41+n ciphersuitelist length
584 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000585 * .. . .. compression methods length
586 * .. . .. compression methods
587 * .. . .. extensions length
588 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 */
Paul Bakker48916f92012-09-16 19:57:18 +0000590 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100592 if( n < 16 || n > 32 ||
593#if defined(POLARSSL_SSL_RENEGOTIATION)
594 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
595#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000596 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200597 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200599 }
600
Paul Bakkera503a632013-08-14 13:48:06 +0200601#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200602 /*
603 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
604 * generate and include a Session ID in the TLS ClientHello."
605 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100606#if defined(POLARSSL_SSL_RENEGOTIATION)
607 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
608#endif
609 if( ssl->session_negotiate->ticket != NULL &&
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200610 ssl->session_negotiate->ticket_len != 0 )
611 {
612 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
613
614 if( ret != 0 )
615 return( ret );
616
617 ssl->session_negotiate->length = n = 32;
618 }
Paul Bakkera503a632013-08-14 13:48:06 +0200619#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
621 *p++ = (unsigned char) n;
622
623 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000624 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
627 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
628
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200629 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200630 n = 0;
631 q = p;
632
633 // Skip writing ciphersuite length for now
634 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
Paul Bakker48916f92012-09-16 19:57:18 +0000636 /*
637 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
638 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100639#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000640 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100641#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000642 {
643 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
644 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200645 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000646 }
647
Paul Bakker2fbefde2013-06-29 16:01:15 +0200648 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200650 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
651
652 if( ciphersuite_info == NULL )
653 continue;
654
655 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
656 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
657 continue;
658
Paul Bakkere3166ce2011-01-27 17:40:50 +0000659 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200660 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
Paul Bakker2fbefde2013-06-29 16:01:15 +0200662 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200663 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
664 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 }
666
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200667 /* Some versions of OpenSSL don't handle it correctly if not at end */
668#if defined(POLARSSL_SSL_FALLBACK_SCSV)
669 if( ssl->fallback == SSL_IS_FALLBACK )
670 {
671 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
672 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
673 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
674 n++;
675 }
676#endif
677
Paul Bakker2fbefde2013-06-29 16:01:15 +0200678 *q++ = (unsigned char)( n >> 7 );
679 *q++ = (unsigned char)( n << 1 );
680
681 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
682
683
Paul Bakker2770fbd2012-07-03 13:30:23 +0000684#if defined(POLARSSL_ZLIB_SUPPORT)
685 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
686 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000687 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000688
689 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000690 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000691 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000692#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000694 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000695
696 *p++ = 1;
697 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200698#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
Paul Bakkerd3edc862013-03-20 16:07:17 +0100700 // First write extensions, then the total length
701 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200702#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100703 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
704 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200705#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100707#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100708 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
709 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100710#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000711
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100712#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
713 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100714 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
715 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200716#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000717
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200718#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100719 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
720 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100721
Paul Bakkerd3edc862013-03-20 16:07:17 +0100722 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
723 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100724#endif
725
Paul Bakker05decb22013-08-15 13:33:48 +0200726#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200727 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
728 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200729#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200730
Paul Bakker1f2bc622013-08-15 13:45:55 +0200731#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200732 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
733 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200734#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200735
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100736#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
737 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
738 ext_len += olen;
739#endif
740
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200741#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
742 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
743 ext_len += olen;
744#endif
745
Paul Bakkera503a632013-08-14 13:48:06 +0200746#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200747 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
748 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200749#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200750
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200751#if defined(POLARSSL_SSL_ALPN)
752 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
753 ext_len += olen;
754#endif
755
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100756 /* olen unused if all extensions are disabled */
757 ((void) olen);
758
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000759 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
760 ext_len ) );
761
Paul Bakkera7036632014-04-30 10:15:38 +0200762 if( ext_len > 0 )
763 {
764 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
765 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
766 p += ext_len;
767 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100768
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 ssl->out_msglen = p - buf;
770 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
771 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
772
773 ssl->state++;
774
775 if( ( ret = ssl_write_record( ssl ) ) != 0 )
776 {
777 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
778 return( ret );
779 }
780
781 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
782
783 return( 0 );
784}
785
Paul Bakker48916f92012-09-16 19:57:18 +0000786static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200787 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000788 size_t len )
789{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000790 int ret;
791
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100792#if defined(POLARSSL_SSL_RENEGOTIATION)
793 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000794 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100795 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000796 if( len != 1 + ssl->verify_data_len * 2 ||
797 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100798 safer_memcmp( buf + 1,
799 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
800 safer_memcmp( buf + 1 + ssl->verify_data_len,
801 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000802 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100803 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000804
805 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
806 return( ret );
807
Paul Bakker48916f92012-09-16 19:57:18 +0000808 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
809 }
810 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100811 else
812#endif /* POLARSSL_SSL_RENEGOTIATION */
813 {
814 if( len != 1 || buf[0] != 0x00 )
815 {
816 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
817
818 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
819 return( ret );
820
821 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
822 }
823
824 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
825 }
Paul Bakker48916f92012-09-16 19:57:18 +0000826
827 return( 0 );
828}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200829
Paul Bakker05decb22013-08-15 13:33:48 +0200830#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200831static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200832 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200833 size_t len )
834{
835 /*
836 * server should use the extension only if we did,
837 * and if so the server's value should match ours (and len is always 1)
838 */
839 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
840 len != 1 ||
841 buf[0] != ssl->mfl_code )
842 {
843 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
844 }
845
846 return( 0 );
847}
Paul Bakker05decb22013-08-15 13:33:48 +0200848#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000849
Paul Bakker1f2bc622013-08-15 13:45:55 +0200850#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200851static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
852 const unsigned char *buf,
853 size_t len )
854{
855 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
856 len != 0 )
857 {
858 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
859 }
860
861 ((void) buf);
862
863 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
864
865 return( 0 );
866}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200867#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200868
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100869#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
870static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
871 const unsigned char *buf,
872 size_t len )
873{
874 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
875 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
876 len != 0 )
877 {
878 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
879 }
880
881 ((void) buf);
882
883 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
884
885 return( 0 );
886}
887#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
888
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200889#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
890static int ssl_parse_extended_ms_ext( ssl_context *ssl,
891 const unsigned char *buf,
892 size_t len )
893{
894 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200895 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200896 len != 0 )
897 {
898 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
899 }
900
901 ((void) buf);
902
903 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
904
905 return( 0 );
906}
907#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
908
Paul Bakkera503a632013-08-14 13:48:06 +0200909#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200910static int ssl_parse_session_ticket_ext( ssl_context *ssl,
911 const unsigned char *buf,
912 size_t len )
913{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200914 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
915 len != 0 )
916 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200917 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200918 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200919
920 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200921
922 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200923
924 return( 0 );
925}
Paul Bakkera503a632013-08-14 13:48:06 +0200926#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200927
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200928#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200929static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
930 const unsigned char *buf,
931 size_t len )
932{
933 size_t list_size;
934 const unsigned char *p;
935
936 list_size = buf[0];
937 if( list_size + 1 != len )
938 {
939 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
940 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
941 }
942
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200943 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200944 while( list_size > 0 )
945 {
946 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
947 p[0] == POLARSSL_ECP_PF_COMPRESSED )
948 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200949 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200950 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
951 return( 0 );
952 }
953
954 list_size--;
955 p++;
956 }
957
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200958 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
959 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200960}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200961#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200962
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200963#if defined(POLARSSL_SSL_ALPN)
964static int ssl_parse_alpn_ext( ssl_context *ssl,
965 const unsigned char *buf, size_t len )
966{
967 size_t list_len, name_len;
968 const char **p;
969
970 /* If we didn't send it, the server shouldn't send it */
971 if( ssl->alpn_list == NULL )
972 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
973
974 /*
975 * opaque ProtocolName<1..2^8-1>;
976 *
977 * struct {
978 * ProtocolName protocol_name_list<2..2^16-1>
979 * } ProtocolNameList;
980 *
981 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
982 */
983
984 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
985 if( len < 4 )
986 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
987
988 list_len = ( buf[0] << 8 ) | buf[1];
989 if( list_len != len - 2 )
990 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
991
992 name_len = buf[2];
993 if( name_len != list_len - 1 )
994 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
995
996 /* Check that the server chosen protocol was in our list and save it */
997 for( p = ssl->alpn_list; *p != NULL; p++ )
998 {
999 if( name_len == strlen( *p ) &&
1000 memcmp( buf + 3, *p, name_len ) == 0 )
1001 {
1002 ssl->alpn_chosen = *p;
1003 return( 0 );
1004 }
1005 }
1006
1007 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1008}
1009#endif /* POLARSSL_SSL_ALPN */
1010
Paul Bakker5121ce52009-01-03 21:22:43 +00001011static int ssl_parse_server_hello( ssl_context *ssl )
1012{
Paul Bakker2770fbd2012-07-03 13:30:23 +00001013 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +00001014 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001015 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001016 unsigned char *buf, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001017#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001018 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001019#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001020 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001021#if defined(POLARSSL_DEBUG_C)
1022 uint32_t t;
1023#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
1025 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1026
1027 /*
1028 * 0 . 0 handshake type
1029 * 1 . 3 handshake length
1030 * 4 . 5 protocol version
1031 * 6 . 9 UNIX time()
1032 * 10 . 37 random bytes
1033 */
1034 buf = ssl->in_msg;
1035
1036 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1037 {
1038 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1039 return( ret );
1040 }
1041
1042 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1043 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001044#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001045 if( ssl->renegotiation == SSL_RENEGOTIATION )
1046 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001047 ssl->renego_records_seen++;
1048
1049 if( ssl->renego_max_records >= 0 &&
1050 ssl->renego_records_seen > ssl->renego_max_records )
1051 {
1052 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1053 "but not honored by server" ) );
1054 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1055 }
1056
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001057 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1058 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1059 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001060#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001061
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001063 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001064 }
1065
1066 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1067 buf[4], buf[5] ) );
1068
1069 if( ssl->in_hslen < 42 ||
1070 buf[0] != SSL_HS_SERVER_HELLO ||
1071 buf[4] != SSL_MAJOR_VERSION_3 )
1072 {
1073 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001074 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 }
1076
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001077 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 {
1079 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001080 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 }
1082
1083 ssl->minor_ver = buf[5];
1084
Paul Bakker1d29fb52012-09-28 13:28:45 +00001085 if( ssl->minor_ver < ssl->min_minor_ver )
1086 {
1087 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001088 " [%d:%d] < [%d:%d]", ssl->major_ver,
1089 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001090
1091 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1092 SSL_ALERT_MSG_PROTOCOL_VERSION );
1093
1094 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1095 }
1096
Paul Bakker1504af52012-02-11 16:17:43 +00001097#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001098 t = ( (uint32_t) buf[6] << 24 )
1099 | ( (uint32_t) buf[7] << 16 )
1100 | ( (uint32_t) buf[8] << 8 )
1101 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001102 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001103#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
Paul Bakker48916f92012-09-16 19:57:18 +00001105 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
1107 n = buf[38];
1108
Paul Bakker5121ce52009-01-03 21:22:43 +00001109 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1110
Paul Bakker48916f92012-09-16 19:57:18 +00001111 if( n > 32 )
1112 {
1113 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1114 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1115 }
1116
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 /*
1118 * 38 . 38 session id length
1119 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001120 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 * 41+n . 41+n chosen compression alg.
1122 * 42+n . 43+n extensions length
1123 * 44+n . 44+n+m extensions
1124 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001125 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 {
1127 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001128 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001129
Paul Bakker48916f92012-09-16 19:57:18 +00001130 if( ( ext_len > 0 && ext_len < 4 ) ||
1131 ssl->in_hslen != 44 + n + ext_len )
1132 {
1133 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1134 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1135 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001137 else if( ssl->in_hslen == 42 + n )
1138 {
1139 ext_len = 0;
1140 }
1141 else
1142 {
1143 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1144 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1145 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
1147 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001148 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Paul Bakker380da532012-04-18 16:10:25 +00001150 /*
1151 * Initialize update checksum functions
1152 */
Paul Bakker68884e32013-01-07 18:20:04 +01001153 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1154
1155 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1156 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001157 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001158 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1159 }
Paul Bakker380da532012-04-18 16:10:25 +00001160
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001161 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1162
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1164 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1165
1166 /*
1167 * Check if the session can be resumed
1168 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001169 if( ssl->handshake->resume == 0 || n == 0 ||
1170#if defined(POLARSSL_SSL_RENEGOTIATION)
1171 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1172#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001173 ssl->session_negotiate->ciphersuite != i ||
1174 ssl->session_negotiate->compression != comp ||
1175 ssl->session_negotiate->length != n ||
1176 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 {
1178 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001179 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001180#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001181 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001182#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001183 ssl->session_negotiate->ciphersuite = i;
1184 ssl->session_negotiate->compression = comp;
1185 ssl->session_negotiate->length = n;
1186 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 }
1188 else
1189 {
1190 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001191
1192 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1193 {
1194 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1195 return( ret );
1196 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 }
1198
1199 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001200 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
Paul Bakkere3166ce2011-01-27 17:40:50 +00001202 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1204
1205 i = 0;
1206 while( 1 )
1207 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001208 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 {
1210 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001211 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001212 }
1213
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001214 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1215 ssl->session_negotiate->ciphersuite )
1216 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001218 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 }
1220
Paul Bakker2770fbd2012-07-03 13:30:23 +00001221 if( comp != SSL_COMPRESS_NULL
1222#if defined(POLARSSL_ZLIB_SUPPORT)
1223 && comp != SSL_COMPRESS_DEFLATE
1224#endif
1225 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 {
1227 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001228 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 }
Paul Bakker48916f92012-09-16 19:57:18 +00001230 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Paul Bakker48916f92012-09-16 19:57:18 +00001232 ext = buf + 44 + n;
1233
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001234 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1235
Paul Bakker48916f92012-09-16 19:57:18 +00001236 while( ext_len )
1237 {
1238 unsigned int ext_id = ( ( ext[0] << 8 )
1239 | ( ext[1] ) );
1240 unsigned int ext_size = ( ( ext[2] << 8 )
1241 | ( ext[3] ) );
1242
1243 if( ext_size + 4 > ext_len )
1244 {
1245 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1246 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1247 }
1248
1249 switch( ext_id )
1250 {
1251 case TLS_EXT_RENEGOTIATION_INFO:
1252 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001253#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001254 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001255#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001256
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001257 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1258 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001259 return( ret );
1260
1261 break;
1262
Paul Bakker05decb22013-08-15 13:33:48 +02001263#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001264 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1265 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1266
1267 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1268 ext + 4, ext_size ) ) != 0 )
1269 {
1270 return( ret );
1271 }
1272
1273 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001274#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001275
Paul Bakker1f2bc622013-08-15 13:45:55 +02001276#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001277 case TLS_EXT_TRUNCATED_HMAC:
1278 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1279
1280 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1281 ext + 4, ext_size ) ) != 0 )
1282 {
1283 return( ret );
1284 }
1285
1286 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001287#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001288
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001289#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1290 case TLS_EXT_ENCRYPT_THEN_MAC:
1291 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1292
1293 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1294 ext + 4, ext_size ) ) != 0 )
1295 {
1296 return( ret );
1297 }
1298
1299 break;
1300#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1301
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001302#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1303 case TLS_EXT_EXTENDED_MASTER_SECRET:
1304 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1305
1306 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1307 ext + 4, ext_size ) ) != 0 )
1308 {
1309 return( ret );
1310 }
1311
1312 break;
1313#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1314
Paul Bakkera503a632013-08-14 13:48:06 +02001315#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001316 case TLS_EXT_SESSION_TICKET:
1317 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1318
1319 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1320 ext + 4, ext_size ) ) != 0 )
1321 {
1322 return( ret );
1323 }
1324
1325 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001326#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001327
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001328#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001329 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1330 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1331
1332 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1333 ext + 4, ext_size ) ) != 0 )
1334 {
1335 return( ret );
1336 }
1337
1338 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001339#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001340
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001341#if defined(POLARSSL_SSL_ALPN)
1342 case TLS_EXT_ALPN:
1343 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1344
1345 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1346 return( ret );
1347
1348 break;
1349#endif /* POLARSSL_SSL_ALPN */
1350
Paul Bakker48916f92012-09-16 19:57:18 +00001351 default:
1352 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1353 ext_id ) );
1354 }
1355
1356 ext_len -= 4 + ext_size;
1357 ext += 4 + ext_size;
1358
1359 if( ext_len > 0 && ext_len < 4 )
1360 {
1361 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1362 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1363 }
1364 }
1365
1366 /*
1367 * Renegotiation security checks
1368 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001369 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1370 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001371 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001372 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1373 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001374 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001375#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001376 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1377 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1378 renegotiation_info_seen == 0 )
1379 {
1380 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1381 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001382 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001383 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1384 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1385 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001386 {
1387 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001388 handshake_failure = 1;
1389 }
1390 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1391 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1392 renegotiation_info_seen == 1 )
1393 {
1394 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1395 handshake_failure = 1;
1396 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001397#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001398
1399 if( handshake_failure == 1 )
1400 {
1401 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1402 return( ret );
1403
Paul Bakker48916f92012-09-16 19:57:18 +00001404 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1405 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
1407 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1408
1409 return( 0 );
1410}
1411
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001412#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1413 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001414static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1415 unsigned char *end )
1416{
1417 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1418
Paul Bakker29e1f122013-04-16 13:07:56 +02001419 /*
1420 * Ephemeral DH parameters:
1421 *
1422 * struct {
1423 * opaque dh_p<1..2^16-1>;
1424 * opaque dh_g<1..2^16-1>;
1425 * opaque dh_Ys<1..2^16-1>;
1426 * } ServerDHParams;
1427 */
1428 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1429 {
1430 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1431 return( ret );
1432 }
1433
1434 if( ssl->handshake->dhm_ctx.len < 64 ||
1435 ssl->handshake->dhm_ctx.len > 512 )
1436 {
1437 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1438 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1439 }
1440
1441 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1442 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1443 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001444
1445 return( ret );
1446}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001447#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1448 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001449
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001450#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001451 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001452 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1453 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1454 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1455static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1456{
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001457 const ecp_curve_info *curve_info;
1458
1459 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1460 if( curve_info == NULL )
1461 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001462 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1463 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001464 }
1465
1466 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001467
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001468#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1469 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1470#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001471 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1472 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001473#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001474 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001475
1476 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1477
1478 return( 0 );
1479}
Paul Bakker9af723c2014-05-01 13:03:14 +02001480#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1481 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1482 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1483 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1484 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001485
1486#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1487 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001488 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001489static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1490 unsigned char **p,
1491 unsigned char *end )
1492{
1493 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1494
Paul Bakker29e1f122013-04-16 13:07:56 +02001495 /*
1496 * Ephemeral ECDH parameters:
1497 *
1498 * struct {
1499 * ECParameters curve_params;
1500 * ECPoint public;
1501 * } ServerECDHParams;
1502 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001503 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1504 (const unsigned char **) p, end ) ) != 0 )
1505 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001506 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001507 return( ret );
1508 }
1509
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001510 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001511 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001512 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001513 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1514 }
1515
Paul Bakker29e1f122013-04-16 13:07:56 +02001516 return( ret );
1517}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001518#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001519 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1520 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001521
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001522#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001523static int ssl_parse_server_psk_hint( ssl_context *ssl,
1524 unsigned char **p,
1525 unsigned char *end )
1526{
1527 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001528 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001529 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001530
1531 /*
1532 * PSK parameters:
1533 *
1534 * opaque psk_identity_hint<0..2^16-1>;
1535 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001536 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001537 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001538
1539 if( (*p) + len > end )
1540 {
1541 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1542 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1543 }
1544
1545 // TODO: Retrieve PSK identity hint and callback to app
1546 //
1547 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001548 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001549
1550 return( ret );
1551}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001552#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001553
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001554#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1555 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1556/*
1557 * Generate a pre-master secret and encrypt it with the server's RSA key
1558 */
1559static int ssl_write_encrypted_pms( ssl_context *ssl,
1560 size_t offset, size_t *olen,
1561 size_t pms_offset )
1562{
1563 int ret;
1564 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1565 unsigned char *p = ssl->handshake->premaster + pms_offset;
1566
1567 /*
1568 * Generate (part of) the pre-master as
1569 * struct {
1570 * ProtocolVersion client_version;
1571 * opaque random[46];
1572 * } PreMasterSecret;
1573 */
1574 p[0] = (unsigned char) ssl->max_major_ver;
1575 p[1] = (unsigned char) ssl->max_minor_ver;
1576
1577 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1578 {
1579 SSL_DEBUG_RET( 1, "f_rng", ret );
1580 return( ret );
1581 }
1582
1583 ssl->handshake->pmslen = 48;
1584
1585 /*
1586 * Now write it out, encrypted
1587 */
1588 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1589 POLARSSL_PK_RSA ) )
1590 {
1591 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1592 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1593 }
1594
1595 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1596 p, ssl->handshake->pmslen,
1597 ssl->out_msg + offset + len_bytes, olen,
1598 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1599 ssl->f_rng, ssl->p_rng ) ) != 0 )
1600 {
1601 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1602 return( ret );
1603 }
1604
1605#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1606 defined(POLARSSL_SSL_PROTO_TLS1_2)
1607 if( len_bytes == 2 )
1608 {
1609 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1610 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1611 *olen += 2;
1612 }
1613#endif
1614
1615 return( 0 );
1616}
1617#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1618 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001619
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001620#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001621#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001622 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1623 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001624static int ssl_parse_signature_algorithm( ssl_context *ssl,
1625 unsigned char **p,
1626 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001627 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001628 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001629{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001630 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001631 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001632 *pk_alg = POLARSSL_PK_NONE;
1633
1634 /* Only in TLS 1.2 */
1635 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1636 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001637 return( 0 );
1638 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001639
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001640 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001641 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1642
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001643 /*
1644 * Get hash algorithm
1645 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001646 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001647 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001648 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1649 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001650 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1651 }
1652
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001653 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001654 * Get signature algorithm
1655 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001656 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001657 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001658 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1659 "SignatureAlgorithm %d", (*p)[1] ) );
1660 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001661 }
1662
1663 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1664 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1665 *p += 2;
1666
1667 return( 0 );
1668}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001669#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001670 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1671 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001672#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001673
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001674
1675#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1676 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1677static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1678{
1679 int ret;
1680 const ecp_keypair *peer_key;
1681
1682 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1683 POLARSSL_PK_ECKEY ) )
1684 {
1685 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1686 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1687 }
1688
1689 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1690
1691 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1692 POLARSSL_ECDH_THEIRS ) ) != 0 )
1693 {
1694 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1695 return( ret );
1696 }
1697
1698 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1699 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001700 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001701 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1702 }
1703
1704 return( ret );
1705}
1706#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1707 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1708
Paul Bakker41c83d32013-03-20 14:39:14 +01001709static int ssl_parse_server_key_exchange( ssl_context *ssl )
1710{
Paul Bakker23986e52011-04-24 08:57:21 +00001711 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001712 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001713 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001714#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001715 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1716 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001717 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001718 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001719 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001720 size_t hashlen;
1721 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001722#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001723
1724 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1725
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001726#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001727 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001728 {
1729 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1730 ssl->state++;
1731 return( 0 );
1732 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001733 ((void) p);
1734 ((void) end);
1735#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001736
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001737#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1738 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1739 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1740 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1741 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001742 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1743 {
1744 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1745 return( ret );
1746 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001747
1748 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1749 ssl->state++;
1750 return( 0 );
1751 }
1752 ((void) p);
1753 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001754#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1755 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001756
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1758 {
1759 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1760 return( ret );
1761 }
1762
1763 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1764 {
1765 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001766 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 }
1768
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001769 /*
1770 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1771 * doesn't use a psk_identity_hint
1772 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001773 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1774 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001775 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1776 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001777 {
1778 ssl->record_read = 1;
1779 goto exit;
1780 }
1781
1782 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1783 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 }
1785
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001786 p = ssl->in_msg + 4;
1787 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001788 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001789
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001790#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1791 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1792 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1793 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1794 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1795 {
1796 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1797 {
1798 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1799 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1800 }
1801 } /* FALLTROUGH */
1802#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1803
1804#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1805 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1806 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1807 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1808 ; /* nothing more to do */
1809 else
1810#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1811 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1812#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1813 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1814 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1815 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001817 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001818 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001819 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001820 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1821 }
1822 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001823 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001824#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1825 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001826#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001827 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001828 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1829 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001830 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001831 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001832 {
1833 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1834 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001835 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1836 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1837 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001838 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001839 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001840#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001841 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001842 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001843 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001844 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001845 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001846 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001847
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001848#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001849 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1850 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001851 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001852 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1853 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001854 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001855 params_len = p - ( ssl->in_msg + 4 );
1856
Paul Bakker29e1f122013-04-16 13:07:56 +02001857 /*
1858 * Handle the digitally-signed structure
1859 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001860#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1861 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001862 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001863 if( ssl_parse_signature_algorithm( ssl, &p, end,
1864 &md_alg, &pk_alg ) != 0 )
1865 {
1866 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1867 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1868 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001869
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001870 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001871 {
1872 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1873 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1874 }
1875 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001876 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001877#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001878#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1879 defined(POLARSSL_SSL_PROTO_TLS1_1)
1880 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001881 {
1882 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001883
Paul Bakker9659dae2013-08-28 16:21:34 +02001884 /* Default hash for ECDSA is SHA-1 */
1885 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1886 md_alg = POLARSSL_MD_SHA1;
1887 }
1888 else
1889#endif
1890 {
1891 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001892 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001893 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001894
1895 /*
1896 * Read signature
1897 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001898 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001899 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001900
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001901 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001902 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001903 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001904 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001907 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001908
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001909 /*
1910 * Compute the hash that has been signed
1911 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001912#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1913 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001914 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001915 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001916 md5_context md5;
1917 sha1_context sha1;
1918
Paul Bakker5b4af392014-06-26 12:09:34 +02001919 md5_init( &md5 );
1920 sha1_init( &sha1 );
1921
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001922 hashlen = 36;
1923
Paul Bakker29e1f122013-04-16 13:07:56 +02001924 /*
1925 * digitally-signed struct {
1926 * opaque md5_hash[16];
1927 * opaque sha_hash[20];
1928 * };
1929 *
1930 * md5_hash
1931 * MD5(ClientHello.random + ServerHello.random
1932 * + ServerParams);
1933 * sha_hash
1934 * SHA(ClientHello.random + ServerHello.random
1935 * + ServerParams);
1936 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001937 md5_starts( &md5 );
1938 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001939 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001940 md5_finish( &md5, hash );
1941
1942 sha1_starts( &sha1 );
1943 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001944 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001945 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001946
1947 md5_free( &md5 );
1948 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001949 }
1950 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001951#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1952 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001953#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1954 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001955 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001956 {
1957 md_context_t ctx;
1958
Paul Bakker84bbeb52014-07-01 14:53:22 +02001959 md_init( &ctx );
1960
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001961 /* Info from md_alg will be used instead */
1962 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001963
1964 /*
1965 * digitally-signed struct {
1966 * opaque client_random[32];
1967 * opaque server_random[32];
1968 * ServerDHParams params;
1969 * };
1970 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001971 if( ( ret = md_init_ctx( &ctx,
1972 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001973 {
1974 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1975 return( ret );
1976 }
1977
1978 md_starts( &ctx );
1979 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001980 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001981 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001982 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001983 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001984 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001985#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1986 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001987 {
Paul Bakker577e0062013-08-28 11:57:20 +02001988 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001989 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001990 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001991
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001992 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1993 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001994
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001995 /*
1996 * Verify signature
1997 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001998 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001999 {
2000 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2001 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2002 }
2003
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002004 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2005 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002006 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002007 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002008 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002009 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002011#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002012 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2013 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002015exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002016 ssl->state++;
2017
2018 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2019
2020 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002021}
2022
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002023#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2024 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2025 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2026 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2027static int ssl_parse_certificate_request( ssl_context *ssl )
2028{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002029 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2030
2031 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2032
2033 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2034 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2035 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2036 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2037 {
2038 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2039 ssl->state++;
2040 return( 0 );
2041 }
2042
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002043 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2044 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002045}
2046#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002047static int ssl_parse_certificate_request( ssl_context *ssl )
2048{
2049 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002050 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002051 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002052 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002053 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002054
2055 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2056
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002057 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2058 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2059 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2060 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2061 {
2062 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2063 ssl->state++;
2064 return( 0 );
2065 }
2066
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 /*
2068 * 0 . 0 handshake type
2069 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002070 * 4 . 4 cert type count
2071 * 5 .. m-1 cert types
2072 * m .. m+1 sig alg length (TLS 1.2 only)
2073 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 * n .. n+1 length of all DNs
2075 * n+2 .. n+3 length of DN 1
2076 * n+4 .. ... Distinguished Name #1
2077 * ... .. ... length of DN 2, etc.
2078 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002079 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002081 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2082 {
2083 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2084 return( ret );
2085 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002087 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2088 {
2089 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2090 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2091 }
2092
2093 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
2095
2096 ssl->client_auth = 0;
2097 ssl->state++;
2098
2099 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2100 ssl->client_auth++;
2101
2102 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2103 ssl->client_auth ? "a" : "no" ) );
2104
Paul Bakker926af752012-11-23 13:38:07 +01002105 if( ssl->client_auth == 0 )
2106 goto exit;
2107
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002108 ssl->record_read = 0;
2109
Paul Bakker926af752012-11-23 13:38:07 +01002110 // TODO: handshake_failure alert for an anonymous server to request
2111 // client authentication
2112
2113 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002114
Paul Bakker926af752012-11-23 13:38:07 +01002115 // Retrieve cert types
2116 //
2117 cert_type_len = buf[4];
2118 n = cert_type_len;
2119
2120 if( ssl->in_hslen < 6 + n )
2121 {
2122 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2123 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2124 }
2125
Paul Bakker73d44312013-05-22 13:56:26 +02002126 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002127 while( cert_type_len > 0 )
2128 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002129#if defined(POLARSSL_RSA_C)
2130 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002131 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002132 {
2133 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2134 break;
2135 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002136 else
2137#endif
2138#if defined(POLARSSL_ECDSA_C)
2139 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002140 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002141 {
2142 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2143 break;
2144 }
2145 else
2146#endif
2147 {
2148 ; /* Unsupported cert type, ignore */
2149 }
Paul Bakker926af752012-11-23 13:38:07 +01002150
2151 cert_type_len--;
2152 p++;
2153 }
2154
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002155#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002156 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2157 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002158 /* Ignored, see comments about hash in write_certificate_verify */
2159 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002160 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2161 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002162
2163 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002164 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002165 n += sig_alg_len;
2166
2167 if( ssl->in_hslen < 6 + n )
2168 {
2169 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2170 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2171 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002172 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002173#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002174
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002175 /* Ignore certificate_authorities, we only have one cert anyway */
2176 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002177 dn_len = ( ( buf[5 + m + n] << 8 )
2178 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002179
2180 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002181 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002182 {
2183 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2184 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2185 }
2186
2187exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2189
2190 return( 0 );
2191}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002192#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2193 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2194 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2195 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002196
2197static int ssl_parse_server_hello_done( ssl_context *ssl )
2198{
2199 int ret;
2200
2201 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2202
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002203 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 {
2205 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2206 {
2207 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2208 return( ret );
2209 }
2210
2211 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2212 {
2213 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002214 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 }
2216 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002217 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002218
2219 if( ssl->in_hslen != 4 ||
2220 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2221 {
2222 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002223 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 }
2225
2226 ssl->state++;
2227
2228 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2229
2230 return( 0 );
2231}
2232
2233static int ssl_write_client_key_exchange( ssl_context *ssl )
2234{
Paul Bakker23986e52011-04-24 08:57:21 +00002235 int ret;
2236 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002237 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002238
2239 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2240
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002241#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002242 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 /*
2245 * DHM key exchange -- send G^X mod P
2246 */
Paul Bakker48916f92012-09-16 19:57:18 +00002247 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
2249 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2250 ssl->out_msg[5] = (unsigned char)( n );
2251 i = 6;
2252
Paul Bakker29b64762012-09-25 09:36:44 +00002253 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002254 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 &ssl->out_msg[i], n,
2256 ssl->f_rng, ssl->p_rng );
2257 if( ret != 0 )
2258 {
2259 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2260 return( ret );
2261 }
2262
Paul Bakker48916f92012-09-16 19:57:18 +00002263 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2264 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002266 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Paul Bakker48916f92012-09-16 19:57:18 +00002268 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2269 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002270 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002271 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 {
2273 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2274 return( ret );
2275 }
2276
Paul Bakker48916f92012-09-16 19:57:18 +00002277 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 }
2279 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002280#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002281#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002282 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2283 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2284 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002285 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002286 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2287 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2288 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002289 {
2290 /*
2291 * ECDH key exchange -- send client public value
2292 */
2293 i = 4;
2294
2295 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2296 &n,
2297 &ssl->out_msg[i], 1000,
2298 ssl->f_rng, ssl->p_rng );
2299 if( ret != 0 )
2300 {
2301 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2302 return( ret );
2303 }
2304
2305 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2306
2307 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2308 &ssl->handshake->pmslen,
2309 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002310 POLARSSL_MPI_MAX_SIZE,
2311 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002312 {
2313 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2314 return( ret );
2315 }
2316
2317 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2318 }
2319 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002320#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002321 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2322 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2323 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002324#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002325 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002326 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002327 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2328 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002329 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002330 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002331 * opaque psk_identity<0..2^16-1>;
2332 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002333 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002334 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2335
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002336 i = 4;
2337 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002338 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2339 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002340
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002341 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2342 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002343
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002344#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002345 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002346 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002347 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002348 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002349 else
2350#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002351#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2352 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2353 {
2354 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2355 return( ret );
2356 }
2357 else
2358#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002359#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002360 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002361 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002362 /*
2363 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2364 */
2365 n = ssl->handshake->dhm_ctx.len;
2366 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2367 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002368
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002369 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002370 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002371 &ssl->out_msg[i], n,
2372 ssl->f_rng, ssl->p_rng );
2373 if( ret != 0 )
2374 {
2375 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2376 return( ret );
2377 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002378 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002379 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002380#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002381#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002382 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002383 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002384 /*
2385 * ClientECDiffieHellmanPublic public;
2386 */
2387 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2388 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2389 ssl->f_rng, ssl->p_rng );
2390 if( ret != 0 )
2391 {
2392 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2393 return( ret );
2394 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002395
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002396 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2397 }
2398 else
2399#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2400 {
2401 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002402 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002403 }
2404
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002405 if( ( ret = ssl_psk_derive_premaster( ssl,
2406 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002407 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002408 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002409 return( ret );
2410 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002411 }
2412 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002413#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002414#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002415 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002417 i = 4;
2418 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002419 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 }
Paul Bakkered27a042013-04-18 22:46:23 +02002421 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002423 {
2424 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002425 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002426 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002427 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 ssl->out_msglen = i + n;
2430 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2431 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2432
2433 ssl->state++;
2434
2435 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2436 {
2437 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2438 return( ret );
2439 }
2440
2441 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2442
2443 return( 0 );
2444}
2445
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002446#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2447 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002448 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2449 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002450static int ssl_write_certificate_verify( ssl_context *ssl )
2451{
Paul Bakkered27a042013-04-18 22:46:23 +02002452 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002453 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
2455 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2456
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002457 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2458 {
2459 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2460 return( ret );
2461 }
2462
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002463 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002464 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002465 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002466 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002467 {
2468 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2469 ssl->state++;
2470 return( 0 );
2471 }
2472
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002473 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2474 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002475}
2476#else
2477static int ssl_write_certificate_verify( ssl_context *ssl )
2478{
2479 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2480 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2481 size_t n = 0, offset = 0;
2482 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002483 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002484 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002485 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002486
2487 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2488
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002489 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2490 {
2491 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2492 return( ret );
2493 }
2494
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002495 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002496 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002497 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002498 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2499 {
2500 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2501 ssl->state++;
2502 return( 0 );
2503 }
2504
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002505 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 {
2507 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2508 ssl->state++;
2509 return( 0 );
2510 }
2511
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002512 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002514 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2515 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 }
2517
2518 /*
2519 * Make an RSA signature of the handshake digests
2520 */
Paul Bakker48916f92012-09-16 19:57:18 +00002521 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002523#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2524 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002525 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002526 {
Paul Bakker926af752012-11-23 13:38:07 +01002527 /*
2528 * digitally-signed struct {
2529 * opaque md5_hash[16];
2530 * opaque sha_hash[20];
2531 * };
2532 *
2533 * md5_hash
2534 * MD5(handshake_messages);
2535 *
2536 * sha_hash
2537 * SHA(handshake_messages);
2538 */
2539 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002540 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002541
2542 /*
2543 * For ECDSA, default hash is SHA-1 only
2544 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002545 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002546 {
2547 hash_start += 16;
2548 hashlen -= 16;
2549 md_alg = POLARSSL_MD_SHA1;
2550 }
Paul Bakker926af752012-11-23 13:38:07 +01002551 }
2552 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002553#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2554 POLARSSL_SSL_PROTO_TLS1_1 */
2555#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2556 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002557 {
2558 /*
2559 * digitally-signed struct {
2560 * opaque handshake_messages[handshake_messages_length];
2561 * };
2562 *
2563 * Taking shortcut here. We assume that the server always allows the
2564 * PRF Hash function and has sent it in the allowed signature
2565 * algorithms list received in the Certificate Request message.
2566 *
2567 * Until we encounter a server that does not, we will take this
2568 * shortcut.
2569 *
2570 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2571 * in order to satisfy 'weird' needs from the server side.
2572 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002573 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2574 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002575 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002576 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002577 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002578 }
2579 else
2580 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002581 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002582 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002583 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002584 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002585
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002586 /* Info from md_alg will be used instead */
2587 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002588 offset = 2;
2589 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002590 else
2591#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002592 {
2593 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002594 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002595 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002596
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002597 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002598 ssl->out_msg + 6 + offset, &n,
2599 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002600 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002601 SSL_DEBUG_RET( 1, "pk_sign", ret );
2602 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002603 }
Paul Bakker926af752012-11-23 13:38:07 +01002604
Paul Bakker1ef83d62012-04-11 12:09:53 +00002605 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2606 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
Paul Bakker1ef83d62012-04-11 12:09:53 +00002608 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2610 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2611
2612 ssl->state++;
2613
2614 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2615 {
2616 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2617 return( ret );
2618 }
2619
2620 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2621
Paul Bakkered27a042013-04-18 22:46:23 +02002622 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002624#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2625 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2626 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
Paul Bakkera503a632013-08-14 13:48:06 +02002628#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002629static int ssl_parse_new_session_ticket( ssl_context *ssl )
2630{
2631 int ret;
2632 uint32_t lifetime;
2633 size_t ticket_len;
2634 unsigned char *ticket;
2635
2636 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2637
2638 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2639 {
2640 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2641 return( ret );
2642 }
2643
2644 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2645 {
2646 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2647 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2648 }
2649
2650 /*
2651 * struct {
2652 * uint32 ticket_lifetime_hint;
2653 * opaque ticket<0..2^16-1>;
2654 * } NewSessionTicket;
2655 *
2656 * 0 . 0 handshake message type
2657 * 1 . 3 handshake message length
2658 * 4 . 7 ticket_lifetime_hint
2659 * 8 . 9 ticket_len (n)
2660 * 10 . 9+n ticket content
2661 */
2662 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2663 ssl->in_hslen < 10 )
2664 {
2665 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2666 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2667 }
2668
2669 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2670 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2671
2672 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2673
2674 if( ticket_len + 10 != ssl->in_hslen )
2675 {
2676 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2677 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2678 }
2679
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002680 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2681
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002682 /* We're not waiting for a NewSessionTicket message any more */
2683 ssl->handshake->new_session_ticket = 0;
2684
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002685 /*
2686 * Zero-length ticket means the server changed his mind and doesn't want
2687 * to send a ticket after all, so just forget it
2688 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002689 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002690 return( 0 );
2691
Paul Bakker34617722014-06-13 17:20:13 +02002692 polarssl_zeroize( ssl->session_negotiate->ticket,
2693 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002694 polarssl_free( ssl->session_negotiate->ticket );
2695 ssl->session_negotiate->ticket = NULL;
2696 ssl->session_negotiate->ticket_len = 0;
2697
2698 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2699 {
2700 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2701 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2702 }
2703
2704 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2705
2706 ssl->session_negotiate->ticket = ticket;
2707 ssl->session_negotiate->ticket_len = ticket_len;
2708 ssl->session_negotiate->ticket_lifetime = lifetime;
2709
2710 /*
2711 * RFC 5077 section 3.4:
2712 * "If the client receives a session ticket from the server, then it
2713 * discards any Session ID that was sent in the ServerHello."
2714 */
2715 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2716 ssl->session_negotiate->length = 0;
2717
2718 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2719
2720 return( 0 );
2721}
Paul Bakkera503a632013-08-14 13:48:06 +02002722#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002723
Paul Bakker5121ce52009-01-03 21:22:43 +00002724/*
Paul Bakker1961b702013-01-25 14:49:24 +01002725 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 */
Paul Bakker1961b702013-01-25 14:49:24 +01002727int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002728{
2729 int ret = 0;
2730
Paul Bakker1961b702013-01-25 14:49:24 +01002731 if( ssl->state == SSL_HANDSHAKE_OVER )
2732 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Paul Bakker1961b702013-01-25 14:49:24 +01002734 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2735
2736 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2737 return( ret );
2738
2739 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 {
Paul Bakker1961b702013-01-25 14:49:24 +01002741 case SSL_HELLO_REQUEST:
2742 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 break;
2744
Paul Bakker1961b702013-01-25 14:49:24 +01002745 /*
2746 * ==> ClientHello
2747 */
2748 case SSL_CLIENT_HELLO:
2749 ret = ssl_write_client_hello( ssl );
2750 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
Paul Bakker1961b702013-01-25 14:49:24 +01002752 /*
2753 * <== ServerHello
2754 * Certificate
2755 * ( ServerKeyExchange )
2756 * ( CertificateRequest )
2757 * ServerHelloDone
2758 */
2759 case SSL_SERVER_HELLO:
2760 ret = ssl_parse_server_hello( ssl );
2761 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
Paul Bakker1961b702013-01-25 14:49:24 +01002763 case SSL_SERVER_CERTIFICATE:
2764 ret = ssl_parse_certificate( ssl );
2765 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Paul Bakker1961b702013-01-25 14:49:24 +01002767 case SSL_SERVER_KEY_EXCHANGE:
2768 ret = ssl_parse_server_key_exchange( ssl );
2769 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
Paul Bakker1961b702013-01-25 14:49:24 +01002771 case SSL_CERTIFICATE_REQUEST:
2772 ret = ssl_parse_certificate_request( ssl );
2773 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
Paul Bakker1961b702013-01-25 14:49:24 +01002775 case SSL_SERVER_HELLO_DONE:
2776 ret = ssl_parse_server_hello_done( ssl );
2777 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
Paul Bakker1961b702013-01-25 14:49:24 +01002779 /*
2780 * ==> ( Certificate/Alert )
2781 * ClientKeyExchange
2782 * ( CertificateVerify )
2783 * ChangeCipherSpec
2784 * Finished
2785 */
2786 case SSL_CLIENT_CERTIFICATE:
2787 ret = ssl_write_certificate( ssl );
2788 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Paul Bakker1961b702013-01-25 14:49:24 +01002790 case SSL_CLIENT_KEY_EXCHANGE:
2791 ret = ssl_write_client_key_exchange( ssl );
2792 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Paul Bakker1961b702013-01-25 14:49:24 +01002794 case SSL_CERTIFICATE_VERIFY:
2795 ret = ssl_write_certificate_verify( ssl );
2796 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
Paul Bakker1961b702013-01-25 14:49:24 +01002798 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2799 ret = ssl_write_change_cipher_spec( ssl );
2800 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
Paul Bakker1961b702013-01-25 14:49:24 +01002802 case SSL_CLIENT_FINISHED:
2803 ret = ssl_write_finished( ssl );
2804 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
Paul Bakker1961b702013-01-25 14:49:24 +01002806 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002807 * <== ( NewSessionTicket )
2808 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002809 * Finished
2810 */
2811 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002812#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002813 if( ssl->handshake->new_session_ticket != 0 )
2814 ret = ssl_parse_new_session_ticket( ssl );
2815 else
Paul Bakkera503a632013-08-14 13:48:06 +02002816#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002817 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002818 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Paul Bakker1961b702013-01-25 14:49:24 +01002820 case SSL_SERVER_FINISHED:
2821 ret = ssl_parse_finished( ssl );
2822 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002823
Paul Bakker1961b702013-01-25 14:49:24 +01002824 case SSL_FLUSH_BUFFERS:
2825 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2826 ssl->state = SSL_HANDSHAKE_WRAPUP;
2827 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
Paul Bakker1961b702013-01-25 14:49:24 +01002829 case SSL_HANDSHAKE_WRAPUP:
2830 ssl_handshake_wrapup( ssl );
2831 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002832
Paul Bakker1961b702013-01-25 14:49:24 +01002833 default:
2834 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2835 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2836 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
2838 return( ret );
2839}
Paul Bakker9af723c2014-05-01 13:03:14 +02002840#endif /* POLARSSL_SSL_CLI_C */