blob: 50d6a68468770f1cf403aa1f18d3bfc1ad0572a1 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#if defined(POLARSSL_PLATFORM_C)
38#include "polarssl/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020039#else
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdlib.h>
45#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#include <basetsd.h>
49typedef UINT32 uint32_t;
50#else
51#include <inttypes.h>
52#endif
53
54#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakker34617722014-06-13 17:20:13 +020058#if defined(POLARSSL_SSL_SESSION_TICKETS)
59/* Implementation that should never be optimized out by the compiler */
60static void polarssl_zeroize( void *v, size_t n ) {
61 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
62}
63#endif
64
Paul Bakker0be444a2013-08-27 21:55:01 +020065#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010066static void ssl_write_hostname_ext( ssl_context *ssl,
67 unsigned char *buf,
68 size_t *olen )
69{
70 unsigned char *p = buf;
71
72 *olen = 0;
73
Paul Bakker66d5d072014-06-17 16:39:18 +020074 if( ssl->hostname == NULL )
Paul Bakkerd3edc862013-03-20 16:07:17 +010075 return;
76
77 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
78 ssl->hostname ) );
79
80 /*
81 * struct {
82 * NameType name_type;
83 * select (name_type) {
84 * case host_name: HostName;
85 * } name;
86 * } ServerName;
87 *
88 * enum {
89 * host_name(0), (255)
90 * } NameType;
91 *
92 * opaque HostName<1..2^16-1>;
93 *
94 * struct {
95 * ServerName server_name_list<1..2^16-1>
96 * } ServerNameList;
97 */
98 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
99 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
100
101 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
102 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
103
104 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
105 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
106
107 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
108 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
109 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
110
111 memcpy( p, ssl->hostname, ssl->hostname_len );
112
113 *olen = ssl->hostname_len + 9;
114}
Paul Bakker0be444a2013-08-27 21:55:01 +0200115#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100117#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118static void ssl_write_renegotiation_ext( ssl_context *ssl,
119 unsigned char *buf,
120 size_t *olen )
121{
122 unsigned char *p = buf;
123
124 *olen = 0;
125
126 if( ssl->renegotiation != SSL_RENEGOTIATION )
127 return;
128
129 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
130
131 /*
132 * Secure renegotiation
133 */
134 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
135 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
136
137 *p++ = 0x00;
138 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
139 *p++ = ssl->verify_data_len & 0xFF;
140
141 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
142
143 *olen = 5 + ssl->verify_data_len;
144}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100145#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100146
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200147#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100148static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
149 unsigned char *buf,
150 size_t *olen )
151{
152 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100153 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200154#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
155 unsigned char *sig_alg_list = buf + 6;
156#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100157
158 *olen = 0;
159
160 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
161 return;
162
163 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
164
165 /*
166 * Prepare signature_algorithms extension (TLS 1.2)
167 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200168#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200169#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100170 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
171 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
172 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
173 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
174#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200175#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100176 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
177 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
178 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
179 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
180#endif
181#if defined(POLARSSL_SHA1_C)
182 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
183 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
184#endif
185#if defined(POLARSSL_MD5_C)
186 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
187 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
188#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200189#endif /* POLARSSL_RSA_C */
190#if defined(POLARSSL_ECDSA_C)
191#if defined(POLARSSL_SHA512_C)
192 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
193 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
194 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
195 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
196#endif
197#if defined(POLARSSL_SHA256_C)
198 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
199 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
200 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
201 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
202#endif
203#if defined(POLARSSL_SHA1_C)
204 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
205 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
206#endif
207#if defined(POLARSSL_MD5_C)
208 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
209 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
210#endif
211#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100212
213 /*
214 * enum {
215 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
216 * sha512(6), (255)
217 * } HashAlgorithm;
218 *
219 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
220 * SignatureAlgorithm;
221 *
222 * struct {
223 * HashAlgorithm hash;
224 * SignatureAlgorithm signature;
225 * } SignatureAndHashAlgorithm;
226 *
227 * SignatureAndHashAlgorithm
228 * supported_signature_algorithms<2..2^16-2>;
229 */
230 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
231 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
232
233 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
234 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
235
236 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
237 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
238
Paul Bakkerd3edc862013-03-20 16:07:17 +0100239 *olen = 6 + sig_alg_len;
240}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200241#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100242
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200243#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100244static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
245 unsigned char *buf,
246 size_t *olen )
247{
248 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100249 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100250 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100251 const ecp_curve_info *info;
252#if defined(POLARSSL_SSL_SET_CURVES)
253 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100254#else
255 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100256#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100257
258 *olen = 0;
259
260 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
261
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100262#if defined(POLARSSL_SSL_SET_CURVES)
263 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200264 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100265 info = ecp_curve_info_from_grp_id( *grp_id );
266#else
267 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
268 {
269#endif
270
271 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
272 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200273 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200274
275 if( elliptic_curve_len == 0 )
276 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100277
278 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
279 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
280
281 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
282 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
283
284 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
285 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
286
Paul Bakkerd3edc862013-03-20 16:07:17 +0100287 *olen = 6 + elliptic_curve_len;
288}
289
290static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
291 unsigned char *buf,
292 size_t *olen )
293{
294 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200295 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100296
297 *olen = 0;
298
299 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
300
301 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
302 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
303
304 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100305 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200306
307 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100308 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
309
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200310 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100311}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200312#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100313
Paul Bakker05decb22013-08-15 13:33:48 +0200314#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200315static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
316 unsigned char *buf,
317 size_t *olen )
318{
319 unsigned char *p = buf;
320
321 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
322 *olen = 0;
323 return;
324 }
325
326 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
327
328 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
329 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
330
331 *p++ = 0x00;
332 *p++ = 1;
333
334 *p++ = ssl->mfl_code;
335
336 *olen = 5;
337}
Paul Bakker05decb22013-08-15 13:33:48 +0200338#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200339
Paul Bakker1f2bc622013-08-15 13:45:55 +0200340#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200341static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
342 unsigned char *buf, size_t *olen )
343{
344 unsigned char *p = buf;
345
346 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
347 {
348 *olen = 0;
349 return;
350 }
351
352 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
353
354 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
355 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
356
357 *p++ = 0x00;
358 *p++ = 0x00;
359
360 *olen = 4;
361}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200362#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200363
Paul Bakkera503a632013-08-14 13:48:06 +0200364#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200365static void ssl_write_session_ticket_ext( ssl_context *ssl,
366 unsigned char *buf, size_t *olen )
367{
368 unsigned char *p = buf;
369 size_t tlen = ssl->session_negotiate->ticket_len;
370
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200371 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
372 {
373 *olen = 0;
374 return;
375 }
376
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200377 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
378
379 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
380 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
381
382 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
383 *p++ = (unsigned char)( ( tlen ) & 0xFF );
384
385 *olen = 4;
386
387 if( ssl->session_negotiate->ticket == NULL ||
388 ssl->session_negotiate->ticket_len == 0 )
389 {
390 return;
391 }
392
393 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
394
395 memcpy( p, ssl->session_negotiate->ticket, tlen );
396
397 *olen += tlen;
398}
Paul Bakkera503a632013-08-14 13:48:06 +0200399#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200400
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200401#if defined(POLARSSL_SSL_ALPN)
402static void ssl_write_alpn_ext( ssl_context *ssl,
403 unsigned char *buf, size_t *olen )
404{
405 unsigned char *p = buf;
406 const char **cur;
407
408 if( ssl->alpn_list == NULL )
409 {
410 *olen = 0;
411 return;
412 }
413
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200414 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200415
416 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
417 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
418
419 /*
420 * opaque ProtocolName<1..2^8-1>;
421 *
422 * struct {
423 * ProtocolName protocol_name_list<2..2^16-1>
424 * } ProtocolNameList;
425 */
426
427 /* Skip writing extension and list length for now */
428 p += 4;
429
430 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
431 {
432 *p = (unsigned char)( strlen( *cur ) & 0xFF );
433 memcpy( p + 1, *cur, *p );
434 p += 1 + *p;
435 }
436
437 *olen = p - buf;
438
439 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
440 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
441 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
442
443 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
444 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
445 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
446}
447#endif /* POLARSSL_SSL_ALPN */
448
Paul Bakker5121ce52009-01-03 21:22:43 +0000449static int ssl_write_client_hello( ssl_context *ssl )
450{
Paul Bakker23986e52011-04-24 08:57:21 +0000451 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100452 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200454 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200455#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200457#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200458 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200459 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
462
Paul Bakkera9a028e2013-11-21 17:31:06 +0100463 if( ssl->f_rng == NULL )
464 {
465 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
466 return( POLARSSL_ERR_SSL_NO_RNG );
467 }
468
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100469#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000470 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100471#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000472 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000473 ssl->major_ver = ssl->min_major_ver;
474 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000475 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
Paul Bakker490ecc82011-10-06 13:04:09 +0000477 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
478 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200479 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
480 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000481 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
483 /*
484 * 0 . 0 handshake type
485 * 1 . 3 handshake length
486 * 4 . 5 highest version supported
487 * 6 . 9 current UNIX time
488 * 10 . 37 random bytes
489 */
490 buf = ssl->out_msg;
491 p = buf + 4;
492
493 *p++ = (unsigned char) ssl->max_major_ver;
494 *p++ = (unsigned char) ssl->max_minor_ver;
495
496 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
497 buf[4], buf[5] ) );
498
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200499#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 t = time( NULL );
501 *p++ = (unsigned char)( t >> 24 );
502 *p++ = (unsigned char)( t >> 16 );
503 *p++ = (unsigned char)( t >> 8 );
504 *p++ = (unsigned char)( t );
505
506 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200507#else
508 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
509 return( ret );
510
511 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200512#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
Paul Bakkera3d195c2011-11-27 21:07:34 +0000514 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
515 return( ret );
516
517 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
Paul Bakker48916f92012-09-16 19:57:18 +0000519 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
522
523 /*
524 * 38 . 38 session id length
525 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000526 * 40+n . 41+n ciphersuitelist length
527 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000528 * .. . .. compression methods length
529 * .. . .. compression methods
530 * .. . .. extensions length
531 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 */
Paul Bakker48916f92012-09-16 19:57:18 +0000533 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100535 if( n < 16 || n > 32 ||
536#if defined(POLARSSL_SSL_RENEGOTIATION)
537 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
538#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000539 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200540 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200542 }
543
Paul Bakkera503a632013-08-14 13:48:06 +0200544#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200545 /*
546 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
547 * generate and include a Session ID in the TLS ClientHello."
548 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100549#if defined(POLARSSL_SSL_RENEGOTIATION)
550 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
551#endif
552 if( ssl->session_negotiate->ticket != NULL &&
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200553 ssl->session_negotiate->ticket_len != 0 )
554 {
555 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
556
557 if( ret != 0 )
558 return( ret );
559
560 ssl->session_negotiate->length = n = 32;
561 }
Paul Bakkera503a632013-08-14 13:48:06 +0200562#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 *p++ = (unsigned char) n;
565
566 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000567 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
570 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
571
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200572 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200573 n = 0;
574 q = p;
575
576 // Skip writing ciphersuite length for now
577 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Paul Bakker48916f92012-09-16 19:57:18 +0000579 /*
580 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
581 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100582#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000583 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100584#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000585 {
586 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
587 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200588 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000589 }
590
Paul Bakker2fbefde2013-06-29 16:01:15 +0200591 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200593 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
594
595 if( ciphersuite_info == NULL )
596 continue;
597
598 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
599 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
600 continue;
601
Paul Bakkere3166ce2011-01-27 17:40:50 +0000602 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200603 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Paul Bakker2fbefde2013-06-29 16:01:15 +0200605 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200606 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
607 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 }
609
Paul Bakker2fbefde2013-06-29 16:01:15 +0200610 *q++ = (unsigned char)( n >> 7 );
611 *q++ = (unsigned char)( n << 1 );
612
613 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
614
615
Paul Bakker2770fbd2012-07-03 13:30:23 +0000616#if defined(POLARSSL_ZLIB_SUPPORT)
617 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
618 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000619 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000620
621 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000622 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000623 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000624#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000626 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628 *p++ = 1;
629 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200630#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Paul Bakkerd3edc862013-03-20 16:07:17 +0100632 // First write extensions, then the total length
633 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200634#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100635 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
636 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200637#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100639#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100640 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
641 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100642#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000643
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200644#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100645 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
646 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200647#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000648
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200649#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100650 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
651 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100652
Paul Bakkerd3edc862013-03-20 16:07:17 +0100653 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
654 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100655#endif
656
Paul Bakker05decb22013-08-15 13:33:48 +0200657#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200658 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
659 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200660#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200661
Paul Bakker1f2bc622013-08-15 13:45:55 +0200662#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200663 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
664 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200665#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200666
Paul Bakkera503a632013-08-14 13:48:06 +0200667#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200668 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
669 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200670#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200671
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200672#if defined(POLARSSL_SSL_ALPN)
673 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
674 ext_len += olen;
675#endif
676
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100677 /* olen unused if all extensions are disabled */
678 ((void) olen);
679
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000680 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
681 ext_len ) );
682
Paul Bakkera7036632014-04-30 10:15:38 +0200683 if( ext_len > 0 )
684 {
685 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
686 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
687 p += ext_len;
688 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100689
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 ssl->out_msglen = p - buf;
691 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
692 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
693
694 ssl->state++;
695
696 if( ( ret = ssl_write_record( ssl ) ) != 0 )
697 {
698 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
699 return( ret );
700 }
701
702 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
703
704 return( 0 );
705}
706
Paul Bakker48916f92012-09-16 19:57:18 +0000707static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200708 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000709 size_t len )
710{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000711 int ret;
712
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100713#if defined(POLARSSL_SSL_RENEGOTIATION)
714 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000715 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100716 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000717 if( len != 1 + ssl->verify_data_len * 2 ||
718 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100719 safer_memcmp( buf + 1,
720 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
721 safer_memcmp( buf + 1 + ssl->verify_data_len,
722 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000723 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100724 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000725
726 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
727 return( ret );
728
Paul Bakker48916f92012-09-16 19:57:18 +0000729 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
730 }
731 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100732 else
733#endif /* POLARSSL_SSL_RENEGOTIATION */
734 {
735 if( len != 1 || buf[0] != 0x00 )
736 {
737 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
738
739 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
740 return( ret );
741
742 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
743 }
744
745 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
746 }
Paul Bakker48916f92012-09-16 19:57:18 +0000747
748 return( 0 );
749}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200750
Paul Bakker05decb22013-08-15 13:33:48 +0200751#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200752static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200753 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200754 size_t len )
755{
756 /*
757 * server should use the extension only if we did,
758 * and if so the server's value should match ours (and len is always 1)
759 */
760 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
761 len != 1 ||
762 buf[0] != ssl->mfl_code )
763 {
764 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
765 }
766
767 return( 0 );
768}
Paul Bakker05decb22013-08-15 13:33:48 +0200769#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000770
Paul Bakker1f2bc622013-08-15 13:45:55 +0200771#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200772static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
773 const unsigned char *buf,
774 size_t len )
775{
776 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
777 len != 0 )
778 {
779 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
780 }
781
782 ((void) buf);
783
784 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
785
786 return( 0 );
787}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200788#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200789
Paul Bakkera503a632013-08-14 13:48:06 +0200790#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200791static int ssl_parse_session_ticket_ext( ssl_context *ssl,
792 const unsigned char *buf,
793 size_t len )
794{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200795 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
796 len != 0 )
797 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200798 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200799 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200800
801 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200802
803 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200804
805 return( 0 );
806}
Paul Bakkera503a632013-08-14 13:48:06 +0200807#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200808
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200809#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200810static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
811 const unsigned char *buf,
812 size_t len )
813{
814 size_t list_size;
815 const unsigned char *p;
816
817 list_size = buf[0];
818 if( list_size + 1 != len )
819 {
820 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
821 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
822 }
823
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200824 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200825 while( list_size > 0 )
826 {
827 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
828 p[0] == POLARSSL_ECP_PF_COMPRESSED )
829 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200830 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200831 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
832 return( 0 );
833 }
834
835 list_size--;
836 p++;
837 }
838
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200839 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
840 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200841}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200842#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200843
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200844#if defined(POLARSSL_SSL_ALPN)
845static int ssl_parse_alpn_ext( ssl_context *ssl,
846 const unsigned char *buf, size_t len )
847{
848 size_t list_len, name_len;
849 const char **p;
850
851 /* If we didn't send it, the server shouldn't send it */
852 if( ssl->alpn_list == NULL )
853 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
854
855 /*
856 * opaque ProtocolName<1..2^8-1>;
857 *
858 * struct {
859 * ProtocolName protocol_name_list<2..2^16-1>
860 * } ProtocolNameList;
861 *
862 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
863 */
864
865 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
866 if( len < 4 )
867 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
868
869 list_len = ( buf[0] << 8 ) | buf[1];
870 if( list_len != len - 2 )
871 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
872
873 name_len = buf[2];
874 if( name_len != list_len - 1 )
875 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
876
877 /* Check that the server chosen protocol was in our list and save it */
878 for( p = ssl->alpn_list; *p != NULL; p++ )
879 {
880 if( name_len == strlen( *p ) &&
881 memcmp( buf + 3, *p, name_len ) == 0 )
882 {
883 ssl->alpn_chosen = *p;
884 return( 0 );
885 }
886 }
887
888 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
889}
890#endif /* POLARSSL_SSL_ALPN */
891
Paul Bakker5121ce52009-01-03 21:22:43 +0000892static int ssl_parse_server_hello( ssl_context *ssl )
893{
Paul Bakker2770fbd2012-07-03 13:30:23 +0000894 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +0000895 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +0200896 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000897 unsigned char *buf, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100898#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000899 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100900#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000901 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200902#if defined(POLARSSL_DEBUG_C)
903 uint32_t t;
904#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
906 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
907
908 /*
909 * 0 . 0 handshake type
910 * 1 . 3 handshake length
911 * 4 . 5 protocol version
912 * 6 . 9 UNIX time()
913 * 10 . 37 random bytes
914 */
915 buf = ssl->in_msg;
916
917 if( ( ret = ssl_read_record( ssl ) ) != 0 )
918 {
919 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
920 return( ret );
921 }
922
923 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
924 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100925#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +0200926 if( ssl->renegotiation == SSL_RENEGOTIATION )
927 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +0200928 ssl->renego_records_seen++;
929
930 if( ssl->renego_max_records >= 0 &&
931 ssl->renego_records_seen > ssl->renego_max_records )
932 {
933 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
934 "but not honored by server" ) );
935 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
936 }
937
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +0200938 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
939 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
940 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100941#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +0200942
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 ||
951 buf[0] != SSL_HS_SERVER_HELLO ||
952 buf[4] != SSL_MAJOR_VERSION_3 )
953 {
954 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000955 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 }
957
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000958 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 {
960 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000961 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000962 }
963
964 ssl->minor_ver = buf[5];
965
Paul Bakker1d29fb52012-09-28 13:28:45 +0000966 if( ssl->minor_ver < ssl->min_minor_ver )
967 {
968 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200969 " [%d:%d] < [%d:%d]", ssl->major_ver,
970 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000971
972 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
973 SSL_ALERT_MSG_PROTOCOL_VERSION );
974
975 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
976 }
977
Paul Bakker1504af52012-02-11 16:17:43 +0000978#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200979 t = ( (uint32_t) buf[6] << 24 )
980 | ( (uint32_t) buf[7] << 16 )
981 | ( (uint32_t) buf[8] << 8 )
982 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200983 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +0000984#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
Paul Bakker48916f92012-09-16 19:57:18 +0000986 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
988 n = buf[38];
989
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
991
Paul Bakker48916f92012-09-16 19:57:18 +0000992 if( n > 32 )
993 {
994 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
995 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
996 }
997
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 /*
999 * 38 . 38 session id length
1000 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001001 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 * 41+n . 41+n chosen compression alg.
1003 * 42+n . 43+n extensions length
1004 * 44+n . 44+n+m extensions
1005 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001006 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001007 {
1008 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001009 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
Paul Bakker48916f92012-09-16 19:57:18 +00001011 if( ( ext_len > 0 && ext_len < 4 ) ||
1012 ssl->in_hslen != 44 + n + ext_len )
1013 {
1014 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1015 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1016 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001017 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001018 else if( ssl->in_hslen == 42 + n )
1019 {
1020 ext_len = 0;
1021 }
1022 else
1023 {
1024 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1025 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1026 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
1028 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001029 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Paul Bakker380da532012-04-18 16:10:25 +00001031 /*
1032 * Initialize update checksum functions
1033 */
Paul Bakker68884e32013-01-07 18:20:04 +01001034 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1035
1036 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1037 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001038 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001039 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1040 }
Paul Bakker380da532012-04-18 16:10:25 +00001041
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001042 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1043
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1045 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1046
1047 /*
1048 * Check if the session can be resumed
1049 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001050 if( ssl->handshake->resume == 0 || n == 0 ||
1051#if defined(POLARSSL_SSL_RENEGOTIATION)
1052 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1053#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001054 ssl->session_negotiate->ciphersuite != i ||
1055 ssl->session_negotiate->compression != comp ||
1056 ssl->session_negotiate->length != n ||
1057 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001058 {
1059 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001060 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001061#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001062 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001063#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001064 ssl->session_negotiate->ciphersuite = i;
1065 ssl->session_negotiate->compression = comp;
1066 ssl->session_negotiate->length = n;
1067 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 }
1069 else
1070 {
1071 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001072
1073 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1074 {
1075 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1076 return( ret );
1077 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 }
1079
1080 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001081 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
Paul Bakkere3166ce2011-01-27 17:40:50 +00001083 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1085
1086 i = 0;
1087 while( 1 )
1088 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001089 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001090 {
1091 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001092 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001093 }
1094
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001095 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1096 ssl->session_negotiate->ciphersuite )
1097 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001098 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001099 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 }
1101
Paul Bakker2770fbd2012-07-03 13:30:23 +00001102 if( comp != SSL_COMPRESS_NULL
1103#if defined(POLARSSL_ZLIB_SUPPORT)
1104 && comp != SSL_COMPRESS_DEFLATE
1105#endif
1106 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 {
1108 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001109 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 }
Paul Bakker48916f92012-09-16 19:57:18 +00001111 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
Paul Bakker48916f92012-09-16 19:57:18 +00001113 ext = buf + 44 + n;
1114
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001115 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1116
Paul Bakker48916f92012-09-16 19:57:18 +00001117 while( ext_len )
1118 {
1119 unsigned int ext_id = ( ( ext[0] << 8 )
1120 | ( ext[1] ) );
1121 unsigned int ext_size = ( ( ext[2] << 8 )
1122 | ( ext[3] ) );
1123
1124 if( ext_size + 4 > ext_len )
1125 {
1126 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1127 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1128 }
1129
1130 switch( ext_id )
1131 {
1132 case TLS_EXT_RENEGOTIATION_INFO:
1133 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001134#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001135 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001136#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001137
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001138 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1139 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001140 return( ret );
1141
1142 break;
1143
Paul Bakker05decb22013-08-15 13:33:48 +02001144#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001145 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1146 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1147
1148 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1149 ext + 4, ext_size ) ) != 0 )
1150 {
1151 return( ret );
1152 }
1153
1154 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001155#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001156
Paul Bakker1f2bc622013-08-15 13:45:55 +02001157#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001158 case TLS_EXT_TRUNCATED_HMAC:
1159 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1160
1161 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1162 ext + 4, ext_size ) ) != 0 )
1163 {
1164 return( ret );
1165 }
1166
1167 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001168#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001169
Paul Bakkera503a632013-08-14 13:48:06 +02001170#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001171 case TLS_EXT_SESSION_TICKET:
1172 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1173
1174 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1175 ext + 4, ext_size ) ) != 0 )
1176 {
1177 return( ret );
1178 }
1179
1180 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001181#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001182
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001183#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001184 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1185 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1186
1187 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1188 ext + 4, ext_size ) ) != 0 )
1189 {
1190 return( ret );
1191 }
1192
1193 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001194#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001195
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001196#if defined(POLARSSL_SSL_ALPN)
1197 case TLS_EXT_ALPN:
1198 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1199
1200 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1201 return( ret );
1202
1203 break;
1204#endif /* POLARSSL_SSL_ALPN */
1205
Paul Bakker48916f92012-09-16 19:57:18 +00001206 default:
1207 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1208 ext_id ) );
1209 }
1210
1211 ext_len -= 4 + ext_size;
1212 ext += 4 + ext_size;
1213
1214 if( ext_len > 0 && ext_len < 4 )
1215 {
1216 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1217 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1218 }
1219 }
1220
1221 /*
1222 * Renegotiation security checks
1223 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001224 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1225 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001226 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001227 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1228 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001229 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001230#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001231 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1232 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1233 renegotiation_info_seen == 0 )
1234 {
1235 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1236 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001237 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001238 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1239 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1240 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001241 {
1242 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001243 handshake_failure = 1;
1244 }
1245 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1246 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1247 renegotiation_info_seen == 1 )
1248 {
1249 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1250 handshake_failure = 1;
1251 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001252#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001253
1254 if( handshake_failure == 1 )
1255 {
1256 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1257 return( ret );
1258
Paul Bakker48916f92012-09-16 19:57:18 +00001259 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1260 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001261
1262 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1263
1264 return( 0 );
1265}
1266
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001267#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1268 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001269static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1270 unsigned char *end )
1271{
1272 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1273
Paul Bakker29e1f122013-04-16 13:07:56 +02001274 /*
1275 * Ephemeral DH parameters:
1276 *
1277 * struct {
1278 * opaque dh_p<1..2^16-1>;
1279 * opaque dh_g<1..2^16-1>;
1280 * opaque dh_Ys<1..2^16-1>;
1281 * } ServerDHParams;
1282 */
1283 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1284 {
1285 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1286 return( ret );
1287 }
1288
1289 if( ssl->handshake->dhm_ctx.len < 64 ||
1290 ssl->handshake->dhm_ctx.len > 512 )
1291 {
1292 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1293 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1294 }
1295
1296 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1297 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1298 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001299
1300 return( ret );
1301}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001302#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1303 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001304
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001305#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001306 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001307 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1308 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1309 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1310static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1311{
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001312 const ecp_curve_info *curve_info;
1313
1314 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1315 if( curve_info == NULL )
1316 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001317 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1318 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001319 }
1320
1321 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001322
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001323#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1324 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1325#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001326 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1327 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001328#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001329 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001330
1331 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1332
1333 return( 0 );
1334}
Paul Bakker9af723c2014-05-01 13:03:14 +02001335#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1336 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1337 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1338 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1339 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001340
1341#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1342 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001343 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001344static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1345 unsigned char **p,
1346 unsigned char *end )
1347{
1348 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1349
Paul Bakker29e1f122013-04-16 13:07:56 +02001350 /*
1351 * Ephemeral ECDH parameters:
1352 *
1353 * struct {
1354 * ECParameters curve_params;
1355 * ECPoint public;
1356 * } ServerECDHParams;
1357 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001358 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1359 (const unsigned char **) p, end ) ) != 0 )
1360 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001361 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001362 return( ret );
1363 }
1364
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001365 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001366 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001367 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001368 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1369 }
1370
Paul Bakker29e1f122013-04-16 13:07:56 +02001371 return( ret );
1372}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001373#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001374 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1375 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001376
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001377#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001378static int ssl_parse_server_psk_hint( ssl_context *ssl,
1379 unsigned char **p,
1380 unsigned char *end )
1381{
1382 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001383 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001384 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001385
1386 /*
1387 * PSK parameters:
1388 *
1389 * opaque psk_identity_hint<0..2^16-1>;
1390 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001391 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001392 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001393
1394 if( (*p) + len > end )
1395 {
1396 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1397 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1398 }
1399
1400 // TODO: Retrieve PSK identity hint and callback to app
1401 //
1402 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001403 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001404
1405 return( ret );
1406}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001407#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001408
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001409#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1410 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1411/*
1412 * Generate a pre-master secret and encrypt it with the server's RSA key
1413 */
1414static int ssl_write_encrypted_pms( ssl_context *ssl,
1415 size_t offset, size_t *olen,
1416 size_t pms_offset )
1417{
1418 int ret;
1419 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1420 unsigned char *p = ssl->handshake->premaster + pms_offset;
1421
1422 /*
1423 * Generate (part of) the pre-master as
1424 * struct {
1425 * ProtocolVersion client_version;
1426 * opaque random[46];
1427 * } PreMasterSecret;
1428 */
1429 p[0] = (unsigned char) ssl->max_major_ver;
1430 p[1] = (unsigned char) ssl->max_minor_ver;
1431
1432 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1433 {
1434 SSL_DEBUG_RET( 1, "f_rng", ret );
1435 return( ret );
1436 }
1437
1438 ssl->handshake->pmslen = 48;
1439
1440 /*
1441 * Now write it out, encrypted
1442 */
1443 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1444 POLARSSL_PK_RSA ) )
1445 {
1446 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1447 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1448 }
1449
1450 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1451 p, ssl->handshake->pmslen,
1452 ssl->out_msg + offset + len_bytes, olen,
1453 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1454 ssl->f_rng, ssl->p_rng ) ) != 0 )
1455 {
1456 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1457 return( ret );
1458 }
1459
1460#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1461 defined(POLARSSL_SSL_PROTO_TLS1_2)
1462 if( len_bytes == 2 )
1463 {
1464 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1465 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1466 *olen += 2;
1467 }
1468#endif
1469
1470 return( 0 );
1471}
1472#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1473 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001474
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001475#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001476#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001477 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1478 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001479static int ssl_parse_signature_algorithm( ssl_context *ssl,
1480 unsigned char **p,
1481 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001482 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001483 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001484{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001485 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001486 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001487 *pk_alg = POLARSSL_PK_NONE;
1488
1489 /* Only in TLS 1.2 */
1490 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1491 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001492 return( 0 );
1493 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001494
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001495 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001496 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1497
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001498 /*
1499 * Get hash algorithm
1500 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001501 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001502 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001503 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1504 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001505 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1506 }
1507
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001508 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001509 * Get signature algorithm
1510 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001511 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001512 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001513 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1514 "SignatureAlgorithm %d", (*p)[1] ) );
1515 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001516 }
1517
1518 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1519 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1520 *p += 2;
1521
1522 return( 0 );
1523}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001524#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001525 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1526 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001527#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001528
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001529
1530#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1531 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1532static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1533{
1534 int ret;
1535 const ecp_keypair *peer_key;
1536
1537 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1538 POLARSSL_PK_ECKEY ) )
1539 {
1540 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1541 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1542 }
1543
1544 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1545
1546 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1547 POLARSSL_ECDH_THEIRS ) ) != 0 )
1548 {
1549 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1550 return( ret );
1551 }
1552
1553 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1554 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001555 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001556 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1557 }
1558
1559 return( ret );
1560}
1561#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1562 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1563
Paul Bakker41c83d32013-03-20 14:39:14 +01001564static int ssl_parse_server_key_exchange( ssl_context *ssl )
1565{
Paul Bakker23986e52011-04-24 08:57:21 +00001566 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001567 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001568 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001569#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001570 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1571 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001572 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001573 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001574 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001575 size_t hashlen;
1576 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001577#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001578
1579 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1580
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001581#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001582 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 {
1584 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1585 ssl->state++;
1586 return( 0 );
1587 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001588 ((void) p);
1589 ((void) end);
1590#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001592#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1593 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1594 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1595 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1596 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001597 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1598 {
1599 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1600 return( ret );
1601 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001602
1603 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1604 ssl->state++;
1605 return( 0 );
1606 }
1607 ((void) p);
1608 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001609#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1610 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001611
Paul Bakker5121ce52009-01-03 21:22:43 +00001612 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1613 {
1614 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1615 return( ret );
1616 }
1617
1618 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1619 {
1620 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001621 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001622 }
1623
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001624 /*
1625 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1626 * doesn't use a psk_identity_hint
1627 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1629 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001630 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1631 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001632 {
1633 ssl->record_read = 1;
1634 goto exit;
1635 }
1636
1637 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1638 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 }
1640
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001641 p = ssl->in_msg + 4;
1642 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001643 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001644
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001645#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1646 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1647 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1648 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1649 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1650 {
1651 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1652 {
1653 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1654 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1655 }
1656 } /* FALLTROUGH */
1657#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1658
1659#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1660 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1661 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1662 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1663 ; /* nothing more to do */
1664 else
1665#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1666 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1667#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1668 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1669 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1670 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001672 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001673 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001674 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001675 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1676 }
1677 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001678 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001679#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1680 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001681#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001682 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001683 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1684 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001685 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001686 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001687 {
1688 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1689 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001690 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1691 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1692 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001693 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001694 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001695#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001696 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001697 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001698 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001699 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001700 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001701 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001702
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001703#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001704 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1705 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001706 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001707 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1708 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001709 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001710 params_len = p - ( ssl->in_msg + 4 );
1711
Paul Bakker29e1f122013-04-16 13:07:56 +02001712 /*
1713 * Handle the digitally-signed structure
1714 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001715#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1716 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001717 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001718 if( ssl_parse_signature_algorithm( ssl, &p, end,
1719 &md_alg, &pk_alg ) != 0 )
1720 {
1721 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1722 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1723 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001724
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001725 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001726 {
1727 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1728 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1729 }
1730 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001731 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001732#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001733#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1734 defined(POLARSSL_SSL_PROTO_TLS1_1)
1735 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001736 {
1737 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001738
Paul Bakker9659dae2013-08-28 16:21:34 +02001739 /* Default hash for ECDSA is SHA-1 */
1740 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1741 md_alg = POLARSSL_MD_SHA1;
1742 }
1743 else
1744#endif
1745 {
1746 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001747 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001748 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001749
1750 /*
1751 * Read signature
1752 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001753 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001754 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001755
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001756 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001757 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001758 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001759 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1760 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001761
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001762 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001763
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001764 /*
1765 * Compute the hash that has been signed
1766 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001767#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1768 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001769 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001770 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001771 md5_context md5;
1772 sha1_context sha1;
1773
Paul Bakker5b4af392014-06-26 12:09:34 +02001774 md5_init( &md5 );
1775 sha1_init( &sha1 );
1776
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001777 hashlen = 36;
1778
Paul Bakker29e1f122013-04-16 13:07:56 +02001779 /*
1780 * digitally-signed struct {
1781 * opaque md5_hash[16];
1782 * opaque sha_hash[20];
1783 * };
1784 *
1785 * md5_hash
1786 * MD5(ClientHello.random + ServerHello.random
1787 * + ServerParams);
1788 * sha_hash
1789 * SHA(ClientHello.random + ServerHello.random
1790 * + ServerParams);
1791 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001792 md5_starts( &md5 );
1793 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001794 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001795 md5_finish( &md5, hash );
1796
1797 sha1_starts( &sha1 );
1798 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001799 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001800 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001801
1802 md5_free( &md5 );
1803 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001804 }
1805 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001806#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1807 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001808#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1809 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001810 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001811 {
1812 md_context_t ctx;
1813
Paul Bakker84bbeb52014-07-01 14:53:22 +02001814 md_init( &ctx );
1815
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001816 /* Info from md_alg will be used instead */
1817 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001818
1819 /*
1820 * digitally-signed struct {
1821 * opaque client_random[32];
1822 * opaque server_random[32];
1823 * ServerDHParams params;
1824 * };
1825 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001826 if( ( ret = md_init_ctx( &ctx,
1827 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001828 {
1829 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1830 return( ret );
1831 }
1832
1833 md_starts( &ctx );
1834 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001835 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001836 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001837 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001838 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001839 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001840#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1841 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001842 {
Paul Bakker577e0062013-08-28 11:57:20 +02001843 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001844 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001845 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001846
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001847 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1848 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001849
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001850 /*
1851 * Verify signature
1852 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001853 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001854 {
1855 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1856 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1857 }
1858
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001859 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1860 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001861 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001862 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001863 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001864 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001866#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001867 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1868 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001870exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 ssl->state++;
1872
1873 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
1874
1875 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001876}
1877
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001878#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1879 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1880 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1881 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1882static int ssl_parse_certificate_request( ssl_context *ssl )
1883{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001884 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
1885
1886 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1887
1888 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1889 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1890 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1891 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1892 {
1893 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
1894 ssl->state++;
1895 return( 0 );
1896 }
1897
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001898 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1899 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001900}
1901#else
Paul Bakker5121ce52009-01-03 21:22:43 +00001902static int ssl_parse_certificate_request( ssl_context *ssl )
1903{
1904 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01001905 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001906 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001907 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001908 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001909
1910 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1911
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001912 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1913 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1914 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1915 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1916 {
1917 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
1918 ssl->state++;
1919 return( 0 );
1920 }
1921
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 /*
1923 * 0 . 0 handshake type
1924 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01001925 * 4 . 4 cert type count
1926 * 5 .. m-1 cert types
1927 * m .. m+1 sig alg length (TLS 1.2 only)
1928 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 * n .. n+1 length of all DNs
1930 * n+2 .. n+3 length of DN 1
1931 * n+4 .. ... Distinguished Name #1
1932 * ... .. ... length of DN 2, etc.
1933 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001934 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001936 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1937 {
1938 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1939 return( ret );
1940 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001942 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1943 {
1944 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1945 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1946 }
1947
1948 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 }
1950
1951 ssl->client_auth = 0;
1952 ssl->state++;
1953
1954 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
1955 ssl->client_auth++;
1956
1957 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
1958 ssl->client_auth ? "a" : "no" ) );
1959
Paul Bakker926af752012-11-23 13:38:07 +01001960 if( ssl->client_auth == 0 )
1961 goto exit;
1962
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001963 ssl->record_read = 0;
1964
Paul Bakker926af752012-11-23 13:38:07 +01001965 // TODO: handshake_failure alert for an anonymous server to request
1966 // client authentication
1967
1968 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001969
Paul Bakker926af752012-11-23 13:38:07 +01001970 // Retrieve cert types
1971 //
1972 cert_type_len = buf[4];
1973 n = cert_type_len;
1974
1975 if( ssl->in_hslen < 6 + n )
1976 {
1977 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1978 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1979 }
1980
Paul Bakker73d44312013-05-22 13:56:26 +02001981 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01001982 while( cert_type_len > 0 )
1983 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001984#if defined(POLARSSL_RSA_C)
1985 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001986 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01001987 {
1988 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
1989 break;
1990 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001991 else
1992#endif
1993#if defined(POLARSSL_ECDSA_C)
1994 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001995 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001996 {
1997 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
1998 break;
1999 }
2000 else
2001#endif
2002 {
2003 ; /* Unsupported cert type, ignore */
2004 }
Paul Bakker926af752012-11-23 13:38:07 +01002005
2006 cert_type_len--;
2007 p++;
2008 }
2009
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002010#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002011 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2012 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002013 /* Ignored, see comments about hash in write_certificate_verify */
2014 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002015 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2016 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002017
2018 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002019 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002020 n += sig_alg_len;
2021
2022 if( ssl->in_hslen < 6 + n )
2023 {
2024 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2025 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2026 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002027 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002028#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002029
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002030 /* Ignore certificate_authorities, we only have one cert anyway */
2031 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002032 dn_len = ( ( buf[5 + m + n] << 8 )
2033 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002034
2035 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002036 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002037 {
2038 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2039 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2040 }
2041
2042exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2044
2045 return( 0 );
2046}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002047#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2048 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2049 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2050 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002051
2052static int ssl_parse_server_hello_done( ssl_context *ssl )
2053{
2054 int ret;
2055
2056 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2057
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002058 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 {
2060 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2061 {
2062 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2063 return( ret );
2064 }
2065
2066 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2067 {
2068 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002069 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 }
2071 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002072 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002073
2074 if( ssl->in_hslen != 4 ||
2075 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2076 {
2077 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002078 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 }
2080
2081 ssl->state++;
2082
2083 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2084
2085 return( 0 );
2086}
2087
2088static int ssl_write_client_key_exchange( ssl_context *ssl )
2089{
Paul Bakker23986e52011-04-24 08:57:21 +00002090 int ret;
2091 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002092 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002093
2094 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2095
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002096#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002097 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 /*
2100 * DHM key exchange -- send G^X mod P
2101 */
Paul Bakker48916f92012-09-16 19:57:18 +00002102 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002103
2104 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2105 ssl->out_msg[5] = (unsigned char)( n );
2106 i = 6;
2107
Paul Bakker29b64762012-09-25 09:36:44 +00002108 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002109 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 &ssl->out_msg[i], n,
2111 ssl->f_rng, ssl->p_rng );
2112 if( ret != 0 )
2113 {
2114 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2115 return( ret );
2116 }
2117
Paul Bakker48916f92012-09-16 19:57:18 +00002118 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2119 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002121 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002122
Paul Bakker48916f92012-09-16 19:57:18 +00002123 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2124 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002125 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002126 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 {
2128 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2129 return( ret );
2130 }
2131
Paul Bakker48916f92012-09-16 19:57:18 +00002132 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 }
2134 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002135#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002136#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002137 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2138 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2139 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002140 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002141 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2142 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2143 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002144 {
2145 /*
2146 * ECDH key exchange -- send client public value
2147 */
2148 i = 4;
2149
2150 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2151 &n,
2152 &ssl->out_msg[i], 1000,
2153 ssl->f_rng, ssl->p_rng );
2154 if( ret != 0 )
2155 {
2156 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2157 return( ret );
2158 }
2159
2160 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2161
2162 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2163 &ssl->handshake->pmslen,
2164 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002165 POLARSSL_MPI_MAX_SIZE,
2166 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002167 {
2168 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2169 return( ret );
2170 }
2171
2172 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2173 }
2174 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002175#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002176 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2177 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2178 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002179#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002180 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002181 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002182 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2183 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002184 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002185 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002186 * opaque psk_identity<0..2^16-1>;
2187 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002188 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002189 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2190
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002191 i = 4;
2192 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002193 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2194 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002195
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002196 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2197 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002198
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002199#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002200 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002201 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002202 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002203 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002204 else
2205#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002206#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2207 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2208 {
2209 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2210 return( ret );
2211 }
2212 else
2213#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002214#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002215 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002216 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002217 /*
2218 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2219 */
2220 n = ssl->handshake->dhm_ctx.len;
2221 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2222 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002223
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002224 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002225 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002226 &ssl->out_msg[i], n,
2227 ssl->f_rng, ssl->p_rng );
2228 if( ret != 0 )
2229 {
2230 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2231 return( ret );
2232 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002233 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002234 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002235#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002236#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002237 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002238 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002239 /*
2240 * ClientECDiffieHellmanPublic public;
2241 */
2242 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2243 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2244 ssl->f_rng, ssl->p_rng );
2245 if( ret != 0 )
2246 {
2247 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2248 return( ret );
2249 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002250
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002251 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2252 }
2253 else
2254#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2255 {
2256 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002257 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002258 }
2259
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002260 if( ( ret = ssl_psk_derive_premaster( ssl,
2261 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002262 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002263 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002264 return( ret );
2265 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002266 }
2267 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002268#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002269#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002270 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002272 i = 4;
2273 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002274 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 }
Paul Bakkered27a042013-04-18 22:46:23 +02002276 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002277#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002278 {
2279 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002280 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002281 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002282 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
Paul Bakkerff60ee62010-03-16 21:09:09 +00002284 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2285 {
2286 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2287 return( ret );
2288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002289
2290 ssl->out_msglen = i + n;
2291 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2292 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2293
2294 ssl->state++;
2295
2296 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2297 {
2298 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2299 return( ret );
2300 }
2301
2302 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2303
2304 return( 0 );
2305}
2306
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2308 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002309 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2310 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002311static int ssl_write_certificate_verify( ssl_context *ssl )
2312{
Paul Bakkered27a042013-04-18 22:46:23 +02002313 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002314
2315 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2316
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002317 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002318 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002319 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002320 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002321 {
2322 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2323 ssl->state++;
2324 return( 0 );
2325 }
2326
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002327 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2328 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002329}
2330#else
2331static int ssl_write_certificate_verify( ssl_context *ssl )
2332{
2333 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2334 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2335 size_t n = 0, offset = 0;
2336 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002337 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002338 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002339 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002340
2341 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2342
2343 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002344 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002345 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002346 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2347 {
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->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 {
2355 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2356 ssl->state++;
2357 return( 0 );
2358 }
2359
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002360 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002362 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2363 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 }
2365
2366 /*
2367 * Make an RSA signature of the handshake digests
2368 */
Paul Bakker48916f92012-09-16 19:57:18 +00002369 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002370
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002371#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2372 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002373 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002374 {
Paul Bakker926af752012-11-23 13:38:07 +01002375 /*
2376 * digitally-signed struct {
2377 * opaque md5_hash[16];
2378 * opaque sha_hash[20];
2379 * };
2380 *
2381 * md5_hash
2382 * MD5(handshake_messages);
2383 *
2384 * sha_hash
2385 * SHA(handshake_messages);
2386 */
2387 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002388 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002389
2390 /*
2391 * For ECDSA, default hash is SHA-1 only
2392 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002393 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002394 {
2395 hash_start += 16;
2396 hashlen -= 16;
2397 md_alg = POLARSSL_MD_SHA1;
2398 }
Paul Bakker926af752012-11-23 13:38:07 +01002399 }
2400 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002401#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2402 POLARSSL_SSL_PROTO_TLS1_1 */
2403#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2404 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002405 {
2406 /*
2407 * digitally-signed struct {
2408 * opaque handshake_messages[handshake_messages_length];
2409 * };
2410 *
2411 * Taking shortcut here. We assume that the server always allows the
2412 * PRF Hash function and has sent it in the allowed signature
2413 * algorithms list received in the Certificate Request message.
2414 *
2415 * Until we encounter a server that does not, we will take this
2416 * shortcut.
2417 *
2418 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2419 * in order to satisfy 'weird' needs from the server side.
2420 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002421 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2422 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002423 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002424 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002425 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002426 }
2427 else
2428 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002429 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002430 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002431 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002432 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002433
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002434 /* Info from md_alg will be used instead */
2435 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002436 offset = 2;
2437 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002438 else
2439#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002440 {
2441 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002442 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002443 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002444
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002445 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002446 ssl->out_msg + 6 + offset, &n,
2447 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002448 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002449 SSL_DEBUG_RET( 1, "pk_sign", ret );
2450 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002451 }
Paul Bakker926af752012-11-23 13:38:07 +01002452
Paul Bakker1ef83d62012-04-11 12:09:53 +00002453 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2454 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
Paul Bakker1ef83d62012-04-11 12:09:53 +00002456 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2458 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2459
2460 ssl->state++;
2461
2462 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2463 {
2464 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2465 return( ret );
2466 }
2467
2468 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2469
Paul Bakkered27a042013-04-18 22:46:23 +02002470 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002471}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002472#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2473 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2474 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002475
Paul Bakkera503a632013-08-14 13:48:06 +02002476#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002477static int ssl_parse_new_session_ticket( ssl_context *ssl )
2478{
2479 int ret;
2480 uint32_t lifetime;
2481 size_t ticket_len;
2482 unsigned char *ticket;
2483
2484 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2485
2486 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2487 {
2488 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2489 return( ret );
2490 }
2491
2492 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2493 {
2494 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2495 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2496 }
2497
2498 /*
2499 * struct {
2500 * uint32 ticket_lifetime_hint;
2501 * opaque ticket<0..2^16-1>;
2502 * } NewSessionTicket;
2503 *
2504 * 0 . 0 handshake message type
2505 * 1 . 3 handshake message length
2506 * 4 . 7 ticket_lifetime_hint
2507 * 8 . 9 ticket_len (n)
2508 * 10 . 9+n ticket content
2509 */
2510 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2511 ssl->in_hslen < 10 )
2512 {
2513 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2514 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2515 }
2516
2517 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2518 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2519
2520 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2521
2522 if( ticket_len + 10 != ssl->in_hslen )
2523 {
2524 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2525 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2526 }
2527
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002528 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2529
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002530 /* We're not waiting for a NewSessionTicket message any more */
2531 ssl->handshake->new_session_ticket = 0;
2532
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002533 /*
2534 * Zero-length ticket means the server changed his mind and doesn't want
2535 * to send a ticket after all, so just forget it
2536 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002537 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002538 return( 0 );
2539
Paul Bakker34617722014-06-13 17:20:13 +02002540 polarssl_zeroize( ssl->session_negotiate->ticket,
2541 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002542 polarssl_free( ssl->session_negotiate->ticket );
2543 ssl->session_negotiate->ticket = NULL;
2544 ssl->session_negotiate->ticket_len = 0;
2545
2546 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2547 {
2548 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2549 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2550 }
2551
2552 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2553
2554 ssl->session_negotiate->ticket = ticket;
2555 ssl->session_negotiate->ticket_len = ticket_len;
2556 ssl->session_negotiate->ticket_lifetime = lifetime;
2557
2558 /*
2559 * RFC 5077 section 3.4:
2560 * "If the client receives a session ticket from the server, then it
2561 * discards any Session ID that was sent in the ServerHello."
2562 */
2563 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2564 ssl->session_negotiate->length = 0;
2565
2566 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2567
2568 return( 0 );
2569}
Paul Bakkera503a632013-08-14 13:48:06 +02002570#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002571
Paul Bakker5121ce52009-01-03 21:22:43 +00002572/*
Paul Bakker1961b702013-01-25 14:49:24 +01002573 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 */
Paul Bakker1961b702013-01-25 14:49:24 +01002575int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002576{
2577 int ret = 0;
2578
Paul Bakker1961b702013-01-25 14:49:24 +01002579 if( ssl->state == SSL_HANDSHAKE_OVER )
2580 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
Paul Bakker1961b702013-01-25 14:49:24 +01002582 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2583
2584 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2585 return( ret );
2586
2587 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 {
Paul Bakker1961b702013-01-25 14:49:24 +01002589 case SSL_HELLO_REQUEST:
2590 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 break;
2592
Paul Bakker1961b702013-01-25 14:49:24 +01002593 /*
2594 * ==> ClientHello
2595 */
2596 case SSL_CLIENT_HELLO:
2597 ret = ssl_write_client_hello( ssl );
2598 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Paul Bakker1961b702013-01-25 14:49:24 +01002600 /*
2601 * <== ServerHello
2602 * Certificate
2603 * ( ServerKeyExchange )
2604 * ( CertificateRequest )
2605 * ServerHelloDone
2606 */
2607 case SSL_SERVER_HELLO:
2608 ret = ssl_parse_server_hello( ssl );
2609 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
Paul Bakker1961b702013-01-25 14:49:24 +01002611 case SSL_SERVER_CERTIFICATE:
2612 ret = ssl_parse_certificate( ssl );
2613 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
Paul Bakker1961b702013-01-25 14:49:24 +01002615 case SSL_SERVER_KEY_EXCHANGE:
2616 ret = ssl_parse_server_key_exchange( ssl );
2617 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
Paul Bakker1961b702013-01-25 14:49:24 +01002619 case SSL_CERTIFICATE_REQUEST:
2620 ret = ssl_parse_certificate_request( ssl );
2621 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
Paul Bakker1961b702013-01-25 14:49:24 +01002623 case SSL_SERVER_HELLO_DONE:
2624 ret = ssl_parse_server_hello_done( ssl );
2625 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
Paul Bakker1961b702013-01-25 14:49:24 +01002627 /*
2628 * ==> ( Certificate/Alert )
2629 * ClientKeyExchange
2630 * ( CertificateVerify )
2631 * ChangeCipherSpec
2632 * Finished
2633 */
2634 case SSL_CLIENT_CERTIFICATE:
2635 ret = ssl_write_certificate( ssl );
2636 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Paul Bakker1961b702013-01-25 14:49:24 +01002638 case SSL_CLIENT_KEY_EXCHANGE:
2639 ret = ssl_write_client_key_exchange( ssl );
2640 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
Paul Bakker1961b702013-01-25 14:49:24 +01002642 case SSL_CERTIFICATE_VERIFY:
2643 ret = ssl_write_certificate_verify( ssl );
2644 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Paul Bakker1961b702013-01-25 14:49:24 +01002646 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2647 ret = ssl_write_change_cipher_spec( ssl );
2648 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Paul Bakker1961b702013-01-25 14:49:24 +01002650 case SSL_CLIENT_FINISHED:
2651 ret = ssl_write_finished( ssl );
2652 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
Paul Bakker1961b702013-01-25 14:49:24 +01002654 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002655 * <== ( NewSessionTicket )
2656 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002657 * Finished
2658 */
2659 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002660#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002661 if( ssl->handshake->new_session_ticket != 0 )
2662 ret = ssl_parse_new_session_ticket( ssl );
2663 else
Paul Bakkera503a632013-08-14 13:48:06 +02002664#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002665 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002666 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
Paul Bakker1961b702013-01-25 14:49:24 +01002668 case SSL_SERVER_FINISHED:
2669 ret = ssl_parse_finished( ssl );
2670 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Paul Bakker1961b702013-01-25 14:49:24 +01002672 case SSL_FLUSH_BUFFERS:
2673 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2674 ssl->state = SSL_HANDSHAKE_WRAPUP;
2675 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Paul Bakker1961b702013-01-25 14:49:24 +01002677 case SSL_HANDSHAKE_WRAPUP:
2678 ssl_handshake_wrapup( ssl );
2679 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002680
Paul Bakker1961b702013-01-25 14:49:24 +01002681 default:
2682 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2683 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
2686 return( ret );
2687}
Paul Bakker9af723c2014-05-01 13:03:14 +02002688#endif /* POLARSSL_SSL_CLI_C */