blob: f11f284735760aa9dd5aab760c251b2d599e7154 [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
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100362#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
363static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
364 unsigned char *buf, size_t *olen )
365{
366 unsigned char *p = buf;
367
368 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
369 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
370 {
371 *olen = 0;
372 return;
373 }
374
375 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
376 "extension" ) );
377
378 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
379 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
380
381 *p++ = 0x00;
382 *p++ = 0x00;
383
384 *olen = 4;
385}
386#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
387
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200388#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
389static void ssl_write_extended_ms_ext( ssl_context *ssl,
390 unsigned char *buf, size_t *olen )
391{
392 unsigned char *p = buf;
393
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200394 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
395 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200396 {
397 *olen = 0;
398 return;
399 }
400
401 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
402 "extension" ) );
403
404 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
405 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
406
407 *p++ = 0x00;
408 *p++ = 0x00;
409
410 *olen = 4;
411}
412#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
413
Paul Bakkera503a632013-08-14 13:48:06 +0200414#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200415static void ssl_write_session_ticket_ext( ssl_context *ssl,
416 unsigned char *buf, size_t *olen )
417{
418 unsigned char *p = buf;
419 size_t tlen = ssl->session_negotiate->ticket_len;
420
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200421 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
422 {
423 *olen = 0;
424 return;
425 }
426
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200427 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
428
429 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
430 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
431
432 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
433 *p++ = (unsigned char)( ( tlen ) & 0xFF );
434
435 *olen = 4;
436
437 if( ssl->session_negotiate->ticket == NULL ||
438 ssl->session_negotiate->ticket_len == 0 )
439 {
440 return;
441 }
442
443 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
444
445 memcpy( p, ssl->session_negotiate->ticket, tlen );
446
447 *olen += tlen;
448}
Paul Bakkera503a632013-08-14 13:48:06 +0200449#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200450
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200451#if defined(POLARSSL_SSL_ALPN)
452static void ssl_write_alpn_ext( ssl_context *ssl,
453 unsigned char *buf, size_t *olen )
454{
455 unsigned char *p = buf;
456 const char **cur;
457
458 if( ssl->alpn_list == NULL )
459 {
460 *olen = 0;
461 return;
462 }
463
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200464 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200465
466 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
467 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
468
469 /*
470 * opaque ProtocolName<1..2^8-1>;
471 *
472 * struct {
473 * ProtocolName protocol_name_list<2..2^16-1>
474 * } ProtocolNameList;
475 */
476
477 /* Skip writing extension and list length for now */
478 p += 4;
479
480 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
481 {
482 *p = (unsigned char)( strlen( *cur ) & 0xFF );
483 memcpy( p + 1, *cur, *p );
484 p += 1 + *p;
485 }
486
487 *olen = p - buf;
488
489 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
490 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
491 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
492
493 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
494 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
495 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
496}
497#endif /* POLARSSL_SSL_ALPN */
498
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200499/*
500 * Generate random bytes for ClientHello
501 */
502static int ssl_generate_random( ssl_context *ssl )
503{
504 int ret;
505 unsigned char *p = ssl->handshake->randbytes;
506#if defined(POLARSSL_HAVE_TIME)
507 time_t t;
508#endif
509
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200510 /*
511 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
512 */
513#if defined(POLARSSL_SSL_PROTO_DTLS)
514 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
515 ssl->handshake->verify_cookie != NULL )
516 {
517 return( 0 );
518 }
519#endif
520
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200521#if defined(POLARSSL_HAVE_TIME)
522 t = time( NULL );
523 *p++ = (unsigned char)( t >> 24 );
524 *p++ = (unsigned char)( t >> 16 );
525 *p++ = (unsigned char)( t >> 8 );
526 *p++ = (unsigned char)( t );
527
528 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
529#else
530 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
531 return( ret );
532
533 p += 4;
534#endif /* POLARSSL_HAVE_TIME */
535
536 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
537 return( ret );
538
539 return( 0 );
540}
541
Paul Bakker5121ce52009-01-03 21:22:43 +0000542static int ssl_write_client_hello( ssl_context *ssl )
543{
Paul Bakker23986e52011-04-24 08:57:21 +0000544 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100545 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200547 unsigned char *p, *q;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200548 unsigned char offer_compress;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200549 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200550 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
552 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
553
Paul Bakkera9a028e2013-11-21 17:31:06 +0100554 if( ssl->f_rng == NULL )
555 {
556 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
557 return( POLARSSL_ERR_SSL_NO_RNG );
558 }
559
Paul Bakker48916f92012-09-16 19:57:18 +0000560 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
561 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000562 ssl->major_ver = ssl->min_major_ver;
563 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000564 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Paul Bakker490ecc82011-10-06 13:04:09 +0000566 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
567 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200568 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
569 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000570 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572 /*
573 * 0 . 0 handshake type
574 * 1 . 3 handshake length
575 * 4 . 5 highest version supported
576 * 6 . 9 current UNIX time
577 * 10 . 37 random bytes
578 */
579 buf = ssl->out_msg;
580 p = buf + 4;
581
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100582 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
583 ssl->transport, p );
584 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
587 buf[4], buf[5] ) );
588
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200589 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
590 {
591 SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200592 return( ret );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200593 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200594
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200595 memcpy( p, ssl->handshake->randbytes, 32 );
596 SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
597 p += 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 /*
600 * 38 . 38 session id length
601 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100602 * 39+n . 39+n DTLS only: cookie length (1 byte)
603 * 40+n . .. DTSL only: cookie
604 * .. . .. ciphersuitelist length (2 bytes)
605 * .. . .. ciphersuitelist
606 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000607 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100608 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000609 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 */
Paul Bakker48916f92012-09-16 19:57:18 +0000611 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
Paul Bakker0a597072012-09-25 21:55:46 +0000613 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
614 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200615 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200617 }
618
Paul Bakkera503a632013-08-14 13:48:06 +0200619#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200620 /*
621 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
622 * generate and include a Session ID in the TLS ClientHello."
623 */
624 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
625 ssl->session_negotiate->ticket != NULL &&
626 ssl->session_negotiate->ticket_len != 0 )
627 {
628 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
629
630 if( ret != 0 )
631 return( ret );
632
633 ssl->session_negotiate->length = n = 32;
634 }
Paul Bakkera503a632013-08-14 13:48:06 +0200635#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
637 *p++ = (unsigned char) n;
638
639 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000640 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000641
642 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
643 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
644
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100645 /*
646 * DTLS cookie
647 */
648#if defined(POLARSSL_SSL_PROTO_DTLS)
649 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
650 {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200651 if( ssl->handshake->verify_cookie == NULL )
652 {
653 SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
654 *p++ = 0;
655 }
656 else
657 {
658 SSL_DEBUG_BUF( 3, "client hello, cookie",
659 ssl->handshake->verify_cookie,
660 ssl->handshake->verify_cookie_len );
661
662 *p++ = ssl->handshake->verify_cookie_len;
663 memcpy( p, ssl->handshake->verify_cookie,
664 ssl->handshake->verify_cookie_len );
665 p += ssl->handshake->verify_cookie_len;
666 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100667 }
668#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000669
Paul Bakker48916f92012-09-16 19:57:18 +0000670 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100671 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000672 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100673 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
674
675 /* Skip writing ciphersuite length for now */
676 n = 0;
677 q = p;
678 p += 2;
679
680 /* Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
Paul Bakker48916f92012-09-16 19:57:18 +0000681 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
682 {
683 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
684 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200685 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000686 }
687
Paul Bakker2fbefde2013-06-29 16:01:15 +0200688 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200690 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
691
692 if( ciphersuite_info == NULL )
693 continue;
694
695 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
696 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
697 continue;
698
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100699#if defined(POLARSSL_SSL_PROTO_DTLS)
700 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
701 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
702 continue;
703#endif
704
Paul Bakkere3166ce2011-01-27 17:40:50 +0000705 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200706 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
Paul Bakker2fbefde2013-06-29 16:01:15 +0200708 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200709 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
710 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 }
712
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200713 /* Some versions of OpenSSL don't handle it correctly if not at end */
714#if defined(POLARSSL_SSL_FALLBACK_SCSV)
715 if( ssl->fallback == SSL_IS_FALLBACK )
716 {
717 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
718 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
719 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
720 n++;
721 }
722#endif
723
Paul Bakker2fbefde2013-06-29 16:01:15 +0200724 *q++ = (unsigned char)( n >> 7 );
725 *q++ = (unsigned char)( n << 1 );
726
727 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
728
Paul Bakker2770fbd2012-07-03 13:30:23 +0000729#if defined(POLARSSL_ZLIB_SUPPORT)
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200730 offer_compress = 1;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000731#else
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200732 offer_compress = 0;
733#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200735 /*
736 * We don't support compression with DTLS right now: is many records come
737 * in the same datagram, uncompressing one could overwrite the next one.
738 * We don't want to add complexity for handling that case unless there is
739 * an actual need for it.
740 */
741#if defined(POLARSSL_SSL_PROTO_DTLS)
742 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
743 offer_compress = 0;
744#endif
745
746 if( offer_compress )
747 {
748 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
749 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
750 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
751
752 *p++ = 2;
753 *p++ = SSL_COMPRESS_DEFLATE;
754 *p++ = SSL_COMPRESS_NULL;
755 }
756 else
757 {
758 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
759 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
760 SSL_COMPRESS_NULL ) );
761
762 *p++ = 1;
763 *p++ = SSL_COMPRESS_NULL;
764 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Paul Bakkerd3edc862013-03-20 16:07:17 +0100766 // First write extensions, then the total length
767 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200768#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100769 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
770 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200771#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
Paul Bakkerd3edc862013-03-20 16:07:17 +0100773 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
774 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000775
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200776#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100777 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
778 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200779#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000780
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200781#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100782 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
783 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100784
Paul Bakkerd3edc862013-03-20 16:07:17 +0100785 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
786 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100787#endif
788
Paul Bakker05decb22013-08-15 13:33:48 +0200789#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200790 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
791 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200792#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200793
Paul Bakker1f2bc622013-08-15 13:45:55 +0200794#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200795 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
796 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200797#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200798
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100799#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
800 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
801 ext_len += olen;
802#endif
803
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200804#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
805 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
806 ext_len += olen;
807#endif
808
Paul Bakkera503a632013-08-14 13:48:06 +0200809#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200810 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
811 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200812#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200813
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200814#if defined(POLARSSL_SSL_ALPN)
815 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
816 ext_len += olen;
817#endif
818
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000819 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
820 ext_len ) );
821
Paul Bakkera7036632014-04-30 10:15:38 +0200822 if( ext_len > 0 )
823 {
824 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
825 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
826 p += ext_len;
827 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100828
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 ssl->out_msglen = p - buf;
830 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
831 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
832
833 ssl->state++;
834
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +0200835#if defined(POLARSSL_SSL_PROTO_DTLS)
836 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
837 ssl_send_flight_completed( ssl );
838#endif
839
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 if( ( ret = ssl_write_record( ssl ) ) != 0 )
841 {
842 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
843 return( ret );
844 }
845
846 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
847
848 return( 0 );
849}
850
Paul Bakker48916f92012-09-16 19:57:18 +0000851static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200852 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000853 size_t len )
854{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000855 int ret;
856
Paul Bakker48916f92012-09-16 19:57:18 +0000857 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
858 {
859 if( len != 1 || buf[0] != 0x0 )
860 {
861 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000862
863 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
864 return( ret );
865
Paul Bakker48916f92012-09-16 19:57:18 +0000866 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
867 }
868
869 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
870 }
871 else
872 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100873 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000874 if( len != 1 + ssl->verify_data_len * 2 ||
875 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100876 safer_memcmp( buf + 1,
877 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
878 safer_memcmp( buf + 1 + ssl->verify_data_len,
879 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000880 {
881 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000882
883 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
884 return( ret );
885
Paul Bakker48916f92012-09-16 19:57:18 +0000886 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
887 }
888 }
889
890 return( 0 );
891}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200892
Paul Bakker05decb22013-08-15 13:33:48 +0200893#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200894static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200895 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200896 size_t len )
897{
898 /*
899 * server should use the extension only if we did,
900 * and if so the server's value should match ours (and len is always 1)
901 */
902 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
903 len != 1 ||
904 buf[0] != ssl->mfl_code )
905 {
906 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
907 }
908
909 return( 0 );
910}
Paul Bakker05decb22013-08-15 13:33:48 +0200911#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000912
Paul Bakker1f2bc622013-08-15 13:45:55 +0200913#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200914static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
915 const unsigned char *buf,
916 size_t len )
917{
918 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
919 len != 0 )
920 {
921 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
922 }
923
924 ((void) buf);
925
926 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
927
928 return( 0 );
929}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200930#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200931
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100932#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
933static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
934 const unsigned char *buf,
935 size_t len )
936{
937 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
938 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
939 len != 0 )
940 {
941 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
942 }
943
944 ((void) buf);
945
946 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
947
948 return( 0 );
949}
950#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
951
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200952#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
953static int ssl_parse_extended_ms_ext( ssl_context *ssl,
954 const unsigned char *buf,
955 size_t len )
956{
957 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200958 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200959 len != 0 )
960 {
961 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
962 }
963
964 ((void) buf);
965
966 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
967
968 return( 0 );
969}
970#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
971
Paul Bakkera503a632013-08-14 13:48:06 +0200972#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200973static int ssl_parse_session_ticket_ext( ssl_context *ssl,
974 const unsigned char *buf,
975 size_t len )
976{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200977 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
978 len != 0 )
979 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200980 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200981 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200982
983 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200984
985 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200986
987 return( 0 );
988}
Paul Bakkera503a632013-08-14 13:48:06 +0200989#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200990
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200991#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200992static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
993 const unsigned char *buf,
994 size_t len )
995{
996 size_t list_size;
997 const unsigned char *p;
998
999 list_size = buf[0];
1000 if( list_size + 1 != len )
1001 {
1002 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1003 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1004 }
1005
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001006 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001007 while( list_size > 0 )
1008 {
1009 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
1010 p[0] == POLARSSL_ECP_PF_COMPRESSED )
1011 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001012 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001013 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1014 return( 0 );
1015 }
1016
1017 list_size--;
1018 p++;
1019 }
1020
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +02001021 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1022 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001023}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001024#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001025
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001026#if defined(POLARSSL_SSL_ALPN)
1027static int ssl_parse_alpn_ext( ssl_context *ssl,
1028 const unsigned char *buf, size_t len )
1029{
1030 size_t list_len, name_len;
1031 const char **p;
1032
1033 /* If we didn't send it, the server shouldn't send it */
1034 if( ssl->alpn_list == NULL )
1035 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1036
1037 /*
1038 * opaque ProtocolName<1..2^8-1>;
1039 *
1040 * struct {
1041 * ProtocolName protocol_name_list<2..2^16-1>
1042 * } ProtocolNameList;
1043 *
1044 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1045 */
1046
1047 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1048 if( len < 4 )
1049 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1050
1051 list_len = ( buf[0] << 8 ) | buf[1];
1052 if( list_len != len - 2 )
1053 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1054
1055 name_len = buf[2];
1056 if( name_len != list_len - 1 )
1057 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1058
1059 /* Check that the server chosen protocol was in our list and save it */
1060 for( p = ssl->alpn_list; *p != NULL; p++ )
1061 {
1062 if( name_len == strlen( *p ) &&
1063 memcmp( buf + 3, *p, name_len ) == 0 )
1064 {
1065 ssl->alpn_chosen = *p;
1066 return( 0 );
1067 }
1068 }
1069
1070 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1071}
1072#endif /* POLARSSL_SSL_ALPN */
1073
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001074/*
1075 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1076 */
1077#if defined(POLARSSL_SSL_PROTO_DTLS)
1078static int ssl_parse_hello_verify_request( ssl_context *ssl )
1079{
Manuel Pégourié-Gonnard069eb792014-09-10 20:08:29 +02001080 const unsigned char *p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001081 int major_ver, minor_ver;
1082 unsigned char cookie_len;
1083
1084 SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
1085
1086 /*
1087 * struct {
1088 * ProtocolVersion server_version;
1089 * opaque cookie<0..2^8-1>;
1090 * } HelloVerifyRequest;
1091 */
1092 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
1093 ssl_read_version( &major_ver, &minor_ver, ssl->transport, p );
1094 p += 2;
1095
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02001096 /*
1097 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1098 * even is lower than our min version.
1099 */
1100 if( major_ver < SSL_MAJOR_VERSION_3 ||
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001101 minor_ver < SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02001102 major_ver > ssl->max_major_ver ||
1103 minor_ver > ssl->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001104 {
1105 SSL_DEBUG_MSG( 1, ( "bad server version" ) );
1106
1107 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1108 SSL_ALERT_MSG_PROTOCOL_VERSION );
1109
1110 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1111 }
1112
1113 cookie_len = *p++;
1114 SSL_DEBUG_BUF( 3, "cookie", (unsigned char *) p, cookie_len );
1115
1116 polarssl_free( ssl->handshake->verify_cookie );
1117
1118 ssl->handshake->verify_cookie = polarssl_malloc( cookie_len );
1119 if( ssl->handshake->verify_cookie == NULL )
1120 {
1121 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", cookie_len ) );
1122 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1123 }
1124
1125 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
1126 ssl->handshake->verify_cookie_len = cookie_len;
1127
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001128 /* Start over at ClientHello */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001129 ssl->state = SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001130 ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001131
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001132 ssl_recv_flight_completed( ssl );
1133
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001134 SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
1135
1136 return( 0 );
1137}
1138#endif /* POLARSSL_SSL_PROTO_DTLS */
1139
Paul Bakker5121ce52009-01-03 21:22:43 +00001140static int ssl_parse_server_hello( ssl_context *ssl )
1141{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001142 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001143 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001144 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001145 unsigned char *buf, *ext;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001146 unsigned char comp, accept_comp;
Paul Bakker48916f92012-09-16 19:57:18 +00001147 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001148 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001149#if defined(POLARSSL_DEBUG_C)
1150 uint32_t t;
1151#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
1153 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1154
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 buf = ssl->in_msg;
1156
1157 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1158 {
1159 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1160 return( ret );
1161 }
1162
1163 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1164 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001165 if( ssl->renegotiation == SSL_RENEGOTIATION )
1166 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001167 ssl->renego_records_seen++;
1168
1169 if( ssl->renego_max_records >= 0 &&
1170 ssl->renego_records_seen > ssl->renego_max_records )
1171 {
1172 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1173 "but not honored by server" ) );
1174 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1175 }
1176
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001177 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1178 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1179 }
1180
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001182 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 }
1184
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001185#if defined(POLARSSL_SSL_PROTO_DTLS)
1186 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1187 {
1188 if( buf[0] == SSL_HS_HELLO_VERIFY_REQUEST )
1189 {
1190 SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1191 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1192 return( ssl_parse_hello_verify_request( ssl ) );
1193 }
1194 else
1195 {
1196 /* We made it through the verification process */
1197 polarssl_free( ssl->handshake->verify_cookie );
1198 ssl->handshake->verify_cookie = NULL;
1199 ssl->handshake->verify_cookie_len = 0;
1200 }
1201 }
1202#endif /* POLARSSL_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001203
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001204 if( ssl->in_hslen < 38 + ssl_hs_hdr_len( ssl ) ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001205 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 {
1207 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001208 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 }
1210
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001211 /*
1212 * 0 . 1 server_version
1213 * 2 . 33 random (maybe including 4 bytes of Unix time)
1214 * 34 . 34 session_id length = n
1215 * 35 . 34+n session_id
1216 * 35+n . 36+n cipher_suite
1217 * 37+n . 37+n compression_method
1218 *
1219 * 38+n . 39+n extensions length (optional)
1220 * 40+n . .. extensions
1221 */
1222 buf += ssl_hs_hdr_len( ssl );
1223
1224 SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001225 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001226 ssl->transport, buf + 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001227
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001228 if( ssl->major_ver < ssl->min_major_ver ||
1229 ssl->minor_ver < ssl->min_minor_ver ||
1230 ssl->major_ver > ssl->max_major_ver ||
1231 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001232 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001233 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
1234 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1235 ssl->min_major_ver, ssl->min_minor_ver,
1236 ssl->major_ver, ssl->minor_ver,
1237 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001238
1239 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1240 SSL_ALERT_MSG_PROTOCOL_VERSION );
1241
1242 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1243 }
1244
Paul Bakker1504af52012-02-11 16:17:43 +00001245#if defined(POLARSSL_DEBUG_C)
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001246 t = ( (uint32_t) buf[2] << 24 )
1247 | ( (uint32_t) buf[3] << 16 )
1248 | ( (uint32_t) buf[4] << 8 )
1249 | ( (uint32_t) buf[5] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001250 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001251#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001253 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001255 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001257 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001258
Paul Bakker48916f92012-09-16 19:57:18 +00001259 if( n > 32 )
1260 {
1261 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1262 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1263 }
1264
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001265 if( ssl->in_hslen > 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001267 ext_len = ( ( buf[38 + n] << 8 )
1268 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Paul Bakker48916f92012-09-16 19:57:18 +00001270 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001271 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001272 {
1273 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1274 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 }
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001277 else if( ssl->in_hslen == 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001278 {
1279 ext_len = 0;
1280 }
1281 else
1282 {
1283 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1284 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001287 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001288 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001289
1290 /*
1291 * Read and check compression
1292 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001293 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001295#if defined(POLARSSL_ZLIB_SUPPORT)
1296 accept_comp = 1;
1297#else
1298 accept_comp = 0;
1299#endif
1300
1301 /* See comments in ssl_write_client_hello() */
1302#if defined(POLARSSL_SSL_PROTO_DTLS)
1303 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1304 accept_comp = 0;
1305#endif
1306
1307 if( ( accept_comp == 0 && comp != SSL_COMPRESS_NULL ) ||
1308 ( comp != SSL_COMPRESS_NULL && comp != SSL_COMPRESS_DEFLATE ) )
1309 {
1310 SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
1311 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1312 }
1313
Paul Bakker380da532012-04-18 16:10:25 +00001314 /*
1315 * Initialize update checksum functions
1316 */
Paul Bakker68884e32013-01-07 18:20:04 +01001317 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1318
1319 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1320 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001321 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001322 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1323 }
Paul Bakker380da532012-04-18 16:10:25 +00001324
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001325 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1326
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001328 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
1330 /*
1331 * Check if the session can be resumed
1332 */
Paul Bakker0a597072012-09-25 21:55:46 +00001333 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1334 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +00001335 ssl->session_negotiate->ciphersuite != i ||
1336 ssl->session_negotiate->compression != comp ||
1337 ssl->session_negotiate->length != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001338 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 {
1340 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001341 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001342#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001343 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001344#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001345 ssl->session_negotiate->ciphersuite = i;
1346 ssl->session_negotiate->compression = comp;
1347 ssl->session_negotiate->length = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001348 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 }
1350 else
1351 {
1352 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001353
1354 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1355 {
1356 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1357 return( ret );
1358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 }
1360
1361 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001362 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
Paul Bakkere3166ce2011-01-27 17:40:50 +00001364 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001365 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
1367 i = 0;
1368 while( 1 )
1369 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001370 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 {
1372 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001373 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 }
1375
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001376 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1377 ssl->session_negotiate->ciphersuite )
1378 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 }
1382
Paul Bakker2770fbd2012-07-03 13:30:23 +00001383 if( comp != SSL_COMPRESS_NULL
1384#if defined(POLARSSL_ZLIB_SUPPORT)
1385 && comp != SSL_COMPRESS_DEFLATE
1386#endif
1387 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 {
1389 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001390 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 }
Paul Bakker48916f92012-09-16 19:57:18 +00001392 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001394 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001395
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001396 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1397
Paul Bakker48916f92012-09-16 19:57:18 +00001398 while( ext_len )
1399 {
1400 unsigned int ext_id = ( ( ext[0] << 8 )
1401 | ( ext[1] ) );
1402 unsigned int ext_size = ( ( ext[2] << 8 )
1403 | ( ext[3] ) );
1404
1405 if( ext_size + 4 > ext_len )
1406 {
1407 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1408 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1409 }
1410
1411 switch( ext_id )
1412 {
1413 case TLS_EXT_RENEGOTIATION_INFO:
1414 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1415 renegotiation_info_seen = 1;
1416
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001417 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1418 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001419 return( ret );
1420
1421 break;
1422
Paul Bakker05decb22013-08-15 13:33:48 +02001423#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001424 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1425 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1426
1427 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1428 ext + 4, ext_size ) ) != 0 )
1429 {
1430 return( ret );
1431 }
1432
1433 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001434#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001435
Paul Bakker1f2bc622013-08-15 13:45:55 +02001436#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001437 case TLS_EXT_TRUNCATED_HMAC:
1438 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1439
1440 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1441 ext + 4, ext_size ) ) != 0 )
1442 {
1443 return( ret );
1444 }
1445
1446 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001447#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001448
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001449#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1450 case TLS_EXT_ENCRYPT_THEN_MAC:
1451 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1452
1453 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1454 ext + 4, ext_size ) ) != 0 )
1455 {
1456 return( ret );
1457 }
1458
1459 break;
1460#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1461
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001462#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1463 case TLS_EXT_EXTENDED_MASTER_SECRET:
1464 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1465
1466 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1467 ext + 4, ext_size ) ) != 0 )
1468 {
1469 return( ret );
1470 }
1471
1472 break;
1473#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1474
Paul Bakkera503a632013-08-14 13:48:06 +02001475#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001476 case TLS_EXT_SESSION_TICKET:
1477 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1478
1479 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1480 ext + 4, ext_size ) ) != 0 )
1481 {
1482 return( ret );
1483 }
1484
1485 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001486#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001487
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001488#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001489 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1490 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1491
1492 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1493 ext + 4, ext_size ) ) != 0 )
1494 {
1495 return( ret );
1496 }
1497
1498 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001499#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001500
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001501#if defined(POLARSSL_SSL_ALPN)
1502 case TLS_EXT_ALPN:
1503 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1504
1505 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1506 return( ret );
1507
1508 break;
1509#endif /* POLARSSL_SSL_ALPN */
1510
Paul Bakker48916f92012-09-16 19:57:18 +00001511 default:
1512 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1513 ext_id ) );
1514 }
1515
1516 ext_len -= 4 + ext_size;
1517 ext += 4 + ext_size;
1518
1519 if( ext_len > 0 && ext_len < 4 )
1520 {
1521 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1522 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1523 }
1524 }
1525
1526 /*
1527 * Renegotiation security checks
1528 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001529 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1530 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001531 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001532 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1533 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001534 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001535 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1536 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1537 renegotiation_info_seen == 0 )
1538 {
1539 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1540 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001541 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001542 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1543 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1544 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001545 {
1546 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001547 handshake_failure = 1;
1548 }
1549 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1550 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1551 renegotiation_info_seen == 1 )
1552 {
1553 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1554 handshake_failure = 1;
1555 }
1556
1557 if( handshake_failure == 1 )
1558 {
1559 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1560 return( ret );
1561
Paul Bakker48916f92012-09-16 19:57:18 +00001562 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001564
1565 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1566
1567 return( 0 );
1568}
1569
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001570#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1571 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001572static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1573 unsigned char *end )
1574{
1575 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1576
Paul Bakker29e1f122013-04-16 13:07:56 +02001577 /*
1578 * Ephemeral DH parameters:
1579 *
1580 * struct {
1581 * opaque dh_p<1..2^16-1>;
1582 * opaque dh_g<1..2^16-1>;
1583 * opaque dh_Ys<1..2^16-1>;
1584 * } ServerDHParams;
1585 */
1586 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1587 {
1588 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1589 return( ret );
1590 }
1591
1592 if( ssl->handshake->dhm_ctx.len < 64 ||
1593 ssl->handshake->dhm_ctx.len > 512 )
1594 {
1595 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1596 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1597 }
1598
1599 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1600 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1601 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001602
1603 return( ret );
1604}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001605#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1606 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001607
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001608#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001609 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001610 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1611 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1612 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1613static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1614{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001615 const ecp_curve_info *curve_info;
1616
1617 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1618 if( curve_info == NULL )
1619 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001620 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1621 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001622 }
1623
1624 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001625
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001626#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1627 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1628#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001629 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1630 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001631#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001632 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001633
1634 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1635
1636 return( 0 );
1637}
Paul Bakker9af723c2014-05-01 13:03:14 +02001638#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1639 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1640 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1641 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1642 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001643
1644#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1645 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001646 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001647static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1648 unsigned char **p,
1649 unsigned char *end )
1650{
1651 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1652
Paul Bakker29e1f122013-04-16 13:07:56 +02001653 /*
1654 * Ephemeral ECDH parameters:
1655 *
1656 * struct {
1657 * ECParameters curve_params;
1658 * ECPoint public;
1659 * } ServerECDHParams;
1660 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001661 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1662 (const unsigned char **) p, end ) ) != 0 )
1663 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001664 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001665 return( ret );
1666 }
1667
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001668 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001669 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001670 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001671 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1672 }
1673
Paul Bakker29e1f122013-04-16 13:07:56 +02001674 return( ret );
1675}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001676#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001677 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1678 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001679
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001680#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001681static int ssl_parse_server_psk_hint( ssl_context *ssl,
1682 unsigned char **p,
1683 unsigned char *end )
1684{
1685 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001686 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001687 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001688
1689 /*
1690 * PSK parameters:
1691 *
1692 * opaque psk_identity_hint<0..2^16-1>;
1693 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001694 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001695 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001696
1697 if( (*p) + len > end )
1698 {
1699 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1700 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1701 }
1702
1703 // TODO: Retrieve PSK identity hint and callback to app
1704 //
1705 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001706 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001707
1708 return( ret );
1709}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001710#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001711
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001712#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1713 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1714/*
1715 * Generate a pre-master secret and encrypt it with the server's RSA key
1716 */
1717static int ssl_write_encrypted_pms( ssl_context *ssl,
1718 size_t offset, size_t *olen,
1719 size_t pms_offset )
1720{
1721 int ret;
1722 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1723 unsigned char *p = ssl->handshake->premaster + pms_offset;
1724
1725 /*
1726 * Generate (part of) the pre-master as
1727 * struct {
1728 * ProtocolVersion client_version;
1729 * opaque random[46];
1730 * } PreMasterSecret;
1731 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001732 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1733 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001734
1735 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1736 {
1737 SSL_DEBUG_RET( 1, "f_rng", ret );
1738 return( ret );
1739 }
1740
1741 ssl->handshake->pmslen = 48;
1742
1743 /*
1744 * Now write it out, encrypted
1745 */
1746 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1747 POLARSSL_PK_RSA ) )
1748 {
1749 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1750 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1751 }
1752
1753 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1754 p, ssl->handshake->pmslen,
1755 ssl->out_msg + offset + len_bytes, olen,
1756 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1757 ssl->f_rng, ssl->p_rng ) ) != 0 )
1758 {
1759 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1760 return( ret );
1761 }
1762
1763#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1764 defined(POLARSSL_SSL_PROTO_TLS1_2)
1765 if( len_bytes == 2 )
1766 {
1767 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1768 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1769 *olen += 2;
1770 }
1771#endif
1772
1773 return( 0 );
1774}
1775#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1776 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001777
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001778#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001779#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001780 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1781 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001782static int ssl_parse_signature_algorithm( ssl_context *ssl,
1783 unsigned char **p,
1784 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001785 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001786 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001787{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001788 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001789 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001790 *pk_alg = POLARSSL_PK_NONE;
1791
1792 /* Only in TLS 1.2 */
1793 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1794 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001795 return( 0 );
1796 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001797
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001798 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001799 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1800
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001801 /*
1802 * Get hash algorithm
1803 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001804 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001805 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001806 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1807 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001808 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1809 }
1810
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001811 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001812 * Get signature algorithm
1813 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001814 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001815 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001816 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1817 "SignatureAlgorithm %d", (*p)[1] ) );
1818 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001819 }
1820
1821 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1822 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1823 *p += 2;
1824
1825 return( 0 );
1826}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001827#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001828 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1829 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001830#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001831
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001832
1833#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1834 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1835static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1836{
1837 int ret;
1838 const ecp_keypair *peer_key;
1839
1840 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1841 POLARSSL_PK_ECKEY ) )
1842 {
1843 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1844 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1845 }
1846
1847 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1848
1849 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1850 POLARSSL_ECDH_THEIRS ) ) != 0 )
1851 {
1852 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1853 return( ret );
1854 }
1855
1856 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1857 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001858 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001859 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1860 }
1861
1862 return( ret );
1863}
1864#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1865 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1866
Paul Bakker41c83d32013-03-20 14:39:14 +01001867static int ssl_parse_server_key_exchange( ssl_context *ssl )
1868{
Paul Bakker23986e52011-04-24 08:57:21 +00001869 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001870 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001871 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001872
1873 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1874
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001875#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001876 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 {
1878 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1879 ssl->state++;
1880 return( 0 );
1881 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001882 ((void) p);
1883 ((void) end);
1884#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001886#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1887 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1888 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1889 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1890 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001891 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1892 {
1893 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1894 return( ret );
1895 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001896
1897 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1898 ssl->state++;
1899 return( 0 );
1900 }
1901 ((void) p);
1902 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001903#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1904 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001905
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1907 {
1908 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1909 return( ret );
1910 }
1911
1912 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1913 {
1914 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001915 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 }
1917
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001918 /*
1919 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1920 * doesn't use a psk_identity_hint
1921 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1923 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001924 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1925 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001926 {
1927 ssl->record_read = 1;
1928 goto exit;
1929 }
1930
1931 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1932 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 }
1934
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001935 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001936 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001937 SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001938
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001939#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1940 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1941 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1942 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1943 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1944 {
1945 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1946 {
1947 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1948 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1949 }
1950 } /* FALLTROUGH */
1951#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1952
1953#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1954 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1955 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1956 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1957 ; /* nothing more to do */
1958 else
1959#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1960 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1961#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1962 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1963 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1964 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001966 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001967 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001968 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001969 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1970 }
1971 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001972 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001973#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1974 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001975#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001976 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001977 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1978 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001979 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001980 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001981 {
1982 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1983 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001984 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1985 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1986 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001987 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001988 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001989#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001990 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001991 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001992 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001993 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001994 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001995 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001996
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001997#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001998 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1999 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02002000 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002001 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2002 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002003 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002004 size_t sig_len, hashlen;
2005 unsigned char hash[64];
2006 md_type_t md_alg = POLARSSL_MD_NONE;
2007 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002008 unsigned char *params = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002009 size_t params_len = p - params;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002010
Paul Bakker29e1f122013-04-16 13:07:56 +02002011 /*
2012 * Handle the digitally-signed structure
2013 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002014#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2015 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002016 {
Paul Bakker9659dae2013-08-28 16:21:34 +02002017 if( ssl_parse_signature_algorithm( ssl, &p, end,
2018 &md_alg, &pk_alg ) != 0 )
2019 {
2020 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2021 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2022 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002023
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002024 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002025 {
2026 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2027 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2028 }
2029 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002030 else
Paul Bakker9af723c2014-05-01 13:03:14 +02002031#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002032#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2033 defined(POLARSSL_SSL_PROTO_TLS1_1)
2034 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002035 {
2036 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002037
Paul Bakker9659dae2013-08-28 16:21:34 +02002038 /* Default hash for ECDSA is SHA-1 */
2039 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
2040 md_alg = POLARSSL_MD_SHA1;
2041 }
2042 else
2043#endif
2044 {
2045 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002046 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02002047 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002048
2049 /*
2050 * Read signature
2051 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002052 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002053 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002054
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002055 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002056 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002057 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01002058 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2059 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002060
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002061 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002062
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002063 /*
2064 * Compute the hash that has been signed
2065 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002066#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2067 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002068 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002069 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002070 md5_context md5;
2071 sha1_context sha1;
2072
Paul Bakker5b4af392014-06-26 12:09:34 +02002073 md5_init( &md5 );
2074 sha1_init( &sha1 );
2075
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002076 hashlen = 36;
2077
Paul Bakker29e1f122013-04-16 13:07:56 +02002078 /*
2079 * digitally-signed struct {
2080 * opaque md5_hash[16];
2081 * opaque sha_hash[20];
2082 * };
2083 *
2084 * md5_hash
2085 * MD5(ClientHello.random + ServerHello.random
2086 * + ServerParams);
2087 * sha_hash
2088 * SHA(ClientHello.random + ServerHello.random
2089 * + ServerParams);
2090 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002091 md5_starts( &md5 );
2092 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002093 md5_update( &md5, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002094 md5_finish( &md5, hash );
2095
2096 sha1_starts( &sha1 );
2097 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002098 sha1_update( &sha1, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002099 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02002100
2101 md5_free( &md5 );
2102 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02002103 }
2104 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002105#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2106 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002107#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2108 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002109 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002110 {
2111 md_context_t ctx;
2112
Paul Bakker84bbeb52014-07-01 14:53:22 +02002113 md_init( &ctx );
2114
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002115 /* Info from md_alg will be used instead */
2116 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02002117
2118 /*
2119 * digitally-signed struct {
2120 * opaque client_random[32];
2121 * opaque server_random[32];
2122 * ServerDHParams params;
2123 * };
2124 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002125 if( ( ret = md_init_ctx( &ctx,
2126 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002127 {
2128 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2129 return( ret );
2130 }
2131
2132 md_starts( &ctx );
2133 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002134 md_update( &ctx, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002135 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002136 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002137 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002138 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002139#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2140 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002141 {
Paul Bakker577e0062013-08-28 11:57:20 +02002142 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002143 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002144 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002145
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002146 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2147 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002148
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002149 /*
2150 * Verify signature
2151 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002152 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002153 {
2154 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2155 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2156 }
2157
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002158 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2159 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002160 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002161 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002162 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002163 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002165#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002166 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2167 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002168
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002169exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 ssl->state++;
2171
2172 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2173
2174 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002175}
2176
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002177#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2178 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2179 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2180 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2181static int ssl_parse_certificate_request( ssl_context *ssl )
2182{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002183 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2184
2185 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2186
2187 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2188 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2189 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2190 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2191 {
2192 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2193 ssl->state++;
2194 return( 0 );
2195 }
2196
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002197 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2198 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002199}
2200#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002201static int ssl_parse_certificate_request( ssl_context *ssl )
2202{
2203 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002204 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002205 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002206 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002207 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002208
2209 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2210
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002211 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2212 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2213 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2214 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2215 {
2216 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2217 ssl->state++;
2218 return( 0 );
2219 }
2220
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002221 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002223 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2224 {
2225 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2226 return( ret );
2227 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002228
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002229 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2230 {
2231 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2232 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2233 }
2234
2235 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 }
2237
2238 ssl->client_auth = 0;
2239 ssl->state++;
2240
2241 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2242 ssl->client_auth++;
2243
2244 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2245 ssl->client_auth ? "a" : "no" ) );
2246
Paul Bakker926af752012-11-23 13:38:07 +01002247 if( ssl->client_auth == 0 )
2248 goto exit;
2249
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002250 ssl->record_read = 0;
2251
Paul Bakker926af752012-11-23 13:38:07 +01002252 // TODO: handshake_failure alert for an anonymous server to request
2253 // client authentication
2254
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002255 /*
2256 * struct {
2257 * ClientCertificateType certificate_types<1..2^8-1>;
2258 * SignatureAndHashAlgorithm
2259 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2260 * DistinguishedName certificate_authorities<0..2^16-1>;
2261 * } CertificateRequest;
2262 */
Paul Bakker926af752012-11-23 13:38:07 +01002263 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002264
Paul Bakker926af752012-11-23 13:38:07 +01002265 // Retrieve cert types
2266 //
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002267 cert_type_len = buf[ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002268 n = cert_type_len;
2269
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002270 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002271 {
2272 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2273 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2274 }
2275
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002276 p = buf + ssl_hs_hdr_len( ssl ) + 1;
Paul Bakker926af752012-11-23 13:38:07 +01002277 while( cert_type_len > 0 )
2278 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002279#if defined(POLARSSL_RSA_C)
2280 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002281 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002282 {
2283 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2284 break;
2285 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002286 else
2287#endif
2288#if defined(POLARSSL_ECDSA_C)
2289 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002290 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002291 {
2292 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2293 break;
2294 }
2295 else
2296#endif
2297 {
2298 ; /* Unsupported cert type, ignore */
2299 }
Paul Bakker926af752012-11-23 13:38:07 +01002300
2301 cert_type_len--;
2302 p++;
2303 }
2304
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002305#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002306 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2307 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002308 /* Ignored, see comments about hash in write_certificate_verify */
2309 // TODO: should check the signature part against our pk_key though
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002310 size_t sig_alg_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2311 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002312
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002313 p = buf + ssl_hs_hdr_len( ssl ) + 3 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002314 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002315 n += sig_alg_len;
2316
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002317 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002318 {
2319 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2320 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2321 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002322 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002323#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002324
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002325 /* Ignore certificate_authorities, we only have one cert anyway */
2326 // TODO: should not send cert if no CA matches
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002327 dn_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + m + n] << 8 )
2328 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002329
2330 n += dn_len;
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002331 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 3 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002332 {
2333 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2334 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2335 }
2336
2337exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002338 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2339
2340 return( 0 );
2341}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002342#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2343 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2344 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2345 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002346
2347static int ssl_parse_server_hello_done( ssl_context *ssl )
2348{
2349 int ret;
2350
2351 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2352
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002353 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 {
2355 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2356 {
2357 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2358 return( ret );
2359 }
2360
2361 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2362 {
2363 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002364 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 }
2366 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002367 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002369 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) ||
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2371 {
2372 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002373 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 }
2375
2376 ssl->state++;
2377
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002378#if defined(POLARSSL_SSL_PROTO_DTLS)
2379 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2380 ssl_recv_flight_completed( ssl );
2381#endif
2382
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2384
2385 return( 0 );
2386}
2387
2388static int ssl_write_client_key_exchange( ssl_context *ssl )
2389{
Paul Bakker23986e52011-04-24 08:57:21 +00002390 int ret;
2391 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002392 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
2394 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2395
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002396#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002397 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 /*
2400 * DHM key exchange -- send G^X mod P
2401 */
Paul Bakker48916f92012-09-16 19:57:18 +00002402 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
2404 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2405 ssl->out_msg[5] = (unsigned char)( n );
2406 i = 6;
2407
Paul Bakker29b64762012-09-25 09:36:44 +00002408 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002409 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 &ssl->out_msg[i], n,
2411 ssl->f_rng, ssl->p_rng );
2412 if( ret != 0 )
2413 {
2414 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2415 return( ret );
2416 }
2417
Paul Bakker48916f92012-09-16 19:57:18 +00002418 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2419 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002421 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
Paul Bakker48916f92012-09-16 19:57:18 +00002423 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2424 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002425 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002426 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 {
2428 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2429 return( ret );
2430 }
2431
Paul Bakker48916f92012-09-16 19:57:18 +00002432 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 }
2434 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002435#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002436#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002437 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2438 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2439 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002440 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002441 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2442 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2443 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002444 {
2445 /*
2446 * ECDH key exchange -- send client public value
2447 */
2448 i = 4;
2449
2450 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2451 &n,
2452 &ssl->out_msg[i], 1000,
2453 ssl->f_rng, ssl->p_rng );
2454 if( ret != 0 )
2455 {
2456 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2457 return( ret );
2458 }
2459
2460 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2461
2462 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2463 &ssl->handshake->pmslen,
2464 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002465 POLARSSL_MPI_MAX_SIZE,
2466 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002467 {
2468 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2469 return( ret );
2470 }
2471
2472 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2473 }
2474 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002475#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002476 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2477 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2478 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002479#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002480 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002481 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002482 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2483 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002484 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002485 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002486 * opaque psk_identity<0..2^16-1>;
2487 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002488 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002489 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2490
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002491 i = 4;
2492 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002493 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2494 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002495
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002496 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2497 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002498
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002499#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002500 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002502 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002503 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002504 else
2505#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002506#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2507 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2508 {
2509 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2510 return( ret );
2511 }
2512 else
2513#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002514#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002515 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002516 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002517 /*
2518 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2519 */
2520 n = ssl->handshake->dhm_ctx.len;
2521 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2522 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002523
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002524 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002525 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002526 &ssl->out_msg[i], n,
2527 ssl->f_rng, ssl->p_rng );
2528 if( ret != 0 )
2529 {
2530 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2531 return( ret );
2532 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002533 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002534 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002535#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002536#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002537 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002538 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002539 /*
2540 * ClientECDiffieHellmanPublic public;
2541 */
2542 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2543 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2544 ssl->f_rng, ssl->p_rng );
2545 if( ret != 0 )
2546 {
2547 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2548 return( ret );
2549 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002550
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002551 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2552 }
2553 else
2554#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2555 {
2556 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002557 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002558 }
2559
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002560 if( ( ret = ssl_psk_derive_premaster( ssl,
2561 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002562 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002563 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002564 return( ret );
2565 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002566 }
2567 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002568#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002569#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002570 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002572 i = 4;
2573 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002574 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
Paul Bakkered27a042013-04-18 22:46:23 +02002576 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002577#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002578 {
2579 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002580 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002581 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 ssl->out_msglen = i + n;
2585 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2586 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2587
2588 ssl->state++;
2589
2590 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2591 {
2592 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2593 return( ret );
2594 }
2595
2596 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2597
2598 return( 0 );
2599}
2600
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002601#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2602 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002603 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2604 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002605static int ssl_write_certificate_verify( ssl_context *ssl )
2606{
Paul Bakkered27a042013-04-18 22:46:23 +02002607 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002608 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
2610 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2611
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002612 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2613 {
2614 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2615 return( ret );
2616 }
2617
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002618 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002619 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002620 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002621 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002622 {
2623 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2624 ssl->state++;
2625 return( 0 );
2626 }
2627
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002628 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2629 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002630}
2631#else
2632static int ssl_write_certificate_verify( ssl_context *ssl )
2633{
2634 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2635 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2636 size_t n = 0, offset = 0;
2637 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002638 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002639 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002640 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002641
2642 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2643
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002644 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2645 {
2646 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2647 return( ret );
2648 }
2649
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002650 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002651 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002652 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002653 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2654 {
2655 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2656 ssl->state++;
2657 return( 0 );
2658 }
2659
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002660 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 {
2662 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2663 ssl->state++;
2664 return( 0 );
2665 }
2666
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002667 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002669 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2670 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 }
2672
2673 /*
2674 * Make an RSA signature of the handshake digests
2675 */
Paul Bakker48916f92012-09-16 19:57:18 +00002676 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002677
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002678#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2679 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002680 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002681 {
Paul Bakker926af752012-11-23 13:38:07 +01002682 /*
2683 * digitally-signed struct {
2684 * opaque md5_hash[16];
2685 * opaque sha_hash[20];
2686 * };
2687 *
2688 * md5_hash
2689 * MD5(handshake_messages);
2690 *
2691 * sha_hash
2692 * SHA(handshake_messages);
2693 */
2694 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002695 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002696
2697 /*
2698 * For ECDSA, default hash is SHA-1 only
2699 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002700 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002701 {
2702 hash_start += 16;
2703 hashlen -= 16;
2704 md_alg = POLARSSL_MD_SHA1;
2705 }
Paul Bakker926af752012-11-23 13:38:07 +01002706 }
2707 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002708#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2709 POLARSSL_SSL_PROTO_TLS1_1 */
2710#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2711 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002712 {
2713 /*
2714 * digitally-signed struct {
2715 * opaque handshake_messages[handshake_messages_length];
2716 * };
2717 *
2718 * Taking shortcut here. We assume that the server always allows the
2719 * PRF Hash function and has sent it in the allowed signature
2720 * algorithms list received in the Certificate Request message.
2721 *
2722 * Until we encounter a server that does not, we will take this
2723 * shortcut.
2724 *
2725 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2726 * in order to satisfy 'weird' needs from the server side.
2727 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002728 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2729 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002730 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002731 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002732 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002733 }
2734 else
2735 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002736 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002737 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002738 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002739 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002740
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002741 /* Info from md_alg will be used instead */
2742 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002743 offset = 2;
2744 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002745 else
2746#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002747 {
2748 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002749 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002750 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002751
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002752 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002753 ssl->out_msg + 6 + offset, &n,
2754 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002755 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002756 SSL_DEBUG_RET( 1, "pk_sign", ret );
2757 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002758 }
Paul Bakker926af752012-11-23 13:38:07 +01002759
Paul Bakker1ef83d62012-04-11 12:09:53 +00002760 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2761 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
Paul Bakker1ef83d62012-04-11 12:09:53 +00002763 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2765 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2766
2767 ssl->state++;
2768
2769 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2770 {
2771 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2772 return( ret );
2773 }
2774
2775 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2776
Paul Bakkered27a042013-04-18 22:46:23 +02002777 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002779#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2780 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2781 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002782
Paul Bakkera503a632013-08-14 13:48:06 +02002783#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002784static int ssl_parse_new_session_ticket( ssl_context *ssl )
2785{
2786 int ret;
2787 uint32_t lifetime;
2788 size_t ticket_len;
2789 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002790 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002791
2792 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2793
2794 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2795 {
2796 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2797 return( ret );
2798 }
2799
2800 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2801 {
2802 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2803 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2804 }
2805
2806 /*
2807 * struct {
2808 * uint32 ticket_lifetime_hint;
2809 * opaque ticket<0..2^16-1>;
2810 * } NewSessionTicket;
2811 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002812 * 0 . 3 ticket_lifetime_hint
2813 * 4 . 5 ticket_len (n)
2814 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002815 */
2816 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002817 ssl->in_hslen < 6 + ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002818 {
2819 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2820 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2821 }
2822
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002823 msg = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002824
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002825 lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
2826 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002827
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002828 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
2829
2830 if( ticket_len + 6 + ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002831 {
2832 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2833 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2834 }
2835
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002836 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2837
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002838 /* We're not waiting for a NewSessionTicket message any more */
2839 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002840 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002841
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002842 /*
2843 * Zero-length ticket means the server changed his mind and doesn't want
2844 * to send a ticket after all, so just forget it
2845 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002846 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002847 return( 0 );
2848
Paul Bakker34617722014-06-13 17:20:13 +02002849 polarssl_zeroize( ssl->session_negotiate->ticket,
2850 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002851 polarssl_free( ssl->session_negotiate->ticket );
2852 ssl->session_negotiate->ticket = NULL;
2853 ssl->session_negotiate->ticket_len = 0;
2854
2855 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2856 {
2857 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2858 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2859 }
2860
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002861 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002862
2863 ssl->session_negotiate->ticket = ticket;
2864 ssl->session_negotiate->ticket_len = ticket_len;
2865 ssl->session_negotiate->ticket_lifetime = lifetime;
2866
2867 /*
2868 * RFC 5077 section 3.4:
2869 * "If the client receives a session ticket from the server, then it
2870 * discards any Session ID that was sent in the ServerHello."
2871 */
2872 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2873 ssl->session_negotiate->length = 0;
2874
2875 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2876
2877 return( 0 );
2878}
Paul Bakkera503a632013-08-14 13:48:06 +02002879#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002880
Paul Bakker5121ce52009-01-03 21:22:43 +00002881/*
Paul Bakker1961b702013-01-25 14:49:24 +01002882 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 */
Paul Bakker1961b702013-01-25 14:49:24 +01002884int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002885{
2886 int ret = 0;
2887
Paul Bakker1961b702013-01-25 14:49:24 +01002888 if( ssl->state == SSL_HANDSHAKE_OVER )
2889 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
Paul Bakker1961b702013-01-25 14:49:24 +01002891 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2892
2893 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2894 return( ret );
2895
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002896#if defined(POLARSSL_SSL_PROTO_DTLS)
2897 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2898 ssl->handshake != NULL &&
2899 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2900 {
2901 if( ( ret = ssl_resend( ssl ) ) != 0 )
2902 return( ret );
2903 }
2904#endif
2905
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002906 /* Change state now, so that it is right in ssl_read_record(), used
2907 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
2908#if defined(POLARSSL_SSL_SESSION_TICKETS)
2909 if( ssl->state == SSL_SERVER_CHANGE_CIPHER_SPEC &&
2910 ssl->handshake->new_session_ticket != 0 )
2911 {
2912 ssl->state = SSL_SERVER_NEW_SESSION_TICKET;
2913 }
2914#endif
2915
Paul Bakker1961b702013-01-25 14:49:24 +01002916 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 {
Paul Bakker1961b702013-01-25 14:49:24 +01002918 case SSL_HELLO_REQUEST:
2919 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002920 break;
2921
Paul Bakker1961b702013-01-25 14:49:24 +01002922 /*
2923 * ==> ClientHello
2924 */
2925 case SSL_CLIENT_HELLO:
2926 ret = ssl_write_client_hello( ssl );
2927 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Paul Bakker1961b702013-01-25 14:49:24 +01002929 /*
2930 * <== ServerHello
2931 * Certificate
2932 * ( ServerKeyExchange )
2933 * ( CertificateRequest )
2934 * ServerHelloDone
2935 */
2936 case SSL_SERVER_HELLO:
2937 ret = ssl_parse_server_hello( ssl );
2938 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Paul Bakker1961b702013-01-25 14:49:24 +01002940 case SSL_SERVER_CERTIFICATE:
2941 ret = ssl_parse_certificate( ssl );
2942 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Paul Bakker1961b702013-01-25 14:49:24 +01002944 case SSL_SERVER_KEY_EXCHANGE:
2945 ret = ssl_parse_server_key_exchange( ssl );
2946 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Paul Bakker1961b702013-01-25 14:49:24 +01002948 case SSL_CERTIFICATE_REQUEST:
2949 ret = ssl_parse_certificate_request( ssl );
2950 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Paul Bakker1961b702013-01-25 14:49:24 +01002952 case SSL_SERVER_HELLO_DONE:
2953 ret = ssl_parse_server_hello_done( ssl );
2954 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Paul Bakker1961b702013-01-25 14:49:24 +01002956 /*
2957 * ==> ( Certificate/Alert )
2958 * ClientKeyExchange
2959 * ( CertificateVerify )
2960 * ChangeCipherSpec
2961 * Finished
2962 */
2963 case SSL_CLIENT_CERTIFICATE:
2964 ret = ssl_write_certificate( ssl );
2965 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Paul Bakker1961b702013-01-25 14:49:24 +01002967 case SSL_CLIENT_KEY_EXCHANGE:
2968 ret = ssl_write_client_key_exchange( ssl );
2969 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
Paul Bakker1961b702013-01-25 14:49:24 +01002971 case SSL_CERTIFICATE_VERIFY:
2972 ret = ssl_write_certificate_verify( ssl );
2973 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Paul Bakker1961b702013-01-25 14:49:24 +01002975 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2976 ret = ssl_write_change_cipher_spec( ssl );
2977 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
Paul Bakker1961b702013-01-25 14:49:24 +01002979 case SSL_CLIENT_FINISHED:
2980 ret = ssl_write_finished( ssl );
2981 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
Paul Bakker1961b702013-01-25 14:49:24 +01002983 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002984 * <== ( NewSessionTicket )
2985 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002986 * Finished
2987 */
Paul Bakkera503a632013-08-14 13:48:06 +02002988#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002989 case SSL_SERVER_NEW_SESSION_TICKET:
2990 ret = ssl_parse_new_session_ticket( ssl );
2991 break;
Paul Bakkera503a632013-08-14 13:48:06 +02002992#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002993
2994 case SSL_SERVER_CHANGE_CIPHER_SPEC:
2995 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002996 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Paul Bakker1961b702013-01-25 14:49:24 +01002998 case SSL_SERVER_FINISHED:
2999 ret = ssl_parse_finished( ssl );
3000 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
Paul Bakker1961b702013-01-25 14:49:24 +01003002 case SSL_FLUSH_BUFFERS:
3003 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3004 ssl->state = SSL_HANDSHAKE_WRAPUP;
3005 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Paul Bakker1961b702013-01-25 14:49:24 +01003007 case SSL_HANDSHAKE_WRAPUP:
3008 ssl_handshake_wrapup( ssl );
3009 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003010
Paul Bakker1961b702013-01-25 14:49:24 +01003011 default:
3012 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3013 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3014 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003015
3016 return( ret );
3017}
Paul Bakker9af723c2014-05-01 13:03:14 +02003018#endif /* POLARSSL_SSL_CLI_C */