blob: 9acc1e1347f6155dd31d21b4de6db903a5a74d74 [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;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200453#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200455#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200456 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200457 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
460
Paul Bakkera9a028e2013-11-21 17:31:06 +0100461 if( ssl->f_rng == NULL )
462 {
463 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
464 return( POLARSSL_ERR_SSL_NO_RNG );
465 }
466
Paul Bakker48916f92012-09-16 19:57:18 +0000467 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
468 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000469 ssl->major_ver = ssl->min_major_ver;
470 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000471 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Paul Bakker490ecc82011-10-06 13:04:09 +0000473 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
474 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200475 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
476 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000477 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479 /*
480 * 0 . 0 handshake type
481 * 1 . 3 handshake length
482 * 4 . 5 highest version supported
483 * 6 . 9 current UNIX time
484 * 10 . 37 random bytes
485 */
486 buf = ssl->out_msg;
487 p = buf + 4;
488
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100489 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
490 ssl->transport, p );
491 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
494 buf[4], buf[5] ) );
495
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200496#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 t = time( NULL );
498 *p++ = (unsigned char)( t >> 24 );
499 *p++ = (unsigned char)( t >> 16 );
500 *p++ = (unsigned char)( t >> 8 );
501 *p++ = (unsigned char)( t );
502
503 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200504#else
505 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
506 return( ret );
507
508 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200509#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Paul Bakkera3d195c2011-11-27 21:07:34 +0000511 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
512 return( ret );
513
514 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Paul Bakker48916f92012-09-16 19:57:18 +0000516 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
519
520 /*
521 * 38 . 38 session id length
522 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100523 * 39+n . 39+n DTLS only: cookie length (1 byte)
524 * 40+n . .. DTSL only: cookie
525 * .. . .. ciphersuitelist length (2 bytes)
526 * .. . .. ciphersuitelist
527 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000528 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100529 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000530 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 */
Paul Bakker48916f92012-09-16 19:57:18 +0000532 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
Paul Bakker0a597072012-09-25 21:55:46 +0000534 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
535 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200536 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200538 }
539
Paul Bakkera503a632013-08-14 13:48:06 +0200540#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200541 /*
542 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
543 * generate and include a Session ID in the TLS ClientHello."
544 */
545 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
546 ssl->session_negotiate->ticket != NULL &&
547 ssl->session_negotiate->ticket_len != 0 )
548 {
549 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
550
551 if( ret != 0 )
552 return( ret );
553
554 ssl->session_negotiate->length = n = 32;
555 }
Paul Bakkera503a632013-08-14 13:48:06 +0200556#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 *p++ = (unsigned char) n;
559
560 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000561 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
563 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
564 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
565
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100566 /*
567 * DTLS cookie
568 */
569#if defined(POLARSSL_SSL_PROTO_DTLS)
570 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
571 {
572 /* TODO-DTLS: for now, just send an empty cookie, later on must send
573 * back the cookie from HelloVerifyRequest */
574 *p++ = 0;
575 }
576#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Paul Bakker48916f92012-09-16 19:57:18 +0000578 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100579 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000580 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100581 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
582
583 /* Skip writing ciphersuite length for now */
584 n = 0;
585 q = p;
586 p += 2;
587
588 /* Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
Paul Bakker48916f92012-09-16 19:57:18 +0000589 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
590 {
591 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
592 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200593 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000594 }
595
Paul Bakker2fbefde2013-06-29 16:01:15 +0200596 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200598 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
599
600 if( ciphersuite_info == NULL )
601 continue;
602
603 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
604 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
605 continue;
606
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100607#if defined(POLARSSL_SSL_PROTO_DTLS)
608 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
609 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
610 continue;
611#endif
612
Paul Bakkere3166ce2011-01-27 17:40:50 +0000613 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200614 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Paul Bakker2fbefde2013-06-29 16:01:15 +0200616 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200617 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
618 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 }
620
Paul Bakker2fbefde2013-06-29 16:01:15 +0200621 *q++ = (unsigned char)( n >> 7 );
622 *q++ = (unsigned char)( n << 1 );
623
624 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
625
626
Paul Bakker2770fbd2012-07-03 13:30:23 +0000627#if defined(POLARSSL_ZLIB_SUPPORT)
628 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
629 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000630 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000631
632 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000633 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000634 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000635#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000637 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
639 *p++ = 1;
640 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200641#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
Paul Bakkerd3edc862013-03-20 16:07:17 +0100643 // First write extensions, then the total length
644 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200645#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100646 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
647 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200648#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Paul Bakkerd3edc862013-03-20 16:07:17 +0100650 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
651 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000652
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200653#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100654 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
655 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200656#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000657
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200658#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100659 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
660 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100661
Paul Bakkerd3edc862013-03-20 16:07:17 +0100662 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
663 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100664#endif
665
Paul Bakker05decb22013-08-15 13:33:48 +0200666#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200667 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
668 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200669#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200670
Paul Bakker1f2bc622013-08-15 13:45:55 +0200671#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200672 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
673 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200674#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200675
Paul Bakkera503a632013-08-14 13:48:06 +0200676#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200677 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
678 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200679#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200680
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200681#if defined(POLARSSL_SSL_ALPN)
682 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
683 ext_len += olen;
684#endif
685
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000686 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
687 ext_len ) );
688
Paul Bakkera7036632014-04-30 10:15:38 +0200689 if( ext_len > 0 )
690 {
691 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
692 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
693 p += ext_len;
694 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100695
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 ssl->out_msglen = p - buf;
697 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
698 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
699
700 ssl->state++;
701
702 if( ( ret = ssl_write_record( ssl ) ) != 0 )
703 {
704 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
705 return( ret );
706 }
707
708 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
709
710 return( 0 );
711}
712
Paul Bakker48916f92012-09-16 19:57:18 +0000713static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200714 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000715 size_t len )
716{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000717 int ret;
718
Paul Bakker48916f92012-09-16 19:57:18 +0000719 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
720 {
721 if( len != 1 || buf[0] != 0x0 )
722 {
723 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000724
725 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
726 return( ret );
727
Paul Bakker48916f92012-09-16 19:57:18 +0000728 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
729 }
730
731 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
732 }
733 else
734 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100735 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000736 if( len != 1 + ssl->verify_data_len * 2 ||
737 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100738 safer_memcmp( buf + 1,
739 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
740 safer_memcmp( buf + 1 + ssl->verify_data_len,
741 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000742 {
743 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000744
745 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
746 return( ret );
747
Paul Bakker48916f92012-09-16 19:57:18 +0000748 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
749 }
750 }
751
752 return( 0 );
753}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200754
Paul Bakker05decb22013-08-15 13:33:48 +0200755#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200756static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200757 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200758 size_t len )
759{
760 /*
761 * server should use the extension only if we did,
762 * and if so the server's value should match ours (and len is always 1)
763 */
764 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
765 len != 1 ||
766 buf[0] != ssl->mfl_code )
767 {
768 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
769 }
770
771 return( 0 );
772}
Paul Bakker05decb22013-08-15 13:33:48 +0200773#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000774
Paul Bakker1f2bc622013-08-15 13:45:55 +0200775#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200776static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
777 const unsigned char *buf,
778 size_t len )
779{
780 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
781 len != 0 )
782 {
783 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
784 }
785
786 ((void) buf);
787
788 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
789
790 return( 0 );
791}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200792#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200793
Paul Bakkera503a632013-08-14 13:48:06 +0200794#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200795static int ssl_parse_session_ticket_ext( ssl_context *ssl,
796 const unsigned char *buf,
797 size_t len )
798{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200799 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
800 len != 0 )
801 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200802 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200803 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200804
805 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200806
807 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200808
809 return( 0 );
810}
Paul Bakkera503a632013-08-14 13:48:06 +0200811#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200812
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200813#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200814static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
815 const unsigned char *buf,
816 size_t len )
817{
818 size_t list_size;
819 const unsigned char *p;
820
821 list_size = buf[0];
822 if( list_size + 1 != len )
823 {
824 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
825 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
826 }
827
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200828 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200829 while( list_size > 0 )
830 {
831 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
832 p[0] == POLARSSL_ECP_PF_COMPRESSED )
833 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200834 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200835 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
836 return( 0 );
837 }
838
839 list_size--;
840 p++;
841 }
842
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200843 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
844 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200845}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200846#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200847
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200848#if defined(POLARSSL_SSL_ALPN)
849static int ssl_parse_alpn_ext( ssl_context *ssl,
850 const unsigned char *buf, size_t len )
851{
852 size_t list_len, name_len;
853 const char **p;
854
855 /* If we didn't send it, the server shouldn't send it */
856 if( ssl->alpn_list == NULL )
857 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
858
859 /*
860 * opaque ProtocolName<1..2^8-1>;
861 *
862 * struct {
863 * ProtocolName protocol_name_list<2..2^16-1>
864 * } ProtocolNameList;
865 *
866 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
867 */
868
869 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
870 if( len < 4 )
871 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
872
873 list_len = ( buf[0] << 8 ) | buf[1];
874 if( list_len != len - 2 )
875 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
876
877 name_len = buf[2];
878 if( name_len != list_len - 1 )
879 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
880
881 /* Check that the server chosen protocol was in our list and save it */
882 for( p = ssl->alpn_list; *p != NULL; p++ )
883 {
884 if( name_len == strlen( *p ) &&
885 memcmp( buf + 3, *p, name_len ) == 0 )
886 {
887 ssl->alpn_chosen = *p;
888 return( 0 );
889 }
890 }
891
892 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
893}
894#endif /* POLARSSL_SSL_ALPN */
895
Paul Bakker5121ce52009-01-03 21:22:43 +0000896static int ssl_parse_server_hello( ssl_context *ssl )
897{
Paul Bakker2770fbd2012-07-03 13:30:23 +0000898 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +0000899 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +0200900 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000901 unsigned char *buf, *ext;
902 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000903 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200904#if defined(POLARSSL_DEBUG_C)
905 uint32_t t;
906#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
908 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
909
910 /*
911 * 0 . 0 handshake type
912 * 1 . 3 handshake length
913 * 4 . 5 protocol version
914 * 6 . 9 UNIX time()
915 * 10 . 37 random bytes
916 */
917 buf = ssl->in_msg;
918
919 if( ( ret = ssl_read_record( ssl ) ) != 0 )
920 {
921 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
922 return( ret );
923 }
924
925 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
926 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +0200927 if( ssl->renegotiation == SSL_RENEGOTIATION )
928 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +0200929 ssl->renego_records_seen++;
930
931 if( ssl->renego_max_records >= 0 &&
932 ssl->renego_records_seen > ssl->renego_max_records )
933 {
934 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
935 "but not honored by server" ) );
936 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
937 }
938
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +0200939 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
940 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
941 }
942
Paul Bakker5121ce52009-01-03 21:22:43 +0000943 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000944 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000945 }
946
947 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
948 buf[4], buf[5] ) );
949
950 if( ssl->in_hslen < 42 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100951 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 {
953 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000954 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 }
956
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100957 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
958 ssl->transport, buf + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100960 if( ssl->major_ver < ssl->min_major_ver ||
961 ssl->minor_ver < ssl->min_minor_ver ||
962 ssl->major_ver > ssl->max_major_ver ||
963 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000964 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100965 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
966 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
967 ssl->min_major_ver, ssl->min_minor_ver,
968 ssl->major_ver, ssl->minor_ver,
969 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000970
971 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
972 SSL_ALERT_MSG_PROTOCOL_VERSION );
973
974 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
975 }
976
Paul Bakker1504af52012-02-11 16:17:43 +0000977#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200978 t = ( (uint32_t) buf[6] << 24 )
979 | ( (uint32_t) buf[7] << 16 )
980 | ( (uint32_t) buf[8] << 8 )
981 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200982 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +0000983#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
Paul Bakker48916f92012-09-16 19:57:18 +0000985 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
987 n = buf[38];
988
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
990
Paul Bakker48916f92012-09-16 19:57:18 +0000991 if( n > 32 )
992 {
993 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
994 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
995 }
996
Paul Bakker5121ce52009-01-03 21:22:43 +0000997 /*
998 * 38 . 38 session id length
999 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001000 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 * 41+n . 41+n chosen compression alg.
1002 * 42+n . 43+n extensions length
1003 * 44+n . 44+n+m extensions
1004 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001005 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 {
1007 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001008 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009
Paul Bakker48916f92012-09-16 19:57:18 +00001010 if( ( ext_len > 0 && ext_len < 4 ) ||
1011 ssl->in_hslen != 44 + n + ext_len )
1012 {
1013 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1014 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1015 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001016 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001017 else if( ssl->in_hslen == 42 + n )
1018 {
1019 ext_len = 0;
1020 }
1021 else
1022 {
1023 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1024 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1025 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
1027 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001028 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
Paul Bakker380da532012-04-18 16:10:25 +00001030 /*
1031 * Initialize update checksum functions
1032 */
Paul Bakker68884e32013-01-07 18:20:04 +01001033 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1034
1035 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1036 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001037 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001038 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1039 }
Paul Bakker380da532012-04-18 16:10:25 +00001040
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001041 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1042
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1044 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1045
1046 /*
1047 * Check if the session can be resumed
1048 */
Paul Bakker0a597072012-09-25 21:55:46 +00001049 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1050 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +00001051 ssl->session_negotiate->ciphersuite != i ||
1052 ssl->session_negotiate->compression != comp ||
1053 ssl->session_negotiate->length != n ||
1054 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 {
1056 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001057 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001058#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001059 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001060#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001061 ssl->session_negotiate->ciphersuite = i;
1062 ssl->session_negotiate->compression = comp;
1063 ssl->session_negotiate->length = n;
1064 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 }
1066 else
1067 {
1068 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001069
1070 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1071 {
1072 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1073 return( ret );
1074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 }
1076
1077 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001078 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Paul Bakkere3166ce2011-01-27 17:40:50 +00001080 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1082
1083 i = 0;
1084 while( 1 )
1085 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001086 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 {
1088 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001089 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090 }
1091
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001092 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1093 ssl->session_negotiate->ciphersuite )
1094 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001096 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 }
1098
Paul Bakker2770fbd2012-07-03 13:30:23 +00001099 if( comp != SSL_COMPRESS_NULL
1100#if defined(POLARSSL_ZLIB_SUPPORT)
1101 && comp != SSL_COMPRESS_DEFLATE
1102#endif
1103 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 {
1105 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001106 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 }
Paul Bakker48916f92012-09-16 19:57:18 +00001108 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
Paul Bakker48916f92012-09-16 19:57:18 +00001110 ext = buf + 44 + n;
1111
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001112 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1113
Paul Bakker48916f92012-09-16 19:57:18 +00001114 while( ext_len )
1115 {
1116 unsigned int ext_id = ( ( ext[0] << 8 )
1117 | ( ext[1] ) );
1118 unsigned int ext_size = ( ( ext[2] << 8 )
1119 | ( ext[3] ) );
1120
1121 if( ext_size + 4 > ext_len )
1122 {
1123 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1124 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1125 }
1126
1127 switch( ext_id )
1128 {
1129 case TLS_EXT_RENEGOTIATION_INFO:
1130 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1131 renegotiation_info_seen = 1;
1132
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001133 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1134 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001135 return( ret );
1136
1137 break;
1138
Paul Bakker05decb22013-08-15 13:33:48 +02001139#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001140 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1141 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1142
1143 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1144 ext + 4, ext_size ) ) != 0 )
1145 {
1146 return( ret );
1147 }
1148
1149 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001150#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001151
Paul Bakker1f2bc622013-08-15 13:45:55 +02001152#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001153 case TLS_EXT_TRUNCATED_HMAC:
1154 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1155
1156 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1157 ext + 4, ext_size ) ) != 0 )
1158 {
1159 return( ret );
1160 }
1161
1162 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001163#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001164
Paul Bakkera503a632013-08-14 13:48:06 +02001165#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001166 case TLS_EXT_SESSION_TICKET:
1167 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1168
1169 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1170 ext + 4, ext_size ) ) != 0 )
1171 {
1172 return( ret );
1173 }
1174
1175 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001176#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001177
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001178#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001179 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1180 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1181
1182 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1183 ext + 4, ext_size ) ) != 0 )
1184 {
1185 return( ret );
1186 }
1187
1188 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001189#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001190
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001191#if defined(POLARSSL_SSL_ALPN)
1192 case TLS_EXT_ALPN:
1193 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1194
1195 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1196 return( ret );
1197
1198 break;
1199#endif /* POLARSSL_SSL_ALPN */
1200
Paul Bakker48916f92012-09-16 19:57:18 +00001201 default:
1202 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1203 ext_id ) );
1204 }
1205
1206 ext_len -= 4 + ext_size;
1207 ext += 4 + ext_size;
1208
1209 if( ext_len > 0 && ext_len < 4 )
1210 {
1211 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1212 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1213 }
1214 }
1215
1216 /*
1217 * Renegotiation security checks
1218 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001219 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1220 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001221 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001222 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1223 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001224 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001225 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1226 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1227 renegotiation_info_seen == 0 )
1228 {
1229 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1230 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001231 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001232 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1233 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1234 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001235 {
1236 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001237 handshake_failure = 1;
1238 }
1239 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1240 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1241 renegotiation_info_seen == 1 )
1242 {
1243 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1244 handshake_failure = 1;
1245 }
1246
1247 if( handshake_failure == 1 )
1248 {
1249 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1250 return( ret );
1251
Paul Bakker48916f92012-09-16 19:57:18 +00001252 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1253 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
1255 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1256
1257 return( 0 );
1258}
1259
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001260#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1261 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001262static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1263 unsigned char *end )
1264{
1265 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1266
Paul Bakker29e1f122013-04-16 13:07:56 +02001267 /*
1268 * Ephemeral DH parameters:
1269 *
1270 * struct {
1271 * opaque dh_p<1..2^16-1>;
1272 * opaque dh_g<1..2^16-1>;
1273 * opaque dh_Ys<1..2^16-1>;
1274 * } ServerDHParams;
1275 */
1276 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1277 {
1278 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1279 return( ret );
1280 }
1281
1282 if( ssl->handshake->dhm_ctx.len < 64 ||
1283 ssl->handshake->dhm_ctx.len > 512 )
1284 {
1285 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1286 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1287 }
1288
1289 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1290 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1291 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001292
1293 return( ret );
1294}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001295#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1296 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001297
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001298#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001299 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001300 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1301 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1302 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1303static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1304{
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001305 const ecp_curve_info *curve_info;
1306
1307 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1308 if( curve_info == NULL )
1309 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001310 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1311 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001312 }
1313
1314 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001315
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001316#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1317 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1318#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001319 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1320 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001321#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001322 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001323
1324 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1325
1326 return( 0 );
1327}
Paul Bakker9af723c2014-05-01 13:03:14 +02001328#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1329 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1330 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1331 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1332 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001333
1334#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1335 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001336 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001337static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1338 unsigned char **p,
1339 unsigned char *end )
1340{
1341 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1342
Paul Bakker29e1f122013-04-16 13:07:56 +02001343 /*
1344 * Ephemeral ECDH parameters:
1345 *
1346 * struct {
1347 * ECParameters curve_params;
1348 * ECPoint public;
1349 * } ServerECDHParams;
1350 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001351 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1352 (const unsigned char **) p, end ) ) != 0 )
1353 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001354 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001355 return( ret );
1356 }
1357
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001358 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001359 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001360 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001361 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1362 }
1363
Paul Bakker29e1f122013-04-16 13:07:56 +02001364 return( ret );
1365}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001366#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001367 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1368 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001369
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001370#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001371static int ssl_parse_server_psk_hint( ssl_context *ssl,
1372 unsigned char **p,
1373 unsigned char *end )
1374{
1375 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001376 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001377 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001378
1379 /*
1380 * PSK parameters:
1381 *
1382 * opaque psk_identity_hint<0..2^16-1>;
1383 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001384 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001385 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001386
1387 if( (*p) + len > end )
1388 {
1389 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1390 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1391 }
1392
1393 // TODO: Retrieve PSK identity hint and callback to app
1394 //
1395 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001396 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001397
1398 return( ret );
1399}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001400#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001401
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001402#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1403 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1404/*
1405 * Generate a pre-master secret and encrypt it with the server's RSA key
1406 */
1407static int ssl_write_encrypted_pms( ssl_context *ssl,
1408 size_t offset, size_t *olen,
1409 size_t pms_offset )
1410{
1411 int ret;
1412 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1413 unsigned char *p = ssl->handshake->premaster + pms_offset;
1414
1415 /*
1416 * Generate (part of) the pre-master as
1417 * struct {
1418 * ProtocolVersion client_version;
1419 * opaque random[46];
1420 * } PreMasterSecret;
1421 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001422 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1423 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001424
1425 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1426 {
1427 SSL_DEBUG_RET( 1, "f_rng", ret );
1428 return( ret );
1429 }
1430
1431 ssl->handshake->pmslen = 48;
1432
1433 /*
1434 * Now write it out, encrypted
1435 */
1436 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1437 POLARSSL_PK_RSA ) )
1438 {
1439 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1440 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1441 }
1442
1443 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1444 p, ssl->handshake->pmslen,
1445 ssl->out_msg + offset + len_bytes, olen,
1446 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1447 ssl->f_rng, ssl->p_rng ) ) != 0 )
1448 {
1449 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1450 return( ret );
1451 }
1452
1453#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1454 defined(POLARSSL_SSL_PROTO_TLS1_2)
1455 if( len_bytes == 2 )
1456 {
1457 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1458 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1459 *olen += 2;
1460 }
1461#endif
1462
1463 return( 0 );
1464}
1465#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1466 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001467
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001468#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001469#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001470 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1471 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001472static int ssl_parse_signature_algorithm( ssl_context *ssl,
1473 unsigned char **p,
1474 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001475 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001476 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001477{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001478 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001479 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001480 *pk_alg = POLARSSL_PK_NONE;
1481
1482 /* Only in TLS 1.2 */
1483 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1484 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001485 return( 0 );
1486 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001487
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001488 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001489 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1490
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001491 /*
1492 * Get hash algorithm
1493 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001494 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001495 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001496 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1497 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001498 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1499 }
1500
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001501 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001502 * Get signature algorithm
1503 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001504 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001505 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001506 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1507 "SignatureAlgorithm %d", (*p)[1] ) );
1508 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001509 }
1510
1511 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1512 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1513 *p += 2;
1514
1515 return( 0 );
1516}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001517#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001518 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1519 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001520#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001521
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001522
1523#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1524 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1525static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1526{
1527 int ret;
1528 const ecp_keypair *peer_key;
1529
1530 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1531 POLARSSL_PK_ECKEY ) )
1532 {
1533 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1534 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1535 }
1536
1537 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1538
1539 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1540 POLARSSL_ECDH_THEIRS ) ) != 0 )
1541 {
1542 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1543 return( ret );
1544 }
1545
1546 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1547 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001548 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001549 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1550 }
1551
1552 return( ret );
1553}
1554#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1555 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1556
Paul Bakker41c83d32013-03-20 14:39:14 +01001557static int ssl_parse_server_key_exchange( ssl_context *ssl )
1558{
Paul Bakker23986e52011-04-24 08:57:21 +00001559 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001560 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001561 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001562#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001563 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1564 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001565 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001566 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001567 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001568 size_t hashlen;
1569 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
1572 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1573
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001574#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001575 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001576 {
1577 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1578 ssl->state++;
1579 return( 0 );
1580 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001581 ((void) p);
1582 ((void) end);
1583#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001584
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001585#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1586 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1587 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1588 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1589 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001590 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1591 {
1592 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1593 return( ret );
1594 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001595
1596 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1597 ssl->state++;
1598 return( 0 );
1599 }
1600 ((void) p);
1601 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001602#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1603 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001604
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1606 {
1607 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1608 return( ret );
1609 }
1610
1611 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1612 {
1613 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001614 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 }
1616
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001617 /*
1618 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1619 * doesn't use a psk_identity_hint
1620 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1622 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001623 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1624 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001625 {
1626 ssl->record_read = 1;
1627 goto exit;
1628 }
1629
1630 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1631 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 }
1633
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001634 p = ssl->in_msg + 4;
1635 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001636 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001637
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001638#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1639 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1640 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1641 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1642 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1643 {
1644 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1645 {
1646 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1647 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1648 }
1649 } /* FALLTROUGH */
1650#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1651
1652#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1653 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1654 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1655 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1656 ; /* nothing more to do */
1657 else
1658#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1659 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1660#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1661 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1662 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1663 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001665 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001666 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001667 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001668 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1669 }
1670 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001671 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001672#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1673 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001674#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001675 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001676 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1677 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001678 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001679 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001680 {
1681 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1682 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001683 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1684 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1685 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001686 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001687 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001688#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001689 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001690 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001691 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001692 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001693 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001694 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001695
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001696#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001697 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1698 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001699 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001700 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1701 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001702 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001703 params_len = p - ( ssl->in_msg + 4 );
1704
Paul Bakker29e1f122013-04-16 13:07:56 +02001705 /*
1706 * Handle the digitally-signed structure
1707 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001708#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1709 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001710 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001711 if( ssl_parse_signature_algorithm( ssl, &p, end,
1712 &md_alg, &pk_alg ) != 0 )
1713 {
1714 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1715 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1716 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001717
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001718 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001719 {
1720 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1721 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1722 }
1723 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001724 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001725#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001726#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1727 defined(POLARSSL_SSL_PROTO_TLS1_1)
1728 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001729 {
1730 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001731
Paul Bakker9659dae2013-08-28 16:21:34 +02001732 /* Default hash for ECDSA is SHA-1 */
1733 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1734 md_alg = POLARSSL_MD_SHA1;
1735 }
1736 else
1737#endif
1738 {
1739 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001740 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001741 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001742
1743 /*
1744 * Read signature
1745 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001746 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001747 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001748
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001749 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001750 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001751 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001752 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1753 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001755 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001756
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001757 /*
1758 * Compute the hash that has been signed
1759 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001760#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1761 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001762 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001763 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001764 md5_context md5;
1765 sha1_context sha1;
1766
Paul Bakker5b4af392014-06-26 12:09:34 +02001767 md5_init( &md5 );
1768 sha1_init( &sha1 );
1769
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001770 hashlen = 36;
1771
Paul Bakker29e1f122013-04-16 13:07:56 +02001772 /*
1773 * digitally-signed struct {
1774 * opaque md5_hash[16];
1775 * opaque sha_hash[20];
1776 * };
1777 *
1778 * md5_hash
1779 * MD5(ClientHello.random + ServerHello.random
1780 * + ServerParams);
1781 * sha_hash
1782 * SHA(ClientHello.random + ServerHello.random
1783 * + ServerParams);
1784 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001785 md5_starts( &md5 );
1786 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001787 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001788 md5_finish( &md5, hash );
1789
1790 sha1_starts( &sha1 );
1791 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001792 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001793 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001794
1795 md5_free( &md5 );
1796 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001797 }
1798 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001799#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1800 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001801#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1802 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001803 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001804 {
1805 md_context_t ctx;
1806
Paul Bakker84bbeb52014-07-01 14:53:22 +02001807 md_init( &ctx );
1808
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001809 /* Info from md_alg will be used instead */
1810 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001811
1812 /*
1813 * digitally-signed struct {
1814 * opaque client_random[32];
1815 * opaque server_random[32];
1816 * ServerDHParams params;
1817 * };
1818 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001819 if( ( ret = md_init_ctx( &ctx,
1820 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001821 {
1822 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1823 return( ret );
1824 }
1825
1826 md_starts( &ctx );
1827 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001828 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001829 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001830 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001831 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001832 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001833#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1834 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001835 {
Paul Bakker577e0062013-08-28 11:57:20 +02001836 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001837 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001838 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001839
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001840 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1841 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001842
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001843 /*
1844 * Verify signature
1845 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001846 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001847 {
1848 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1849 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1850 }
1851
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001852 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1853 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001854 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001855 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001856 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001857 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001858 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001859#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001860 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1861 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001863exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 ssl->state++;
1865
1866 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
1867
1868 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001869}
1870
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001871#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1872 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1873 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1874 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1875static int ssl_parse_certificate_request( ssl_context *ssl )
1876{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001877 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
1878
1879 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1880
1881 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1882 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1883 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1884 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1885 {
1886 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
1887 ssl->state++;
1888 return( 0 );
1889 }
1890
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001891 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1892 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001893}
1894#else
Paul Bakker5121ce52009-01-03 21:22:43 +00001895static int ssl_parse_certificate_request( ssl_context *ssl )
1896{
1897 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01001898 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001899 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001900 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001901 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001902
1903 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1904
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001905 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1906 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1907 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1908 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1909 {
1910 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
1911 ssl->state++;
1912 return( 0 );
1913 }
1914
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 /*
1916 * 0 . 0 handshake type
1917 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01001918 * 4 . 4 cert type count
1919 * 5 .. m-1 cert types
1920 * m .. m+1 sig alg length (TLS 1.2 only)
1921 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 * n .. n+1 length of all DNs
1923 * n+2 .. n+3 length of DN 1
1924 * n+4 .. ... Distinguished Name #1
1925 * ... .. ... length of DN 2, etc.
1926 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001927 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001929 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1930 {
1931 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1932 return( ret );
1933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001934
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001935 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1936 {
1937 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1938 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1939 }
1940
1941 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 }
1943
1944 ssl->client_auth = 0;
1945 ssl->state++;
1946
1947 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
1948 ssl->client_auth++;
1949
1950 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
1951 ssl->client_auth ? "a" : "no" ) );
1952
Paul Bakker926af752012-11-23 13:38:07 +01001953 if( ssl->client_auth == 0 )
1954 goto exit;
1955
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001956 ssl->record_read = 0;
1957
Paul Bakker926af752012-11-23 13:38:07 +01001958 // TODO: handshake_failure alert for an anonymous server to request
1959 // client authentication
1960
1961 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001962
Paul Bakker926af752012-11-23 13:38:07 +01001963 // Retrieve cert types
1964 //
1965 cert_type_len = buf[4];
1966 n = cert_type_len;
1967
1968 if( ssl->in_hslen < 6 + n )
1969 {
1970 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1971 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1972 }
1973
Paul Bakker73d44312013-05-22 13:56:26 +02001974 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01001975 while( cert_type_len > 0 )
1976 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001977#if defined(POLARSSL_RSA_C)
1978 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001979 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01001980 {
1981 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
1982 break;
1983 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001984 else
1985#endif
1986#if defined(POLARSSL_ECDSA_C)
1987 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001988 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001989 {
1990 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
1991 break;
1992 }
1993 else
1994#endif
1995 {
1996 ; /* Unsupported cert type, ignore */
1997 }
Paul Bakker926af752012-11-23 13:38:07 +01001998
1999 cert_type_len--;
2000 p++;
2001 }
2002
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002003#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002004 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2005 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002006 /* Ignored, see comments about hash in write_certificate_verify */
2007 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002008 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2009 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002010
2011 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002012 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002013 n += sig_alg_len;
2014
2015 if( ssl->in_hslen < 6 + n )
2016 {
2017 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2018 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2019 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002020 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002021#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002022
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002023 /* Ignore certificate_authorities, we only have one cert anyway */
2024 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002025 dn_len = ( ( buf[5 + m + n] << 8 )
2026 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002027
2028 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002029 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002030 {
2031 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2032 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2033 }
2034
2035exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2037
2038 return( 0 );
2039}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002040#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2041 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2042 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2043 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002044
2045static int ssl_parse_server_hello_done( ssl_context *ssl )
2046{
2047 int ret;
2048
2049 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2050
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002051 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 {
2053 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2054 {
2055 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2056 return( ret );
2057 }
2058
2059 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2060 {
2061 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002062 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 }
2064 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002065 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002066
2067 if( ssl->in_hslen != 4 ||
2068 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2069 {
2070 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002071 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 }
2073
2074 ssl->state++;
2075
2076 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2077
2078 return( 0 );
2079}
2080
2081static int ssl_write_client_key_exchange( ssl_context *ssl )
2082{
Paul Bakker23986e52011-04-24 08:57:21 +00002083 int ret;
2084 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002085 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
2087 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2088
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002089#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002090 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 /*
2093 * DHM key exchange -- send G^X mod P
2094 */
Paul Bakker48916f92012-09-16 19:57:18 +00002095 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
2097 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2098 ssl->out_msg[5] = (unsigned char)( n );
2099 i = 6;
2100
Paul Bakker29b64762012-09-25 09:36:44 +00002101 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002102 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 &ssl->out_msg[i], n,
2104 ssl->f_rng, ssl->p_rng );
2105 if( ret != 0 )
2106 {
2107 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2108 return( ret );
2109 }
2110
Paul Bakker48916f92012-09-16 19:57:18 +00002111 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2112 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002114 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
Paul Bakker48916f92012-09-16 19:57:18 +00002116 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2117 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002118 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002119 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 {
2121 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2122 return( ret );
2123 }
2124
Paul Bakker48916f92012-09-16 19:57:18 +00002125 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
2127 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002128#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002129#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002130 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2131 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2132 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002133 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002134 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2135 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2136 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002137 {
2138 /*
2139 * ECDH key exchange -- send client public value
2140 */
2141 i = 4;
2142
2143 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2144 &n,
2145 &ssl->out_msg[i], 1000,
2146 ssl->f_rng, ssl->p_rng );
2147 if( ret != 0 )
2148 {
2149 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2150 return( ret );
2151 }
2152
2153 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2154
2155 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2156 &ssl->handshake->pmslen,
2157 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002158 POLARSSL_MPI_MAX_SIZE,
2159 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002160 {
2161 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2162 return( ret );
2163 }
2164
2165 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2166 }
2167 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002168#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002169 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2170 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2171 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002172#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002173 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002174 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002175 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2176 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002177 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002178 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002179 * opaque psk_identity<0..2^16-1>;
2180 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002181 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002182 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2183
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002184 i = 4;
2185 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002186 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2187 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002188
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002189 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2190 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002191
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002192#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002193 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002194 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002195 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002196 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002197 else
2198#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002199#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2200 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2201 {
2202 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2203 return( ret );
2204 }
2205 else
2206#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002207#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002208 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002209 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002210 /*
2211 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2212 */
2213 n = ssl->handshake->dhm_ctx.len;
2214 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2215 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002216
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002217 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002218 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002219 &ssl->out_msg[i], n,
2220 ssl->f_rng, ssl->p_rng );
2221 if( ret != 0 )
2222 {
2223 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2224 return( ret );
2225 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002226 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002227 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002228#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002229#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002230 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002231 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002232 /*
2233 * ClientECDiffieHellmanPublic public;
2234 */
2235 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2236 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2237 ssl->f_rng, ssl->p_rng );
2238 if( ret != 0 )
2239 {
2240 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2241 return( ret );
2242 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002243
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002244 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2245 }
2246 else
2247#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2248 {
2249 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002250 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002251 }
2252
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002253 if( ( ret = ssl_psk_derive_premaster( ssl,
2254 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002255 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002256 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002257 return( ret );
2258 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002259 }
2260 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002261#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002262#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002263 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002265 i = 4;
2266 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002267 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 }
Paul Bakkered27a042013-04-18 22:46:23 +02002269 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002270#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002271 {
2272 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002273 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002274 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
Paul Bakkerff60ee62010-03-16 21:09:09 +00002277 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2278 {
2279 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2280 return( ret );
2281 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
2283 ssl->out_msglen = i + n;
2284 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2285 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2286
2287 ssl->state++;
2288
2289 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2290 {
2291 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2292 return( ret );
2293 }
2294
2295 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2296
2297 return( 0 );
2298}
2299
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002300#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2301 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002302 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2303 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002304static int ssl_write_certificate_verify( ssl_context *ssl )
2305{
Paul Bakkered27a042013-04-18 22:46:23 +02002306 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002307
2308 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2309
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002310 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002311 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002312 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002313 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002314 {
2315 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2316 ssl->state++;
2317 return( 0 );
2318 }
2319
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002320 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2321 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002322}
2323#else
2324static int ssl_write_certificate_verify( ssl_context *ssl )
2325{
2326 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2327 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2328 size_t n = 0, offset = 0;
2329 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002330 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002331 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002332 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002333
2334 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2335
2336 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002337 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002338 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002339 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2340 {
2341 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2342 ssl->state++;
2343 return( 0 );
2344 }
2345
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002346 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 {
2348 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2349 ssl->state++;
2350 return( 0 );
2351 }
2352
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002353 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002355 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2356 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 }
2358
2359 /*
2360 * Make an RSA signature of the handshake digests
2361 */
Paul Bakker48916f92012-09-16 19:57:18 +00002362 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002364#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2365 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002366 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002367 {
Paul Bakker926af752012-11-23 13:38:07 +01002368 /*
2369 * digitally-signed struct {
2370 * opaque md5_hash[16];
2371 * opaque sha_hash[20];
2372 * };
2373 *
2374 * md5_hash
2375 * MD5(handshake_messages);
2376 *
2377 * sha_hash
2378 * SHA(handshake_messages);
2379 */
2380 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002381 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002382
2383 /*
2384 * For ECDSA, default hash is SHA-1 only
2385 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002386 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002387 {
2388 hash_start += 16;
2389 hashlen -= 16;
2390 md_alg = POLARSSL_MD_SHA1;
2391 }
Paul Bakker926af752012-11-23 13:38:07 +01002392 }
2393 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002394#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2395 POLARSSL_SSL_PROTO_TLS1_1 */
2396#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2397 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002398 {
2399 /*
2400 * digitally-signed struct {
2401 * opaque handshake_messages[handshake_messages_length];
2402 * };
2403 *
2404 * Taking shortcut here. We assume that the server always allows the
2405 * PRF Hash function and has sent it in the allowed signature
2406 * algorithms list received in the Certificate Request message.
2407 *
2408 * Until we encounter a server that does not, we will take this
2409 * shortcut.
2410 *
2411 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2412 * in order to satisfy 'weird' needs from the server side.
2413 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002414 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2415 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002416 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002417 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002418 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002419 }
2420 else
2421 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002422 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002423 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002424 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002425 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002426
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002427 /* Info from md_alg will be used instead */
2428 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002429 offset = 2;
2430 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002431 else
2432#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002433 {
2434 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002435 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002436 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002437
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002438 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002439 ssl->out_msg + 6 + offset, &n,
2440 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002441 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002442 SSL_DEBUG_RET( 1, "pk_sign", ret );
2443 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002444 }
Paul Bakker926af752012-11-23 13:38:07 +01002445
Paul Bakker1ef83d62012-04-11 12:09:53 +00002446 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2447 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Paul Bakker1ef83d62012-04-11 12:09:53 +00002449 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2451 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2452
2453 ssl->state++;
2454
2455 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2456 {
2457 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2458 return( ret );
2459 }
2460
2461 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2462
Paul Bakkered27a042013-04-18 22:46:23 +02002463 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002464}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002465#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2466 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2467 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Paul Bakkera503a632013-08-14 13:48:06 +02002469#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002470static int ssl_parse_new_session_ticket( ssl_context *ssl )
2471{
2472 int ret;
2473 uint32_t lifetime;
2474 size_t ticket_len;
2475 unsigned char *ticket;
2476
2477 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2478
2479 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2480 {
2481 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2482 return( ret );
2483 }
2484
2485 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2486 {
2487 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2488 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2489 }
2490
2491 /*
2492 * struct {
2493 * uint32 ticket_lifetime_hint;
2494 * opaque ticket<0..2^16-1>;
2495 * } NewSessionTicket;
2496 *
2497 * 0 . 0 handshake message type
2498 * 1 . 3 handshake message length
2499 * 4 . 7 ticket_lifetime_hint
2500 * 8 . 9 ticket_len (n)
2501 * 10 . 9+n ticket content
2502 */
2503 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2504 ssl->in_hslen < 10 )
2505 {
2506 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2507 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2508 }
2509
2510 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2511 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2512
2513 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2514
2515 if( ticket_len + 10 != ssl->in_hslen )
2516 {
2517 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2518 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2519 }
2520
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002521 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2522
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002523 /* We're not waiting for a NewSessionTicket message any more */
2524 ssl->handshake->new_session_ticket = 0;
2525
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002526 /*
2527 * Zero-length ticket means the server changed his mind and doesn't want
2528 * to send a ticket after all, so just forget it
2529 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002530 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002531 return( 0 );
2532
Paul Bakker34617722014-06-13 17:20:13 +02002533 polarssl_zeroize( ssl->session_negotiate->ticket,
2534 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002535 polarssl_free( ssl->session_negotiate->ticket );
2536 ssl->session_negotiate->ticket = NULL;
2537 ssl->session_negotiate->ticket_len = 0;
2538
2539 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2540 {
2541 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2542 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2543 }
2544
2545 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2546
2547 ssl->session_negotiate->ticket = ticket;
2548 ssl->session_negotiate->ticket_len = ticket_len;
2549 ssl->session_negotiate->ticket_lifetime = lifetime;
2550
2551 /*
2552 * RFC 5077 section 3.4:
2553 * "If the client receives a session ticket from the server, then it
2554 * discards any Session ID that was sent in the ServerHello."
2555 */
2556 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2557 ssl->session_negotiate->length = 0;
2558
2559 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2560
2561 return( 0 );
2562}
Paul Bakkera503a632013-08-14 13:48:06 +02002563#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002564
Paul Bakker5121ce52009-01-03 21:22:43 +00002565/*
Paul Bakker1961b702013-01-25 14:49:24 +01002566 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 */
Paul Bakker1961b702013-01-25 14:49:24 +01002568int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002569{
2570 int ret = 0;
2571
Paul Bakker1961b702013-01-25 14:49:24 +01002572 if( ssl->state == SSL_HANDSHAKE_OVER )
2573 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Paul Bakker1961b702013-01-25 14:49:24 +01002575 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2576
2577 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2578 return( ret );
2579
2580 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 {
Paul Bakker1961b702013-01-25 14:49:24 +01002582 case SSL_HELLO_REQUEST:
2583 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 break;
2585
Paul Bakker1961b702013-01-25 14:49:24 +01002586 /*
2587 * ==> ClientHello
2588 */
2589 case SSL_CLIENT_HELLO:
2590 ret = ssl_write_client_hello( ssl );
2591 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Paul Bakker1961b702013-01-25 14:49:24 +01002593 /*
2594 * <== ServerHello
2595 * Certificate
2596 * ( ServerKeyExchange )
2597 * ( CertificateRequest )
2598 * ServerHelloDone
2599 */
2600 case SSL_SERVER_HELLO:
2601 ret = ssl_parse_server_hello( ssl );
2602 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Paul Bakker1961b702013-01-25 14:49:24 +01002604 case SSL_SERVER_CERTIFICATE:
2605 ret = ssl_parse_certificate( ssl );
2606 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
Paul Bakker1961b702013-01-25 14:49:24 +01002608 case SSL_SERVER_KEY_EXCHANGE:
2609 ret = ssl_parse_server_key_exchange( ssl );
2610 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Paul Bakker1961b702013-01-25 14:49:24 +01002612 case SSL_CERTIFICATE_REQUEST:
2613 ret = ssl_parse_certificate_request( ssl );
2614 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Paul Bakker1961b702013-01-25 14:49:24 +01002616 case SSL_SERVER_HELLO_DONE:
2617 ret = ssl_parse_server_hello_done( ssl );
2618 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Paul Bakker1961b702013-01-25 14:49:24 +01002620 /*
2621 * ==> ( Certificate/Alert )
2622 * ClientKeyExchange
2623 * ( CertificateVerify )
2624 * ChangeCipherSpec
2625 * Finished
2626 */
2627 case SSL_CLIENT_CERTIFICATE:
2628 ret = ssl_write_certificate( ssl );
2629 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Paul Bakker1961b702013-01-25 14:49:24 +01002631 case SSL_CLIENT_KEY_EXCHANGE:
2632 ret = ssl_write_client_key_exchange( ssl );
2633 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Paul Bakker1961b702013-01-25 14:49:24 +01002635 case SSL_CERTIFICATE_VERIFY:
2636 ret = ssl_write_certificate_verify( ssl );
2637 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Paul Bakker1961b702013-01-25 14:49:24 +01002639 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2640 ret = ssl_write_change_cipher_spec( ssl );
2641 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
Paul Bakker1961b702013-01-25 14:49:24 +01002643 case SSL_CLIENT_FINISHED:
2644 ret = ssl_write_finished( ssl );
2645 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
Paul Bakker1961b702013-01-25 14:49:24 +01002647 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002648 * <== ( NewSessionTicket )
2649 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002650 * Finished
2651 */
2652 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002653#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002654 if( ssl->handshake->new_session_ticket != 0 )
2655 ret = ssl_parse_new_session_ticket( ssl );
2656 else
Paul Bakkera503a632013-08-14 13:48:06 +02002657#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002658 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002659 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Paul Bakker1961b702013-01-25 14:49:24 +01002661 case SSL_SERVER_FINISHED:
2662 ret = ssl_parse_finished( ssl );
2663 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
Paul Bakker1961b702013-01-25 14:49:24 +01002665 case SSL_FLUSH_BUFFERS:
2666 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2667 ssl->state = SSL_HANDSHAKE_WRAPUP;
2668 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Paul Bakker1961b702013-01-25 14:49:24 +01002670 case SSL_HANDSHAKE_WRAPUP:
2671 ssl_handshake_wrapup( ssl );
2672 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002673
Paul Bakker1961b702013-01-25 14:49:24 +01002674 default:
2675 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2676 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
2679 return( ret );
2680}
Paul Bakker9af723c2014-05-01 13:03:14 +02002681#endif /* POLARSSL_SSL_CLI_C */