blob: b5dae23a6b8525a694c7dac4f1c2d74feb82fab8 [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
117static void ssl_write_renegotiation_ext( ssl_context *ssl,
118 unsigned char *buf,
119 size_t *olen )
120{
121 unsigned char *p = buf;
122
123 *olen = 0;
124
125 if( ssl->renegotiation != SSL_RENEGOTIATION )
126 return;
127
128 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
129
130 /*
131 * Secure renegotiation
132 */
133 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
134 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
135
136 *p++ = 0x00;
137 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
138 *p++ = ssl->verify_data_len & 0xFF;
139
140 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
141
142 *olen = 5 + ssl->verify_data_len;
143}
144
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200145#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100146static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
147 unsigned char *buf,
148 size_t *olen )
149{
150 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100151 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200152#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
153 unsigned char *sig_alg_list = buf + 6;
154#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100155
156 *olen = 0;
157
158 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
159 return;
160
161 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
162
163 /*
164 * Prepare signature_algorithms extension (TLS 1.2)
165 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200166#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200167#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100168 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
169 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
170 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
171 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
172#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200173#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100174 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
175 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
176 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
177 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
178#endif
179#if defined(POLARSSL_SHA1_C)
180 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
181 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
182#endif
183#if defined(POLARSSL_MD5_C)
184 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
185 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
186#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200187#endif /* POLARSSL_RSA_C */
188#if defined(POLARSSL_ECDSA_C)
189#if defined(POLARSSL_SHA512_C)
190 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
191 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
192 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
193 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
194#endif
195#if defined(POLARSSL_SHA256_C)
196 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
197 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
198 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
199 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
200#endif
201#if defined(POLARSSL_SHA1_C)
202 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
203 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
204#endif
205#if defined(POLARSSL_MD5_C)
206 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
207 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
208#endif
209#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100210
211 /*
212 * enum {
213 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
214 * sha512(6), (255)
215 * } HashAlgorithm;
216 *
217 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
218 * SignatureAlgorithm;
219 *
220 * struct {
221 * HashAlgorithm hash;
222 * SignatureAlgorithm signature;
223 * } SignatureAndHashAlgorithm;
224 *
225 * SignatureAndHashAlgorithm
226 * supported_signature_algorithms<2..2^16-2>;
227 */
228 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
229 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
230
231 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
232 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
233
234 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
235 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
236
Paul Bakkerd3edc862013-03-20 16:07:17 +0100237 *olen = 6 + sig_alg_len;
238}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200239#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100240
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200241#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100242static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
243 unsigned char *buf,
244 size_t *olen )
245{
246 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100247 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100248 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100249 const ecp_curve_info *info;
250#if defined(POLARSSL_SSL_SET_CURVES)
251 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100252#else
253 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100254#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100255
256 *olen = 0;
257
258 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
259
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100260#if defined(POLARSSL_SSL_SET_CURVES)
261 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200262 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100263 info = ecp_curve_info_from_grp_id( *grp_id );
264#else
265 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
266 {
267#endif
268
269 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
270 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200271 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200272
273 if( elliptic_curve_len == 0 )
274 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100275
276 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
277 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
278
279 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
280 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
281
282 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
283 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
284
Paul Bakkerd3edc862013-03-20 16:07:17 +0100285 *olen = 6 + elliptic_curve_len;
286}
287
288static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
289 unsigned char *buf,
290 size_t *olen )
291{
292 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200293 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100294
295 *olen = 0;
296
297 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
298
299 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
300 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
301
302 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100303 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200304
305 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100306 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
307
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200308 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100309}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200310#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100311
Paul Bakker05decb22013-08-15 13:33:48 +0200312#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200313static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
314 unsigned char *buf,
315 size_t *olen )
316{
317 unsigned char *p = buf;
318
319 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
320 *olen = 0;
321 return;
322 }
323
324 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
325
326 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
327 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
328
329 *p++ = 0x00;
330 *p++ = 1;
331
332 *p++ = ssl->mfl_code;
333
334 *olen = 5;
335}
Paul Bakker05decb22013-08-15 13:33:48 +0200336#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200337
Paul Bakker1f2bc622013-08-15 13:45:55 +0200338#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200339static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
340 unsigned char *buf, size_t *olen )
341{
342 unsigned char *p = buf;
343
344 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
345 {
346 *olen = 0;
347 return;
348 }
349
350 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
351
352 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
353 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
354
355 *p++ = 0x00;
356 *p++ = 0x00;
357
358 *olen = 4;
359}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200360#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200361
Paul Bakkera503a632013-08-14 13:48:06 +0200362#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200363static void ssl_write_session_ticket_ext( ssl_context *ssl,
364 unsigned char *buf, size_t *olen )
365{
366 unsigned char *p = buf;
367 size_t tlen = ssl->session_negotiate->ticket_len;
368
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200369 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
370 {
371 *olen = 0;
372 return;
373 }
374
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200375 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
376
377 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
378 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
379
380 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
381 *p++ = (unsigned char)( ( tlen ) & 0xFF );
382
383 *olen = 4;
384
385 if( ssl->session_negotiate->ticket == NULL ||
386 ssl->session_negotiate->ticket_len == 0 )
387 {
388 return;
389 }
390
391 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
392
393 memcpy( p, ssl->session_negotiate->ticket, tlen );
394
395 *olen += tlen;
396}
Paul Bakkera503a632013-08-14 13:48:06 +0200397#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200398
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200399#if defined(POLARSSL_SSL_ALPN)
400static void ssl_write_alpn_ext( ssl_context *ssl,
401 unsigned char *buf, size_t *olen )
402{
403 unsigned char *p = buf;
404 const char **cur;
405
406 if( ssl->alpn_list == NULL )
407 {
408 *olen = 0;
409 return;
410 }
411
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200412 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200413
414 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
415 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
416
417 /*
418 * opaque ProtocolName<1..2^8-1>;
419 *
420 * struct {
421 * ProtocolName protocol_name_list<2..2^16-1>
422 * } ProtocolNameList;
423 */
424
425 /* Skip writing extension and list length for now */
426 p += 4;
427
428 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
429 {
430 *p = (unsigned char)( strlen( *cur ) & 0xFF );
431 memcpy( p + 1, *cur, *p );
432 p += 1 + *p;
433 }
434
435 *olen = p - buf;
436
437 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
438 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
439 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
440
441 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
442 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
443 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
444}
445#endif /* POLARSSL_SSL_ALPN */
446
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200447/*
448 * Generate random bytes for ClientHello
449 */
450static int ssl_generate_random( ssl_context *ssl )
451{
452 int ret;
453 unsigned char *p = ssl->handshake->randbytes;
454#if defined(POLARSSL_HAVE_TIME)
455 time_t t;
456#endif
457
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200458 /*
459 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
460 */
461#if defined(POLARSSL_SSL_PROTO_DTLS)
462 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
463 ssl->handshake->verify_cookie != NULL )
464 {
465 return( 0 );
466 }
467#endif
468
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200469#if defined(POLARSSL_HAVE_TIME)
470 t = time( NULL );
471 *p++ = (unsigned char)( t >> 24 );
472 *p++ = (unsigned char)( t >> 16 );
473 *p++ = (unsigned char)( t >> 8 );
474 *p++ = (unsigned char)( t );
475
476 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
477#else
478 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
479 return( ret );
480
481 p += 4;
482#endif /* POLARSSL_HAVE_TIME */
483
484 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
485 return( ret );
486
487 return( 0 );
488}
489
Paul Bakker5121ce52009-01-03 21:22:43 +0000490static int ssl_write_client_hello( ssl_context *ssl )
491{
Paul Bakker23986e52011-04-24 08:57:21 +0000492 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100493 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200495 unsigned char *p, *q;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200496 unsigned char offer_compress;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200497 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200498 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
501
Paul Bakkera9a028e2013-11-21 17:31:06 +0100502 if( ssl->f_rng == NULL )
503 {
504 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
505 return( POLARSSL_ERR_SSL_NO_RNG );
506 }
507
Paul Bakker48916f92012-09-16 19:57:18 +0000508 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
509 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000510 ssl->major_ver = ssl->min_major_ver;
511 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000512 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
Paul Bakker490ecc82011-10-06 13:04:09 +0000514 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
515 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200516 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
517 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000518 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520 /*
521 * 0 . 0 handshake type
522 * 1 . 3 handshake length
523 * 4 . 5 highest version supported
524 * 6 . 9 current UNIX time
525 * 10 . 37 random bytes
526 */
527 buf = ssl->out_msg;
528 p = buf + 4;
529
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100530 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
531 ssl->transport, p );
532 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
534 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
535 buf[4], buf[5] ) );
536
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200537 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
538 {
539 SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200540 return( ret );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200541 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200542
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200543 memcpy( p, ssl->handshake->randbytes, 32 );
544 SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
545 p += 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 /*
548 * 38 . 38 session id length
549 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100550 * 39+n . 39+n DTLS only: cookie length (1 byte)
551 * 40+n . .. DTSL only: cookie
552 * .. . .. ciphersuitelist length (2 bytes)
553 * .. . .. ciphersuitelist
554 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000555 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100556 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000557 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 */
Paul Bakker48916f92012-09-16 19:57:18 +0000559 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
Paul Bakker0a597072012-09-25 21:55:46 +0000561 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
562 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200563 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200565 }
566
Paul Bakkera503a632013-08-14 13:48:06 +0200567#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200568 /*
569 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
570 * generate and include a Session ID in the TLS ClientHello."
571 */
572 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
573 ssl->session_negotiate->ticket != NULL &&
574 ssl->session_negotiate->ticket_len != 0 )
575 {
576 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
577
578 if( ret != 0 )
579 return( ret );
580
581 ssl->session_negotiate->length = n = 32;
582 }
Paul Bakkera503a632013-08-14 13:48:06 +0200583#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585 *p++ = (unsigned char) n;
586
587 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000588 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
591 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
592
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100593 /*
594 * DTLS cookie
595 */
596#if defined(POLARSSL_SSL_PROTO_DTLS)
597 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
598 {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200599 if( ssl->handshake->verify_cookie == NULL )
600 {
601 SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
602 *p++ = 0;
603 }
604 else
605 {
606 SSL_DEBUG_BUF( 3, "client hello, cookie",
607 ssl->handshake->verify_cookie,
608 ssl->handshake->verify_cookie_len );
609
610 *p++ = ssl->handshake->verify_cookie_len;
611 memcpy( p, ssl->handshake->verify_cookie,
612 ssl->handshake->verify_cookie_len );
613 p += ssl->handshake->verify_cookie_len;
614 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100615 }
616#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
Paul Bakker48916f92012-09-16 19:57:18 +0000618 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100619 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000620 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100621 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
622
623 /* Skip writing ciphersuite length for now */
624 n = 0;
625 q = p;
626 p += 2;
627
628 /* Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
Paul Bakker48916f92012-09-16 19:57:18 +0000629 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
630 {
631 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
632 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200633 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000634 }
635
Paul Bakker2fbefde2013-06-29 16:01:15 +0200636 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200638 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
639
640 if( ciphersuite_info == NULL )
641 continue;
642
643 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
644 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
645 continue;
646
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100647#if defined(POLARSSL_SSL_PROTO_DTLS)
648 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
649 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
650 continue;
651#endif
652
Paul Bakkere3166ce2011-01-27 17:40:50 +0000653 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200654 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Paul Bakker2fbefde2013-06-29 16:01:15 +0200656 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200657 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
658 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 }
660
Paul Bakker2fbefde2013-06-29 16:01:15 +0200661 *q++ = (unsigned char)( n >> 7 );
662 *q++ = (unsigned char)( n << 1 );
663
664 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
665
Paul Bakker2770fbd2012-07-03 13:30:23 +0000666#if defined(POLARSSL_ZLIB_SUPPORT)
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200667 offer_compress = 1;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000668#else
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200669 offer_compress = 0;
670#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200672 /*
673 * We don't support compression with DTLS right now: is many records come
674 * in the same datagram, uncompressing one could overwrite the next one.
675 * We don't want to add complexity for handling that case unless there is
676 * an actual need for it.
677 */
678#if defined(POLARSSL_SSL_PROTO_DTLS)
679 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
680 offer_compress = 0;
681#endif
682
683 if( offer_compress )
684 {
685 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
686 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
687 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
688
689 *p++ = 2;
690 *p++ = SSL_COMPRESS_DEFLATE;
691 *p++ = SSL_COMPRESS_NULL;
692 }
693 else
694 {
695 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
696 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
697 SSL_COMPRESS_NULL ) );
698
699 *p++ = 1;
700 *p++ = SSL_COMPRESS_NULL;
701 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
Paul Bakkerd3edc862013-03-20 16:07:17 +0100703 // First write extensions, then the total length
704 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200705#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100706 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
707 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200708#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000709
Paul Bakkerd3edc862013-03-20 16:07:17 +0100710 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
711 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000712
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200713#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100714 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
715 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200716#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000717
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200718#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100719 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
720 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100721
Paul Bakkerd3edc862013-03-20 16:07:17 +0100722 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
723 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100724#endif
725
Paul Bakker05decb22013-08-15 13:33:48 +0200726#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200727 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
728 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200729#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200730
Paul Bakker1f2bc622013-08-15 13:45:55 +0200731#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200732 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
733 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200734#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200735
Paul Bakkera503a632013-08-14 13:48:06 +0200736#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200737 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
738 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200739#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200740
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200741#if defined(POLARSSL_SSL_ALPN)
742 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
743 ext_len += olen;
744#endif
745
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000746 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
747 ext_len ) );
748
Paul Bakkera7036632014-04-30 10:15:38 +0200749 if( ext_len > 0 )
750 {
751 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
752 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
753 p += ext_len;
754 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100755
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 ssl->out_msglen = p - buf;
757 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
758 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
759
760 ssl->state++;
761
762 if( ( ret = ssl_write_record( ssl ) ) != 0 )
763 {
764 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
765 return( ret );
766 }
767
768 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
769
770 return( 0 );
771}
772
Paul Bakker48916f92012-09-16 19:57:18 +0000773static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200774 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000775 size_t len )
776{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000777 int ret;
778
Paul Bakker48916f92012-09-16 19:57:18 +0000779 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
780 {
781 if( len != 1 || buf[0] != 0x0 )
782 {
783 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000784
785 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
786 return( ret );
787
Paul Bakker48916f92012-09-16 19:57:18 +0000788 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
789 }
790
791 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
792 }
793 else
794 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100795 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000796 if( len != 1 + ssl->verify_data_len * 2 ||
797 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100798 safer_memcmp( buf + 1,
799 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
800 safer_memcmp( buf + 1 + ssl->verify_data_len,
801 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000802 {
803 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000804
805 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
806 return( ret );
807
Paul Bakker48916f92012-09-16 19:57:18 +0000808 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
809 }
810 }
811
812 return( 0 );
813}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200814
Paul Bakker05decb22013-08-15 13:33:48 +0200815#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200816static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200817 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200818 size_t len )
819{
820 /*
821 * server should use the extension only if we did,
822 * and if so the server's value should match ours (and len is always 1)
823 */
824 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
825 len != 1 ||
826 buf[0] != ssl->mfl_code )
827 {
828 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
829 }
830
831 return( 0 );
832}
Paul Bakker05decb22013-08-15 13:33:48 +0200833#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000834
Paul Bakker1f2bc622013-08-15 13:45:55 +0200835#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200836static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
837 const unsigned char *buf,
838 size_t len )
839{
840 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
841 len != 0 )
842 {
843 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
844 }
845
846 ((void) buf);
847
848 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
849
850 return( 0 );
851}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200852#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200853
Paul Bakkera503a632013-08-14 13:48:06 +0200854#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200855static int ssl_parse_session_ticket_ext( ssl_context *ssl,
856 const unsigned char *buf,
857 size_t len )
858{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200859 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
860 len != 0 )
861 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200862 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200863 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200864
865 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200866
867 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200868
869 return( 0 );
870}
Paul Bakkera503a632013-08-14 13:48:06 +0200871#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200872
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200873#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200874static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
875 const unsigned char *buf,
876 size_t len )
877{
878 size_t list_size;
879 const unsigned char *p;
880
881 list_size = buf[0];
882 if( list_size + 1 != len )
883 {
884 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
885 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
886 }
887
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200888 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200889 while( list_size > 0 )
890 {
891 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
892 p[0] == POLARSSL_ECP_PF_COMPRESSED )
893 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200894 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200895 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
896 return( 0 );
897 }
898
899 list_size--;
900 p++;
901 }
902
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200903 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
904 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200905}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200906#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200907
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200908#if defined(POLARSSL_SSL_ALPN)
909static int ssl_parse_alpn_ext( ssl_context *ssl,
910 const unsigned char *buf, size_t len )
911{
912 size_t list_len, name_len;
913 const char **p;
914
915 /* If we didn't send it, the server shouldn't send it */
916 if( ssl->alpn_list == NULL )
917 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
918
919 /*
920 * opaque ProtocolName<1..2^8-1>;
921 *
922 * struct {
923 * ProtocolName protocol_name_list<2..2^16-1>
924 * } ProtocolNameList;
925 *
926 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
927 */
928
929 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
930 if( len < 4 )
931 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
932
933 list_len = ( buf[0] << 8 ) | buf[1];
934 if( list_len != len - 2 )
935 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
936
937 name_len = buf[2];
938 if( name_len != list_len - 1 )
939 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
940
941 /* Check that the server chosen protocol was in our list and save it */
942 for( p = ssl->alpn_list; *p != NULL; p++ )
943 {
944 if( name_len == strlen( *p ) &&
945 memcmp( buf + 3, *p, name_len ) == 0 )
946 {
947 ssl->alpn_chosen = *p;
948 return( 0 );
949 }
950 }
951
952 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
953}
954#endif /* POLARSSL_SSL_ALPN */
955
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200956/*
957 * Parse HelloVerifyRequest. Only called after verifying the HS type.
958 */
959#if defined(POLARSSL_SSL_PROTO_DTLS)
960static int ssl_parse_hello_verify_request( ssl_context *ssl )
961{
Manuel Pégourié-Gonnard069eb792014-09-10 20:08:29 +0200962 const unsigned char *p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200963 int major_ver, minor_ver;
964 unsigned char cookie_len;
965
966 SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
967
968 /*
969 * struct {
970 * ProtocolVersion server_version;
971 * opaque cookie<0..2^8-1>;
972 * } HelloVerifyRequest;
973 */
974 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
975 ssl_read_version( &major_ver, &minor_ver, ssl->transport, p );
976 p += 2;
977
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +0200978 /*
979 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
980 * even is lower than our min version.
981 */
982 if( major_ver < SSL_MAJOR_VERSION_3 ||
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200983 minor_ver < SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +0200984 major_ver > ssl->max_major_ver ||
985 minor_ver > ssl->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200986 {
987 SSL_DEBUG_MSG( 1, ( "bad server version" ) );
988
989 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
990 SSL_ALERT_MSG_PROTOCOL_VERSION );
991
992 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
993 }
994
995 cookie_len = *p++;
996 SSL_DEBUG_BUF( 3, "cookie", (unsigned char *) p, cookie_len );
997
998 polarssl_free( ssl->handshake->verify_cookie );
999
1000 ssl->handshake->verify_cookie = polarssl_malloc( cookie_len );
1001 if( ssl->handshake->verify_cookie == NULL )
1002 {
1003 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", cookie_len ) );
1004 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1005 }
1006
1007 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
1008 ssl->handshake->verify_cookie_len = cookie_len;
1009
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001010 /* Start over at ClientHello */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001011 ssl->state = SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001012 ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001013
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001014 ssl_recv_flight_completed( ssl );
1015
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001016 SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
1017
1018 return( 0 );
1019}
1020#endif /* POLARSSL_SSL_PROTO_DTLS */
1021
Paul Bakker5121ce52009-01-03 21:22:43 +00001022static int ssl_parse_server_hello( ssl_context *ssl )
1023{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001024 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001025 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001026 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001027 unsigned char *buf, *ext;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001028 unsigned char comp, accept_comp;
Paul Bakker48916f92012-09-16 19:57:18 +00001029 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001030 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001031#if defined(POLARSSL_DEBUG_C)
1032 uint32_t t;
1033#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
1035 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1036
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 buf = ssl->in_msg;
1038
1039 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1040 {
1041 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1042 return( ret );
1043 }
1044
1045 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1046 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001047 if( ssl->renegotiation == SSL_RENEGOTIATION )
1048 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001049 ssl->renego_records_seen++;
1050
1051 if( ssl->renego_max_records >= 0 &&
1052 ssl->renego_records_seen > ssl->renego_max_records )
1053 {
1054 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1055 "but not honored by server" ) );
1056 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1057 }
1058
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001059 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1060 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1061 }
1062
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001064 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 }
1066
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001067#if defined(POLARSSL_SSL_PROTO_DTLS)
1068 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1069 {
1070 if( buf[0] == SSL_HS_HELLO_VERIFY_REQUEST )
1071 {
1072 SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1073 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1074 return( ssl_parse_hello_verify_request( ssl ) );
1075 }
1076 else
1077 {
1078 /* We made it through the verification process */
1079 polarssl_free( ssl->handshake->verify_cookie );
1080 ssl->handshake->verify_cookie = NULL;
1081 ssl->handshake->verify_cookie_len = 0;
1082 }
1083 }
1084#endif /* POLARSSL_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001086 if( ssl->in_hslen < 38 + ssl_hs_hdr_len( ssl ) ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001087 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 {
1089 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001090 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 }
1092
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001093 /*
1094 * 0 . 1 server_version
1095 * 2 . 33 random (maybe including 4 bytes of Unix time)
1096 * 34 . 34 session_id length = n
1097 * 35 . 34+n session_id
1098 * 35+n . 36+n cipher_suite
1099 * 37+n . 37+n compression_method
1100 *
1101 * 38+n . 39+n extensions length (optional)
1102 * 40+n . .. extensions
1103 */
1104 buf += ssl_hs_hdr_len( ssl );
1105
1106 SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001107 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001108 ssl->transport, buf + 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001110 if( ssl->major_ver < ssl->min_major_ver ||
1111 ssl->minor_ver < ssl->min_minor_ver ||
1112 ssl->major_ver > ssl->max_major_ver ||
1113 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001114 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001115 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
1116 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1117 ssl->min_major_ver, ssl->min_minor_ver,
1118 ssl->major_ver, ssl->minor_ver,
1119 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001120
1121 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1122 SSL_ALERT_MSG_PROTOCOL_VERSION );
1123
1124 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1125 }
1126
Paul Bakker1504af52012-02-11 16:17:43 +00001127#if defined(POLARSSL_DEBUG_C)
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001128 t = ( (uint32_t) buf[2] << 24 )
1129 | ( (uint32_t) buf[3] << 16 )
1130 | ( (uint32_t) buf[4] << 8 )
1131 | ( (uint32_t) buf[5] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001132 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001133#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001135 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001137 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001139 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
Paul Bakker48916f92012-09-16 19:57:18 +00001141 if( n > 32 )
1142 {
1143 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1144 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1145 }
1146
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001147 if( ssl->in_hslen > 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001149 ext_len = ( ( buf[38 + n] << 8 )
1150 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
Paul Bakker48916f92012-09-16 19:57:18 +00001152 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001153 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001154 {
1155 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1156 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1157 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 }
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001159 else if( ssl->in_hslen == 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001160 {
1161 ext_len = 0;
1162 }
1163 else
1164 {
1165 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1166 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1167 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001169 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001170 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001171
1172 /*
1173 * Read and check compression
1174 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001175 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001177#if defined(POLARSSL_ZLIB_SUPPORT)
1178 accept_comp = 1;
1179#else
1180 accept_comp = 0;
1181#endif
1182
1183 /* See comments in ssl_write_client_hello() */
1184#if defined(POLARSSL_SSL_PROTO_DTLS)
1185 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1186 accept_comp = 0;
1187#endif
1188
1189 if( ( accept_comp == 0 && comp != SSL_COMPRESS_NULL ) ||
1190 ( comp != SSL_COMPRESS_NULL && comp != SSL_COMPRESS_DEFLATE ) )
1191 {
1192 SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
1193 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1194 }
1195
Paul Bakker380da532012-04-18 16:10:25 +00001196 /*
1197 * Initialize update checksum functions
1198 */
Paul Bakker68884e32013-01-07 18:20:04 +01001199 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1200
1201 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1202 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001203 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001204 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1205 }
Paul Bakker380da532012-04-18 16:10:25 +00001206
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001207 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1208
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001210 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
1212 /*
1213 * Check if the session can be resumed
1214 */
Paul Bakker0a597072012-09-25 21:55:46 +00001215 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1216 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +00001217 ssl->session_negotiate->ciphersuite != i ||
1218 ssl->session_negotiate->compression != comp ||
1219 ssl->session_negotiate->length != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001220 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 {
1222 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001223 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001224#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001225 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001226#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001227 ssl->session_negotiate->ciphersuite = i;
1228 ssl->session_negotiate->compression = comp;
1229 ssl->session_negotiate->length = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001230 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 }
1232 else
1233 {
1234 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001235
1236 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1237 {
1238 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1239 return( ret );
1240 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 }
1242
1243 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001244 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Paul Bakkere3166ce2011-01-27 17:40:50 +00001246 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001247 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
1249 i = 0;
1250 while( 1 )
1251 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001252 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 {
1254 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001255 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 }
1257
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001258 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1259 ssl->session_negotiate->ciphersuite )
1260 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001262 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 }
1264
Paul Bakker2770fbd2012-07-03 13:30:23 +00001265 if( comp != SSL_COMPRESS_NULL
1266#if defined(POLARSSL_ZLIB_SUPPORT)
1267 && comp != SSL_COMPRESS_DEFLATE
1268#endif
1269 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 {
1271 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001272 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001273 }
Paul Bakker48916f92012-09-16 19:57:18 +00001274 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001275
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001276 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001277
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001278 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1279
Paul Bakker48916f92012-09-16 19:57:18 +00001280 while( ext_len )
1281 {
1282 unsigned int ext_id = ( ( ext[0] << 8 )
1283 | ( ext[1] ) );
1284 unsigned int ext_size = ( ( ext[2] << 8 )
1285 | ( ext[3] ) );
1286
1287 if( ext_size + 4 > ext_len )
1288 {
1289 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1290 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1291 }
1292
1293 switch( ext_id )
1294 {
1295 case TLS_EXT_RENEGOTIATION_INFO:
1296 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1297 renegotiation_info_seen = 1;
1298
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001299 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1300 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001301 return( ret );
1302
1303 break;
1304
Paul Bakker05decb22013-08-15 13:33:48 +02001305#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001306 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1307 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1308
1309 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1310 ext + 4, ext_size ) ) != 0 )
1311 {
1312 return( ret );
1313 }
1314
1315 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001316#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001317
Paul Bakker1f2bc622013-08-15 13:45:55 +02001318#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001319 case TLS_EXT_TRUNCATED_HMAC:
1320 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1321
1322 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1323 ext + 4, ext_size ) ) != 0 )
1324 {
1325 return( ret );
1326 }
1327
1328 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001329#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001330
Paul Bakkera503a632013-08-14 13:48:06 +02001331#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001332 case TLS_EXT_SESSION_TICKET:
1333 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1334
1335 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1336 ext + 4, ext_size ) ) != 0 )
1337 {
1338 return( ret );
1339 }
1340
1341 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001342#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001343
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001344#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001345 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1346 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1347
1348 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1349 ext + 4, ext_size ) ) != 0 )
1350 {
1351 return( ret );
1352 }
1353
1354 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001355#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001356
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001357#if defined(POLARSSL_SSL_ALPN)
1358 case TLS_EXT_ALPN:
1359 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1360
1361 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1362 return( ret );
1363
1364 break;
1365#endif /* POLARSSL_SSL_ALPN */
1366
Paul Bakker48916f92012-09-16 19:57:18 +00001367 default:
1368 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1369 ext_id ) );
1370 }
1371
1372 ext_len -= 4 + ext_size;
1373 ext += 4 + ext_size;
1374
1375 if( ext_len > 0 && ext_len < 4 )
1376 {
1377 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1378 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1379 }
1380 }
1381
1382 /*
1383 * Renegotiation security checks
1384 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001385 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1386 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001387 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001388 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1389 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001390 }
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 }
1412
1413 if( handshake_failure == 1 )
1414 {
1415 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1416 return( ret );
1417
Paul Bakker48916f92012-09-16 19:57:18 +00001418 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
1421 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1422
1423 return( 0 );
1424}
1425
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001426#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1427 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001428static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1429 unsigned char *end )
1430{
1431 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1432
Paul Bakker29e1f122013-04-16 13:07:56 +02001433 /*
1434 * Ephemeral DH parameters:
1435 *
1436 * struct {
1437 * opaque dh_p<1..2^16-1>;
1438 * opaque dh_g<1..2^16-1>;
1439 * opaque dh_Ys<1..2^16-1>;
1440 * } ServerDHParams;
1441 */
1442 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1443 {
1444 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1445 return( ret );
1446 }
1447
1448 if( ssl->handshake->dhm_ctx.len < 64 ||
1449 ssl->handshake->dhm_ctx.len > 512 )
1450 {
1451 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1452 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1453 }
1454
1455 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1456 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1457 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001458
1459 return( ret );
1460}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001461#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1462 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001463
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001464#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001465 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001466 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1467 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1468 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1469static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1470{
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001471 const ecp_curve_info *curve_info;
1472
1473 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1474 if( curve_info == NULL )
1475 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001476 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1477 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001478 }
1479
1480 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001481
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001482#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1483 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1484#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001485 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1486 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001487#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001488 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001489
1490 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1491
1492 return( 0 );
1493}
Paul Bakker9af723c2014-05-01 13:03:14 +02001494#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1495 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1496 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1497 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1498 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001499
1500#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1501 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001502 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001503static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1504 unsigned char **p,
1505 unsigned char *end )
1506{
1507 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1508
Paul Bakker29e1f122013-04-16 13:07:56 +02001509 /*
1510 * Ephemeral ECDH parameters:
1511 *
1512 * struct {
1513 * ECParameters curve_params;
1514 * ECPoint public;
1515 * } ServerECDHParams;
1516 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001517 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1518 (const unsigned char **) p, end ) ) != 0 )
1519 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001520 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001521 return( ret );
1522 }
1523
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001524 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001525 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001526 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001527 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1528 }
1529
Paul Bakker29e1f122013-04-16 13:07:56 +02001530 return( ret );
1531}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001532#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001533 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1534 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001535
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001536#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001537static int ssl_parse_server_psk_hint( ssl_context *ssl,
1538 unsigned char **p,
1539 unsigned char *end )
1540{
1541 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001542 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001543 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001544
1545 /*
1546 * PSK parameters:
1547 *
1548 * opaque psk_identity_hint<0..2^16-1>;
1549 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001550 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001551 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001552
1553 if( (*p) + len > end )
1554 {
1555 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1556 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1557 }
1558
1559 // TODO: Retrieve PSK identity hint and callback to app
1560 //
1561 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001562 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001563
1564 return( ret );
1565}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001566#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001567
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001568#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1569 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1570/*
1571 * Generate a pre-master secret and encrypt it with the server's RSA key
1572 */
1573static int ssl_write_encrypted_pms( ssl_context *ssl,
1574 size_t offset, size_t *olen,
1575 size_t pms_offset )
1576{
1577 int ret;
1578 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1579 unsigned char *p = ssl->handshake->premaster + pms_offset;
1580
1581 /*
1582 * Generate (part of) the pre-master as
1583 * struct {
1584 * ProtocolVersion client_version;
1585 * opaque random[46];
1586 * } PreMasterSecret;
1587 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001588 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1589 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001590
1591 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1592 {
1593 SSL_DEBUG_RET( 1, "f_rng", ret );
1594 return( ret );
1595 }
1596
1597 ssl->handshake->pmslen = 48;
1598
1599 /*
1600 * Now write it out, encrypted
1601 */
1602 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1603 POLARSSL_PK_RSA ) )
1604 {
1605 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1606 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1607 }
1608
1609 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1610 p, ssl->handshake->pmslen,
1611 ssl->out_msg + offset + len_bytes, olen,
1612 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1613 ssl->f_rng, ssl->p_rng ) ) != 0 )
1614 {
1615 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1616 return( ret );
1617 }
1618
1619#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1620 defined(POLARSSL_SSL_PROTO_TLS1_2)
1621 if( len_bytes == 2 )
1622 {
1623 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1624 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1625 *olen += 2;
1626 }
1627#endif
1628
1629 return( 0 );
1630}
1631#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1632 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001633
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001634#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001635#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001636 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1637 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001638static int ssl_parse_signature_algorithm( ssl_context *ssl,
1639 unsigned char **p,
1640 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001641 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001642 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001643{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001644 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001645 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001646 *pk_alg = POLARSSL_PK_NONE;
1647
1648 /* Only in TLS 1.2 */
1649 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1650 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001651 return( 0 );
1652 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001653
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001654 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001655 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1656
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001657 /*
1658 * Get hash algorithm
1659 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001660 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001661 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001662 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1663 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001664 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1665 }
1666
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001667 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001668 * Get signature algorithm
1669 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001670 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001671 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001672 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1673 "SignatureAlgorithm %d", (*p)[1] ) );
1674 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001675 }
1676
1677 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1678 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1679 *p += 2;
1680
1681 return( 0 );
1682}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001683#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001684 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1685 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001686#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001687
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001688
1689#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1690 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1691static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1692{
1693 int ret;
1694 const ecp_keypair *peer_key;
1695
1696 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1697 POLARSSL_PK_ECKEY ) )
1698 {
1699 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1700 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1701 }
1702
1703 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1704
1705 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1706 POLARSSL_ECDH_THEIRS ) ) != 0 )
1707 {
1708 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1709 return( ret );
1710 }
1711
1712 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1713 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001714 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001715 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1716 }
1717
1718 return( ret );
1719}
1720#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1721 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1722
Paul Bakker41c83d32013-03-20 14:39:14 +01001723static int ssl_parse_server_key_exchange( ssl_context *ssl )
1724{
Paul Bakker23986e52011-04-24 08:57:21 +00001725 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001726 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001727 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001728
1729 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1730
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001731#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001732 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001733 {
1734 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1735 ssl->state++;
1736 return( 0 );
1737 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001738 ((void) p);
1739 ((void) end);
1740#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001742#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1743 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1744 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1745 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1746 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001747 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1748 {
1749 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1750 return( ret );
1751 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001752
1753 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1754 ssl->state++;
1755 return( 0 );
1756 }
1757 ((void) p);
1758 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001759#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1760 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001761
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1763 {
1764 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1765 return( ret );
1766 }
1767
1768 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1769 {
1770 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001771 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 }
1773
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001774 /*
1775 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1776 * doesn't use a psk_identity_hint
1777 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1779 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001780 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1781 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001782 {
1783 ssl->record_read = 1;
1784 goto exit;
1785 }
1786
1787 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1788 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 }
1790
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001791 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001792 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001793 SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001794
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001795#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1796 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1797 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1798 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1799 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1800 {
1801 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1802 {
1803 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1804 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1805 }
1806 } /* FALLTROUGH */
1807#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1808
1809#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1810 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1811 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1812 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1813 ; /* nothing more to do */
1814 else
1815#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1816 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1817#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1818 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1819 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1820 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001821 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001822 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001823 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001824 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001825 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1826 }
1827 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001828 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001829#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1830 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001831#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001832 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001833 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1834 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001835 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001836 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001837 {
1838 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1839 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001840 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1841 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1842 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001843 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001844 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001845#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001846 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001847 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001848 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001849 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001850 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001851 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001852
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001853#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001854 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1855 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001856 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001857 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1858 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001859 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00001860 size_t sig_len, hashlen;
1861 unsigned char hash[64];
1862 md_type_t md_alg = POLARSSL_MD_NONE;
1863 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001864 unsigned char *params = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00001865 size_t params_len = p - params;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001866
Paul Bakker29e1f122013-04-16 13:07:56 +02001867 /*
1868 * Handle the digitally-signed structure
1869 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001870#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1871 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001872 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001873 if( ssl_parse_signature_algorithm( ssl, &p, end,
1874 &md_alg, &pk_alg ) != 0 )
1875 {
1876 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1877 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1878 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001879
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001880 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001881 {
1882 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1883 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1884 }
1885 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001886 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001887#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001888#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1889 defined(POLARSSL_SSL_PROTO_TLS1_1)
1890 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001891 {
1892 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001893
Paul Bakker9659dae2013-08-28 16:21:34 +02001894 /* Default hash for ECDSA is SHA-1 */
1895 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1896 md_alg = POLARSSL_MD_SHA1;
1897 }
1898 else
1899#endif
1900 {
1901 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001902 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001903 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001904
1905 /*
1906 * Read signature
1907 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001908 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001909 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001910
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001911 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001912 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001913 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001914 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1915 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001916
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001917 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001918
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001919 /*
1920 * Compute the hash that has been signed
1921 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001922#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1923 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001924 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001925 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001926 md5_context md5;
1927 sha1_context sha1;
1928
Paul Bakker5b4af392014-06-26 12:09:34 +02001929 md5_init( &md5 );
1930 sha1_init( &sha1 );
1931
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001932 hashlen = 36;
1933
Paul Bakker29e1f122013-04-16 13:07:56 +02001934 /*
1935 * digitally-signed struct {
1936 * opaque md5_hash[16];
1937 * opaque sha_hash[20];
1938 * };
1939 *
1940 * md5_hash
1941 * MD5(ClientHello.random + ServerHello.random
1942 * + ServerParams);
1943 * sha_hash
1944 * SHA(ClientHello.random + ServerHello.random
1945 * + ServerParams);
1946 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001947 md5_starts( &md5 );
1948 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001949 md5_update( &md5, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001950 md5_finish( &md5, hash );
1951
1952 sha1_starts( &sha1 );
1953 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001954 sha1_update( &sha1, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001955 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001956
1957 md5_free( &md5 );
1958 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001959 }
1960 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001961#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1962 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001963#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1964 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001965 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001966 {
1967 md_context_t ctx;
1968
Paul Bakker84bbeb52014-07-01 14:53:22 +02001969 md_init( &ctx );
1970
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001971 /* Info from md_alg will be used instead */
1972 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001973
1974 /*
1975 * digitally-signed struct {
1976 * opaque client_random[32];
1977 * opaque server_random[32];
1978 * ServerDHParams params;
1979 * };
1980 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001981 if( ( ret = md_init_ctx( &ctx,
1982 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001983 {
1984 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1985 return( ret );
1986 }
1987
1988 md_starts( &ctx );
1989 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001990 md_update( &ctx, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001991 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001992 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001993 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001994 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001995#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1996 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001997 {
Paul Bakker577e0062013-08-28 11:57:20 +02001998 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001999 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002000 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002001
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002002 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2003 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002004
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002005 /*
2006 * Verify signature
2007 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002008 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002009 {
2010 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2011 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2012 }
2013
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002014 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2015 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002016 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002017 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002018 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002019 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002021#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002022 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2023 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002024
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002025exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 ssl->state++;
2027
2028 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2029
2030 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031}
2032
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002033#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2034 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2035 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2036 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2037static int ssl_parse_certificate_request( ssl_context *ssl )
2038{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002039 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2040
2041 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2042
2043 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2044 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2045 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2046 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2047 {
2048 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2049 ssl->state++;
2050 return( 0 );
2051 }
2052
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002053 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2054 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002055}
2056#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002057static int ssl_parse_certificate_request( ssl_context *ssl )
2058{
2059 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002060 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002061 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002062 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002063 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002064
2065 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2066
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002067 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2068 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2069 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2070 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2071 {
2072 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2073 ssl->state++;
2074 return( 0 );
2075 }
2076
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002077 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002079 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2080 {
2081 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2082 return( ret );
2083 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002084
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002085 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2086 {
2087 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2088 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2089 }
2090
2091 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093
2094 ssl->client_auth = 0;
2095 ssl->state++;
2096
2097 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2098 ssl->client_auth++;
2099
2100 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2101 ssl->client_auth ? "a" : "no" ) );
2102
Paul Bakker926af752012-11-23 13:38:07 +01002103 if( ssl->client_auth == 0 )
2104 goto exit;
2105
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002106 ssl->record_read = 0;
2107
Paul Bakker926af752012-11-23 13:38:07 +01002108 // TODO: handshake_failure alert for an anonymous server to request
2109 // client authentication
2110
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002111 /*
2112 * struct {
2113 * ClientCertificateType certificate_types<1..2^8-1>;
2114 * SignatureAndHashAlgorithm
2115 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2116 * DistinguishedName certificate_authorities<0..2^16-1>;
2117 * } CertificateRequest;
2118 */
Paul Bakker926af752012-11-23 13:38:07 +01002119 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002120
Paul Bakker926af752012-11-23 13:38:07 +01002121 // Retrieve cert types
2122 //
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002123 cert_type_len = buf[ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002124 n = cert_type_len;
2125
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002126 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002127 {
2128 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2129 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2130 }
2131
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002132 p = buf + ssl_hs_hdr_len( ssl ) + 1;
Paul Bakker926af752012-11-23 13:38:07 +01002133 while( cert_type_len > 0 )
2134 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002135#if defined(POLARSSL_RSA_C)
2136 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002137 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002138 {
2139 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2140 break;
2141 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002142 else
2143#endif
2144#if defined(POLARSSL_ECDSA_C)
2145 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002146 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002147 {
2148 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2149 break;
2150 }
2151 else
2152#endif
2153 {
2154 ; /* Unsupported cert type, ignore */
2155 }
Paul Bakker926af752012-11-23 13:38:07 +01002156
2157 cert_type_len--;
2158 p++;
2159 }
2160
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002161#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002162 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2163 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002164 /* Ignored, see comments about hash in write_certificate_verify */
2165 // TODO: should check the signature part against our pk_key though
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002166 size_t sig_alg_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2167 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002168
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002169 p = buf + ssl_hs_hdr_len( ssl ) + 3 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002170 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002171 n += sig_alg_len;
2172
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002173 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002174 {
2175 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2176 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2177 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002178 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002179#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002180
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002181 /* Ignore certificate_authorities, we only have one cert anyway */
2182 // TODO: should not send cert if no CA matches
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002183 dn_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + m + n] << 8 )
2184 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002185
2186 n += dn_len;
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002187 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 3 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002188 {
2189 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2190 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2191 }
2192
2193exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2195
2196 return( 0 );
2197}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002198#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2199 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2200 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2201 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
2203static int ssl_parse_server_hello_done( ssl_context *ssl )
2204{
2205 int ret;
2206
2207 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2208
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002209 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 {
2211 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2212 {
2213 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2214 return( ret );
2215 }
2216
2217 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2218 {
2219 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002220 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 }
2222 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002223 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002224
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002225 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) ||
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2227 {
2228 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002229 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 }
2231
2232 ssl->state++;
2233
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002234#if defined(POLARSSL_SSL_PROTO_DTLS)
2235 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2236 ssl_recv_flight_completed( ssl );
2237#endif
2238
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2240
2241 return( 0 );
2242}
2243
2244static int ssl_write_client_key_exchange( ssl_context *ssl )
2245{
Paul Bakker23986e52011-04-24 08:57:21 +00002246 int ret;
2247 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002248 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
2250 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2251
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002252#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002253 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 /*
2256 * DHM key exchange -- send G^X mod P
2257 */
Paul Bakker48916f92012-09-16 19:57:18 +00002258 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002259
2260 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2261 ssl->out_msg[5] = (unsigned char)( n );
2262 i = 6;
2263
Paul Bakker29b64762012-09-25 09:36:44 +00002264 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002265 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 &ssl->out_msg[i], n,
2267 ssl->f_rng, ssl->p_rng );
2268 if( ret != 0 )
2269 {
2270 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2271 return( ret );
2272 }
2273
Paul Bakker48916f92012-09-16 19:57:18 +00002274 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2275 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002277 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
Paul Bakker48916f92012-09-16 19:57:18 +00002279 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2280 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002281 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002282 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 {
2284 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2285 return( ret );
2286 }
2287
Paul Bakker48916f92012-09-16 19:57:18 +00002288 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 }
2290 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002291#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002292#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002293 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2294 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2295 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002296 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002297 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2298 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2299 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002300 {
2301 /*
2302 * ECDH key exchange -- send client public value
2303 */
2304 i = 4;
2305
2306 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2307 &n,
2308 &ssl->out_msg[i], 1000,
2309 ssl->f_rng, ssl->p_rng );
2310 if( ret != 0 )
2311 {
2312 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2313 return( ret );
2314 }
2315
2316 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2317
2318 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2319 &ssl->handshake->pmslen,
2320 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002321 POLARSSL_MPI_MAX_SIZE,
2322 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002323 {
2324 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2325 return( ret );
2326 }
2327
2328 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2329 }
2330 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002331#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002332 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2333 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2334 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002335#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002336 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002337 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002338 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2339 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002340 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002341 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002342 * opaque psk_identity<0..2^16-1>;
2343 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002344 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002345 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2346
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002347 i = 4;
2348 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002349 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2350 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002351
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002352 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2353 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002354
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002355#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002356 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002357 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002358 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002359 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002360 else
2361#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002362#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2363 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2364 {
2365 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2366 return( ret );
2367 }
2368 else
2369#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002370#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002371 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002372 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002373 /*
2374 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2375 */
2376 n = ssl->handshake->dhm_ctx.len;
2377 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2378 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002379
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002380 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002381 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002382 &ssl->out_msg[i], n,
2383 ssl->f_rng, ssl->p_rng );
2384 if( ret != 0 )
2385 {
2386 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2387 return( ret );
2388 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002389 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002390 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002391#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002392#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002393 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002394 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002395 /*
2396 * ClientECDiffieHellmanPublic public;
2397 */
2398 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2399 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2400 ssl->f_rng, ssl->p_rng );
2401 if( ret != 0 )
2402 {
2403 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2404 return( ret );
2405 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002406
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002407 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2408 }
2409 else
2410#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2411 {
2412 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002413 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002414 }
2415
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002416 if( ( ret = ssl_psk_derive_premaster( ssl,
2417 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002418 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002419 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420 return( ret );
2421 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422 }
2423 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002424#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002425#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002426 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002428 i = 4;
2429 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002430 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 }
Paul Bakkered27a042013-04-18 22:46:23 +02002432 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002433#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002434 {
2435 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002436 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002437 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002438 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002439
Paul Bakkerff60ee62010-03-16 21:09:09 +00002440 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2441 {
2442 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2443 return( ret );
2444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
2446 ssl->out_msglen = i + n;
2447 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2448 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2449
2450 ssl->state++;
2451
2452 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2453 {
2454 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2455 return( ret );
2456 }
2457
2458 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2459
2460 return( 0 );
2461}
2462
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002463#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2464 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002465 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2466 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002467static int ssl_write_certificate_verify( ssl_context *ssl )
2468{
Paul Bakkered27a042013-04-18 22:46:23 +02002469 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
2471 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2472
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002473 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002474 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002475 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002476 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002477 {
2478 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2479 ssl->state++;
2480 return( 0 );
2481 }
2482
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002483 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2484 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002485}
2486#else
2487static int ssl_write_certificate_verify( ssl_context *ssl )
2488{
2489 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2490 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2491 size_t n = 0, offset = 0;
2492 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002493 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002494 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002495 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002496
2497 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2498
2499 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002500 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002501 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002502 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2503 {
2504 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2505 ssl->state++;
2506 return( 0 );
2507 }
2508
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002509 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 {
2511 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2512 ssl->state++;
2513 return( 0 );
2514 }
2515
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002516 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002518 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2519 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 }
2521
2522 /*
2523 * Make an RSA signature of the handshake digests
2524 */
Paul Bakker48916f92012-09-16 19:57:18 +00002525 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002527#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2528 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002529 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002530 {
Paul Bakker926af752012-11-23 13:38:07 +01002531 /*
2532 * digitally-signed struct {
2533 * opaque md5_hash[16];
2534 * opaque sha_hash[20];
2535 * };
2536 *
2537 * md5_hash
2538 * MD5(handshake_messages);
2539 *
2540 * sha_hash
2541 * SHA(handshake_messages);
2542 */
2543 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002544 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002545
2546 /*
2547 * For ECDSA, default hash is SHA-1 only
2548 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002549 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002550 {
2551 hash_start += 16;
2552 hashlen -= 16;
2553 md_alg = POLARSSL_MD_SHA1;
2554 }
Paul Bakker926af752012-11-23 13:38:07 +01002555 }
2556 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002557#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2558 POLARSSL_SSL_PROTO_TLS1_1 */
2559#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2560 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002561 {
2562 /*
2563 * digitally-signed struct {
2564 * opaque handshake_messages[handshake_messages_length];
2565 * };
2566 *
2567 * Taking shortcut here. We assume that the server always allows the
2568 * PRF Hash function and has sent it in the allowed signature
2569 * algorithms list received in the Certificate Request message.
2570 *
2571 * Until we encounter a server that does not, we will take this
2572 * shortcut.
2573 *
2574 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2575 * in order to satisfy 'weird' needs from the server side.
2576 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002577 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2578 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002579 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002580 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002581 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002582 }
2583 else
2584 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002585 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002586 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002587 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002588 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002589
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002590 /* Info from md_alg will be used instead */
2591 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002592 offset = 2;
2593 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002594 else
2595#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002596 {
2597 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002598 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002599 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002600
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002601 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002602 ssl->out_msg + 6 + offset, &n,
2603 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002604 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002605 SSL_DEBUG_RET( 1, "pk_sign", ret );
2606 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002607 }
Paul Bakker926af752012-11-23 13:38:07 +01002608
Paul Bakker1ef83d62012-04-11 12:09:53 +00002609 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2610 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Paul Bakker1ef83d62012-04-11 12:09:53 +00002612 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2614 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2615
2616 ssl->state++;
2617
2618 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2619 {
2620 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2621 return( ret );
2622 }
2623
2624 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2625
Paul Bakkered27a042013-04-18 22:46:23 +02002626 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002627}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002628#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2629 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2630 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Paul Bakkera503a632013-08-14 13:48:06 +02002632#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002633static int ssl_parse_new_session_ticket( ssl_context *ssl )
2634{
2635 int ret;
2636 uint32_t lifetime;
2637 size_t ticket_len;
2638 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002639 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002640
2641 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2642
2643 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2644 {
2645 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2646 return( ret );
2647 }
2648
2649 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2650 {
2651 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2652 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2653 }
2654
2655 /*
2656 * struct {
2657 * uint32 ticket_lifetime_hint;
2658 * opaque ticket<0..2^16-1>;
2659 * } NewSessionTicket;
2660 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002661 * 0 . 3 ticket_lifetime_hint
2662 * 4 . 5 ticket_len (n)
2663 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002664 */
2665 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002666 ssl->in_hslen < 6 + ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002667 {
2668 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2669 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2670 }
2671
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002672 msg = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002673
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002674 lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
2675 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002676
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002677 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
2678
2679 if( ticket_len + 6 + ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002680 {
2681 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2682 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2683 }
2684
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002685 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2686
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002687 /* We're not waiting for a NewSessionTicket message any more */
2688 ssl->handshake->new_session_ticket = 0;
2689
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002690 /*
2691 * Zero-length ticket means the server changed his mind and doesn't want
2692 * to send a ticket after all, so just forget it
2693 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002694 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002695 return( 0 );
2696
Paul Bakker34617722014-06-13 17:20:13 +02002697 polarssl_zeroize( ssl->session_negotiate->ticket,
2698 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002699 polarssl_free( ssl->session_negotiate->ticket );
2700 ssl->session_negotiate->ticket = NULL;
2701 ssl->session_negotiate->ticket_len = 0;
2702
2703 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2704 {
2705 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2706 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2707 }
2708
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002709 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002710
2711 ssl->session_negotiate->ticket = ticket;
2712 ssl->session_negotiate->ticket_len = ticket_len;
2713 ssl->session_negotiate->ticket_lifetime = lifetime;
2714
2715 /*
2716 * RFC 5077 section 3.4:
2717 * "If the client receives a session ticket from the server, then it
2718 * discards any Session ID that was sent in the ServerHello."
2719 */
2720 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2721 ssl->session_negotiate->length = 0;
2722
2723 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2724
2725 return( 0 );
2726}
Paul Bakkera503a632013-08-14 13:48:06 +02002727#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002728
Paul Bakker5121ce52009-01-03 21:22:43 +00002729/*
Paul Bakker1961b702013-01-25 14:49:24 +01002730 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 */
Paul Bakker1961b702013-01-25 14:49:24 +01002732int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002733{
2734 int ret = 0;
2735
Paul Bakker1961b702013-01-25 14:49:24 +01002736 if( ssl->state == SSL_HANDSHAKE_OVER )
2737 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
Paul Bakker1961b702013-01-25 14:49:24 +01002739 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2740
2741 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2742 return( ret );
2743
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002744#if defined(POLARSSL_SSL_PROTO_DTLS)
2745 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2746 ssl->handshake != NULL &&
2747 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2748 {
2749 if( ( ret = ssl_resend( ssl ) ) != 0 )
2750 return( ret );
2751 }
2752#endif
2753
Paul Bakker1961b702013-01-25 14:49:24 +01002754 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 */