blob: 964154a4b500df7e874b1378bd790db728e18ee3 [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 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (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 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200608 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000609#endif
610 if( ssl->session_negotiate->ticket != NULL &&
611 ssl->session_negotiate->ticket_len != 0 )
612 {
613 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200614
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000615 if( ret != 0 )
616 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200617
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000618 ssl->session_negotiate->length = n = 32;
619 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200620 }
Paul Bakkera503a632013-08-14 13:48:06 +0200621#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
623 *p++ = (unsigned char) n;
624
625 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000626 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
629 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
630
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200631 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200632 n = 0;
633 q = p;
634
635 // Skip writing ciphersuite length for now
636 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
Paul Bakker2fbefde2013-06-29 16:01:15 +0200638 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200640 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
641
642 if( ciphersuite_info == NULL )
643 continue;
644
645 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
646 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
647 continue;
648
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100649 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
650 ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
651 continue;
652
Paul Bakkere3166ce2011-01-27 17:40:50 +0000653 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200654 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Paul Bakker2fbefde2013-06-29 16:01:15 +0200656 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200657 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
658 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 }
660
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +0000661 /*
662 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
663 */
664#if defined(POLARSSL_SSL_RENEGOTIATION)
665 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
666#endif
667 {
668 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
669 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
670 n++;
671 }
672
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200673 /* Some versions of OpenSSL don't handle it correctly if not at end */
674#if defined(POLARSSL_SSL_FALLBACK_SCSV)
675 if( ssl->fallback == SSL_IS_FALLBACK )
676 {
677 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
678 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
679 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
680 n++;
681 }
682#endif
683
Paul Bakker2fbefde2013-06-29 16:01:15 +0200684 *q++ = (unsigned char)( n >> 7 );
685 *q++ = (unsigned char)( n << 1 );
686
687 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
688
689
Paul Bakker2770fbd2012-07-03 13:30:23 +0000690#if defined(POLARSSL_ZLIB_SUPPORT)
691 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
692 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000693 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000694
695 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000696 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000697 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000698#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000700 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000701
702 *p++ = 1;
703 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200704#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Paul Bakkerd3edc862013-03-20 16:07:17 +0100706 // First write extensions, then the total length
707 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200708#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100709 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
710 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200711#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100713#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100714 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
715 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100716#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000717
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100718#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
719 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100720 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
721 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200722#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000723
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200724#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100725 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
726 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100727
Paul Bakkerd3edc862013-03-20 16:07:17 +0100728 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
729 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100730#endif
731
Paul Bakker05decb22013-08-15 13:33:48 +0200732#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200733 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
734 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200735#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200736
Paul Bakker1f2bc622013-08-15 13:45:55 +0200737#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200738 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
739 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200740#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200741
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100742#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
743 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
744 ext_len += olen;
745#endif
746
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200747#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
748 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
749 ext_len += olen;
750#endif
751
Paul Bakkera503a632013-08-14 13:48:06 +0200752#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200753 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
754 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200755#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200756
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200757#if defined(POLARSSL_SSL_ALPN)
758 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
759 ext_len += olen;
760#endif
761
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100762 /* olen unused if all extensions are disabled */
763 ((void) olen);
764
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000765 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
766 ext_len ) );
767
Paul Bakkera7036632014-04-30 10:15:38 +0200768 if( ext_len > 0 )
769 {
770 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
771 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
772 p += ext_len;
773 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100774
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 ssl->out_msglen = p - buf;
776 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
777 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
778
779 ssl->state++;
780
781 if( ( ret = ssl_write_record( ssl ) ) != 0 )
782 {
783 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
784 return( ret );
785 }
786
787 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
788
789 return( 0 );
790}
791
Paul Bakker48916f92012-09-16 19:57:18 +0000792static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200793 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000794 size_t len )
795{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000796 int ret;
797
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100798#if defined(POLARSSL_SSL_RENEGOTIATION)
799 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000800 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100801 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000802 if( len != 1 + ssl->verify_data_len * 2 ||
803 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100804 safer_memcmp( buf + 1,
805 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
806 safer_memcmp( buf + 1 + ssl->verify_data_len,
807 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000808 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100809 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000810
811 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
812 return( ret );
813
Paul Bakker48916f92012-09-16 19:57:18 +0000814 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
815 }
816 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100817 else
818#endif /* POLARSSL_SSL_RENEGOTIATION */
819 {
820 if( len != 1 || buf[0] != 0x00 )
821 {
822 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
823
824 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
825 return( ret );
826
827 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
828 }
829
830 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
831 }
Paul Bakker48916f92012-09-16 19:57:18 +0000832
833 return( 0 );
834}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200835
Paul Bakker05decb22013-08-15 13:33:48 +0200836#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200837static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200838 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200839 size_t len )
840{
841 /*
842 * server should use the extension only if we did,
843 * and if so the server's value should match ours (and len is always 1)
844 */
845 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
846 len != 1 ||
847 buf[0] != ssl->mfl_code )
848 {
849 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
850 }
851
852 return( 0 );
853}
Paul Bakker05decb22013-08-15 13:33:48 +0200854#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000855
Paul Bakker1f2bc622013-08-15 13:45:55 +0200856#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200857static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
858 const unsigned char *buf,
859 size_t len )
860{
861 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
862 len != 0 )
863 {
864 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
865 }
866
867 ((void) buf);
868
869 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
870
871 return( 0 );
872}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200873#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200874
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100875#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
876static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
877 const unsigned char *buf,
878 size_t len )
879{
880 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
881 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
882 len != 0 )
883 {
884 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
885 }
886
887 ((void) buf);
888
889 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
890
891 return( 0 );
892}
893#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
894
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200895#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
896static int ssl_parse_extended_ms_ext( ssl_context *ssl,
897 const unsigned char *buf,
898 size_t len )
899{
900 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200901 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200902 len != 0 )
903 {
904 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
905 }
906
907 ((void) buf);
908
909 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
910
911 return( 0 );
912}
913#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
914
Paul Bakkera503a632013-08-14 13:48:06 +0200915#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200916static int ssl_parse_session_ticket_ext( ssl_context *ssl,
917 const unsigned char *buf,
918 size_t len )
919{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200920 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
921 len != 0 )
922 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200923 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200924 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200925
926 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200927
928 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200929
930 return( 0 );
931}
Paul Bakkera503a632013-08-14 13:48:06 +0200932#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200933
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200934#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200935static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
936 const unsigned char *buf,
937 size_t len )
938{
939 size_t list_size;
940 const unsigned char *p;
941
942 list_size = buf[0];
943 if( list_size + 1 != len )
944 {
945 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
946 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
947 }
948
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200949 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200950 while( list_size > 0 )
951 {
952 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
953 p[0] == POLARSSL_ECP_PF_COMPRESSED )
954 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200955 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200956 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
957 return( 0 );
958 }
959
960 list_size--;
961 p++;
962 }
963
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200964 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
965 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200966}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200967#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200968
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200969#if defined(POLARSSL_SSL_ALPN)
970static int ssl_parse_alpn_ext( ssl_context *ssl,
971 const unsigned char *buf, size_t len )
972{
973 size_t list_len, name_len;
974 const char **p;
975
976 /* If we didn't send it, the server shouldn't send it */
977 if( ssl->alpn_list == NULL )
978 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
979
980 /*
981 * opaque ProtocolName<1..2^8-1>;
982 *
983 * struct {
984 * ProtocolName protocol_name_list<2..2^16-1>
985 * } ProtocolNameList;
986 *
987 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
988 */
989
990 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
991 if( len < 4 )
992 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
993
994 list_len = ( buf[0] << 8 ) | buf[1];
995 if( list_len != len - 2 )
996 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
997
998 name_len = buf[2];
999 if( name_len != list_len - 1 )
1000 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1001
1002 /* Check that the server chosen protocol was in our list and save it */
1003 for( p = ssl->alpn_list; *p != NULL; p++ )
1004 {
1005 if( name_len == strlen( *p ) &&
1006 memcmp( buf + 3, *p, name_len ) == 0 )
1007 {
1008 ssl->alpn_chosen = *p;
1009 return( 0 );
1010 }
1011 }
1012
1013 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1014}
1015#endif /* POLARSSL_SSL_ALPN */
1016
Paul Bakker5121ce52009-01-03 21:22:43 +00001017static int ssl_parse_server_hello( ssl_context *ssl )
1018{
Paul Bakker2770fbd2012-07-03 13:30:23 +00001019 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +00001020 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001021 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001022 unsigned char *buf, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001023#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001024 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001025#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001026 int handshake_failure = 0;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001027 const ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001028#if defined(POLARSSL_DEBUG_C)
1029 uint32_t t;
1030#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
1032 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1033
1034 /*
1035 * 0 . 0 handshake type
1036 * 1 . 3 handshake length
1037 * 4 . 5 protocol version
1038 * 6 . 9 UNIX time()
1039 * 10 . 37 random bytes
1040 */
1041 buf = ssl->in_msg;
1042
1043 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1044 {
1045 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1046 return( ret );
1047 }
1048
1049 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1050 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001051#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001052 if( ssl->renegotiation == SSL_RENEGOTIATION )
1053 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001054 ssl->renego_records_seen++;
1055
1056 if( ssl->renego_max_records >= 0 &&
1057 ssl->renego_records_seen > ssl->renego_max_records )
1058 {
1059 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1060 "but not honored by server" ) );
1061 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1062 }
1063
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001064 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1065 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1066 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001067#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001068
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001070 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001071 }
1072
1073 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1074 buf[4], buf[5] ) );
1075
1076 if( ssl->in_hslen < 42 ||
1077 buf[0] != SSL_HS_SERVER_HELLO ||
1078 buf[4] != SSL_MAJOR_VERSION_3 )
1079 {
1080 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001081 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 }
1083
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001084 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 {
1086 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001087 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 }
1089
1090 ssl->minor_ver = buf[5];
1091
Paul Bakker1d29fb52012-09-28 13:28:45 +00001092 if( ssl->minor_ver < ssl->min_minor_ver )
1093 {
1094 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001095 " [%d:%d] < [%d:%d]", ssl->major_ver,
1096 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001097
1098 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1099 SSL_ALERT_MSG_PROTOCOL_VERSION );
1100
1101 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1102 }
1103
Paul Bakker1504af52012-02-11 16:17:43 +00001104#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001105 t = ( (uint32_t) buf[6] << 24 )
1106 | ( (uint32_t) buf[7] << 16 )
1107 | ( (uint32_t) buf[8] << 8 )
1108 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001109 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
Paul Bakker48916f92012-09-16 19:57:18 +00001112 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001113
1114 n = buf[38];
1115
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1117
Paul Bakker48916f92012-09-16 19:57:18 +00001118 if( n > 32 )
1119 {
1120 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1121 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1122 }
1123
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 /*
1125 * 38 . 38 session id length
1126 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001127 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 * 41+n . 41+n chosen compression alg.
1129 * 42+n . 43+n extensions length
1130 * 44+n . 44+n+m extensions
1131 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001132 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 {
1134 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001135 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
Paul Bakker48916f92012-09-16 19:57:18 +00001137 if( ( ext_len > 0 && ext_len < 4 ) ||
1138 ssl->in_hslen != 44 + n + ext_len )
1139 {
1140 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1141 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1142 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001143 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001144 else if( ssl->in_hslen == 42 + n )
1145 {
1146 ext_len = 0;
1147 }
1148 else
1149 {
1150 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1151 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1152 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
1154 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001155 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Paul Bakker380da532012-04-18 16:10:25 +00001157 /*
1158 * Initialize update checksum functions
1159 */
Paul Bakker68884e32013-01-07 18:20:04 +01001160 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1161
1162 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1163 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001164 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001165 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1166 }
Paul Bakker380da532012-04-18 16:10:25 +00001167
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001168 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1169
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1171 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1172
1173 /*
1174 * Check if the session can be resumed
1175 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001176 if( ssl->handshake->resume == 0 || n == 0 ||
1177#if defined(POLARSSL_SSL_RENEGOTIATION)
1178 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1179#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001180 ssl->session_negotiate->ciphersuite != i ||
1181 ssl->session_negotiate->compression != comp ||
1182 ssl->session_negotiate->length != n ||
1183 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 {
1185 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001186 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001187#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001188 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001189#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001190 ssl->session_negotiate->ciphersuite = i;
1191 ssl->session_negotiate->compression = comp;
1192 ssl->session_negotiate->length = n;
1193 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 }
1195 else
1196 {
1197 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001198
1199 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1200 {
1201 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1202 return( ret );
1203 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 }
1205
1206 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001207 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001208
Paul Bakkere3166ce2011-01-27 17:40:50 +00001209 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1211
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001212 suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1213 if( suite_info == NULL ||
1214 ( ssl->arc4_disabled &&
1215 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) )
1216 {
1217 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1218 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1219 }
1220
1221
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 i = 0;
1223 while( 1 )
1224 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001225 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
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 }
1230
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001231 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1232 ssl->session_negotiate->ciphersuite )
1233 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001235 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 }
1237
Paul Bakker2770fbd2012-07-03 13:30:23 +00001238 if( comp != SSL_COMPRESS_NULL
1239#if defined(POLARSSL_ZLIB_SUPPORT)
1240 && comp != SSL_COMPRESS_DEFLATE
1241#endif
1242 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 {
1244 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001245 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 }
Paul Bakker48916f92012-09-16 19:57:18 +00001247 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
Paul Bakker48916f92012-09-16 19:57:18 +00001249 ext = buf + 44 + n;
1250
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001251 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1252
Paul Bakker48916f92012-09-16 19:57:18 +00001253 while( ext_len )
1254 {
1255 unsigned int ext_id = ( ( ext[0] << 8 )
1256 | ( ext[1] ) );
1257 unsigned int ext_size = ( ( ext[2] << 8 )
1258 | ( ext[3] ) );
1259
1260 if( ext_size + 4 > ext_len )
1261 {
1262 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1263 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1264 }
1265
1266 switch( ext_id )
1267 {
1268 case TLS_EXT_RENEGOTIATION_INFO:
1269 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001270#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001271 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001272#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001273
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001274 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1275 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001276 return( ret );
1277
1278 break;
1279
Paul Bakker05decb22013-08-15 13:33:48 +02001280#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001281 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1282 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1283
1284 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1285 ext + 4, ext_size ) ) != 0 )
1286 {
1287 return( ret );
1288 }
1289
1290 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001291#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001292
Paul Bakker1f2bc622013-08-15 13:45:55 +02001293#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001294 case TLS_EXT_TRUNCATED_HMAC:
1295 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1296
1297 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1298 ext + 4, ext_size ) ) != 0 )
1299 {
1300 return( ret );
1301 }
1302
1303 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001304#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001305
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001306#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1307 case TLS_EXT_ENCRYPT_THEN_MAC:
1308 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1309
1310 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1311 ext + 4, ext_size ) ) != 0 )
1312 {
1313 return( ret );
1314 }
1315
1316 break;
1317#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1318
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001319#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1320 case TLS_EXT_EXTENDED_MASTER_SECRET:
1321 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1322
1323 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1324 ext + 4, ext_size ) ) != 0 )
1325 {
1326 return( ret );
1327 }
1328
1329 break;
1330#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1331
Paul Bakkera503a632013-08-14 13:48:06 +02001332#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001333 case TLS_EXT_SESSION_TICKET:
1334 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1335
1336 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1337 ext + 4, ext_size ) ) != 0 )
1338 {
1339 return( ret );
1340 }
1341
1342 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001343#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001344
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001345#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001346 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1347 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1348
1349 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1350 ext + 4, ext_size ) ) != 0 )
1351 {
1352 return( ret );
1353 }
1354
1355 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001356#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001357
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001358#if defined(POLARSSL_SSL_ALPN)
1359 case TLS_EXT_ALPN:
1360 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1361
1362 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1363 return( ret );
1364
1365 break;
1366#endif /* POLARSSL_SSL_ALPN */
1367
Paul Bakker48916f92012-09-16 19:57:18 +00001368 default:
1369 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1370 ext_id ) );
1371 }
1372
1373 ext_len -= 4 + ext_size;
1374 ext += 4 + ext_size;
1375
1376 if( ext_len > 0 && ext_len < 4 )
1377 {
1378 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1379 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1380 }
1381 }
1382
1383 /*
1384 * Renegotiation security checks
1385 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001386 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1387 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001388 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001389 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1390 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001391 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001392#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001393 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1394 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1395 renegotiation_info_seen == 0 )
1396 {
1397 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1398 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001399 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001400 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1401 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1402 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001403 {
1404 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001405 handshake_failure = 1;
1406 }
1407 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1408 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1409 renegotiation_info_seen == 1 )
1410 {
1411 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1412 handshake_failure = 1;
1413 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001414#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001415
1416 if( handshake_failure == 1 )
1417 {
1418 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1419 return( ret );
1420
Paul Bakker48916f92012-09-16 19:57:18 +00001421 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001423
1424 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1425
1426 return( 0 );
1427}
1428
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001429#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1430 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001431static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1432 unsigned char *end )
1433{
1434 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1435
Paul Bakker29e1f122013-04-16 13:07:56 +02001436 /*
1437 * Ephemeral DH parameters:
1438 *
1439 * struct {
1440 * opaque dh_p<1..2^16-1>;
1441 * opaque dh_g<1..2^16-1>;
1442 * opaque dh_Ys<1..2^16-1>;
1443 * } ServerDHParams;
1444 */
1445 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1446 {
1447 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1448 return( ret );
1449 }
1450
1451 if( ssl->handshake->dhm_ctx.len < 64 ||
1452 ssl->handshake->dhm_ctx.len > 512 )
1453 {
1454 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1455 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1456 }
1457
1458 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1459 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1460 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001461
1462 return( ret );
1463}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001464#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1465 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001466
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001467#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001468 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001469 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1470 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1471 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1472static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1473{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001474 const ecp_curve_info *curve_info;
1475
1476 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1477 if( curve_info == NULL )
1478 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001479 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1480 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001481 }
1482
1483 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001484
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001485#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1486 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1487#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001488 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1489 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001490#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001491 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001492
1493 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1494
1495 return( 0 );
1496}
Paul Bakker9af723c2014-05-01 13:03:14 +02001497#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1498 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1499 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1500 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1501 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001502
1503#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1504 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001505 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001506static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1507 unsigned char **p,
1508 unsigned char *end )
1509{
1510 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1511
Paul Bakker29e1f122013-04-16 13:07:56 +02001512 /*
1513 * Ephemeral ECDH parameters:
1514 *
1515 * struct {
1516 * ECParameters curve_params;
1517 * ECPoint public;
1518 * } ServerECDHParams;
1519 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001520 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1521 (const unsigned char **) p, end ) ) != 0 )
1522 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001523 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001524 return( ret );
1525 }
1526
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001527 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001528 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001529 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001530 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1531 }
1532
Paul Bakker29e1f122013-04-16 13:07:56 +02001533 return( ret );
1534}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001535#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001536 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1537 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001538
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001539#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001540static int ssl_parse_server_psk_hint( ssl_context *ssl,
1541 unsigned char **p,
1542 unsigned char *end )
1543{
1544 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001545 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001546 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001547
1548 /*
1549 * PSK parameters:
1550 *
1551 * opaque psk_identity_hint<0..2^16-1>;
1552 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001553 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001554 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001555
1556 if( (*p) + len > end )
1557 {
1558 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1559 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1560 }
1561
1562 // TODO: Retrieve PSK identity hint and callback to app
1563 //
1564 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001565 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001566
1567 return( ret );
1568}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001569#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001570
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001571#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1572 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1573/*
1574 * Generate a pre-master secret and encrypt it with the server's RSA key
1575 */
1576static int ssl_write_encrypted_pms( ssl_context *ssl,
1577 size_t offset, size_t *olen,
1578 size_t pms_offset )
1579{
1580 int ret;
1581 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1582 unsigned char *p = ssl->handshake->premaster + pms_offset;
1583
1584 /*
1585 * Generate (part of) the pre-master as
1586 * struct {
1587 * ProtocolVersion client_version;
1588 * opaque random[46];
1589 * } PreMasterSecret;
1590 */
1591 p[0] = (unsigned char) ssl->max_major_ver;
1592 p[1] = (unsigned char) ssl->max_minor_ver;
1593
1594 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1595 {
1596 SSL_DEBUG_RET( 1, "f_rng", ret );
1597 return( ret );
1598 }
1599
1600 ssl->handshake->pmslen = 48;
1601
1602 /*
1603 * Now write it out, encrypted
1604 */
1605 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1606 POLARSSL_PK_RSA ) )
1607 {
1608 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1609 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1610 }
1611
1612 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1613 p, ssl->handshake->pmslen,
1614 ssl->out_msg + offset + len_bytes, olen,
1615 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1616 ssl->f_rng, ssl->p_rng ) ) != 0 )
1617 {
1618 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1619 return( ret );
1620 }
1621
1622#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1623 defined(POLARSSL_SSL_PROTO_TLS1_2)
1624 if( len_bytes == 2 )
1625 {
1626 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1627 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1628 *olen += 2;
1629 }
1630#endif
1631
1632 return( 0 );
1633}
1634#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1635 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001636
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001637#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001638#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001639 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1640 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001641static int ssl_parse_signature_algorithm( ssl_context *ssl,
1642 unsigned char **p,
1643 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001644 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001645 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001646{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001647 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001648 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001649 *pk_alg = POLARSSL_PK_NONE;
1650
1651 /* Only in TLS 1.2 */
1652 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1653 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001654 return( 0 );
1655 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001656
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001657 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001658 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1659
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001660 /*
1661 * Get hash algorithm
1662 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001663 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001664 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001665 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1666 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001667 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1668 }
1669
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001670 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001671 * Get signature algorithm
1672 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001673 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001674 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001675 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1676 "SignatureAlgorithm %d", (*p)[1] ) );
1677 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001678 }
1679
1680 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1681 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1682 *p += 2;
1683
1684 return( 0 );
1685}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001686#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001687 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1688 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001689#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001690
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001691
1692#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1693 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1694static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1695{
1696 int ret;
1697 const ecp_keypair *peer_key;
1698
1699 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1700 POLARSSL_PK_ECKEY ) )
1701 {
1702 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1703 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1704 }
1705
1706 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1707
1708 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1709 POLARSSL_ECDH_THEIRS ) ) != 0 )
1710 {
1711 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1712 return( ret );
1713 }
1714
1715 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1716 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001717 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001718 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1719 }
1720
1721 return( ret );
1722}
1723#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1724 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1725
Paul Bakker41c83d32013-03-20 14:39:14 +01001726static int ssl_parse_server_key_exchange( ssl_context *ssl )
1727{
Paul Bakker23986e52011-04-24 08:57:21 +00001728 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001729 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001730 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001731#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001732 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1733 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001734 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001735 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001736 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001737 size_t hashlen;
1738 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001739#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001740
1741 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1742
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001743#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001744 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001745 {
1746 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1747 ssl->state++;
1748 return( 0 );
1749 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001750 ((void) p);
1751 ((void) end);
1752#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001753
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001754#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1755 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1756 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1757 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1758 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001759 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1760 {
1761 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1762 return( ret );
1763 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001764
1765 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1766 ssl->state++;
1767 return( 0 );
1768 }
1769 ((void) p);
1770 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001771#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1772 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001773
Paul Bakker5121ce52009-01-03 21:22:43 +00001774 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1775 {
1776 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1777 return( ret );
1778 }
1779
1780 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1781 {
1782 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001783 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 }
1785
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001786 /*
1787 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1788 * doesn't use a psk_identity_hint
1789 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1791 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001792 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1793 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001794 {
1795 ssl->record_read = 1;
1796 goto exit;
1797 }
1798
1799 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1800 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001801 }
1802
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001803 p = ssl->in_msg + 4;
1804 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001805 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001806
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001807#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1808 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1809 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1810 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1811 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1812 {
1813 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1814 {
1815 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1816 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1817 }
1818 } /* FALLTROUGH */
1819#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1820
1821#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1822 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1823 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1824 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1825 ; /* nothing more to do */
1826 else
1827#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1828 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1829#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1830 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1831 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1832 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001834 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001835 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001836 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001837 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1838 }
1839 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001840 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001841#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1842 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001843#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001844 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001845 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1846 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001847 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001848 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001849 {
1850 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1851 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001852 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1853 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1854 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001855 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001856 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001857#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001858 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001859 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001860 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001861 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001862 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001863 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001864
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001865#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001866 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1867 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001868 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001869 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1870 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001871 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001872 params_len = p - ( ssl->in_msg + 4 );
1873
Paul Bakker29e1f122013-04-16 13:07:56 +02001874 /*
1875 * Handle the digitally-signed structure
1876 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001877#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1878 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001879 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001880 if( ssl_parse_signature_algorithm( ssl, &p, end,
1881 &md_alg, &pk_alg ) != 0 )
1882 {
1883 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1884 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1885 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001886
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001887 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001888 {
1889 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1890 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1891 }
1892 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001893 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001894#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001895#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1896 defined(POLARSSL_SSL_PROTO_TLS1_1)
1897 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001898 {
1899 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001900
Paul Bakker9659dae2013-08-28 16:21:34 +02001901 /* Default hash for ECDSA is SHA-1 */
1902 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1903 md_alg = POLARSSL_MD_SHA1;
1904 }
1905 else
1906#endif
1907 {
1908 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001909 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001910 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001911
1912 /*
1913 * Read signature
1914 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001915 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001916 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001917
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001918 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001919 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001920 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001921 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1922 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001923
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001924 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001925
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001926 /*
1927 * Compute the hash that has been signed
1928 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001929#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1930 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001931 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001932 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001933 md5_context md5;
1934 sha1_context sha1;
1935
Paul Bakker5b4af392014-06-26 12:09:34 +02001936 md5_init( &md5 );
1937 sha1_init( &sha1 );
1938
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001939 hashlen = 36;
1940
Paul Bakker29e1f122013-04-16 13:07:56 +02001941 /*
1942 * digitally-signed struct {
1943 * opaque md5_hash[16];
1944 * opaque sha_hash[20];
1945 * };
1946 *
1947 * md5_hash
1948 * MD5(ClientHello.random + ServerHello.random
1949 * + ServerParams);
1950 * sha_hash
1951 * SHA(ClientHello.random + ServerHello.random
1952 * + ServerParams);
1953 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001954 md5_starts( &md5 );
1955 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001956 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001957 md5_finish( &md5, hash );
1958
1959 sha1_starts( &sha1 );
1960 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001961 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001962 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001963
1964 md5_free( &md5 );
1965 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001966 }
1967 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001968#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1969 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001970#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1971 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001972 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001973 {
1974 md_context_t ctx;
1975
Paul Bakker84bbeb52014-07-01 14:53:22 +02001976 md_init( &ctx );
1977
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001978 /* Info from md_alg will be used instead */
1979 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001980
1981 /*
1982 * digitally-signed struct {
1983 * opaque client_random[32];
1984 * opaque server_random[32];
1985 * ServerDHParams params;
1986 * };
1987 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001988 if( ( ret = md_init_ctx( &ctx,
1989 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001990 {
1991 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1992 return( ret );
1993 }
1994
1995 md_starts( &ctx );
1996 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001997 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001998 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001999 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002000 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002001 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002002#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2003 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002004 {
Paul Bakker577e0062013-08-28 11:57:20 +02002005 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002006 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002007 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002008
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002009 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2010 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002011
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002012 /*
2013 * Verify signature
2014 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002015 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002016 {
2017 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2018 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2019 }
2020
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002021 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2022 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002023 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002024 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002025 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002026 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002028#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002029 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2030 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002031
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002032exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002033 ssl->state++;
2034
2035 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2036
2037 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002038}
2039
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002040#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2041 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2042 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2043 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2044static int ssl_parse_certificate_request( ssl_context *ssl )
2045{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002046 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2047
2048 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2049
2050 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2051 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2052 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2053 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2054 {
2055 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2056 ssl->state++;
2057 return( 0 );
2058 }
2059
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002060 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2061 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002062}
2063#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002064static int ssl_parse_certificate_request( ssl_context *ssl )
2065{
2066 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002067 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002068 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002069 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002070 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
2072 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2073
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002074 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2075 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2076 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2077 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2078 {
2079 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2080 ssl->state++;
2081 return( 0 );
2082 }
2083
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 /*
2085 * 0 . 0 handshake type
2086 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002087 * 4 . 4 cert type count
2088 * 5 .. m-1 cert types
2089 * m .. m+1 sig alg length (TLS 1.2 only)
2090 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 * n .. n+1 length of all DNs
2092 * n+2 .. n+3 length of DN 1
2093 * n+4 .. ... Distinguished Name #1
2094 * ... .. ... length of DN 2, etc.
2095 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002096 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002098 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2099 {
2100 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2101 return( ret );
2102 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002103
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002104 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2105 {
2106 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2107 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2108 }
2109
2110 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 }
2112
2113 ssl->client_auth = 0;
2114 ssl->state++;
2115
2116 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2117 ssl->client_auth++;
2118
2119 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2120 ssl->client_auth ? "a" : "no" ) );
2121
Paul Bakker926af752012-11-23 13:38:07 +01002122 if( ssl->client_auth == 0 )
2123 goto exit;
2124
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002125 ssl->record_read = 0;
2126
Paul Bakker926af752012-11-23 13:38:07 +01002127 // TODO: handshake_failure alert for an anonymous server to request
2128 // client authentication
2129
2130 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002131
Paul Bakker926af752012-11-23 13:38:07 +01002132 // Retrieve cert types
2133 //
2134 cert_type_len = buf[4];
2135 n = cert_type_len;
2136
2137 if( ssl->in_hslen < 6 + n )
2138 {
2139 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2140 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2141 }
2142
Paul Bakker73d44312013-05-22 13:56:26 +02002143 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002144 while( cert_type_len > 0 )
2145 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002146#if defined(POLARSSL_RSA_C)
2147 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002148 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002149 {
2150 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2151 break;
2152 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002153 else
2154#endif
2155#if defined(POLARSSL_ECDSA_C)
2156 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002157 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002158 {
2159 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2160 break;
2161 }
2162 else
2163#endif
2164 {
2165 ; /* Unsupported cert type, ignore */
2166 }
Paul Bakker926af752012-11-23 13:38:07 +01002167
2168 cert_type_len--;
2169 p++;
2170 }
2171
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002172#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002173 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2174 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002175 /* Ignored, see comments about hash in write_certificate_verify */
2176 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002177 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2178 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002179
2180 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002181 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002182 n += sig_alg_len;
2183
2184 if( ssl->in_hslen < 6 + n )
2185 {
2186 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2187 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2188 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002189 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002190#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002191
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002192 /* Ignore certificate_authorities, we only have one cert anyway */
2193 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002194 dn_len = ( ( buf[5 + m + n] << 8 )
2195 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002196
2197 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002198 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002199 {
2200 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2201 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2202 }
2203
2204exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002205 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2206
2207 return( 0 );
2208}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002209#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2210 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2211 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2212 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002213
2214static int ssl_parse_server_hello_done( ssl_context *ssl )
2215{
2216 int ret;
2217
2218 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2219
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002220 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 {
2222 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2223 {
2224 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2225 return( ret );
2226 }
2227
2228 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2229 {
2230 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002231 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 }
2233 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002234 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002235
2236 if( ssl->in_hslen != 4 ||
2237 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2238 {
2239 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002240 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 }
2242
2243 ssl->state++;
2244
2245 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2246
2247 return( 0 );
2248}
2249
2250static int ssl_write_client_key_exchange( ssl_context *ssl )
2251{
Paul Bakker23986e52011-04-24 08:57:21 +00002252 int ret;
2253 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002254 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
2256 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2257
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002258#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002259 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 /*
2262 * DHM key exchange -- send G^X mod P
2263 */
Paul Bakker48916f92012-09-16 19:57:18 +00002264 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
2266 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2267 ssl->out_msg[5] = (unsigned char)( n );
2268 i = 6;
2269
Paul Bakker29b64762012-09-25 09:36:44 +00002270 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002271 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 &ssl->out_msg[i], n,
2273 ssl->f_rng, ssl->p_rng );
2274 if( ret != 0 )
2275 {
2276 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2277 return( ret );
2278 }
2279
Paul Bakker48916f92012-09-16 19:57:18 +00002280 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2281 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002283 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002284
Paul Bakker48916f92012-09-16 19:57:18 +00002285 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2286 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002287 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002288 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 {
2290 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2291 return( ret );
2292 }
2293
Paul Bakker48916f92012-09-16 19:57:18 +00002294 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 }
2296 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002297#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002298#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002299 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2300 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2301 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002302 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002303 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2304 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2305 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002306 {
2307 /*
2308 * ECDH key exchange -- send client public value
2309 */
2310 i = 4;
2311
2312 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2313 &n,
2314 &ssl->out_msg[i], 1000,
2315 ssl->f_rng, ssl->p_rng );
2316 if( ret != 0 )
2317 {
2318 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2319 return( ret );
2320 }
2321
2322 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2323
2324 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2325 &ssl->handshake->pmslen,
2326 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002327 POLARSSL_MPI_MAX_SIZE,
2328 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002329 {
2330 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2331 return( ret );
2332 }
2333
2334 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2335 }
2336 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002337#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002338 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2339 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2340 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002341#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002342 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002343 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002344 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2345 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002346 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002347 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002348 * opaque psk_identity<0..2^16-1>;
2349 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002350 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002351 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2352
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002353 i = 4;
2354 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002355 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2356 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002357
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002358 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2359 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002360
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002361#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002362 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002363 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002364 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002365 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002366 else
2367#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002368#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2369 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2370 {
2371 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2372 return( ret );
2373 }
2374 else
2375#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002376#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002377 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002378 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002379 /*
2380 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2381 */
2382 n = ssl->handshake->dhm_ctx.len;
2383 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2384 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002385
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002386 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002387 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002388 &ssl->out_msg[i], n,
2389 ssl->f_rng, ssl->p_rng );
2390 if( ret != 0 )
2391 {
2392 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2393 return( ret );
2394 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002395 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002396 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002397#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002398#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002399 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002400 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002401 /*
2402 * ClientECDiffieHellmanPublic public;
2403 */
2404 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2405 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2406 ssl->f_rng, ssl->p_rng );
2407 if( ret != 0 )
2408 {
2409 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2410 return( ret );
2411 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002412
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002413 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2414 }
2415 else
2416#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2417 {
2418 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002419 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420 }
2421
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002422 if( ( ret = ssl_psk_derive_premaster( ssl,
2423 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002424 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002425 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002426 return( ret );
2427 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002428 }
2429 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002430#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002431#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002432 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002434 i = 4;
2435 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002436 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 }
Paul Bakkered27a042013-04-18 22:46:23 +02002438 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002439#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002440 {
2441 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002442 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002443 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 ssl->out_msglen = i + n;
2447 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2448 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2449
2450 ssl->state++;
2451
2452 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2453 {
2454 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2455 return( ret );
2456 }
2457
2458 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2459
2460 return( 0 );
2461}
2462
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002463#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2464 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002465 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2466 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002467static int ssl_write_certificate_verify( ssl_context *ssl )
2468{
Paul Bakkered27a042013-04-18 22:46:23 +02002469 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002470 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
2472 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2473
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002474 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2475 {
2476 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2477 return( ret );
2478 }
2479
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002480 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002481 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002482 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002483 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002484 {
2485 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2486 ssl->state++;
2487 return( 0 );
2488 }
2489
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002490 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2491 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002492}
2493#else
2494static int ssl_write_certificate_verify( ssl_context *ssl )
2495{
2496 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2497 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2498 size_t n = 0, offset = 0;
2499 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002500 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002502 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002503
2504 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2505
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002506 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2507 {
2508 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2509 return( ret );
2510 }
2511
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002512 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002513 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002514 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002515 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2516 {
2517 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2518 ssl->state++;
2519 return( 0 );
2520 }
2521
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002522 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 {
2524 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2525 ssl->state++;
2526 return( 0 );
2527 }
2528
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002529 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002531 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2532 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 }
2534
2535 /*
2536 * Make an RSA signature of the handshake digests
2537 */
Paul Bakker48916f92012-09-16 19:57:18 +00002538 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002540#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2541 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002542 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002543 {
Paul Bakker926af752012-11-23 13:38:07 +01002544 /*
2545 * digitally-signed struct {
2546 * opaque md5_hash[16];
2547 * opaque sha_hash[20];
2548 * };
2549 *
2550 * md5_hash
2551 * MD5(handshake_messages);
2552 *
2553 * sha_hash
2554 * SHA(handshake_messages);
2555 */
2556 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002557 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002558
2559 /*
2560 * For ECDSA, default hash is SHA-1 only
2561 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002562 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002563 {
2564 hash_start += 16;
2565 hashlen -= 16;
2566 md_alg = POLARSSL_MD_SHA1;
2567 }
Paul Bakker926af752012-11-23 13:38:07 +01002568 }
2569 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002570#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2571 POLARSSL_SSL_PROTO_TLS1_1 */
2572#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2573 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002574 {
2575 /*
2576 * digitally-signed struct {
2577 * opaque handshake_messages[handshake_messages_length];
2578 * };
2579 *
2580 * Taking shortcut here. We assume that the server always allows the
2581 * PRF Hash function and has sent it in the allowed signature
2582 * algorithms list received in the Certificate Request message.
2583 *
2584 * Until we encounter a server that does not, we will take this
2585 * shortcut.
2586 *
2587 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2588 * in order to satisfy 'weird' needs from the server side.
2589 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002590 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2591 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002592 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002593 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002594 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002595 }
2596 else
2597 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002598 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002599 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002600 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002601 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002602
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002603 /* Info from md_alg will be used instead */
2604 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002605 offset = 2;
2606 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002607 else
2608#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002609 {
2610 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002611 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002612 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002613
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002614 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002615 ssl->out_msg + 6 + offset, &n,
2616 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002617 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002618 SSL_DEBUG_RET( 1, "pk_sign", ret );
2619 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002620 }
Paul Bakker926af752012-11-23 13:38:07 +01002621
Paul Bakker1ef83d62012-04-11 12:09:53 +00002622 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2623 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Paul Bakker1ef83d62012-04-11 12:09:53 +00002625 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2627 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2628
2629 ssl->state++;
2630
2631 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2632 {
2633 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2634 return( ret );
2635 }
2636
2637 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2638
Paul Bakkered27a042013-04-18 22:46:23 +02002639 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002641#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2642 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2643 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
Paul Bakkera503a632013-08-14 13:48:06 +02002645#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002646static int ssl_parse_new_session_ticket( ssl_context *ssl )
2647{
2648 int ret;
2649 uint32_t lifetime;
2650 size_t ticket_len;
2651 unsigned char *ticket;
2652
2653 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2654
2655 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2656 {
2657 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2658 return( ret );
2659 }
2660
2661 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2662 {
2663 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2664 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2665 }
2666
2667 /*
2668 * struct {
2669 * uint32 ticket_lifetime_hint;
2670 * opaque ticket<0..2^16-1>;
2671 * } NewSessionTicket;
2672 *
2673 * 0 . 0 handshake message type
2674 * 1 . 3 handshake message length
2675 * 4 . 7 ticket_lifetime_hint
2676 * 8 . 9 ticket_len (n)
2677 * 10 . 9+n ticket content
2678 */
2679 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2680 ssl->in_hslen < 10 )
2681 {
2682 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2683 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2684 }
2685
2686 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2687 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2688
2689 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2690
2691 if( ticket_len + 10 != ssl->in_hslen )
2692 {
2693 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2694 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2695 }
2696
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002697 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2698
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002699 /* We're not waiting for a NewSessionTicket message any more */
2700 ssl->handshake->new_session_ticket = 0;
2701
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002702 /*
2703 * Zero-length ticket means the server changed his mind and doesn't want
2704 * to send a ticket after all, so just forget it
2705 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002706 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002707 return( 0 );
2708
Paul Bakker34617722014-06-13 17:20:13 +02002709 polarssl_zeroize( ssl->session_negotiate->ticket,
2710 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002711 polarssl_free( ssl->session_negotiate->ticket );
2712 ssl->session_negotiate->ticket = NULL;
2713 ssl->session_negotiate->ticket_len = 0;
2714
2715 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2716 {
2717 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2718 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2719 }
2720
2721 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2722
2723 ssl->session_negotiate->ticket = ticket;
2724 ssl->session_negotiate->ticket_len = ticket_len;
2725 ssl->session_negotiate->ticket_lifetime = lifetime;
2726
2727 /*
2728 * RFC 5077 section 3.4:
2729 * "If the client receives a session ticket from the server, then it
2730 * discards any Session ID that was sent in the ServerHello."
2731 */
2732 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2733 ssl->session_negotiate->length = 0;
2734
2735 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2736
2737 return( 0 );
2738}
Paul Bakkera503a632013-08-14 13:48:06 +02002739#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002740
Paul Bakker5121ce52009-01-03 21:22:43 +00002741/*
Paul Bakker1961b702013-01-25 14:49:24 +01002742 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 */
Paul Bakker1961b702013-01-25 14:49:24 +01002744int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002745{
2746 int ret = 0;
2747
Paul Bakker1961b702013-01-25 14:49:24 +01002748 if( ssl->state == SSL_HANDSHAKE_OVER )
2749 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Paul Bakker1961b702013-01-25 14:49:24 +01002751 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2752
2753 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2754 return( ret );
2755
2756 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 {
Paul Bakker1961b702013-01-25 14:49:24 +01002758 case SSL_HELLO_REQUEST:
2759 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 break;
2761
Paul Bakker1961b702013-01-25 14:49:24 +01002762 /*
2763 * ==> ClientHello
2764 */
2765 case SSL_CLIENT_HELLO:
2766 ret = ssl_write_client_hello( ssl );
2767 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002768
Paul Bakker1961b702013-01-25 14:49:24 +01002769 /*
2770 * <== ServerHello
2771 * Certificate
2772 * ( ServerKeyExchange )
2773 * ( CertificateRequest )
2774 * ServerHelloDone
2775 */
2776 case SSL_SERVER_HELLO:
2777 ret = ssl_parse_server_hello( ssl );
2778 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002779
Paul Bakker1961b702013-01-25 14:49:24 +01002780 case SSL_SERVER_CERTIFICATE:
2781 ret = ssl_parse_certificate( ssl );
2782 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
Paul Bakker1961b702013-01-25 14:49:24 +01002784 case SSL_SERVER_KEY_EXCHANGE:
2785 ret = ssl_parse_server_key_exchange( ssl );
2786 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Paul Bakker1961b702013-01-25 14:49:24 +01002788 case SSL_CERTIFICATE_REQUEST:
2789 ret = ssl_parse_certificate_request( ssl );
2790 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Paul Bakker1961b702013-01-25 14:49:24 +01002792 case SSL_SERVER_HELLO_DONE:
2793 ret = ssl_parse_server_hello_done( ssl );
2794 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
Paul Bakker1961b702013-01-25 14:49:24 +01002796 /*
2797 * ==> ( Certificate/Alert )
2798 * ClientKeyExchange
2799 * ( CertificateVerify )
2800 * ChangeCipherSpec
2801 * Finished
2802 */
2803 case SSL_CLIENT_CERTIFICATE:
2804 ret = ssl_write_certificate( ssl );
2805 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
Paul Bakker1961b702013-01-25 14:49:24 +01002807 case SSL_CLIENT_KEY_EXCHANGE:
2808 ret = ssl_write_client_key_exchange( ssl );
2809 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
Paul Bakker1961b702013-01-25 14:49:24 +01002811 case SSL_CERTIFICATE_VERIFY:
2812 ret = ssl_write_certificate_verify( ssl );
2813 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
Paul Bakker1961b702013-01-25 14:49:24 +01002815 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2816 ret = ssl_write_change_cipher_spec( ssl );
2817 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002818
Paul Bakker1961b702013-01-25 14:49:24 +01002819 case SSL_CLIENT_FINISHED:
2820 ret = ssl_write_finished( ssl );
2821 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
Paul Bakker1961b702013-01-25 14:49:24 +01002823 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002824 * <== ( NewSessionTicket )
2825 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002826 * Finished
2827 */
2828 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002829#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002830 if( ssl->handshake->new_session_ticket != 0 )
2831 ret = ssl_parse_new_session_ticket( ssl );
2832 else
Paul Bakkera503a632013-08-14 13:48:06 +02002833#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002834 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002835 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Paul Bakker1961b702013-01-25 14:49:24 +01002837 case SSL_SERVER_FINISHED:
2838 ret = ssl_parse_finished( ssl );
2839 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Paul Bakker1961b702013-01-25 14:49:24 +01002841 case SSL_FLUSH_BUFFERS:
2842 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2843 ssl->state = SSL_HANDSHAKE_WRAPUP;
2844 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Paul Bakker1961b702013-01-25 14:49:24 +01002846 case SSL_HANDSHAKE_WRAPUP:
2847 ssl_handshake_wrapup( ssl );
2848 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002849
Paul Bakker1961b702013-01-25 14:49:24 +01002850 default:
2851 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2852 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
2855 return( ret );
2856}
Paul Bakker9af723c2014-05-01 13:03:14 +02002857#endif /* POLARSSL_SSL_CLI_C */