blob: e72cedfe15e0768df6d256932fceb66394cd6d23 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#if defined(POLARSSL_PLATFORM_C)
38#include "polarssl/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020039#else
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdlib.h>
45#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#include <basetsd.h>
49typedef UINT32 uint32_t;
50#else
51#include <inttypes.h>
52#endif
53
54#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakker34617722014-06-13 17:20:13 +020058#if defined(POLARSSL_SSL_SESSION_TICKETS)
59/* Implementation that should never be optimized out by the compiler */
60static void polarssl_zeroize( void *v, size_t n ) {
61 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
62}
63#endif
64
Paul Bakker0be444a2013-08-27 21:55:01 +020065#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010066static void ssl_write_hostname_ext( ssl_context *ssl,
67 unsigned char *buf,
68 size_t *olen )
69{
70 unsigned char *p = buf;
71
72 *olen = 0;
73
Paul Bakker66d5d072014-06-17 16:39:18 +020074 if( ssl->hostname == NULL )
Paul Bakkerd3edc862013-03-20 16:07:17 +010075 return;
76
77 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
78 ssl->hostname ) );
79
80 /*
81 * struct {
82 * NameType name_type;
83 * select (name_type) {
84 * case host_name: HostName;
85 * } name;
86 * } ServerName;
87 *
88 * enum {
89 * host_name(0), (255)
90 * } NameType;
91 *
92 * opaque HostName<1..2^16-1>;
93 *
94 * struct {
95 * ServerName server_name_list<1..2^16-1>
96 * } ServerNameList;
97 */
98 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
99 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
100
101 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
102 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
103
104 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
105 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
106
107 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
108 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
109 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
110
111 memcpy( p, ssl->hostname, ssl->hostname_len );
112
113 *olen = ssl->hostname_len + 9;
114}
Paul Bakker0be444a2013-08-27 21:55:01 +0200115#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
117static void ssl_write_renegotiation_ext( ssl_context *ssl,
118 unsigned char *buf,
119 size_t *olen )
120{
121 unsigned char *p = buf;
122
123 *olen = 0;
124
125 if( ssl->renegotiation != SSL_RENEGOTIATION )
126 return;
127
128 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
129
130 /*
131 * Secure renegotiation
132 */
133 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
134 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
135
136 *p++ = 0x00;
137 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
138 *p++ = ssl->verify_data_len & 0xFF;
139
140 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
141
142 *olen = 5 + ssl->verify_data_len;
143}
144
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200145#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100146static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
147 unsigned char *buf,
148 size_t *olen )
149{
150 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100151 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200152#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
153 unsigned char *sig_alg_list = buf + 6;
154#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100155
156 *olen = 0;
157
158 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
159 return;
160
161 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
162
163 /*
164 * Prepare signature_algorithms extension (TLS 1.2)
165 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200166#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200167#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100168 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
169 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
170 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
171 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
172#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200173#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100174 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
175 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
176 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
177 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
178#endif
179#if defined(POLARSSL_SHA1_C)
180 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
181 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
182#endif
183#if defined(POLARSSL_MD5_C)
184 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
185 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
186#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200187#endif /* POLARSSL_RSA_C */
188#if defined(POLARSSL_ECDSA_C)
189#if defined(POLARSSL_SHA512_C)
190 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
191 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
192 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
193 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
194#endif
195#if defined(POLARSSL_SHA256_C)
196 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
197 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
198 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
199 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
200#endif
201#if defined(POLARSSL_SHA1_C)
202 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
203 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
204#endif
205#if defined(POLARSSL_MD5_C)
206 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
207 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
208#endif
209#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100210
211 /*
212 * enum {
213 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
214 * sha512(6), (255)
215 * } HashAlgorithm;
216 *
217 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
218 * SignatureAlgorithm;
219 *
220 * struct {
221 * HashAlgorithm hash;
222 * SignatureAlgorithm signature;
223 * } SignatureAndHashAlgorithm;
224 *
225 * SignatureAndHashAlgorithm
226 * supported_signature_algorithms<2..2^16-2>;
227 */
228 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
229 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
230
231 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
232 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
233
234 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
235 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
236
Paul Bakkerd3edc862013-03-20 16:07:17 +0100237 *olen = 6 + sig_alg_len;
238}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200239#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100240
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200241#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100242static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
243 unsigned char *buf,
244 size_t *olen )
245{
246 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100247 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100248 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100249 const ecp_curve_info *info;
250#if defined(POLARSSL_SSL_SET_CURVES)
251 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100252#else
253 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100254#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100255
256 *olen = 0;
257
258 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
259
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100260#if defined(POLARSSL_SSL_SET_CURVES)
261 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200262 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100263 info = ecp_curve_info_from_grp_id( *grp_id );
264#else
265 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
266 {
267#endif
268
269 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
270 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200271 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200272
273 if( elliptic_curve_len == 0 )
274 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100275
276 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
277 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
278
279 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
280 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
281
282 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
283 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
284
Paul Bakkerd3edc862013-03-20 16:07:17 +0100285 *olen = 6 + elliptic_curve_len;
286}
287
288static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
289 unsigned char *buf,
290 size_t *olen )
291{
292 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200293 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100294
295 *olen = 0;
296
297 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
298
299 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
300 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
301
302 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100303 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200304
305 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100306 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
307
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200308 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100309}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200310#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100311
Paul Bakker05decb22013-08-15 13:33:48 +0200312#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200313static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
314 unsigned char *buf,
315 size_t *olen )
316{
317 unsigned char *p = buf;
318
319 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
320 *olen = 0;
321 return;
322 }
323
324 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
325
326 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
327 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
328
329 *p++ = 0x00;
330 *p++ = 1;
331
332 *p++ = ssl->mfl_code;
333
334 *olen = 5;
335}
Paul Bakker05decb22013-08-15 13:33:48 +0200336#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200337
Paul Bakker1f2bc622013-08-15 13:45:55 +0200338#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200339static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
340 unsigned char *buf, size_t *olen )
341{
342 unsigned char *p = buf;
343
344 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
345 {
346 *olen = 0;
347 return;
348 }
349
350 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
351
352 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
353 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
354
355 *p++ = 0x00;
356 *p++ = 0x00;
357
358 *olen = 4;
359}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200360#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200361
Paul Bakkera503a632013-08-14 13:48:06 +0200362#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200363static void ssl_write_session_ticket_ext( ssl_context *ssl,
364 unsigned char *buf, size_t *olen )
365{
366 unsigned char *p = buf;
367 size_t tlen = ssl->session_negotiate->ticket_len;
368
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200369 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
370 {
371 *olen = 0;
372 return;
373 }
374
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200375 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
376
377 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
378 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
379
380 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
381 *p++ = (unsigned char)( ( tlen ) & 0xFF );
382
383 *olen = 4;
384
385 if( ssl->session_negotiate->ticket == NULL ||
386 ssl->session_negotiate->ticket_len == 0 )
387 {
388 return;
389 }
390
391 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
392
393 memcpy( p, ssl->session_negotiate->ticket, tlen );
394
395 *olen += tlen;
396}
Paul Bakkera503a632013-08-14 13:48:06 +0200397#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200398
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200399#if defined(POLARSSL_SSL_ALPN)
400static void ssl_write_alpn_ext( ssl_context *ssl,
401 unsigned char *buf, size_t *olen )
402{
403 unsigned char *p = buf;
404 const char **cur;
405
406 if( ssl->alpn_list == NULL )
407 {
408 *olen = 0;
409 return;
410 }
411
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200412 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200413
414 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
415 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
416
417 /*
418 * opaque ProtocolName<1..2^8-1>;
419 *
420 * struct {
421 * ProtocolName protocol_name_list<2..2^16-1>
422 * } ProtocolNameList;
423 */
424
425 /* Skip writing extension and list length for now */
426 p += 4;
427
428 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
429 {
430 *p = (unsigned char)( strlen( *cur ) & 0xFF );
431 memcpy( p + 1, *cur, *p );
432 p += 1 + *p;
433 }
434
435 *olen = p - buf;
436
437 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
438 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
439 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
440
441 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
442 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
443 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
444}
445#endif /* POLARSSL_SSL_ALPN */
446
Paul Bakker5121ce52009-01-03 21:22:43 +0000447static int ssl_write_client_hello( ssl_context *ssl )
448{
Paul Bakker23986e52011-04-24 08:57:21 +0000449 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100450 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200452 unsigned char *p, *q;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200453 unsigned char offer_compress;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200454#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200456#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200457 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200458 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
460 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
461
Paul Bakkera9a028e2013-11-21 17:31:06 +0100462 if( ssl->f_rng == NULL )
463 {
464 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
465 return( POLARSSL_ERR_SSL_NO_RNG );
466 }
467
Paul Bakker48916f92012-09-16 19:57:18 +0000468 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
469 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000470 ssl->major_ver = ssl->min_major_ver;
471 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000472 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Paul Bakker490ecc82011-10-06 13:04:09 +0000474 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
475 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200476 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
477 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000478 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480 /*
481 * 0 . 0 handshake type
482 * 1 . 3 handshake length
483 * 4 . 5 highest version supported
484 * 6 . 9 current UNIX time
485 * 10 . 37 random bytes
486 */
487 buf = ssl->out_msg;
488 p = buf + 4;
489
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100490 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
491 ssl->transport, p );
492 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
495 buf[4], buf[5] ) );
496
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200497#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 t = time( NULL );
499 *p++ = (unsigned char)( t >> 24 );
500 *p++ = (unsigned char)( t >> 16 );
501 *p++ = (unsigned char)( t >> 8 );
502 *p++ = (unsigned char)( t );
503
504 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200505#else
506 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
507 return( ret );
508
509 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200510#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
Paul Bakkera3d195c2011-11-27 21:07:34 +0000512 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
513 return( ret );
514
515 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Paul Bakker48916f92012-09-16 19:57:18 +0000517 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
520
521 /*
522 * 38 . 38 session id length
523 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100524 * 39+n . 39+n DTLS only: cookie length (1 byte)
525 * 40+n . .. DTSL only: cookie
526 * .. . .. ciphersuitelist length (2 bytes)
527 * .. . .. ciphersuitelist
528 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000529 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100530 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000531 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 */
Paul Bakker48916f92012-09-16 19:57:18 +0000533 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
Paul Bakker0a597072012-09-25 21:55:46 +0000535 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
536 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200537 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200539 }
540
Paul Bakkera503a632013-08-14 13:48:06 +0200541#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200542 /*
543 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
544 * generate and include a Session ID in the TLS ClientHello."
545 */
546 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
547 ssl->session_negotiate->ticket != NULL &&
548 ssl->session_negotiate->ticket_len != 0 )
549 {
550 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
551
552 if( ret != 0 )
553 return( ret );
554
555 ssl->session_negotiate->length = n = 32;
556 }
Paul Bakkera503a632013-08-14 13:48:06 +0200557#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 *p++ = (unsigned char) n;
560
561 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000562 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
565 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
566
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100567 /*
568 * DTLS cookie
569 */
570#if defined(POLARSSL_SSL_PROTO_DTLS)
571 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
572 {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200573 if( ssl->handshake->verify_cookie == NULL )
574 {
575 SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
576 *p++ = 0;
577 }
578 else
579 {
580 SSL_DEBUG_BUF( 3, "client hello, cookie",
581 ssl->handshake->verify_cookie,
582 ssl->handshake->verify_cookie_len );
583
584 *p++ = ssl->handshake->verify_cookie_len;
585 memcpy( p, ssl->handshake->verify_cookie,
586 ssl->handshake->verify_cookie_len );
587 p += ssl->handshake->verify_cookie_len;
588 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100589 }
590#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Paul Bakker48916f92012-09-16 19:57:18 +0000592 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100593 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000594 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100595 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
596
597 /* Skip writing ciphersuite length for now */
598 n = 0;
599 q = p;
600 p += 2;
601
602 /* Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
Paul Bakker48916f92012-09-16 19:57:18 +0000603 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
604 {
605 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
606 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200607 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000608 }
609
Paul Bakker2fbefde2013-06-29 16:01:15 +0200610 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200612 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
613
614 if( ciphersuite_info == NULL )
615 continue;
616
617 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
618 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
619 continue;
620
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100621#if defined(POLARSSL_SSL_PROTO_DTLS)
622 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
623 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
624 continue;
625#endif
626
Paul Bakkere3166ce2011-01-27 17:40:50 +0000627 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200628 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Paul Bakker2fbefde2013-06-29 16:01:15 +0200630 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200631 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
632 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 }
634
Paul Bakker2fbefde2013-06-29 16:01:15 +0200635 *q++ = (unsigned char)( n >> 7 );
636 *q++ = (unsigned char)( n << 1 );
637
638 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
639
Paul Bakker2770fbd2012-07-03 13:30:23 +0000640#if defined(POLARSSL_ZLIB_SUPPORT)
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200641 offer_compress = 1;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000642#else
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200643 offer_compress = 0;
644#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200646 /*
647 * We don't support compression with DTLS right now: is many records come
648 * in the same datagram, uncompressing one could overwrite the next one.
649 * We don't want to add complexity for handling that case unless there is
650 * an actual need for it.
651 */
652#if defined(POLARSSL_SSL_PROTO_DTLS)
653 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
654 offer_compress = 0;
655#endif
656
657 if( offer_compress )
658 {
659 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
660 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
661 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
662
663 *p++ = 2;
664 *p++ = SSL_COMPRESS_DEFLATE;
665 *p++ = SSL_COMPRESS_NULL;
666 }
667 else
668 {
669 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
670 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
671 SSL_COMPRESS_NULL ) );
672
673 *p++ = 1;
674 *p++ = SSL_COMPRESS_NULL;
675 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
Paul Bakkerd3edc862013-03-20 16:07:17 +0100677 // First write extensions, then the total length
678 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200679#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100680 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
681 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200682#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Paul Bakkerd3edc862013-03-20 16:07:17 +0100684 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
685 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000686
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200687#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100688 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
689 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200690#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000691
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200692#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100693 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
694 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100695
Paul Bakkerd3edc862013-03-20 16:07:17 +0100696 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
697 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100698#endif
699
Paul Bakker05decb22013-08-15 13:33:48 +0200700#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200701 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
702 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200703#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200704
Paul Bakker1f2bc622013-08-15 13:45:55 +0200705#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200706 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
707 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200708#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200709
Paul Bakkera503a632013-08-14 13:48:06 +0200710#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200711 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
712 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200713#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200714
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200715#if defined(POLARSSL_SSL_ALPN)
716 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
717 ext_len += olen;
718#endif
719
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000720 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
721 ext_len ) );
722
Paul Bakkera7036632014-04-30 10:15:38 +0200723 if( ext_len > 0 )
724 {
725 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
726 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
727 p += ext_len;
728 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100729
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 ssl->out_msglen = p - buf;
731 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
732 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
733
734 ssl->state++;
735
736 if( ( ret = ssl_write_record( ssl ) ) != 0 )
737 {
738 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
739 return( ret );
740 }
741
742 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
743
744 return( 0 );
745}
746
Paul Bakker48916f92012-09-16 19:57:18 +0000747static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200748 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000749 size_t len )
750{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000751 int ret;
752
Paul Bakker48916f92012-09-16 19:57:18 +0000753 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
754 {
755 if( len != 1 || buf[0] != 0x0 )
756 {
757 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000758
759 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
760 return( ret );
761
Paul Bakker48916f92012-09-16 19:57:18 +0000762 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
763 }
764
765 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
766 }
767 else
768 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100769 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000770 if( len != 1 + ssl->verify_data_len * 2 ||
771 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100772 safer_memcmp( buf + 1,
773 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
774 safer_memcmp( buf + 1 + ssl->verify_data_len,
775 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000776 {
777 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000778
779 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
780 return( ret );
781
Paul Bakker48916f92012-09-16 19:57:18 +0000782 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
783 }
784 }
785
786 return( 0 );
787}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200788
Paul Bakker05decb22013-08-15 13:33:48 +0200789#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200790static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200791 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200792 size_t len )
793{
794 /*
795 * server should use the extension only if we did,
796 * and if so the server's value should match ours (and len is always 1)
797 */
798 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
799 len != 1 ||
800 buf[0] != ssl->mfl_code )
801 {
802 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
803 }
804
805 return( 0 );
806}
Paul Bakker05decb22013-08-15 13:33:48 +0200807#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000808
Paul Bakker1f2bc622013-08-15 13:45:55 +0200809#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200810static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
811 const unsigned char *buf,
812 size_t len )
813{
814 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
815 len != 0 )
816 {
817 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
818 }
819
820 ((void) buf);
821
822 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
823
824 return( 0 );
825}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200826#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200827
Paul Bakkera503a632013-08-14 13:48:06 +0200828#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200829static int ssl_parse_session_ticket_ext( ssl_context *ssl,
830 const unsigned char *buf,
831 size_t len )
832{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200833 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
834 len != 0 )
835 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200836 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200837 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200838
839 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200840
841 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200842
843 return( 0 );
844}
Paul Bakkera503a632013-08-14 13:48:06 +0200845#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200846
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200847#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200848static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
849 const unsigned char *buf,
850 size_t len )
851{
852 size_t list_size;
853 const unsigned char *p;
854
855 list_size = buf[0];
856 if( list_size + 1 != len )
857 {
858 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
859 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
860 }
861
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200862 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200863 while( list_size > 0 )
864 {
865 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
866 p[0] == POLARSSL_ECP_PF_COMPRESSED )
867 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200868 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200869 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
870 return( 0 );
871 }
872
873 list_size--;
874 p++;
875 }
876
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200877 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
878 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200879}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200880#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200881
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200882#if defined(POLARSSL_SSL_ALPN)
883static int ssl_parse_alpn_ext( ssl_context *ssl,
884 const unsigned char *buf, size_t len )
885{
886 size_t list_len, name_len;
887 const char **p;
888
889 /* If we didn't send it, the server shouldn't send it */
890 if( ssl->alpn_list == NULL )
891 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
892
893 /*
894 * opaque ProtocolName<1..2^8-1>;
895 *
896 * struct {
897 * ProtocolName protocol_name_list<2..2^16-1>
898 * } ProtocolNameList;
899 *
900 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
901 */
902
903 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
904 if( len < 4 )
905 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
906
907 list_len = ( buf[0] << 8 ) | buf[1];
908 if( list_len != len - 2 )
909 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
910
911 name_len = buf[2];
912 if( name_len != list_len - 1 )
913 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
914
915 /* Check that the server chosen protocol was in our list and save it */
916 for( p = ssl->alpn_list; *p != NULL; p++ )
917 {
918 if( name_len == strlen( *p ) &&
919 memcmp( buf + 3, *p, name_len ) == 0 )
920 {
921 ssl->alpn_chosen = *p;
922 return( 0 );
923 }
924 }
925
926 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
927}
928#endif /* POLARSSL_SSL_ALPN */
929
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200930/*
931 * Parse HelloVerifyRequest. Only called after verifying the HS type.
932 */
933#if defined(POLARSSL_SSL_PROTO_DTLS)
934static int ssl_parse_hello_verify_request( ssl_context *ssl )
935{
936 const unsigned char *p = ssl->in_msg + 4;
937 int major_ver, minor_ver;
938 unsigned char cookie_len;
939
940 SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
941
942 /*
943 * struct {
944 * ProtocolVersion server_version;
945 * opaque cookie<0..2^8-1>;
946 * } HelloVerifyRequest;
947 */
948 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
949 ssl_read_version( &major_ver, &minor_ver, ssl->transport, p );
950 p += 2;
951
952 if( major_ver != SSL_MAJOR_VERSION_3 ||
953 minor_ver < SSL_MINOR_VERSION_2 ||
954 minor_ver > SSL_MINOR_VERSION_3 )
955 {
956 SSL_DEBUG_MSG( 1, ( "bad server version" ) );
957
958 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
959 SSL_ALERT_MSG_PROTOCOL_VERSION );
960
961 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
962 }
963
964 cookie_len = *p++;
965 SSL_DEBUG_BUF( 3, "cookie", (unsigned char *) p, cookie_len );
966
967 polarssl_free( ssl->handshake->verify_cookie );
968
969 ssl->handshake->verify_cookie = polarssl_malloc( cookie_len );
970 if( ssl->handshake->verify_cookie == NULL )
971 {
972 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", cookie_len ) );
973 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
974 }
975
976 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
977 ssl->handshake->verify_cookie_len = cookie_len;
978
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +0200979 /* Start over at ClientHello */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200980 ssl->state = SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +0200981 ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200982
983 SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
984
985 return( 0 );
986}
987#endif /* POLARSSL_SSL_PROTO_DTLS */
988
Paul Bakker5121ce52009-01-03 21:22:43 +0000989static int ssl_parse_server_hello( ssl_context *ssl )
990{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200991 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +0000992 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +0200993 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000994 unsigned char *buf, *ext;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200995 unsigned char comp, accept_comp;
Paul Bakker48916f92012-09-16 19:57:18 +0000996 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000997 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200998#if defined(POLARSSL_DEBUG_C)
999 uint32_t t;
1000#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
1002 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1003
1004 /*
1005 * 0 . 0 handshake type
1006 * 1 . 3 handshake length
1007 * 4 . 5 protocol version
1008 * 6 . 9 UNIX time()
1009 * 10 . 37 random bytes
1010 */
1011 buf = ssl->in_msg;
1012
1013 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1014 {
1015 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1016 return( ret );
1017 }
1018
1019 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1020 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001021 if( ssl->renegotiation == SSL_RENEGOTIATION )
1022 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001023 ssl->renego_records_seen++;
1024
1025 if( ssl->renego_max_records >= 0 &&
1026 ssl->renego_records_seen > ssl->renego_max_records )
1027 {
1028 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1029 "but not honored by server" ) );
1030 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1031 }
1032
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001033 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1034 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1035 }
1036
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001038 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 }
1040
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001041#if defined(POLARSSL_SSL_PROTO_DTLS)
1042 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1043 {
1044 if( buf[0] == SSL_HS_HELLO_VERIFY_REQUEST )
1045 {
1046 SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1047 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1048 return( ssl_parse_hello_verify_request( ssl ) );
1049 }
1050 else
1051 {
1052 /* We made it through the verification process */
1053 polarssl_free( ssl->handshake->verify_cookie );
1054 ssl->handshake->verify_cookie = NULL;
1055 ssl->handshake->verify_cookie_len = 0;
1056 }
1057 }
1058#endif /* POLARSSL_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
1060 if( ssl->in_hslen < 42 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001061 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 {
1063 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001064 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 }
1066
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001067 SSL_DEBUG_BUF( 3, "server hello, version", buf + 4, 2 );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001068 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1069 ssl->transport, buf + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001071 if( ssl->major_ver < ssl->min_major_ver ||
1072 ssl->minor_ver < ssl->min_minor_ver ||
1073 ssl->major_ver > ssl->max_major_ver ||
1074 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001075 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001076 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
1077 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1078 ssl->min_major_ver, ssl->min_minor_ver,
1079 ssl->major_ver, ssl->minor_ver,
1080 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001081
1082 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1083 SSL_ALERT_MSG_PROTOCOL_VERSION );
1084
1085 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1086 }
1087
Paul Bakker1504af52012-02-11 16:17:43 +00001088#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001089 t = ( (uint32_t) buf[6] << 24 )
1090 | ( (uint32_t) buf[7] << 16 )
1091 | ( (uint32_t) buf[8] << 8 )
1092 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001093 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001094#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Paul Bakker48916f92012-09-16 19:57:18 +00001096 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
1098 n = buf[38];
1099
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1101
Paul Bakker48916f92012-09-16 19:57:18 +00001102 if( n > 32 )
1103 {
1104 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1105 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1106 }
1107
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 /*
1109 * 38 . 38 session id length
1110 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001111 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 * 41+n . 41+n chosen compression alg.
1113 * 42+n . 43+n extensions length
1114 * 44+n . 44+n+m extensions
1115 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001116 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 {
1118 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001119 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Paul Bakker48916f92012-09-16 19:57:18 +00001121 if( ( ext_len > 0 && ext_len < 4 ) ||
1122 ssl->in_hslen != 44 + n + ext_len )
1123 {
1124 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1125 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1126 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001128 else if( ssl->in_hslen == 42 + n )
1129 {
1130 ext_len = 0;
1131 }
1132 else
1133 {
1134 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1135 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1136 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001138 /* ciphersuite (used later) */
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001140
1141 /*
1142 * Read and check compression
1143 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001144 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001145
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001146#if defined(POLARSSL_ZLIB_SUPPORT)
1147 accept_comp = 1;
1148#else
1149 accept_comp = 0;
1150#endif
1151
1152 /* See comments in ssl_write_client_hello() */
1153#if defined(POLARSSL_SSL_PROTO_DTLS)
1154 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1155 accept_comp = 0;
1156#endif
1157
1158 if( ( accept_comp == 0 && comp != SSL_COMPRESS_NULL ) ||
1159 ( comp != SSL_COMPRESS_NULL && comp != SSL_COMPRESS_DEFLATE ) )
1160 {
1161 SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
1162 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1163 }
1164
Paul Bakker380da532012-04-18 16:10:25 +00001165 /*
1166 * Initialize update checksum functions
1167 */
Paul Bakker68884e32013-01-07 18:20:04 +01001168 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1169
1170 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1171 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001172 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001173 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1174 }
Paul Bakker380da532012-04-18 16:10:25 +00001175
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001176 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1177
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1179 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1180
1181 /*
1182 * Check if the session can be resumed
1183 */
Paul Bakker0a597072012-09-25 21:55:46 +00001184 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1185 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +00001186 ssl->session_negotiate->ciphersuite != i ||
1187 ssl->session_negotiate->compression != comp ||
1188 ssl->session_negotiate->length != n ||
1189 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 {
1191 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001192 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001193#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001194 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001195#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001196 ssl->session_negotiate->ciphersuite = i;
1197 ssl->session_negotiate->compression = comp;
1198 ssl->session_negotiate->length = n;
1199 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 }
1201 else
1202 {
1203 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001204
1205 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1206 {
1207 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1208 return( ret );
1209 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 }
1211
1212 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001213 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
Paul Bakkere3166ce2011-01-27 17:40:50 +00001215 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1217
1218 i = 0;
1219 while( 1 )
1220 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001221 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 {
1223 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001224 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 }
1226
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001227 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1228 ssl->session_negotiate->ciphersuite )
1229 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001231 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 }
1233
Paul Bakker2770fbd2012-07-03 13:30:23 +00001234 if( comp != SSL_COMPRESS_NULL
1235#if defined(POLARSSL_ZLIB_SUPPORT)
1236 && comp != SSL_COMPRESS_DEFLATE
1237#endif
1238 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 {
1240 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001241 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 }
Paul Bakker48916f92012-09-16 19:57:18 +00001243 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
Paul Bakker48916f92012-09-16 19:57:18 +00001245 ext = buf + 44 + n;
1246
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001247 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1248
Paul Bakker48916f92012-09-16 19:57:18 +00001249 while( ext_len )
1250 {
1251 unsigned int ext_id = ( ( ext[0] << 8 )
1252 | ( ext[1] ) );
1253 unsigned int ext_size = ( ( ext[2] << 8 )
1254 | ( ext[3] ) );
1255
1256 if( ext_size + 4 > ext_len )
1257 {
1258 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1259 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1260 }
1261
1262 switch( ext_id )
1263 {
1264 case TLS_EXT_RENEGOTIATION_INFO:
1265 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1266 renegotiation_info_seen = 1;
1267
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001268 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1269 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001270 return( ret );
1271
1272 break;
1273
Paul Bakker05decb22013-08-15 13:33:48 +02001274#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001275 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1276 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1277
1278 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1279 ext + 4, ext_size ) ) != 0 )
1280 {
1281 return( ret );
1282 }
1283
1284 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001285#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001286
Paul Bakker1f2bc622013-08-15 13:45:55 +02001287#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001288 case TLS_EXT_TRUNCATED_HMAC:
1289 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1290
1291 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1292 ext + 4, ext_size ) ) != 0 )
1293 {
1294 return( ret );
1295 }
1296
1297 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001298#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001299
Paul Bakkera503a632013-08-14 13:48:06 +02001300#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001301 case TLS_EXT_SESSION_TICKET:
1302 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1303
1304 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1305 ext + 4, ext_size ) ) != 0 )
1306 {
1307 return( ret );
1308 }
1309
1310 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001311#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001312
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001313#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001314 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1315 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1316
1317 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1318 ext + 4, ext_size ) ) != 0 )
1319 {
1320 return( ret );
1321 }
1322
1323 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001324#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001325
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001326#if defined(POLARSSL_SSL_ALPN)
1327 case TLS_EXT_ALPN:
1328 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1329
1330 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1331 return( ret );
1332
1333 break;
1334#endif /* POLARSSL_SSL_ALPN */
1335
Paul Bakker48916f92012-09-16 19:57:18 +00001336 default:
1337 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1338 ext_id ) );
1339 }
1340
1341 ext_len -= 4 + ext_size;
1342 ext += 4 + ext_size;
1343
1344 if( ext_len > 0 && ext_len < 4 )
1345 {
1346 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1347 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1348 }
1349 }
1350
1351 /*
1352 * Renegotiation security checks
1353 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001354 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1355 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001356 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001357 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1358 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001359 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001360 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1361 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1362 renegotiation_info_seen == 0 )
1363 {
1364 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1365 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001366 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001367 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1368 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1369 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001370 {
1371 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001372 handshake_failure = 1;
1373 }
1374 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1375 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1376 renegotiation_info_seen == 1 )
1377 {
1378 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1379 handshake_failure = 1;
1380 }
1381
1382 if( handshake_failure == 1 )
1383 {
1384 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1385 return( ret );
1386
Paul Bakker48916f92012-09-16 19:57:18 +00001387 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
1390 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1391
1392 return( 0 );
1393}
1394
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001395#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1396 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001397static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1398 unsigned char *end )
1399{
1400 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1401
Paul Bakker29e1f122013-04-16 13:07:56 +02001402 /*
1403 * Ephemeral DH parameters:
1404 *
1405 * struct {
1406 * opaque dh_p<1..2^16-1>;
1407 * opaque dh_g<1..2^16-1>;
1408 * opaque dh_Ys<1..2^16-1>;
1409 * } ServerDHParams;
1410 */
1411 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1412 {
1413 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1414 return( ret );
1415 }
1416
1417 if( ssl->handshake->dhm_ctx.len < 64 ||
1418 ssl->handshake->dhm_ctx.len > 512 )
1419 {
1420 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1421 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1422 }
1423
1424 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1425 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1426 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001427
1428 return( ret );
1429}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001430#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1431 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001432
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001433#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001434 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001435 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1436 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1437 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1438static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1439{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001440 const ecp_curve_info *curve_info;
1441
1442 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1443 if( curve_info == NULL )
1444 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001445 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1446 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001447 }
1448
1449 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001450
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001451#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1452 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1453#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001454 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1455 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001456#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001457 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001458
1459 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1460
1461 return( 0 );
1462}
Paul Bakker9af723c2014-05-01 13:03:14 +02001463#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1464 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1465 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1466 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1467 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001468
1469#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1470 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001471 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001472static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1473 unsigned char **p,
1474 unsigned char *end )
1475{
1476 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1477
Paul Bakker29e1f122013-04-16 13:07:56 +02001478 /*
1479 * Ephemeral ECDH parameters:
1480 *
1481 * struct {
1482 * ECParameters curve_params;
1483 * ECPoint public;
1484 * } ServerECDHParams;
1485 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001486 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1487 (const unsigned char **) p, end ) ) != 0 )
1488 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001489 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001490 return( ret );
1491 }
1492
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001493 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001494 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001495 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001496 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1497 }
1498
Paul Bakker29e1f122013-04-16 13:07:56 +02001499 return( ret );
1500}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001501#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001502 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1503 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001504
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001505#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001506static int ssl_parse_server_psk_hint( ssl_context *ssl,
1507 unsigned char **p,
1508 unsigned char *end )
1509{
1510 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001511 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001512 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001513
1514 /*
1515 * PSK parameters:
1516 *
1517 * opaque psk_identity_hint<0..2^16-1>;
1518 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001519 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001520 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001521
1522 if( (*p) + len > end )
1523 {
1524 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1525 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1526 }
1527
1528 // TODO: Retrieve PSK identity hint and callback to app
1529 //
1530 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001531 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001532
1533 return( ret );
1534}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001535#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001536
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001537#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1538 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1539/*
1540 * Generate a pre-master secret and encrypt it with the server's RSA key
1541 */
1542static int ssl_write_encrypted_pms( ssl_context *ssl,
1543 size_t offset, size_t *olen,
1544 size_t pms_offset )
1545{
1546 int ret;
1547 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1548 unsigned char *p = ssl->handshake->premaster + pms_offset;
1549
1550 /*
1551 * Generate (part of) the pre-master as
1552 * struct {
1553 * ProtocolVersion client_version;
1554 * opaque random[46];
1555 * } PreMasterSecret;
1556 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001557 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1558 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001559
1560 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1561 {
1562 SSL_DEBUG_RET( 1, "f_rng", ret );
1563 return( ret );
1564 }
1565
1566 ssl->handshake->pmslen = 48;
1567
1568 /*
1569 * Now write it out, encrypted
1570 */
1571 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1572 POLARSSL_PK_RSA ) )
1573 {
1574 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1575 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1576 }
1577
1578 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1579 p, ssl->handshake->pmslen,
1580 ssl->out_msg + offset + len_bytes, olen,
1581 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1582 ssl->f_rng, ssl->p_rng ) ) != 0 )
1583 {
1584 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1585 return( ret );
1586 }
1587
1588#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1589 defined(POLARSSL_SSL_PROTO_TLS1_2)
1590 if( len_bytes == 2 )
1591 {
1592 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1593 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1594 *olen += 2;
1595 }
1596#endif
1597
1598 return( 0 );
1599}
1600#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1601 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001602
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001603#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001604#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001605 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1606 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001607static int ssl_parse_signature_algorithm( ssl_context *ssl,
1608 unsigned char **p,
1609 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001610 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001611 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001612{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001613 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001614 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001615 *pk_alg = POLARSSL_PK_NONE;
1616
1617 /* Only in TLS 1.2 */
1618 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1619 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001620 return( 0 );
1621 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001622
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001623 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001624 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1625
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001626 /*
1627 * Get hash algorithm
1628 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001629 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001630 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001631 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1632 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001633 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1634 }
1635
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001636 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001637 * Get signature algorithm
1638 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001639 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001640 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001641 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1642 "SignatureAlgorithm %d", (*p)[1] ) );
1643 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001644 }
1645
1646 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1647 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1648 *p += 2;
1649
1650 return( 0 );
1651}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001652#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001653 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1654 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001655#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001656
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001657
1658#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1659 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1660static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1661{
1662 int ret;
1663 const ecp_keypair *peer_key;
1664
1665 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1666 POLARSSL_PK_ECKEY ) )
1667 {
1668 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1669 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1670 }
1671
1672 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1673
1674 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1675 POLARSSL_ECDH_THEIRS ) ) != 0 )
1676 {
1677 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1678 return( ret );
1679 }
1680
1681 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1682 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001683 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001684 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1685 }
1686
1687 return( ret );
1688}
1689#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1690 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1691
Paul Bakker41c83d32013-03-20 14:39:14 +01001692static int ssl_parse_server_key_exchange( ssl_context *ssl )
1693{
Paul Bakker23986e52011-04-24 08:57:21 +00001694 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001695 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001696 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001697#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001698 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1699 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001700 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001701 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001702 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001703 size_t hashlen;
1704 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001705#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001706
1707 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1708
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001709#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001710 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001711 {
1712 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1713 ssl->state++;
1714 return( 0 );
1715 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001716 ((void) p);
1717 ((void) end);
1718#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001719
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001720#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1721 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1722 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1723 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1724 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001725 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1726 {
1727 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1728 return( ret );
1729 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001730
1731 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1732 ssl->state++;
1733 return( 0 );
1734 }
1735 ((void) p);
1736 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001737#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1738 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001739
Paul Bakker5121ce52009-01-03 21:22:43 +00001740 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1741 {
1742 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1743 return( ret );
1744 }
1745
1746 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1747 {
1748 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001749 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750 }
1751
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001752 /*
1753 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1754 * doesn't use a psk_identity_hint
1755 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001756 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1757 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001758 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1759 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001760 {
1761 ssl->record_read = 1;
1762 goto exit;
1763 }
1764
1765 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1766 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001767 }
1768
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001769 p = ssl->in_msg + 4;
1770 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001771 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001772
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001773#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1774 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1775 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1776 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1777 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1778 {
1779 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1780 {
1781 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1782 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1783 }
1784 } /* FALLTROUGH */
1785#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1786
1787#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1788 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1789 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1790 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1791 ; /* nothing more to do */
1792 else
1793#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1794 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1795#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1796 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1797 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1798 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001800 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001801 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001802 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001803 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1804 }
1805 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001806 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001807#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1808 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001809#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001810 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001811 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1812 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001813 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001814 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001815 {
1816 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1817 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001818 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1819 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1820 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001821 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001822 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001823#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001824 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001825 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001826 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001827 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001828 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001829 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001830
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001831#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001832 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1833 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001834 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001835 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1836 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001837 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001838 params_len = p - ( ssl->in_msg + 4 );
1839
Paul Bakker29e1f122013-04-16 13:07:56 +02001840 /*
1841 * Handle the digitally-signed structure
1842 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001843#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1844 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001845 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001846 if( ssl_parse_signature_algorithm( ssl, &p, end,
1847 &md_alg, &pk_alg ) != 0 )
1848 {
1849 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1850 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1851 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001852
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001853 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001854 {
1855 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1856 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1857 }
1858 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001859 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001860#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001861#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1862 defined(POLARSSL_SSL_PROTO_TLS1_1)
1863 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001864 {
1865 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001866
Paul Bakker9659dae2013-08-28 16:21:34 +02001867 /* Default hash for ECDSA is SHA-1 */
1868 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1869 md_alg = POLARSSL_MD_SHA1;
1870 }
1871 else
1872#endif
1873 {
1874 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001875 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001876 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001877
1878 /*
1879 * Read signature
1880 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001881 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001882 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001883
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001884 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001885 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001886 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001887 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1888 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001889
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001890 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001891
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001892 /*
1893 * Compute the hash that has been signed
1894 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001895#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1896 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001897 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001898 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001899 md5_context md5;
1900 sha1_context sha1;
1901
Paul Bakker5b4af392014-06-26 12:09:34 +02001902 md5_init( &md5 );
1903 sha1_init( &sha1 );
1904
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001905 hashlen = 36;
1906
Paul Bakker29e1f122013-04-16 13:07:56 +02001907 /*
1908 * digitally-signed struct {
1909 * opaque md5_hash[16];
1910 * opaque sha_hash[20];
1911 * };
1912 *
1913 * md5_hash
1914 * MD5(ClientHello.random + ServerHello.random
1915 * + ServerParams);
1916 * sha_hash
1917 * SHA(ClientHello.random + ServerHello.random
1918 * + ServerParams);
1919 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001920 md5_starts( &md5 );
1921 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001922 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001923 md5_finish( &md5, hash );
1924
1925 sha1_starts( &sha1 );
1926 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001927 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001928 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001929
1930 md5_free( &md5 );
1931 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001932 }
1933 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001934#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1935 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001936#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1937 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001938 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001939 {
1940 md_context_t ctx;
1941
Paul Bakker84bbeb52014-07-01 14:53:22 +02001942 md_init( &ctx );
1943
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001944 /* Info from md_alg will be used instead */
1945 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001946
1947 /*
1948 * digitally-signed struct {
1949 * opaque client_random[32];
1950 * opaque server_random[32];
1951 * ServerDHParams params;
1952 * };
1953 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001954 if( ( ret = md_init_ctx( &ctx,
1955 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001956 {
1957 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1958 return( ret );
1959 }
1960
1961 md_starts( &ctx );
1962 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001963 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001964 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001965 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001966 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001967 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001968#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1969 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001970 {
Paul Bakker577e0062013-08-28 11:57:20 +02001971 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001972 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001973 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001974
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001975 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1976 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001977
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001978 /*
1979 * Verify signature
1980 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001981 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001982 {
1983 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1984 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1985 }
1986
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001987 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1988 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001989 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001990 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001991 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001992 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001993 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001994#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001995 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1996 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001998exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 ssl->state++;
2000
2001 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2002
2003 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004}
2005
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002006#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2007 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2008 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2009 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2010static int ssl_parse_certificate_request( ssl_context *ssl )
2011{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002012 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2013
2014 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2015
2016 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2017 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2018 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2019 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2020 {
2021 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2022 ssl->state++;
2023 return( 0 );
2024 }
2025
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002026 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2027 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002028}
2029#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002030static int ssl_parse_certificate_request( ssl_context *ssl )
2031{
2032 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002033 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002034 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002035 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002036 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002037
2038 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2039
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002040 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2041 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2042 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2043 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2044 {
2045 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2046 ssl->state++;
2047 return( 0 );
2048 }
2049
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 /*
2051 * 0 . 0 handshake type
2052 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002053 * 4 . 4 cert type count
2054 * 5 .. m-1 cert types
2055 * m .. m+1 sig alg length (TLS 1.2 only)
2056 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 * n .. n+1 length of all DNs
2058 * n+2 .. n+3 length of DN 1
2059 * n+4 .. ... Distinguished Name #1
2060 * ... .. ... length of DN 2, etc.
2061 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002062 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002064 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2065 {
2066 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2067 return( ret );
2068 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002070 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2071 {
2072 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2073 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2074 }
2075
2076 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 }
2078
2079 ssl->client_auth = 0;
2080 ssl->state++;
2081
2082 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2083 ssl->client_auth++;
2084
2085 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2086 ssl->client_auth ? "a" : "no" ) );
2087
Paul Bakker926af752012-11-23 13:38:07 +01002088 if( ssl->client_auth == 0 )
2089 goto exit;
2090
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002091 ssl->record_read = 0;
2092
Paul Bakker926af752012-11-23 13:38:07 +01002093 // TODO: handshake_failure alert for an anonymous server to request
2094 // client authentication
2095
2096 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002097
Paul Bakker926af752012-11-23 13:38:07 +01002098 // Retrieve cert types
2099 //
2100 cert_type_len = buf[4];
2101 n = cert_type_len;
2102
2103 if( ssl->in_hslen < 6 + n )
2104 {
2105 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2106 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2107 }
2108
Paul Bakker73d44312013-05-22 13:56:26 +02002109 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002110 while( cert_type_len > 0 )
2111 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002112#if defined(POLARSSL_RSA_C)
2113 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002114 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002115 {
2116 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2117 break;
2118 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002119 else
2120#endif
2121#if defined(POLARSSL_ECDSA_C)
2122 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002123 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002124 {
2125 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2126 break;
2127 }
2128 else
2129#endif
2130 {
2131 ; /* Unsupported cert type, ignore */
2132 }
Paul Bakker926af752012-11-23 13:38:07 +01002133
2134 cert_type_len--;
2135 p++;
2136 }
2137
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002138#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002139 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2140 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002141 /* Ignored, see comments about hash in write_certificate_verify */
2142 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002143 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2144 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002145
2146 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002147 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002148 n += sig_alg_len;
2149
2150 if( ssl->in_hslen < 6 + n )
2151 {
2152 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2153 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2154 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002155 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002156#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002157
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002158 /* Ignore certificate_authorities, we only have one cert anyway */
2159 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002160 dn_len = ( ( buf[5 + m + n] << 8 )
2161 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002162
2163 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002164 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002165 {
2166 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2167 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2168 }
2169
2170exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002171 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2172
2173 return( 0 );
2174}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002175#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2176 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2177 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2178 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002179
2180static int ssl_parse_server_hello_done( ssl_context *ssl )
2181{
2182 int ret;
2183
2184 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2185
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002186 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 {
2188 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2189 {
2190 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2191 return( ret );
2192 }
2193
2194 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2195 {
2196 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002197 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
2199 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002200 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002201
2202 if( ssl->in_hslen != 4 ||
2203 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2204 {
2205 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002206 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 }
2208
2209 ssl->state++;
2210
2211 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2212
2213 return( 0 );
2214}
2215
2216static int ssl_write_client_key_exchange( ssl_context *ssl )
2217{
Paul Bakker23986e52011-04-24 08:57:21 +00002218 int ret;
2219 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002220 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002221
2222 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2223
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002224#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002225 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 /*
2228 * DHM key exchange -- send G^X mod P
2229 */
Paul Bakker48916f92012-09-16 19:57:18 +00002230 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002231
2232 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2233 ssl->out_msg[5] = (unsigned char)( n );
2234 i = 6;
2235
Paul Bakker29b64762012-09-25 09:36:44 +00002236 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002237 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 &ssl->out_msg[i], n,
2239 ssl->f_rng, ssl->p_rng );
2240 if( ret != 0 )
2241 {
2242 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2243 return( ret );
2244 }
2245
Paul Bakker48916f92012-09-16 19:57:18 +00002246 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2247 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002249 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002250
Paul Bakker48916f92012-09-16 19:57:18 +00002251 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2252 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002253 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002254 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 {
2256 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2257 return( ret );
2258 }
2259
Paul Bakker48916f92012-09-16 19:57:18 +00002260 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002263#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002264#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002265 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2266 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2267 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002268 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002269 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2270 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2271 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002272 {
2273 /*
2274 * ECDH key exchange -- send client public value
2275 */
2276 i = 4;
2277
2278 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2279 &n,
2280 &ssl->out_msg[i], 1000,
2281 ssl->f_rng, ssl->p_rng );
2282 if( ret != 0 )
2283 {
2284 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2285 return( ret );
2286 }
2287
2288 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2289
2290 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2291 &ssl->handshake->pmslen,
2292 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002293 POLARSSL_MPI_MAX_SIZE,
2294 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002295 {
2296 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2297 return( ret );
2298 }
2299
2300 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2301 }
2302 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002303#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002304 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2305 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2306 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002307#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002308 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002309 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002310 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2311 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002312 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002313 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002314 * opaque psk_identity<0..2^16-1>;
2315 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002316 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002317 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2318
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002319 i = 4;
2320 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002321 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2322 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002323
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002324 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2325 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002326
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002327#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002328 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002329 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002330 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002331 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002332 else
2333#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002334#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2335 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2336 {
2337 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2338 return( ret );
2339 }
2340 else
2341#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002342#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002343 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002344 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002345 /*
2346 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2347 */
2348 n = ssl->handshake->dhm_ctx.len;
2349 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2350 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002351
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002352 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002353 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002354 &ssl->out_msg[i], n,
2355 ssl->f_rng, ssl->p_rng );
2356 if( ret != 0 )
2357 {
2358 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2359 return( ret );
2360 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002361 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002362 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002363#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002364#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002365 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002366 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002367 /*
2368 * ClientECDiffieHellmanPublic public;
2369 */
2370 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2371 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2372 ssl->f_rng, ssl->p_rng );
2373 if( ret != 0 )
2374 {
2375 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2376 return( ret );
2377 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002378
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002379 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2380 }
2381 else
2382#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2383 {
2384 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002385 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386 }
2387
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002388 if( ( ret = ssl_psk_derive_premaster( ssl,
2389 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002390 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002391 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002392 return( ret );
2393 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394 }
2395 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002396#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002397#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002398 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002400 i = 4;
2401 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002402 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 }
Paul Bakkered27a042013-04-18 22:46:23 +02002404 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002405#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002406 {
2407 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002408 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002409 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002411
Paul Bakkerff60ee62010-03-16 21:09:09 +00002412 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2413 {
2414 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2415 return( ret );
2416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
2418 ssl->out_msglen = i + n;
2419 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2420 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2421
2422 ssl->state++;
2423
2424 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2425 {
2426 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2427 return( ret );
2428 }
2429
2430 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2431
2432 return( 0 );
2433}
2434
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002435#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2436 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002437 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2438 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002439static int ssl_write_certificate_verify( ssl_context *ssl )
2440{
Paul Bakkered27a042013-04-18 22:46:23 +02002441 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
2443 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2444
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002445 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002446 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002447 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002448 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002449 {
2450 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2451 ssl->state++;
2452 return( 0 );
2453 }
2454
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002455 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2456 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002457}
2458#else
2459static int ssl_write_certificate_verify( ssl_context *ssl )
2460{
2461 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2462 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2463 size_t n = 0, offset = 0;
2464 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002465 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002466 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002467 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002468
2469 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2470
2471 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002472 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002473 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002474 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2475 {
2476 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2477 ssl->state++;
2478 return( 0 );
2479 }
2480
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002481 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 {
2483 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2484 ssl->state++;
2485 return( 0 );
2486 }
2487
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002488 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002490 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2491 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 }
2493
2494 /*
2495 * Make an RSA signature of the handshake digests
2496 */
Paul Bakker48916f92012-09-16 19:57:18 +00002497 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002499#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2500 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002501 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002502 {
Paul Bakker926af752012-11-23 13:38:07 +01002503 /*
2504 * digitally-signed struct {
2505 * opaque md5_hash[16];
2506 * opaque sha_hash[20];
2507 * };
2508 *
2509 * md5_hash
2510 * MD5(handshake_messages);
2511 *
2512 * sha_hash
2513 * SHA(handshake_messages);
2514 */
2515 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002516 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002517
2518 /*
2519 * For ECDSA, default hash is SHA-1 only
2520 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002521 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002522 {
2523 hash_start += 16;
2524 hashlen -= 16;
2525 md_alg = POLARSSL_MD_SHA1;
2526 }
Paul Bakker926af752012-11-23 13:38:07 +01002527 }
2528 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002529#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2530 POLARSSL_SSL_PROTO_TLS1_1 */
2531#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2532 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002533 {
2534 /*
2535 * digitally-signed struct {
2536 * opaque handshake_messages[handshake_messages_length];
2537 * };
2538 *
2539 * Taking shortcut here. We assume that the server always allows the
2540 * PRF Hash function and has sent it in the allowed signature
2541 * algorithms list received in the Certificate Request message.
2542 *
2543 * Until we encounter a server that does not, we will take this
2544 * shortcut.
2545 *
2546 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2547 * in order to satisfy 'weird' needs from the server side.
2548 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002549 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2550 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002551 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002552 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002553 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002554 }
2555 else
2556 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002557 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002558 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002559 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002560 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002561
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002562 /* Info from md_alg will be used instead */
2563 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002564 offset = 2;
2565 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002566 else
2567#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002568 {
2569 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002570 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002571 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002572
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002573 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002574 ssl->out_msg + 6 + offset, &n,
2575 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002576 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002577 SSL_DEBUG_RET( 1, "pk_sign", ret );
2578 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002579 }
Paul Bakker926af752012-11-23 13:38:07 +01002580
Paul Bakker1ef83d62012-04-11 12:09:53 +00002581 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2582 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
Paul Bakker1ef83d62012-04-11 12:09:53 +00002584 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2586 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
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 certificate verify" ) );
2597
Paul Bakkered27a042013-04-18 22:46:23 +02002598 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002600#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2601 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2602 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Paul Bakkera503a632013-08-14 13:48:06 +02002604#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002605static int ssl_parse_new_session_ticket( ssl_context *ssl )
2606{
2607 int ret;
2608 uint32_t lifetime;
2609 size_t ticket_len;
2610 unsigned char *ticket;
2611
2612 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2613
2614 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2615 {
2616 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2617 return( ret );
2618 }
2619
2620 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2621 {
2622 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2623 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2624 }
2625
2626 /*
2627 * struct {
2628 * uint32 ticket_lifetime_hint;
2629 * opaque ticket<0..2^16-1>;
2630 * } NewSessionTicket;
2631 *
2632 * 0 . 0 handshake message type
2633 * 1 . 3 handshake message length
2634 * 4 . 7 ticket_lifetime_hint
2635 * 8 . 9 ticket_len (n)
2636 * 10 . 9+n ticket content
2637 */
2638 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2639 ssl->in_hslen < 10 )
2640 {
2641 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2642 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2643 }
2644
2645 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2646 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2647
2648 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2649
2650 if( ticket_len + 10 != ssl->in_hslen )
2651 {
2652 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2653 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2654 }
2655
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002656 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2657
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002658 /* We're not waiting for a NewSessionTicket message any more */
2659 ssl->handshake->new_session_ticket = 0;
2660
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002661 /*
2662 * Zero-length ticket means the server changed his mind and doesn't want
2663 * to send a ticket after all, so just forget it
2664 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002665 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002666 return( 0 );
2667
Paul Bakker34617722014-06-13 17:20:13 +02002668 polarssl_zeroize( ssl->session_negotiate->ticket,
2669 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002670 polarssl_free( ssl->session_negotiate->ticket );
2671 ssl->session_negotiate->ticket = NULL;
2672 ssl->session_negotiate->ticket_len = 0;
2673
2674 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2675 {
2676 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2677 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2678 }
2679
2680 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2681
2682 ssl->session_negotiate->ticket = ticket;
2683 ssl->session_negotiate->ticket_len = ticket_len;
2684 ssl->session_negotiate->ticket_lifetime = lifetime;
2685
2686 /*
2687 * RFC 5077 section 3.4:
2688 * "If the client receives a session ticket from the server, then it
2689 * discards any Session ID that was sent in the ServerHello."
2690 */
2691 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2692 ssl->session_negotiate->length = 0;
2693
2694 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2695
2696 return( 0 );
2697}
Paul Bakkera503a632013-08-14 13:48:06 +02002698#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002699
Paul Bakker5121ce52009-01-03 21:22:43 +00002700/*
Paul Bakker1961b702013-01-25 14:49:24 +01002701 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 */
Paul Bakker1961b702013-01-25 14:49:24 +01002703int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704{
2705 int ret = 0;
2706
Paul Bakker1961b702013-01-25 14:49:24 +01002707 if( ssl->state == SSL_HANDSHAKE_OVER )
2708 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Paul Bakker1961b702013-01-25 14:49:24 +01002710 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2711
2712 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2713 return( ret );
2714
2715 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 {
Paul Bakker1961b702013-01-25 14:49:24 +01002717 case SSL_HELLO_REQUEST:
2718 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 break;
2720
Paul Bakker1961b702013-01-25 14:49:24 +01002721 /*
2722 * ==> ClientHello
2723 */
2724 case SSL_CLIENT_HELLO:
2725 ret = ssl_write_client_hello( ssl );
2726 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Paul Bakker1961b702013-01-25 14:49:24 +01002728 /*
2729 * <== ServerHello
2730 * Certificate
2731 * ( ServerKeyExchange )
2732 * ( CertificateRequest )
2733 * ServerHelloDone
2734 */
2735 case SSL_SERVER_HELLO:
2736 ret = ssl_parse_server_hello( ssl );
2737 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
Paul Bakker1961b702013-01-25 14:49:24 +01002739 case SSL_SERVER_CERTIFICATE:
2740 ret = ssl_parse_certificate( ssl );
2741 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Paul Bakker1961b702013-01-25 14:49:24 +01002743 case SSL_SERVER_KEY_EXCHANGE:
2744 ret = ssl_parse_server_key_exchange( ssl );
2745 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
Paul Bakker1961b702013-01-25 14:49:24 +01002747 case SSL_CERTIFICATE_REQUEST:
2748 ret = ssl_parse_certificate_request( ssl );
2749 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Paul Bakker1961b702013-01-25 14:49:24 +01002751 case SSL_SERVER_HELLO_DONE:
2752 ret = ssl_parse_server_hello_done( ssl );
2753 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002754
Paul Bakker1961b702013-01-25 14:49:24 +01002755 /*
2756 * ==> ( Certificate/Alert )
2757 * ClientKeyExchange
2758 * ( CertificateVerify )
2759 * ChangeCipherSpec
2760 * Finished
2761 */
2762 case SSL_CLIENT_CERTIFICATE:
2763 ret = ssl_write_certificate( ssl );
2764 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
Paul Bakker1961b702013-01-25 14:49:24 +01002766 case SSL_CLIENT_KEY_EXCHANGE:
2767 ret = ssl_write_client_key_exchange( ssl );
2768 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
Paul Bakker1961b702013-01-25 14:49:24 +01002770 case SSL_CERTIFICATE_VERIFY:
2771 ret = ssl_write_certificate_verify( ssl );
2772 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Paul Bakker1961b702013-01-25 14:49:24 +01002774 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2775 ret = ssl_write_change_cipher_spec( ssl );
2776 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
Paul Bakker1961b702013-01-25 14:49:24 +01002778 case SSL_CLIENT_FINISHED:
2779 ret = ssl_write_finished( ssl );
2780 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Paul Bakker1961b702013-01-25 14:49:24 +01002782 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002783 * <== ( NewSessionTicket )
2784 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002785 * Finished
2786 */
2787 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002788#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002789 if( ssl->handshake->new_session_ticket != 0 )
2790 ret = ssl_parse_new_session_ticket( ssl );
2791 else
Paul Bakkera503a632013-08-14 13:48:06 +02002792#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002793 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002794 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
Paul Bakker1961b702013-01-25 14:49:24 +01002796 case SSL_SERVER_FINISHED:
2797 ret = ssl_parse_finished( ssl );
2798 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
Paul Bakker1961b702013-01-25 14:49:24 +01002800 case SSL_FLUSH_BUFFERS:
2801 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2802 ssl->state = SSL_HANDSHAKE_WRAPUP;
2803 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
Paul Bakker1961b702013-01-25 14:49:24 +01002805 case SSL_HANDSHAKE_WRAPUP:
2806 ssl_handshake_wrapup( ssl );
2807 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002808
Paul Bakker1961b702013-01-25 14:49:24 +01002809 default:
2810 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2811 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2812 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
2814 return( ret );
2815}
Paul Bakker9af723c2014-05-01 13:03:14 +02002816#endif /* POLARSSL_SSL_CLI_C */