blob: 77ae8b4da7a80ad3ef3e6a35960119b5ab1b4319 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#if defined(POLARSSL_PLATFORM_C)
38#include "polarssl/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020039#else
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdlib.h>
45#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#include <basetsd.h>
49typedef UINT32 uint32_t;
50#else
51#include <inttypes.h>
52#endif
53
54#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakker34617722014-06-13 17:20:13 +020058#if defined(POLARSSL_SSL_SESSION_TICKETS)
59/* Implementation that should never be optimized out by the compiler */
60static void polarssl_zeroize( void *v, size_t n ) {
61 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
62}
63#endif
64
Paul Bakker0be444a2013-08-27 21:55:01 +020065#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010066static void ssl_write_hostname_ext( ssl_context *ssl,
67 unsigned char *buf,
68 size_t *olen )
69{
70 unsigned char *p = buf;
71
72 *olen = 0;
73
Paul Bakker66d5d072014-06-17 16:39:18 +020074 if( ssl->hostname == NULL )
Paul Bakkerd3edc862013-03-20 16:07:17 +010075 return;
76
77 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
78 ssl->hostname ) );
79
80 /*
81 * struct {
82 * NameType name_type;
83 * select (name_type) {
84 * case host_name: HostName;
85 * } name;
86 * } ServerName;
87 *
88 * enum {
89 * host_name(0), (255)
90 * } NameType;
91 *
92 * opaque HostName<1..2^16-1>;
93 *
94 * struct {
95 * ServerName server_name_list<1..2^16-1>
96 * } ServerNameList;
97 */
98 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
99 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
100
101 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
102 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
103
104 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
105 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
106
107 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
108 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
109 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
110
111 memcpy( p, ssl->hostname, ssl->hostname_len );
112
113 *olen = ssl->hostname_len + 9;
114}
Paul Bakker0be444a2013-08-27 21:55:01 +0200115#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100117#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118static void ssl_write_renegotiation_ext( ssl_context *ssl,
119 unsigned char *buf,
120 size_t *olen )
121{
122 unsigned char *p = buf;
123
124 *olen = 0;
125
126 if( ssl->renegotiation != SSL_RENEGOTIATION )
127 return;
128
129 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
130
131 /*
132 * Secure renegotiation
133 */
134 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
135 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
136
137 *p++ = 0x00;
138 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
139 *p++ = ssl->verify_data_len & 0xFF;
140
141 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
142
143 *olen = 5 + ssl->verify_data_len;
144}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100145#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100146
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100147/*
148 * Only if we handle at least one key exchange that needs signatures.
149 */
150#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
151 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100152static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
153 unsigned char *buf,
154 size_t *olen )
155{
156 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100157 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200158#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
159 unsigned char *sig_alg_list = buf + 6;
160#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100161
162 *olen = 0;
163
164 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
165 return;
166
167 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
168
169 /*
170 * Prepare signature_algorithms extension (TLS 1.2)
171 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200172#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200173#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100174 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
175 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
176 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
177 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
178#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200179#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100180 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
181 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
182 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
183 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
184#endif
185#if defined(POLARSSL_SHA1_C)
186 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
187 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
188#endif
189#if defined(POLARSSL_MD5_C)
190 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
191 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
192#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200193#endif /* POLARSSL_RSA_C */
194#if defined(POLARSSL_ECDSA_C)
195#if defined(POLARSSL_SHA512_C)
196 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
197 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
198 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
199 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
200#endif
201#if defined(POLARSSL_SHA256_C)
202 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
203 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
204 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
205 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
206#endif
207#if defined(POLARSSL_SHA1_C)
208 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
209 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
210#endif
211#if defined(POLARSSL_MD5_C)
212 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
213 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
214#endif
215#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100216
217 /*
218 * enum {
219 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
220 * sha512(6), (255)
221 * } HashAlgorithm;
222 *
223 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
224 * SignatureAlgorithm;
225 *
226 * struct {
227 * HashAlgorithm hash;
228 * SignatureAlgorithm signature;
229 * } SignatureAndHashAlgorithm;
230 *
231 * SignatureAndHashAlgorithm
232 * supported_signature_algorithms<2..2^16-2>;
233 */
234 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
235 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
236
237 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
238 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
239
240 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
241 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
242
Paul Bakkerd3edc862013-03-20 16:07:17 +0100243 *olen = 6 + sig_alg_len;
244}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100245#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
246 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100247
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200248#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100249static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
250 unsigned char *buf,
251 size_t *olen )
252{
253 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100254 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100255 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100256 const ecp_curve_info *info;
257#if defined(POLARSSL_SSL_SET_CURVES)
258 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100259#else
260 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100261#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100262
263 *olen = 0;
264
265 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
266
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100267#if defined(POLARSSL_SSL_SET_CURVES)
268 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200269 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100270 info = ecp_curve_info_from_grp_id( *grp_id );
271#else
272 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
273 {
274#endif
275
276 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
277 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200278 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200279
280 if( elliptic_curve_len == 0 )
281 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100282
283 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
284 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
285
286 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
287 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
288
289 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
290 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
291
Paul Bakkerd3edc862013-03-20 16:07:17 +0100292 *olen = 6 + elliptic_curve_len;
293}
294
295static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
296 unsigned char *buf,
297 size_t *olen )
298{
299 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200300 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100301
302 *olen = 0;
303
304 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
305
306 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
307 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
308
309 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100310 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200311
312 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100313 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
314
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200315 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100316}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200317#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100318
Paul Bakker05decb22013-08-15 13:33:48 +0200319#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200320static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
321 unsigned char *buf,
322 size_t *olen )
323{
324 unsigned char *p = buf;
325
326 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
327 *olen = 0;
328 return;
329 }
330
331 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
332
333 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
334 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
335
336 *p++ = 0x00;
337 *p++ = 1;
338
339 *p++ = ssl->mfl_code;
340
341 *olen = 5;
342}
Paul Bakker05decb22013-08-15 13:33:48 +0200343#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200344
Paul Bakker1f2bc622013-08-15 13:45:55 +0200345#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200346static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
347 unsigned char *buf, size_t *olen )
348{
349 unsigned char *p = buf;
350
351 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
352 {
353 *olen = 0;
354 return;
355 }
356
357 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
358
359 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
360 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
361
362 *p++ = 0x00;
363 *p++ = 0x00;
364
365 *olen = 4;
366}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200367#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200368
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100369#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
370static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
371 unsigned char *buf, size_t *olen )
372{
373 unsigned char *p = buf;
374
375 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
376 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
377 {
378 *olen = 0;
379 return;
380 }
381
382 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
383 "extension" ) );
384
385 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
386 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
387
388 *p++ = 0x00;
389 *p++ = 0x00;
390
391 *olen = 4;
392}
393#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
394
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200395#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
396static void ssl_write_extended_ms_ext( ssl_context *ssl,
397 unsigned char *buf, size_t *olen )
398{
399 unsigned char *p = buf;
400
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200401 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
402 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200403 {
404 *olen = 0;
405 return;
406 }
407
408 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
409 "extension" ) );
410
411 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
412 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
413
414 *p++ = 0x00;
415 *p++ = 0x00;
416
417 *olen = 4;
418}
419#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
420
Paul Bakkera503a632013-08-14 13:48:06 +0200421#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200422static void ssl_write_session_ticket_ext( ssl_context *ssl,
423 unsigned char *buf, size_t *olen )
424{
425 unsigned char *p = buf;
426 size_t tlen = ssl->session_negotiate->ticket_len;
427
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200428 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
429 {
430 *olen = 0;
431 return;
432 }
433
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200434 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
435
436 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
437 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
438
439 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
440 *p++ = (unsigned char)( ( tlen ) & 0xFF );
441
442 *olen = 4;
443
444 if( ssl->session_negotiate->ticket == NULL ||
445 ssl->session_negotiate->ticket_len == 0 )
446 {
447 return;
448 }
449
450 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
451
452 memcpy( p, ssl->session_negotiate->ticket, tlen );
453
454 *olen += tlen;
455}
Paul Bakkera503a632013-08-14 13:48:06 +0200456#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200457
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200458#if defined(POLARSSL_SSL_ALPN)
459static void ssl_write_alpn_ext( ssl_context *ssl,
460 unsigned char *buf, size_t *olen )
461{
462 unsigned char *p = buf;
463 const char **cur;
464
465 if( ssl->alpn_list == NULL )
466 {
467 *olen = 0;
468 return;
469 }
470
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200471 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200472
473 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
474 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
475
476 /*
477 * opaque ProtocolName<1..2^8-1>;
478 *
479 * struct {
480 * ProtocolName protocol_name_list<2..2^16-1>
481 * } ProtocolNameList;
482 */
483
484 /* Skip writing extension and list length for now */
485 p += 4;
486
487 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
488 {
489 *p = (unsigned char)( strlen( *cur ) & 0xFF );
490 memcpy( p + 1, *cur, *p );
491 p += 1 + *p;
492 }
493
494 *olen = p - buf;
495
496 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
497 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
498 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
499
500 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
501 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
502 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
503}
504#endif /* POLARSSL_SSL_ALPN */
505
Paul Bakker5121ce52009-01-03 21:22:43 +0000506static int ssl_write_client_hello( ssl_context *ssl )
507{
Paul Bakker23986e52011-04-24 08:57:21 +0000508 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100509 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200511 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200512#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200514#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200515 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200516 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
519
Paul Bakkera9a028e2013-11-21 17:31:06 +0100520 if( ssl->f_rng == NULL )
521 {
522 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
523 return( POLARSSL_ERR_SSL_NO_RNG );
524 }
525
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100526#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000527 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100528#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000529 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000530 ssl->major_ver = ssl->min_major_ver;
531 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000532 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
Paul Bakker490ecc82011-10-06 13:04:09 +0000534 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
535 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200536 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
537 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000538 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540 /*
541 * 0 . 0 handshake type
542 * 1 . 3 handshake length
543 * 4 . 5 highest version supported
544 * 6 . 9 current UNIX time
545 * 10 . 37 random bytes
546 */
547 buf = ssl->out_msg;
548 p = buf + 4;
549
550 *p++ = (unsigned char) ssl->max_major_ver;
551 *p++ = (unsigned char) ssl->max_minor_ver;
552
553 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
554 buf[4], buf[5] ) );
555
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200556#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 t = time( NULL );
558 *p++ = (unsigned char)( t >> 24 );
559 *p++ = (unsigned char)( t >> 16 );
560 *p++ = (unsigned char)( t >> 8 );
561 *p++ = (unsigned char)( t );
562
563 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200564#else
565 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
566 return( ret );
567
568 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200569#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Paul Bakkera3d195c2011-11-27 21:07:34 +0000571 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
572 return( ret );
573
574 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Paul Bakker48916f92012-09-16 19:57:18 +0000576 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
579
580 /*
581 * 38 . 38 session id length
582 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000583 * 40+n . 41+n ciphersuitelist length
584 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000585 * .. . .. compression methods length
586 * .. . .. compression methods
587 * .. . .. extensions length
588 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 */
Paul Bakker48916f92012-09-16 19:57:18 +0000590 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100592 if( n < 16 || n > 32 ||
593#if defined(POLARSSL_SSL_RENEGOTIATION)
594 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
595#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000596 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200597 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200599 }
600
Paul Bakkera503a632013-08-14 13:48:06 +0200601#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200602 /*
603 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
604 * generate and include a Session ID in the TLS ClientHello."
605 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100606#if defined(POLARSSL_SSL_RENEGOTIATION)
607 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
608#endif
609 if( ssl->session_negotiate->ticket != NULL &&
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200610 ssl->session_negotiate->ticket_len != 0 )
611 {
612 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
613
614 if( ret != 0 )
615 return( ret );
616
617 ssl->session_negotiate->length = n = 32;
618 }
Paul Bakkera503a632013-08-14 13:48:06 +0200619#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
621 *p++ = (unsigned char) n;
622
623 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000624 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
627 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
628
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200629 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200630 n = 0;
631 q = p;
632
633 // Skip writing ciphersuite length for now
634 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
Paul Bakker48916f92012-09-16 19:57:18 +0000636 /*
637 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
638 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100639#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000640 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100641#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000642 {
643 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
644 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200645 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000646 }
647
Paul Bakker2fbefde2013-06-29 16:01:15 +0200648 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200650 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
651
652 if( ciphersuite_info == NULL )
653 continue;
654
655 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
656 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
657 continue;
658
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100659 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
660 ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
661 continue;
662
Paul Bakkere3166ce2011-01-27 17:40:50 +0000663 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200664 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
Paul Bakker2fbefde2013-06-29 16:01:15 +0200666 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200667 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
668 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 }
670
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200671 /* Some versions of OpenSSL don't handle it correctly if not at end */
672#if defined(POLARSSL_SSL_FALLBACK_SCSV)
673 if( ssl->fallback == SSL_IS_FALLBACK )
674 {
675 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
676 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
677 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
678 n++;
679 }
680#endif
681
Paul Bakker2fbefde2013-06-29 16:01:15 +0200682 *q++ = (unsigned char)( n >> 7 );
683 *q++ = (unsigned char)( n << 1 );
684
685 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
686
687
Paul Bakker2770fbd2012-07-03 13:30:23 +0000688#if defined(POLARSSL_ZLIB_SUPPORT)
689 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
690 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000691 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000692
693 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000694 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000695 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000696#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000698 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
700 *p++ = 1;
701 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200702#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Paul Bakkerd3edc862013-03-20 16:07:17 +0100704 // First write extensions, then the total length
705 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200706#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100707 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
708 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200709#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100711#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100712 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
713 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100714#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000715
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100716#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
717 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100718 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
719 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200720#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000721
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200722#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100723 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
724 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100725
Paul Bakkerd3edc862013-03-20 16:07:17 +0100726 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
727 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100728#endif
729
Paul Bakker05decb22013-08-15 13:33:48 +0200730#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200731 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
732 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200733#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200734
Paul Bakker1f2bc622013-08-15 13:45:55 +0200735#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200736 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
737 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200738#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200739
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100740#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
741 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
742 ext_len += olen;
743#endif
744
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200745#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
746 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
747 ext_len += olen;
748#endif
749
Paul Bakkera503a632013-08-14 13:48:06 +0200750#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200751 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
752 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200753#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200754
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200755#if defined(POLARSSL_SSL_ALPN)
756 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
757 ext_len += olen;
758#endif
759
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100760 /* olen unused if all extensions are disabled */
761 ((void) olen);
762
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000763 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
764 ext_len ) );
765
Paul Bakkera7036632014-04-30 10:15:38 +0200766 if( ext_len > 0 )
767 {
768 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
769 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
770 p += ext_len;
771 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100772
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 ssl->out_msglen = p - buf;
774 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
775 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
776
777 ssl->state++;
778
779 if( ( ret = ssl_write_record( ssl ) ) != 0 )
780 {
781 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
782 return( ret );
783 }
784
785 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
786
787 return( 0 );
788}
789
Paul Bakker48916f92012-09-16 19:57:18 +0000790static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200791 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000792 size_t len )
793{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000794 int ret;
795
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100796#if defined(POLARSSL_SSL_RENEGOTIATION)
797 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000798 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100799 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000800 if( len != 1 + ssl->verify_data_len * 2 ||
801 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100802 safer_memcmp( buf + 1,
803 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
804 safer_memcmp( buf + 1 + ssl->verify_data_len,
805 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000806 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100807 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000808
809 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
810 return( ret );
811
Paul Bakker48916f92012-09-16 19:57:18 +0000812 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
813 }
814 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100815 else
816#endif /* POLARSSL_SSL_RENEGOTIATION */
817 {
818 if( len != 1 || buf[0] != 0x00 )
819 {
820 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
821
822 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
823 return( ret );
824
825 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
826 }
827
828 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
829 }
Paul Bakker48916f92012-09-16 19:57:18 +0000830
831 return( 0 );
832}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200833
Paul Bakker05decb22013-08-15 13:33:48 +0200834#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200835static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200836 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200837 size_t len )
838{
839 /*
840 * server should use the extension only if we did,
841 * and if so the server's value should match ours (and len is always 1)
842 */
843 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
844 len != 1 ||
845 buf[0] != ssl->mfl_code )
846 {
847 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
848 }
849
850 return( 0 );
851}
Paul Bakker05decb22013-08-15 13:33:48 +0200852#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000853
Paul Bakker1f2bc622013-08-15 13:45:55 +0200854#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200855static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
856 const unsigned char *buf,
857 size_t len )
858{
859 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
860 len != 0 )
861 {
862 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
863 }
864
865 ((void) buf);
866
867 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
868
869 return( 0 );
870}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200871#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200872
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100873#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
874static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
875 const unsigned char *buf,
876 size_t len )
877{
878 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
879 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
880 len != 0 )
881 {
882 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
883 }
884
885 ((void) buf);
886
887 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
888
889 return( 0 );
890}
891#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
892
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200893#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
894static int ssl_parse_extended_ms_ext( ssl_context *ssl,
895 const unsigned char *buf,
896 size_t len )
897{
898 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200899 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200900 len != 0 )
901 {
902 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
903 }
904
905 ((void) buf);
906
907 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
908
909 return( 0 );
910}
911#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
912
Paul Bakkera503a632013-08-14 13:48:06 +0200913#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200914static int ssl_parse_session_ticket_ext( ssl_context *ssl,
915 const unsigned char *buf,
916 size_t len )
917{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200918 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
919 len != 0 )
920 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200921 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200922 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200923
924 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200925
926 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200927
928 return( 0 );
929}
Paul Bakkera503a632013-08-14 13:48:06 +0200930#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200931
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200932#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200933static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
934 const unsigned char *buf,
935 size_t len )
936{
937 size_t list_size;
938 const unsigned char *p;
939
940 list_size = buf[0];
941 if( list_size + 1 != len )
942 {
943 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
944 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
945 }
946
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200947 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200948 while( list_size > 0 )
949 {
950 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
951 p[0] == POLARSSL_ECP_PF_COMPRESSED )
952 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200953 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200954 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
955 return( 0 );
956 }
957
958 list_size--;
959 p++;
960 }
961
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200962 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
963 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200964}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200965#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200966
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200967#if defined(POLARSSL_SSL_ALPN)
968static int ssl_parse_alpn_ext( ssl_context *ssl,
969 const unsigned char *buf, size_t len )
970{
971 size_t list_len, name_len;
972 const char **p;
973
974 /* If we didn't send it, the server shouldn't send it */
975 if( ssl->alpn_list == NULL )
976 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
977
978 /*
979 * opaque ProtocolName<1..2^8-1>;
980 *
981 * struct {
982 * ProtocolName protocol_name_list<2..2^16-1>
983 * } ProtocolNameList;
984 *
985 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
986 */
987
988 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
989 if( len < 4 )
990 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
991
992 list_len = ( buf[0] << 8 ) | buf[1];
993 if( list_len != len - 2 )
994 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
995
996 name_len = buf[2];
997 if( name_len != list_len - 1 )
998 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
999
1000 /* Check that the server chosen protocol was in our list and save it */
1001 for( p = ssl->alpn_list; *p != NULL; p++ )
1002 {
1003 if( name_len == strlen( *p ) &&
1004 memcmp( buf + 3, *p, name_len ) == 0 )
1005 {
1006 ssl->alpn_chosen = *p;
1007 return( 0 );
1008 }
1009 }
1010
1011 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1012}
1013#endif /* POLARSSL_SSL_ALPN */
1014
Paul Bakker5121ce52009-01-03 21:22:43 +00001015static int ssl_parse_server_hello( ssl_context *ssl )
1016{
Paul Bakker2770fbd2012-07-03 13:30:23 +00001017 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +00001018 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001019 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001020 unsigned char *buf, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001021#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001022 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001023#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001024 int handshake_failure = 0;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001025 const ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001026#if defined(POLARSSL_DEBUG_C)
1027 uint32_t t;
1028#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
1030 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1031
1032 /*
1033 * 0 . 0 handshake type
1034 * 1 . 3 handshake length
1035 * 4 . 5 protocol version
1036 * 6 . 9 UNIX time()
1037 * 10 . 37 random bytes
1038 */
1039 buf = ssl->in_msg;
1040
1041 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1042 {
1043 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1044 return( ret );
1045 }
1046
1047 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1048 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001049#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001050 if( ssl->renegotiation == SSL_RENEGOTIATION )
1051 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001052 ssl->renego_records_seen++;
1053
1054 if( ssl->renego_max_records >= 0 &&
1055 ssl->renego_records_seen > ssl->renego_max_records )
1056 {
1057 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1058 "but not honored by server" ) );
1059 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1060 }
1061
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001062 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1063 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1064 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001065#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001066
Paul Bakker5121ce52009-01-03 21:22:43 +00001067 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001068 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 }
1070
1071 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1072 buf[4], buf[5] ) );
1073
1074 if( ssl->in_hslen < 42 ||
1075 buf[0] != SSL_HS_SERVER_HELLO ||
1076 buf[4] != SSL_MAJOR_VERSION_3 )
1077 {
1078 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001079 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 }
1081
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001082 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 {
1084 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001085 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 }
1087
1088 ssl->minor_ver = buf[5];
1089
Paul Bakker1d29fb52012-09-28 13:28:45 +00001090 if( ssl->minor_ver < ssl->min_minor_ver )
1091 {
1092 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001093 " [%d:%d] < [%d:%d]", ssl->major_ver,
1094 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001095
1096 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1097 SSL_ALERT_MSG_PROTOCOL_VERSION );
1098
1099 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1100 }
1101
Paul Bakker1504af52012-02-11 16:17:43 +00001102#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001103 t = ( (uint32_t) buf[6] << 24 )
1104 | ( (uint32_t) buf[7] << 16 )
1105 | ( (uint32_t) buf[8] << 8 )
1106 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001107 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001108#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
Paul Bakker48916f92012-09-16 19:57:18 +00001110 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
1112 n = buf[38];
1113
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1115
Paul Bakker48916f92012-09-16 19:57:18 +00001116 if( n > 32 )
1117 {
1118 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1119 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1120 }
1121
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 /*
1123 * 38 . 38 session id length
1124 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001125 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 * 41+n . 41+n chosen compression alg.
1127 * 42+n . 43+n extensions length
1128 * 44+n . 44+n+m extensions
1129 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001130 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 {
1132 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001133 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Paul Bakker48916f92012-09-16 19:57:18 +00001135 if( ( ext_len > 0 && ext_len < 4 ) ||
1136 ssl->in_hslen != 44 + n + ext_len )
1137 {
1138 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1139 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1140 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001141 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001142 else if( ssl->in_hslen == 42 + n )
1143 {
1144 ext_len = 0;
1145 }
1146 else
1147 {
1148 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1149 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1150 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
1152 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001153 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
Paul Bakker380da532012-04-18 16:10:25 +00001155 /*
1156 * Initialize update checksum functions
1157 */
Paul Bakker68884e32013-01-07 18:20:04 +01001158 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1159
1160 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1161 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001162 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001163 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1164 }
Paul Bakker380da532012-04-18 16:10:25 +00001165
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001166 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1167
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1169 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1170
1171 /*
1172 * Check if the session can be resumed
1173 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001174 if( ssl->handshake->resume == 0 || n == 0 ||
1175#if defined(POLARSSL_SSL_RENEGOTIATION)
1176 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1177#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001178 ssl->session_negotiate->ciphersuite != i ||
1179 ssl->session_negotiate->compression != comp ||
1180 ssl->session_negotiate->length != n ||
1181 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 {
1183 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001184 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001185#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001186 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001187#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001188 ssl->session_negotiate->ciphersuite = i;
1189 ssl->session_negotiate->compression = comp;
1190 ssl->session_negotiate->length = n;
1191 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 }
1193 else
1194 {
1195 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001196
1197 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1198 {
1199 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1200 return( ret );
1201 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 }
1203
1204 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001205 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001206
Paul Bakkere3166ce2011-01-27 17:40:50 +00001207 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1209
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001210 suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1211 if( suite_info == NULL ||
1212 ( ssl->arc4_disabled &&
1213 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) )
1214 {
1215 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1216 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1217 }
1218
1219
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 i = 0;
1221 while( 1 )
1222 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001223 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 {
1225 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001226 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 }
1228
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001229 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1230 ssl->session_negotiate->ciphersuite )
1231 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001233 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 }
1235
Paul Bakker2770fbd2012-07-03 13:30:23 +00001236 if( comp != SSL_COMPRESS_NULL
1237#if defined(POLARSSL_ZLIB_SUPPORT)
1238 && comp != SSL_COMPRESS_DEFLATE
1239#endif
1240 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 {
1242 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001243 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 }
Paul Bakker48916f92012-09-16 19:57:18 +00001245 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Paul Bakker48916f92012-09-16 19:57:18 +00001247 ext = buf + 44 + n;
1248
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001249 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1250
Paul Bakker48916f92012-09-16 19:57:18 +00001251 while( ext_len )
1252 {
1253 unsigned int ext_id = ( ( ext[0] << 8 )
1254 | ( ext[1] ) );
1255 unsigned int ext_size = ( ( ext[2] << 8 )
1256 | ( ext[3] ) );
1257
1258 if( ext_size + 4 > ext_len )
1259 {
1260 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1261 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1262 }
1263
1264 switch( ext_id )
1265 {
1266 case TLS_EXT_RENEGOTIATION_INFO:
1267 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001268#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001269 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001270#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001271
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001272 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1273 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001274 return( ret );
1275
1276 break;
1277
Paul Bakker05decb22013-08-15 13:33:48 +02001278#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001279 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1280 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1281
1282 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1283 ext + 4, ext_size ) ) != 0 )
1284 {
1285 return( ret );
1286 }
1287
1288 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001289#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001290
Paul Bakker1f2bc622013-08-15 13:45:55 +02001291#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001292 case TLS_EXT_TRUNCATED_HMAC:
1293 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1294
1295 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1296 ext + 4, ext_size ) ) != 0 )
1297 {
1298 return( ret );
1299 }
1300
1301 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001302#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001303
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001304#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1305 case TLS_EXT_ENCRYPT_THEN_MAC:
1306 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1307
1308 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1309 ext + 4, ext_size ) ) != 0 )
1310 {
1311 return( ret );
1312 }
1313
1314 break;
1315#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1316
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001317#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1318 case TLS_EXT_EXTENDED_MASTER_SECRET:
1319 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1320
1321 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1322 ext + 4, ext_size ) ) != 0 )
1323 {
1324 return( ret );
1325 }
1326
1327 break;
1328#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1329
Paul Bakkera503a632013-08-14 13:48:06 +02001330#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001331 case TLS_EXT_SESSION_TICKET:
1332 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1333
1334 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1335 ext + 4, ext_size ) ) != 0 )
1336 {
1337 return( ret );
1338 }
1339
1340 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001341#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001342
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001343#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001344 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1345 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1346
1347 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1348 ext + 4, ext_size ) ) != 0 )
1349 {
1350 return( ret );
1351 }
1352
1353 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001354#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001355
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001356#if defined(POLARSSL_SSL_ALPN)
1357 case TLS_EXT_ALPN:
1358 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1359
1360 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1361 return( ret );
1362
1363 break;
1364#endif /* POLARSSL_SSL_ALPN */
1365
Paul Bakker48916f92012-09-16 19:57:18 +00001366 default:
1367 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1368 ext_id ) );
1369 }
1370
1371 ext_len -= 4 + ext_size;
1372 ext += 4 + ext_size;
1373
1374 if( ext_len > 0 && ext_len < 4 )
1375 {
1376 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1377 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1378 }
1379 }
1380
1381 /*
1382 * Renegotiation security checks
1383 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001384 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1385 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001386 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001387 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1388 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001389 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001390#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001391 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1392 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1393 renegotiation_info_seen == 0 )
1394 {
1395 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1396 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001397 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001398 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1399 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1400 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001401 {
1402 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001403 handshake_failure = 1;
1404 }
1405 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1406 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1407 renegotiation_info_seen == 1 )
1408 {
1409 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1410 handshake_failure = 1;
1411 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001412#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001413
1414 if( handshake_failure == 1 )
1415 {
1416 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1417 return( ret );
1418
Paul Bakker48916f92012-09-16 19:57:18 +00001419 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1423
1424 return( 0 );
1425}
1426
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001427#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1428 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001429static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1430 unsigned char *end )
1431{
1432 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1433
Paul Bakker29e1f122013-04-16 13:07:56 +02001434 /*
1435 * Ephemeral DH parameters:
1436 *
1437 * struct {
1438 * opaque dh_p<1..2^16-1>;
1439 * opaque dh_g<1..2^16-1>;
1440 * opaque dh_Ys<1..2^16-1>;
1441 * } ServerDHParams;
1442 */
1443 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1444 {
1445 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1446 return( ret );
1447 }
1448
1449 if( ssl->handshake->dhm_ctx.len < 64 ||
1450 ssl->handshake->dhm_ctx.len > 512 )
1451 {
1452 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1453 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1454 }
1455
1456 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1457 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1458 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001459
1460 return( ret );
1461}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001462#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1463 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001464
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001465#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001466 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001467 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1468 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1469 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1470static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1471{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001472 const ecp_curve_info *curve_info;
1473
1474 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1475 if( curve_info == NULL )
1476 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001477 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1478 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001479 }
1480
1481 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001482
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001483#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1484 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1485#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001486 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1487 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001488#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001489 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001490
1491 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1492
1493 return( 0 );
1494}
Paul Bakker9af723c2014-05-01 13:03:14 +02001495#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1496 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1497 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1498 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1499 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001500
1501#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1502 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001503 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001504static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1505 unsigned char **p,
1506 unsigned char *end )
1507{
1508 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1509
Paul Bakker29e1f122013-04-16 13:07:56 +02001510 /*
1511 * Ephemeral ECDH parameters:
1512 *
1513 * struct {
1514 * ECParameters curve_params;
1515 * ECPoint public;
1516 * } ServerECDHParams;
1517 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001518 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1519 (const unsigned char **) p, end ) ) != 0 )
1520 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001521 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001522 return( ret );
1523 }
1524
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001525 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001526 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001527 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001528 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1529 }
1530
Paul Bakker29e1f122013-04-16 13:07:56 +02001531 return( ret );
1532}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001533#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001534 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1535 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001536
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001537#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001538static int ssl_parse_server_psk_hint( ssl_context *ssl,
1539 unsigned char **p,
1540 unsigned char *end )
1541{
1542 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001543 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001544 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001545
1546 /*
1547 * PSK parameters:
1548 *
1549 * opaque psk_identity_hint<0..2^16-1>;
1550 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001551 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001552 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001553
1554 if( (*p) + len > end )
1555 {
1556 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1557 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1558 }
1559
1560 // TODO: Retrieve PSK identity hint and callback to app
1561 //
1562 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001563 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001564
1565 return( ret );
1566}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001567#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001568
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001569#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1570 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1571/*
1572 * Generate a pre-master secret and encrypt it with the server's RSA key
1573 */
1574static int ssl_write_encrypted_pms( ssl_context *ssl,
1575 size_t offset, size_t *olen,
1576 size_t pms_offset )
1577{
1578 int ret;
1579 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1580 unsigned char *p = ssl->handshake->premaster + pms_offset;
1581
1582 /*
1583 * Generate (part of) the pre-master as
1584 * struct {
1585 * ProtocolVersion client_version;
1586 * opaque random[46];
1587 * } PreMasterSecret;
1588 */
1589 p[0] = (unsigned char) ssl->max_major_ver;
1590 p[1] = (unsigned char) ssl->max_minor_ver;
1591
1592 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1593 {
1594 SSL_DEBUG_RET( 1, "f_rng", ret );
1595 return( ret );
1596 }
1597
1598 ssl->handshake->pmslen = 48;
1599
1600 /*
1601 * Now write it out, encrypted
1602 */
1603 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1604 POLARSSL_PK_RSA ) )
1605 {
1606 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1607 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1608 }
1609
1610 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1611 p, ssl->handshake->pmslen,
1612 ssl->out_msg + offset + len_bytes, olen,
1613 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1614 ssl->f_rng, ssl->p_rng ) ) != 0 )
1615 {
1616 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1617 return( ret );
1618 }
1619
1620#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1621 defined(POLARSSL_SSL_PROTO_TLS1_2)
1622 if( len_bytes == 2 )
1623 {
1624 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1625 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1626 *olen += 2;
1627 }
1628#endif
1629
1630 return( 0 );
1631}
1632#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1633 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001634
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001635#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001636#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001637 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1638 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001639static int ssl_parse_signature_algorithm( ssl_context *ssl,
1640 unsigned char **p,
1641 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001642 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001643 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001644{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001645 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001646 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001647 *pk_alg = POLARSSL_PK_NONE;
1648
1649 /* Only in TLS 1.2 */
1650 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1651 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001652 return( 0 );
1653 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001654
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001655 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001656 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1657
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001658 /*
1659 * Get hash algorithm
1660 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001661 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001662 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001663 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1664 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001665 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1666 }
1667
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001668 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001669 * Get signature algorithm
1670 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001671 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001672 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001673 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1674 "SignatureAlgorithm %d", (*p)[1] ) );
1675 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001676 }
1677
1678 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1679 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1680 *p += 2;
1681
1682 return( 0 );
1683}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001684#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001685 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1686 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001687#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001688
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001689
1690#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1691 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1692static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1693{
1694 int ret;
1695 const ecp_keypair *peer_key;
1696
1697 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1698 POLARSSL_PK_ECKEY ) )
1699 {
1700 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1701 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1702 }
1703
1704 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1705
1706 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1707 POLARSSL_ECDH_THEIRS ) ) != 0 )
1708 {
1709 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1710 return( ret );
1711 }
1712
1713 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1714 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001715 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001716 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1717 }
1718
1719 return( ret );
1720}
1721#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1722 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1723
Paul Bakker41c83d32013-03-20 14:39:14 +01001724static int ssl_parse_server_key_exchange( ssl_context *ssl )
1725{
Paul Bakker23986e52011-04-24 08:57:21 +00001726 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001727 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001728 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001729#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001730 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1731 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001732 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001733 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001734 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001735 size_t hashlen;
1736 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001737#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
1739 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1740
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001741#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001742 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001743 {
1744 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1745 ssl->state++;
1746 return( 0 );
1747 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001748 ((void) p);
1749 ((void) end);
1750#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001752#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1753 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1754 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1755 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1756 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001757 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1758 {
1759 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1760 return( ret );
1761 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001762
1763 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1764 ssl->state++;
1765 return( 0 );
1766 }
1767 ((void) p);
1768 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001769#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1770 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001771
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1773 {
1774 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1775 return( ret );
1776 }
1777
1778 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1779 {
1780 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001781 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 }
1783
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001784 /*
1785 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1786 * doesn't use a psk_identity_hint
1787 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1789 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001790 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1791 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001792 {
1793 ssl->record_read = 1;
1794 goto exit;
1795 }
1796
1797 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1798 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 }
1800
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001801 p = ssl->in_msg + 4;
1802 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001803 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001804
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001805#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1806 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1807 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1808 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1809 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1810 {
1811 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1812 {
1813 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1814 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1815 }
1816 } /* FALLTROUGH */
1817#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1818
1819#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1820 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1821 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1822 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1823 ; /* nothing more to do */
1824 else
1825#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1826 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1827#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1828 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1829 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1830 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001832 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001833 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001834 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001835 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1836 }
1837 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001838 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001839#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1840 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001841#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001842 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001843 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1844 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001845 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001846 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001847 {
1848 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1849 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001850 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1851 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1852 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001853 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001854 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001855#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001856 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001857 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001858 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001859 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001860 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001861 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001862
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001863#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001864 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1865 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001866 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001867 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1868 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001869 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001870 params_len = p - ( ssl->in_msg + 4 );
1871
Paul Bakker29e1f122013-04-16 13:07:56 +02001872 /*
1873 * Handle the digitally-signed structure
1874 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001875#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1876 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001877 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001878 if( ssl_parse_signature_algorithm( ssl, &p, end,
1879 &md_alg, &pk_alg ) != 0 )
1880 {
1881 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1882 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1883 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001884
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001885 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001886 {
1887 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1888 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1889 }
1890 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001891 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001892#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001893#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1894 defined(POLARSSL_SSL_PROTO_TLS1_1)
1895 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001896 {
1897 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001898
Paul Bakker9659dae2013-08-28 16:21:34 +02001899 /* Default hash for ECDSA is SHA-1 */
1900 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1901 md_alg = POLARSSL_MD_SHA1;
1902 }
1903 else
1904#endif
1905 {
1906 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001907 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001908 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001909
1910 /*
1911 * Read signature
1912 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001913 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001914 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001915
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001916 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001917 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001918 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001919 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1920 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001921
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001922 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001923
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001924 /*
1925 * Compute the hash that has been signed
1926 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001927#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1928 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001929 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001930 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001931 md5_context md5;
1932 sha1_context sha1;
1933
Paul Bakker5b4af392014-06-26 12:09:34 +02001934 md5_init( &md5 );
1935 sha1_init( &sha1 );
1936
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001937 hashlen = 36;
1938
Paul Bakker29e1f122013-04-16 13:07:56 +02001939 /*
1940 * digitally-signed struct {
1941 * opaque md5_hash[16];
1942 * opaque sha_hash[20];
1943 * };
1944 *
1945 * md5_hash
1946 * MD5(ClientHello.random + ServerHello.random
1947 * + ServerParams);
1948 * sha_hash
1949 * SHA(ClientHello.random + ServerHello.random
1950 * + ServerParams);
1951 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001952 md5_starts( &md5 );
1953 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001954 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001955 md5_finish( &md5, hash );
1956
1957 sha1_starts( &sha1 );
1958 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001959 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001960 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001961
1962 md5_free( &md5 );
1963 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001964 }
1965 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001966#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1967 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001968#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1969 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001970 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001971 {
1972 md_context_t ctx;
1973
Paul Bakker84bbeb52014-07-01 14:53:22 +02001974 md_init( &ctx );
1975
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001976 /* Info from md_alg will be used instead */
1977 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001978
1979 /*
1980 * digitally-signed struct {
1981 * opaque client_random[32];
1982 * opaque server_random[32];
1983 * ServerDHParams params;
1984 * };
1985 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001986 if( ( ret = md_init_ctx( &ctx,
1987 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001988 {
1989 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1990 return( ret );
1991 }
1992
1993 md_starts( &ctx );
1994 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001995 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001996 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001997 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001998 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001999 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002000#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2001 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002002 {
Paul Bakker577e0062013-08-28 11:57:20 +02002003 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002004 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002005 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002006
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002007 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2008 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002009
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002010 /*
2011 * Verify signature
2012 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002013 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002014 {
2015 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2016 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2017 }
2018
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002019 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2020 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002021 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002022 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002023 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002024 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002026#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002027 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2028 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002029
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002030exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 ssl->state++;
2032
2033 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2034
2035 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002036}
2037
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002038#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2039 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2040 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2041 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2042static int ssl_parse_certificate_request( ssl_context *ssl )
2043{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002044 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2045
2046 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2047
2048 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2049 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2050 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2051 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2052 {
2053 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2054 ssl->state++;
2055 return( 0 );
2056 }
2057
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002058 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2059 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002060}
2061#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002062static int ssl_parse_certificate_request( ssl_context *ssl )
2063{
2064 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002065 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002066 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002067 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002068 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
2070 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2071
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002072 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2073 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2074 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2075 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2076 {
2077 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2078 ssl->state++;
2079 return( 0 );
2080 }
2081
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 /*
2083 * 0 . 0 handshake type
2084 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002085 * 4 . 4 cert type count
2086 * 5 .. m-1 cert types
2087 * m .. m+1 sig alg length (TLS 1.2 only)
2088 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 * n .. n+1 length of all DNs
2090 * n+2 .. n+3 length of DN 1
2091 * n+4 .. ... Distinguished Name #1
2092 * ... .. ... length of DN 2, etc.
2093 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002094 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002096 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2097 {
2098 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2099 return( ret );
2100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002102 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2103 {
2104 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2105 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2106 }
2107
2108 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 }
2110
2111 ssl->client_auth = 0;
2112 ssl->state++;
2113
2114 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2115 ssl->client_auth++;
2116
2117 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2118 ssl->client_auth ? "a" : "no" ) );
2119
Paul Bakker926af752012-11-23 13:38:07 +01002120 if( ssl->client_auth == 0 )
2121 goto exit;
2122
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002123 ssl->record_read = 0;
2124
Paul Bakker926af752012-11-23 13:38:07 +01002125 // TODO: handshake_failure alert for an anonymous server to request
2126 // client authentication
2127
2128 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002129
Paul Bakker926af752012-11-23 13:38:07 +01002130 // Retrieve cert types
2131 //
2132 cert_type_len = buf[4];
2133 n = cert_type_len;
2134
2135 if( ssl->in_hslen < 6 + n )
2136 {
2137 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2138 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2139 }
2140
Paul Bakker73d44312013-05-22 13:56:26 +02002141 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002142 while( cert_type_len > 0 )
2143 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002144#if defined(POLARSSL_RSA_C)
2145 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002146 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002147 {
2148 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2149 break;
2150 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002151 else
2152#endif
2153#if defined(POLARSSL_ECDSA_C)
2154 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002155 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002156 {
2157 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2158 break;
2159 }
2160 else
2161#endif
2162 {
2163 ; /* Unsupported cert type, ignore */
2164 }
Paul Bakker926af752012-11-23 13:38:07 +01002165
2166 cert_type_len--;
2167 p++;
2168 }
2169
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002170#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002171 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2172 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002173 /* Ignored, see comments about hash in write_certificate_verify */
2174 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002175 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2176 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002177
2178 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002179 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002180 n += sig_alg_len;
2181
2182 if( ssl->in_hslen < 6 + n )
2183 {
2184 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2185 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2186 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002187 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002188#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002189
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002190 /* Ignore certificate_authorities, we only have one cert anyway */
2191 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002192 dn_len = ( ( buf[5 + m + n] << 8 )
2193 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002194
2195 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002196 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002197 {
2198 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2199 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2200 }
2201
2202exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2204
2205 return( 0 );
2206}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002207#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2208 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2209 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2210 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
2212static int ssl_parse_server_hello_done( ssl_context *ssl )
2213{
2214 int ret;
2215
2216 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2217
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002218 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 {
2220 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2221 {
2222 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2223 return( ret );
2224 }
2225
2226 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2227 {
2228 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002229 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 }
2231 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002232 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002233
2234 if( ssl->in_hslen != 4 ||
2235 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2236 {
2237 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002238 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
2240
2241 ssl->state++;
2242
2243 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2244
2245 return( 0 );
2246}
2247
2248static int ssl_write_client_key_exchange( ssl_context *ssl )
2249{
Paul Bakker23986e52011-04-24 08:57:21 +00002250 int ret;
2251 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002252 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002253
2254 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2255
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002256#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002257 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 /*
2260 * DHM key exchange -- send G^X mod P
2261 */
Paul Bakker48916f92012-09-16 19:57:18 +00002262 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
2264 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2265 ssl->out_msg[5] = (unsigned char)( n );
2266 i = 6;
2267
Paul Bakker29b64762012-09-25 09:36:44 +00002268 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002269 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 &ssl->out_msg[i], n,
2271 ssl->f_rng, ssl->p_rng );
2272 if( ret != 0 )
2273 {
2274 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2275 return( ret );
2276 }
2277
Paul Bakker48916f92012-09-16 19:57:18 +00002278 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2279 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002281 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
Paul Bakker48916f92012-09-16 19:57:18 +00002283 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2284 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002285 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002286 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 {
2288 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2289 return( ret );
2290 }
2291
Paul Bakker48916f92012-09-16 19:57:18 +00002292 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 }
2294 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002295#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002296#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002297 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2298 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2299 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002300 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002301 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2302 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2303 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002304 {
2305 /*
2306 * ECDH key exchange -- send client public value
2307 */
2308 i = 4;
2309
2310 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2311 &n,
2312 &ssl->out_msg[i], 1000,
2313 ssl->f_rng, ssl->p_rng );
2314 if( ret != 0 )
2315 {
2316 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2317 return( ret );
2318 }
2319
2320 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2321
2322 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2323 &ssl->handshake->pmslen,
2324 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002325 POLARSSL_MPI_MAX_SIZE,
2326 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002327 {
2328 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2329 return( ret );
2330 }
2331
2332 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2333 }
2334 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002335#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002336 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2337 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2338 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002339#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002340 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002341 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002342 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2343 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002344 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002345 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002346 * opaque psk_identity<0..2^16-1>;
2347 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002348 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002349 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2350
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002351 i = 4;
2352 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002353 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2354 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002355
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002356 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2357 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002358
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002359#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002360 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002361 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002362 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002363 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002364 else
2365#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002366#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2367 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2368 {
2369 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2370 return( ret );
2371 }
2372 else
2373#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002374#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002375 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002376 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002377 /*
2378 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2379 */
2380 n = ssl->handshake->dhm_ctx.len;
2381 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2382 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002383
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002384 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002385 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002386 &ssl->out_msg[i], n,
2387 ssl->f_rng, ssl->p_rng );
2388 if( ret != 0 )
2389 {
2390 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2391 return( ret );
2392 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002393 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002394 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002395#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002396#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002397 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002398 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002399 /*
2400 * ClientECDiffieHellmanPublic public;
2401 */
2402 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2403 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2404 ssl->f_rng, ssl->p_rng );
2405 if( ret != 0 )
2406 {
2407 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2408 return( ret );
2409 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002410
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002411 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2412 }
2413 else
2414#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2415 {
2416 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002417 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002418 }
2419
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002420 if( ( ret = ssl_psk_derive_premaster( ssl,
2421 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002423 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002424 return( ret );
2425 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002426 }
2427 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002428#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002429#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002430 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002432 i = 4;
2433 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002434 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 }
Paul Bakkered27a042013-04-18 22:46:23 +02002436 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002437#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002438 {
2439 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002440 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002441 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 ssl->out_msglen = i + n;
2445 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2446 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2447
2448 ssl->state++;
2449
2450 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2451 {
2452 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2453 return( ret );
2454 }
2455
2456 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2457
2458 return( 0 );
2459}
2460
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002461#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2462 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002463 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2464 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002465static int ssl_write_certificate_verify( ssl_context *ssl )
2466{
Paul Bakkered27a042013-04-18 22:46:23 +02002467 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002468 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
2470 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2471
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002472 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2473 {
2474 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2475 return( ret );
2476 }
2477
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002478 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002479 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002480 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002481 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002482 {
2483 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2484 ssl->state++;
2485 return( 0 );
2486 }
2487
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002488 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2489 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490}
2491#else
2492static int ssl_write_certificate_verify( ssl_context *ssl )
2493{
2494 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2495 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2496 size_t n = 0, offset = 0;
2497 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002498 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002499 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002500 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501
2502 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2503
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002504 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2505 {
2506 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2507 return( ret );
2508 }
2509
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002510 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002511 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002512 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002513 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2514 {
2515 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2516 ssl->state++;
2517 return( 0 );
2518 }
2519
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002520 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 {
2522 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2523 ssl->state++;
2524 return( 0 );
2525 }
2526
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002527 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002529 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2530 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 }
2532
2533 /*
2534 * Make an RSA signature of the handshake digests
2535 */
Paul Bakker48916f92012-09-16 19:57:18 +00002536 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002538#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2539 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002540 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002541 {
Paul Bakker926af752012-11-23 13:38:07 +01002542 /*
2543 * digitally-signed struct {
2544 * opaque md5_hash[16];
2545 * opaque sha_hash[20];
2546 * };
2547 *
2548 * md5_hash
2549 * MD5(handshake_messages);
2550 *
2551 * sha_hash
2552 * SHA(handshake_messages);
2553 */
2554 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002555 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002556
2557 /*
2558 * For ECDSA, default hash is SHA-1 only
2559 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002560 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002561 {
2562 hash_start += 16;
2563 hashlen -= 16;
2564 md_alg = POLARSSL_MD_SHA1;
2565 }
Paul Bakker926af752012-11-23 13:38:07 +01002566 }
2567 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002568#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2569 POLARSSL_SSL_PROTO_TLS1_1 */
2570#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2571 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002572 {
2573 /*
2574 * digitally-signed struct {
2575 * opaque handshake_messages[handshake_messages_length];
2576 * };
2577 *
2578 * Taking shortcut here. We assume that the server always allows the
2579 * PRF Hash function and has sent it in the allowed signature
2580 * algorithms list received in the Certificate Request message.
2581 *
2582 * Until we encounter a server that does not, we will take this
2583 * shortcut.
2584 *
2585 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2586 * in order to satisfy 'weird' needs from the server side.
2587 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002588 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2589 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002590 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002591 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002592 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002593 }
2594 else
2595 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002596 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002597 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002598 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002599 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002600
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002601 /* Info from md_alg will be used instead */
2602 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002603 offset = 2;
2604 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002605 else
2606#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002607 {
2608 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002609 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002610 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002611
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002612 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002613 ssl->out_msg + 6 + offset, &n,
2614 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002615 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002616 SSL_DEBUG_RET( 1, "pk_sign", ret );
2617 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002618 }
Paul Bakker926af752012-11-23 13:38:07 +01002619
Paul Bakker1ef83d62012-04-11 12:09:53 +00002620 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2621 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
Paul Bakker1ef83d62012-04-11 12:09:53 +00002623 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2625 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2626
2627 ssl->state++;
2628
2629 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2630 {
2631 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2632 return( ret );
2633 }
2634
2635 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2636
Paul Bakkered27a042013-04-18 22:46:23 +02002637 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002639#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2640 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2641 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
Paul Bakkera503a632013-08-14 13:48:06 +02002643#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002644static int ssl_parse_new_session_ticket( ssl_context *ssl )
2645{
2646 int ret;
2647 uint32_t lifetime;
2648 size_t ticket_len;
2649 unsigned char *ticket;
2650
2651 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2652
2653 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2654 {
2655 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2656 return( ret );
2657 }
2658
2659 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2660 {
2661 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2662 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2663 }
2664
2665 /*
2666 * struct {
2667 * uint32 ticket_lifetime_hint;
2668 * opaque ticket<0..2^16-1>;
2669 * } NewSessionTicket;
2670 *
2671 * 0 . 0 handshake message type
2672 * 1 . 3 handshake message length
2673 * 4 . 7 ticket_lifetime_hint
2674 * 8 . 9 ticket_len (n)
2675 * 10 . 9+n ticket content
2676 */
2677 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2678 ssl->in_hslen < 10 )
2679 {
2680 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2681 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2682 }
2683
2684 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2685 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2686
2687 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2688
2689 if( ticket_len + 10 != ssl->in_hslen )
2690 {
2691 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2692 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2693 }
2694
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002695 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2696
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002697 /* We're not waiting for a NewSessionTicket message any more */
2698 ssl->handshake->new_session_ticket = 0;
2699
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002700 /*
2701 * Zero-length ticket means the server changed his mind and doesn't want
2702 * to send a ticket after all, so just forget it
2703 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002704 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002705 return( 0 );
2706
Paul Bakker34617722014-06-13 17:20:13 +02002707 polarssl_zeroize( ssl->session_negotiate->ticket,
2708 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002709 polarssl_free( ssl->session_negotiate->ticket );
2710 ssl->session_negotiate->ticket = NULL;
2711 ssl->session_negotiate->ticket_len = 0;
2712
2713 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2714 {
2715 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2716 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2717 }
2718
2719 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2720
2721 ssl->session_negotiate->ticket = ticket;
2722 ssl->session_negotiate->ticket_len = ticket_len;
2723 ssl->session_negotiate->ticket_lifetime = lifetime;
2724
2725 /*
2726 * RFC 5077 section 3.4:
2727 * "If the client receives a session ticket from the server, then it
2728 * discards any Session ID that was sent in the ServerHello."
2729 */
2730 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2731 ssl->session_negotiate->length = 0;
2732
2733 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2734
2735 return( 0 );
2736}
Paul Bakkera503a632013-08-14 13:48:06 +02002737#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002738
Paul Bakker5121ce52009-01-03 21:22:43 +00002739/*
Paul Bakker1961b702013-01-25 14:49:24 +01002740 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 */
Paul Bakker1961b702013-01-25 14:49:24 +01002742int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002743{
2744 int ret = 0;
2745
Paul Bakker1961b702013-01-25 14:49:24 +01002746 if( ssl->state == SSL_HANDSHAKE_OVER )
2747 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Paul Bakker1961b702013-01-25 14:49:24 +01002749 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2750
2751 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2752 return( ret );
2753
2754 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 {
Paul Bakker1961b702013-01-25 14:49:24 +01002756 case SSL_HELLO_REQUEST:
2757 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 break;
2759
Paul Bakker1961b702013-01-25 14:49:24 +01002760 /*
2761 * ==> ClientHello
2762 */
2763 case SSL_CLIENT_HELLO:
2764 ret = ssl_write_client_hello( ssl );
2765 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Paul Bakker1961b702013-01-25 14:49:24 +01002767 /*
2768 * <== ServerHello
2769 * Certificate
2770 * ( ServerKeyExchange )
2771 * ( CertificateRequest )
2772 * ServerHelloDone
2773 */
2774 case SSL_SERVER_HELLO:
2775 ret = ssl_parse_server_hello( ssl );
2776 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
Paul Bakker1961b702013-01-25 14:49:24 +01002778 case SSL_SERVER_CERTIFICATE:
2779 ret = ssl_parse_certificate( ssl );
2780 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Paul Bakker1961b702013-01-25 14:49:24 +01002782 case SSL_SERVER_KEY_EXCHANGE:
2783 ret = ssl_parse_server_key_exchange( ssl );
2784 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
Paul Bakker1961b702013-01-25 14:49:24 +01002786 case SSL_CERTIFICATE_REQUEST:
2787 ret = ssl_parse_certificate_request( ssl );
2788 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002789
Paul Bakker1961b702013-01-25 14:49:24 +01002790 case SSL_SERVER_HELLO_DONE:
2791 ret = ssl_parse_server_hello_done( ssl );
2792 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Paul Bakker1961b702013-01-25 14:49:24 +01002794 /*
2795 * ==> ( Certificate/Alert )
2796 * ClientKeyExchange
2797 * ( CertificateVerify )
2798 * ChangeCipherSpec
2799 * Finished
2800 */
2801 case SSL_CLIENT_CERTIFICATE:
2802 ret = ssl_write_certificate( ssl );
2803 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
Paul Bakker1961b702013-01-25 14:49:24 +01002805 case SSL_CLIENT_KEY_EXCHANGE:
2806 ret = ssl_write_client_key_exchange( ssl );
2807 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
Paul Bakker1961b702013-01-25 14:49:24 +01002809 case SSL_CERTIFICATE_VERIFY:
2810 ret = ssl_write_certificate_verify( ssl );
2811 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Paul Bakker1961b702013-01-25 14:49:24 +01002813 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2814 ret = ssl_write_change_cipher_spec( ssl );
2815 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
Paul Bakker1961b702013-01-25 14:49:24 +01002817 case SSL_CLIENT_FINISHED:
2818 ret = ssl_write_finished( ssl );
2819 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
Paul Bakker1961b702013-01-25 14:49:24 +01002821 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002822 * <== ( NewSessionTicket )
2823 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002824 * Finished
2825 */
2826 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002827#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002828 if( ssl->handshake->new_session_ticket != 0 )
2829 ret = ssl_parse_new_session_ticket( ssl );
2830 else
Paul Bakkera503a632013-08-14 13:48:06 +02002831#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002832 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002833 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Paul Bakker1961b702013-01-25 14:49:24 +01002835 case SSL_SERVER_FINISHED:
2836 ret = ssl_parse_finished( ssl );
2837 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
Paul Bakker1961b702013-01-25 14:49:24 +01002839 case SSL_FLUSH_BUFFERS:
2840 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2841 ssl->state = SSL_HANDSHAKE_WRAPUP;
2842 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Paul Bakker1961b702013-01-25 14:49:24 +01002844 case SSL_HANDSHAKE_WRAPUP:
2845 ssl_handshake_wrapup( ssl );
2846 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002847
Paul Bakker1961b702013-01-25 14:49:24 +01002848 default:
2849 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2850 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2851 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
2853 return( ret );
2854}
Paul Bakker9af723c2014-05-01 13:03:14 +02002855#endif /* POLARSSL_SSL_CLI_C */