blob: 45d97c99b677b60fd113f890ae815fd85dbb032a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#if defined(POLARSSL_PLATFORM_C)
38#include "polarssl/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020039#else
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdlib.h>
45#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#include <basetsd.h>
49typedef UINT32 uint32_t;
50#else
51#include <inttypes.h>
52#endif
53
54#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakker34617722014-06-13 17:20:13 +020058#if defined(POLARSSL_SSL_SESSION_TICKETS)
59/* Implementation that should never be optimized out by the compiler */
60static void polarssl_zeroize( void *v, size_t n ) {
61 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
62}
63#endif
64
Paul Bakker0be444a2013-08-27 21:55:01 +020065#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010066static void ssl_write_hostname_ext( ssl_context *ssl,
67 unsigned char *buf,
68 size_t *olen )
69{
70 unsigned char *p = buf;
71
72 *olen = 0;
73
Paul Bakker66d5d072014-06-17 16:39:18 +020074 if( ssl->hostname == NULL )
Paul Bakkerd3edc862013-03-20 16:07:17 +010075 return;
76
77 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
78 ssl->hostname ) );
79
80 /*
81 * struct {
82 * NameType name_type;
83 * select (name_type) {
84 * case host_name: HostName;
85 * } name;
86 * } ServerName;
87 *
88 * enum {
89 * host_name(0), (255)
90 * } NameType;
91 *
92 * opaque HostName<1..2^16-1>;
93 *
94 * struct {
95 * ServerName server_name_list<1..2^16-1>
96 * } ServerNameList;
97 */
98 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
99 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
100
101 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
102 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
103
104 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
105 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
106
107 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
108 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
109 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
110
111 memcpy( p, ssl->hostname, ssl->hostname_len );
112
113 *olen = ssl->hostname_len + 9;
114}
Paul Bakker0be444a2013-08-27 21:55:01 +0200115#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
117static void ssl_write_renegotiation_ext( ssl_context *ssl,
118 unsigned char *buf,
119 size_t *olen )
120{
121 unsigned char *p = buf;
122
123 *olen = 0;
124
125 if( ssl->renegotiation != SSL_RENEGOTIATION )
126 return;
127
128 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
129
130 /*
131 * Secure renegotiation
132 */
133 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
134 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
135
136 *p++ = 0x00;
137 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
138 *p++ = ssl->verify_data_len & 0xFF;
139
140 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
141
142 *olen = 5 + ssl->verify_data_len;
143}
144
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200145#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100146static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
147 unsigned char *buf,
148 size_t *olen )
149{
150 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100151 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200152#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
153 unsigned char *sig_alg_list = buf + 6;
154#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100155
156 *olen = 0;
157
158 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
159 return;
160
161 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
162
163 /*
164 * Prepare signature_algorithms extension (TLS 1.2)
165 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200166#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200167#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100168 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
169 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
170 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
171 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
172#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200173#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100174 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
175 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
176 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
177 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
178#endif
179#if defined(POLARSSL_SHA1_C)
180 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
181 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
182#endif
183#if defined(POLARSSL_MD5_C)
184 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
185 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
186#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200187#endif /* POLARSSL_RSA_C */
188#if defined(POLARSSL_ECDSA_C)
189#if defined(POLARSSL_SHA512_C)
190 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
191 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
192 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
193 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
194#endif
195#if defined(POLARSSL_SHA256_C)
196 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
197 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
198 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
199 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
200#endif
201#if defined(POLARSSL_SHA1_C)
202 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
203 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
204#endif
205#if defined(POLARSSL_MD5_C)
206 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
207 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
208#endif
209#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100210
211 /*
212 * enum {
213 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
214 * sha512(6), (255)
215 * } HashAlgorithm;
216 *
217 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
218 * SignatureAlgorithm;
219 *
220 * struct {
221 * HashAlgorithm hash;
222 * SignatureAlgorithm signature;
223 * } SignatureAndHashAlgorithm;
224 *
225 * SignatureAndHashAlgorithm
226 * supported_signature_algorithms<2..2^16-2>;
227 */
228 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
229 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
230
231 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
232 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
233
234 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
235 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
236
Paul Bakkerd3edc862013-03-20 16:07:17 +0100237 *olen = 6 + sig_alg_len;
238}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200239#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100240
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200241#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100242static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
243 unsigned char *buf,
244 size_t *olen )
245{
246 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100247 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100248 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100249 const ecp_curve_info *info;
250#if defined(POLARSSL_SSL_SET_CURVES)
251 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100252#else
253 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100254#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100255
256 *olen = 0;
257
258 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
259
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100260#if defined(POLARSSL_SSL_SET_CURVES)
261 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200262 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100263 info = ecp_curve_info_from_grp_id( *grp_id );
264#else
265 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
266 {
267#endif
268
269 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
270 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200271 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200272
273 if( elliptic_curve_len == 0 )
274 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100275
276 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
277 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
278
279 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
280 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
281
282 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
283 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
284
Paul Bakkerd3edc862013-03-20 16:07:17 +0100285 *olen = 6 + elliptic_curve_len;
286}
287
288static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
289 unsigned char *buf,
290 size_t *olen )
291{
292 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200293 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100294
295 *olen = 0;
296
297 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
298
299 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
300 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
301
302 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100303 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200304
305 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100306 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
307
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200308 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100309}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200310#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100311
Paul Bakker05decb22013-08-15 13:33:48 +0200312#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200313static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
314 unsigned char *buf,
315 size_t *olen )
316{
317 unsigned char *p = buf;
318
319 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
320 *olen = 0;
321 return;
322 }
323
324 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
325
326 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
327 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
328
329 *p++ = 0x00;
330 *p++ = 1;
331
332 *p++ = ssl->mfl_code;
333
334 *olen = 5;
335}
Paul Bakker05decb22013-08-15 13:33:48 +0200336#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200337
Paul Bakker1f2bc622013-08-15 13:45:55 +0200338#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200339static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
340 unsigned char *buf, size_t *olen )
341{
342 unsigned char *p = buf;
343
344 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
345 {
346 *olen = 0;
347 return;
348 }
349
350 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
351
352 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
353 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
354
355 *p++ = 0x00;
356 *p++ = 0x00;
357
358 *olen = 4;
359}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200360#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200361
Paul Bakkera503a632013-08-14 13:48:06 +0200362#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200363static void ssl_write_session_ticket_ext( ssl_context *ssl,
364 unsigned char *buf, size_t *olen )
365{
366 unsigned char *p = buf;
367 size_t tlen = ssl->session_negotiate->ticket_len;
368
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200369 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
370 {
371 *olen = 0;
372 return;
373 }
374
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200375 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
376
377 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
378 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
379
380 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
381 *p++ = (unsigned char)( ( tlen ) & 0xFF );
382
383 *olen = 4;
384
385 if( ssl->session_negotiate->ticket == NULL ||
386 ssl->session_negotiate->ticket_len == 0 )
387 {
388 return;
389 }
390
391 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
392
393 memcpy( p, ssl->session_negotiate->ticket, tlen );
394
395 *olen += tlen;
396}
Paul Bakkera503a632013-08-14 13:48:06 +0200397#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200398
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200399#if defined(POLARSSL_SSL_ALPN)
400static void ssl_write_alpn_ext( ssl_context *ssl,
401 unsigned char *buf, size_t *olen )
402{
403 unsigned char *p = buf;
404 const char **cur;
405
406 if( ssl->alpn_list == NULL )
407 {
408 *olen = 0;
409 return;
410 }
411
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200412 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200413
414 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
415 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
416
417 /*
418 * opaque ProtocolName<1..2^8-1>;
419 *
420 * struct {
421 * ProtocolName protocol_name_list<2..2^16-1>
422 * } ProtocolNameList;
423 */
424
425 /* Skip writing extension and list length for now */
426 p += 4;
427
428 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
429 {
430 *p = (unsigned char)( strlen( *cur ) & 0xFF );
431 memcpy( p + 1, *cur, *p );
432 p += 1 + *p;
433 }
434
435 *olen = p - buf;
436
437 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
438 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
439 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
440
441 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
442 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
443 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
444}
445#endif /* POLARSSL_SSL_ALPN */
446
Paul Bakker5121ce52009-01-03 21:22:43 +0000447static int ssl_write_client_hello( ssl_context *ssl )
448{
Paul Bakker23986e52011-04-24 08:57:21 +0000449 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100450 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200452 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200453#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200455#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200456 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200457 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
460
Paul Bakkera9a028e2013-11-21 17:31:06 +0100461 if( ssl->f_rng == NULL )
462 {
463 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
464 return( POLARSSL_ERR_SSL_NO_RNG );
465 }
466
Paul Bakker48916f92012-09-16 19:57:18 +0000467 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
468 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000469 ssl->major_ver = ssl->min_major_ver;
470 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000471 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Paul Bakker490ecc82011-10-06 13:04:09 +0000473 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
474 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200475 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
476 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000477 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479 /*
480 * 0 . 0 handshake type
481 * 1 . 3 handshake length
482 * 4 . 5 highest version supported
483 * 6 . 9 current UNIX time
484 * 10 . 37 random bytes
485 */
486 buf = ssl->out_msg;
487 p = buf + 4;
488
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100489 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
490 ssl->transport, p );
491 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
494 buf[4], buf[5] ) );
495
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200496#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 t = time( NULL );
498 *p++ = (unsigned char)( t >> 24 );
499 *p++ = (unsigned char)( t >> 16 );
500 *p++ = (unsigned char)( t >> 8 );
501 *p++ = (unsigned char)( t );
502
503 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200504#else
505 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
506 return( ret );
507
508 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200509#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Paul Bakkera3d195c2011-11-27 21:07:34 +0000511 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
512 return( ret );
513
514 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Paul Bakker48916f92012-09-16 19:57:18 +0000516 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
519
520 /*
521 * 38 . 38 session id length
522 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000523 * 40+n . 41+n ciphersuitelist length
524 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000525 * .. . .. compression methods length
526 * .. . .. compression methods
527 * .. . .. extensions length
528 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 */
Paul Bakker48916f92012-09-16 19:57:18 +0000530 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Paul Bakker0a597072012-09-25 21:55:46 +0000532 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
533 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200534 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200536 }
537
Paul Bakkera503a632013-08-14 13:48:06 +0200538#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200539 /*
540 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
541 * generate and include a Session ID in the TLS ClientHello."
542 */
543 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
544 ssl->session_negotiate->ticket != NULL &&
545 ssl->session_negotiate->ticket_len != 0 )
546 {
547 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
548
549 if( ret != 0 )
550 return( ret );
551
552 ssl->session_negotiate->length = n = 32;
553 }
Paul Bakkera503a632013-08-14 13:48:06 +0200554#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556 *p++ = (unsigned char) n;
557
558 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000559 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
561 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
562 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
563
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200564 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200565 n = 0;
566 q = p;
567
568 // Skip writing ciphersuite length for now
569 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Paul Bakker48916f92012-09-16 19:57:18 +0000571 /*
572 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
573 */
574 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
575 {
576 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
577 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200578 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000579 }
580
Paul Bakker2fbefde2013-06-29 16:01:15 +0200581 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200583 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
584
585 if( ciphersuite_info == NULL )
586 continue;
587
588 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
589 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
590 continue;
591
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100592#if defined(POLARSSL_SSL_PROTO_DTLS)
593 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
594 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
595 continue;
596#endif
597
Paul Bakkere3166ce2011-01-27 17:40:50 +0000598 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200599 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Paul Bakker2fbefde2013-06-29 16:01:15 +0200601 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200602 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
603 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 }
605
Paul Bakker2fbefde2013-06-29 16:01:15 +0200606 *q++ = (unsigned char)( n >> 7 );
607 *q++ = (unsigned char)( n << 1 );
608
609 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
610
611
Paul Bakker2770fbd2012-07-03 13:30:23 +0000612#if defined(POLARSSL_ZLIB_SUPPORT)
613 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
614 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000615 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000616
617 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000618 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000619 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000620#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000622 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
624 *p++ = 1;
625 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200626#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Paul Bakkerd3edc862013-03-20 16:07:17 +0100628 // First write extensions, then the total length
629 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200630#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100631 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
632 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200633#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
Paul Bakkerd3edc862013-03-20 16:07:17 +0100635 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
636 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000637
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200638#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100639 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
640 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200641#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000642
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200643#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100644 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
645 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100646
Paul Bakkerd3edc862013-03-20 16:07:17 +0100647 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
648 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100649#endif
650
Paul Bakker05decb22013-08-15 13:33:48 +0200651#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200652 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
653 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200654#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200655
Paul Bakker1f2bc622013-08-15 13:45:55 +0200656#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200657 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
658 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200659#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200660
Paul Bakkera503a632013-08-14 13:48:06 +0200661#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200662 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
663 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200664#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200665
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200666#if defined(POLARSSL_SSL_ALPN)
667 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
668 ext_len += olen;
669#endif
670
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000671 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
672 ext_len ) );
673
Paul Bakkera7036632014-04-30 10:15:38 +0200674 if( ext_len > 0 )
675 {
676 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
677 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
678 p += ext_len;
679 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100680
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 ssl->out_msglen = p - buf;
682 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
683 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
684
685 ssl->state++;
686
687 if( ( ret = ssl_write_record( ssl ) ) != 0 )
688 {
689 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
690 return( ret );
691 }
692
693 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
694
695 return( 0 );
696}
697
Paul Bakker48916f92012-09-16 19:57:18 +0000698static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200699 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000700 size_t len )
701{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000702 int ret;
703
Paul Bakker48916f92012-09-16 19:57:18 +0000704 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
705 {
706 if( len != 1 || buf[0] != 0x0 )
707 {
708 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000709
710 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
711 return( ret );
712
Paul Bakker48916f92012-09-16 19:57:18 +0000713 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
714 }
715
716 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
717 }
718 else
719 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100720 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000721 if( len != 1 + ssl->verify_data_len * 2 ||
722 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100723 safer_memcmp( buf + 1,
724 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
725 safer_memcmp( buf + 1 + ssl->verify_data_len,
726 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000727 {
728 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000729
730 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
731 return( ret );
732
Paul Bakker48916f92012-09-16 19:57:18 +0000733 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
734 }
735 }
736
737 return( 0 );
738}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200739
Paul Bakker05decb22013-08-15 13:33:48 +0200740#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200741static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200742 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200743 size_t len )
744{
745 /*
746 * server should use the extension only if we did,
747 * and if so the server's value should match ours (and len is always 1)
748 */
749 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
750 len != 1 ||
751 buf[0] != ssl->mfl_code )
752 {
753 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
754 }
755
756 return( 0 );
757}
Paul Bakker05decb22013-08-15 13:33:48 +0200758#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000759
Paul Bakker1f2bc622013-08-15 13:45:55 +0200760#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200761static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
762 const unsigned char *buf,
763 size_t len )
764{
765 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
766 len != 0 )
767 {
768 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
769 }
770
771 ((void) buf);
772
773 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
774
775 return( 0 );
776}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200777#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200778
Paul Bakkera503a632013-08-14 13:48:06 +0200779#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200780static int ssl_parse_session_ticket_ext( ssl_context *ssl,
781 const unsigned char *buf,
782 size_t len )
783{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200784 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
785 len != 0 )
786 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200787 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200788 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200789
790 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200791
792 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200793
794 return( 0 );
795}
Paul Bakkera503a632013-08-14 13:48:06 +0200796#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200797
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200798#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200799static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
800 const unsigned char *buf,
801 size_t len )
802{
803 size_t list_size;
804 const unsigned char *p;
805
806 list_size = buf[0];
807 if( list_size + 1 != len )
808 {
809 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
810 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
811 }
812
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200813 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200814 while( list_size > 0 )
815 {
816 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
817 p[0] == POLARSSL_ECP_PF_COMPRESSED )
818 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200819 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200820 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
821 return( 0 );
822 }
823
824 list_size--;
825 p++;
826 }
827
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200828 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
829 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200830}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200831#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200832
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200833#if defined(POLARSSL_SSL_ALPN)
834static int ssl_parse_alpn_ext( ssl_context *ssl,
835 const unsigned char *buf, size_t len )
836{
837 size_t list_len, name_len;
838 const char **p;
839
840 /* If we didn't send it, the server shouldn't send it */
841 if( ssl->alpn_list == NULL )
842 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
843
844 /*
845 * opaque ProtocolName<1..2^8-1>;
846 *
847 * struct {
848 * ProtocolName protocol_name_list<2..2^16-1>
849 * } ProtocolNameList;
850 *
851 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
852 */
853
854 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
855 if( len < 4 )
856 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
857
858 list_len = ( buf[0] << 8 ) | buf[1];
859 if( list_len != len - 2 )
860 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
861
862 name_len = buf[2];
863 if( name_len != list_len - 1 )
864 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
865
866 /* Check that the server chosen protocol was in our list and save it */
867 for( p = ssl->alpn_list; *p != NULL; p++ )
868 {
869 if( name_len == strlen( *p ) &&
870 memcmp( buf + 3, *p, name_len ) == 0 )
871 {
872 ssl->alpn_chosen = *p;
873 return( 0 );
874 }
875 }
876
877 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
878}
879#endif /* POLARSSL_SSL_ALPN */
880
Paul Bakker5121ce52009-01-03 21:22:43 +0000881static int ssl_parse_server_hello( ssl_context *ssl )
882{
Paul Bakker2770fbd2012-07-03 13:30:23 +0000883 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +0000884 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +0200885 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000886 unsigned char *buf, *ext;
887 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000888 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200889#if defined(POLARSSL_DEBUG_C)
890 uint32_t t;
891#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000892
893 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
894
895 /*
896 * 0 . 0 handshake type
897 * 1 . 3 handshake length
898 * 4 . 5 protocol version
899 * 6 . 9 UNIX time()
900 * 10 . 37 random bytes
901 */
902 buf = ssl->in_msg;
903
904 if( ( ret = ssl_read_record( ssl ) ) != 0 )
905 {
906 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
907 return( ret );
908 }
909
910 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
911 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +0200912 if( ssl->renegotiation == SSL_RENEGOTIATION )
913 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +0200914 ssl->renego_records_seen++;
915
916 if( ssl->renego_max_records >= 0 &&
917 ssl->renego_records_seen > ssl->renego_max_records )
918 {
919 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
920 "but not honored by server" ) );
921 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
922 }
923
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +0200924 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
925 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
926 }
927
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000929 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 }
931
932 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
933 buf[4], buf[5] ) );
934
935 if( ssl->in_hslen < 42 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100936 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +0000937 {
938 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000939 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 }
941
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100942 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
943 ssl->transport, buf + 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000944
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100945 if( ssl->major_ver < ssl->min_major_ver ||
946 ssl->minor_ver < ssl->min_minor_ver ||
947 ssl->major_ver > ssl->max_major_ver ||
948 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +0000949 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100950 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
951 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
952 ssl->min_major_ver, ssl->min_minor_ver,
953 ssl->major_ver, ssl->minor_ver,
954 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000955
956 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
957 SSL_ALERT_MSG_PROTOCOL_VERSION );
958
959 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
960 }
961
Paul Bakker1504af52012-02-11 16:17:43 +0000962#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200963 t = ( (uint32_t) buf[6] << 24 )
964 | ( (uint32_t) buf[7] << 16 )
965 | ( (uint32_t) buf[8] << 8 )
966 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200967 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +0000968#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000969
Paul Bakker48916f92012-09-16 19:57:18 +0000970 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
972 n = buf[38];
973
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
975
Paul Bakker48916f92012-09-16 19:57:18 +0000976 if( n > 32 )
977 {
978 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
979 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
980 }
981
Paul Bakker5121ce52009-01-03 21:22:43 +0000982 /*
983 * 38 . 38 session id length
984 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000985 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 * 41+n . 41+n chosen compression alg.
987 * 42+n . 43+n extensions length
988 * 44+n . 44+n+m extensions
989 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +0200990 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 {
992 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +0000993 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
Paul Bakker48916f92012-09-16 19:57:18 +0000995 if( ( ext_len > 0 && ext_len < 4 ) ||
996 ssl->in_hslen != 44 + n + ext_len )
997 {
998 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
999 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1000 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001002 else if( ssl->in_hslen == 42 + n )
1003 {
1004 ext_len = 0;
1005 }
1006 else
1007 {
1008 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1009 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1010 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001011
1012 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001013 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
Paul Bakker380da532012-04-18 16:10:25 +00001015 /*
1016 * Initialize update checksum functions
1017 */
Paul Bakker68884e32013-01-07 18:20:04 +01001018 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1019
1020 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1021 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001022 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001023 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1024 }
Paul Bakker380da532012-04-18 16:10:25 +00001025
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001026 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1027
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1029 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1030
1031 /*
1032 * Check if the session can be resumed
1033 */
Paul Bakker0a597072012-09-25 21:55:46 +00001034 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1035 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +00001036 ssl->session_negotiate->ciphersuite != i ||
1037 ssl->session_negotiate->compression != comp ||
1038 ssl->session_negotiate->length != n ||
1039 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 {
1041 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001042 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001043#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001044 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001045#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001046 ssl->session_negotiate->ciphersuite = i;
1047 ssl->session_negotiate->compression = comp;
1048 ssl->session_negotiate->length = n;
1049 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 }
1051 else
1052 {
1053 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001054
1055 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1056 {
1057 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1058 return( ret );
1059 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 }
1061
1062 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001063 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001064
Paul Bakkere3166ce2011-01-27 17:40:50 +00001065 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1067
1068 i = 0;
1069 while( 1 )
1070 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001071 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 {
1073 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001074 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 }
1076
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001077 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1078 ssl->session_negotiate->ciphersuite )
1079 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001081 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 }
1083
Paul Bakker2770fbd2012-07-03 13:30:23 +00001084 if( comp != SSL_COMPRESS_NULL
1085#if defined(POLARSSL_ZLIB_SUPPORT)
1086 && comp != SSL_COMPRESS_DEFLATE
1087#endif
1088 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 {
1090 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001091 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001092 }
Paul Bakker48916f92012-09-16 19:57:18 +00001093 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Paul Bakker48916f92012-09-16 19:57:18 +00001095 ext = buf + 44 + n;
1096
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001097 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1098
Paul Bakker48916f92012-09-16 19:57:18 +00001099 while( ext_len )
1100 {
1101 unsigned int ext_id = ( ( ext[0] << 8 )
1102 | ( ext[1] ) );
1103 unsigned int ext_size = ( ( ext[2] << 8 )
1104 | ( ext[3] ) );
1105
1106 if( ext_size + 4 > ext_len )
1107 {
1108 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1109 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1110 }
1111
1112 switch( ext_id )
1113 {
1114 case TLS_EXT_RENEGOTIATION_INFO:
1115 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1116 renegotiation_info_seen = 1;
1117
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001118 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1119 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001120 return( ret );
1121
1122 break;
1123
Paul Bakker05decb22013-08-15 13:33:48 +02001124#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001125 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1126 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1127
1128 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1129 ext + 4, ext_size ) ) != 0 )
1130 {
1131 return( ret );
1132 }
1133
1134 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001135#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001136
Paul Bakker1f2bc622013-08-15 13:45:55 +02001137#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001138 case TLS_EXT_TRUNCATED_HMAC:
1139 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1140
1141 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1142 ext + 4, ext_size ) ) != 0 )
1143 {
1144 return( ret );
1145 }
1146
1147 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001148#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001149
Paul Bakkera503a632013-08-14 13:48:06 +02001150#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001151 case TLS_EXT_SESSION_TICKET:
1152 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1153
1154 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1155 ext + 4, ext_size ) ) != 0 )
1156 {
1157 return( ret );
1158 }
1159
1160 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001161#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001162
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001163#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001164 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1165 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1166
1167 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1168 ext + 4, ext_size ) ) != 0 )
1169 {
1170 return( ret );
1171 }
1172
1173 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001174#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001175
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001176#if defined(POLARSSL_SSL_ALPN)
1177 case TLS_EXT_ALPN:
1178 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1179
1180 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1181 return( ret );
1182
1183 break;
1184#endif /* POLARSSL_SSL_ALPN */
1185
Paul Bakker48916f92012-09-16 19:57:18 +00001186 default:
1187 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1188 ext_id ) );
1189 }
1190
1191 ext_len -= 4 + ext_size;
1192 ext += 4 + ext_size;
1193
1194 if( ext_len > 0 && ext_len < 4 )
1195 {
1196 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1197 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1198 }
1199 }
1200
1201 /*
1202 * Renegotiation security checks
1203 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001204 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1205 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001206 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001207 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1208 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001209 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001210 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1211 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1212 renegotiation_info_seen == 0 )
1213 {
1214 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1215 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001216 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001217 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1218 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1219 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001220 {
1221 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001222 handshake_failure = 1;
1223 }
1224 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1225 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1226 renegotiation_info_seen == 1 )
1227 {
1228 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1229 handshake_failure = 1;
1230 }
1231
1232 if( handshake_failure == 1 )
1233 {
1234 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1235 return( ret );
1236
Paul Bakker48916f92012-09-16 19:57:18 +00001237 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1238 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
1240 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1241
1242 return( 0 );
1243}
1244
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001245#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1246 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001247static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1248 unsigned char *end )
1249{
1250 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1251
Paul Bakker29e1f122013-04-16 13:07:56 +02001252 /*
1253 * Ephemeral DH parameters:
1254 *
1255 * struct {
1256 * opaque dh_p<1..2^16-1>;
1257 * opaque dh_g<1..2^16-1>;
1258 * opaque dh_Ys<1..2^16-1>;
1259 * } ServerDHParams;
1260 */
1261 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1262 {
1263 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1264 return( ret );
1265 }
1266
1267 if( ssl->handshake->dhm_ctx.len < 64 ||
1268 ssl->handshake->dhm_ctx.len > 512 )
1269 {
1270 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1271 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1272 }
1273
1274 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1275 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1276 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001277
1278 return( ret );
1279}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001280#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1281 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001282
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001283#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001284 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001285 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1286 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1287 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1288static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1289{
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001290 const ecp_curve_info *curve_info;
1291
1292 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1293 if( curve_info == NULL )
1294 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001295 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1296 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001297 }
1298
1299 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001300
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001301#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1302 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1303#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001304 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1305 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001306#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001307 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001308
1309 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1310
1311 return( 0 );
1312}
Paul Bakker9af723c2014-05-01 13:03:14 +02001313#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1314 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1315 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1316 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1317 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001318
1319#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1320 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001321 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001322static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1323 unsigned char **p,
1324 unsigned char *end )
1325{
1326 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1327
Paul Bakker29e1f122013-04-16 13:07:56 +02001328 /*
1329 * Ephemeral ECDH parameters:
1330 *
1331 * struct {
1332 * ECParameters curve_params;
1333 * ECPoint public;
1334 * } ServerECDHParams;
1335 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001336 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1337 (const unsigned char **) p, end ) ) != 0 )
1338 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001339 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001340 return( ret );
1341 }
1342
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001343 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001344 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001345 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001346 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1347 }
1348
Paul Bakker29e1f122013-04-16 13:07:56 +02001349 return( ret );
1350}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001351#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001352 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1353 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001354
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001355#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001356static int ssl_parse_server_psk_hint( ssl_context *ssl,
1357 unsigned char **p,
1358 unsigned char *end )
1359{
1360 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001361 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001362 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001363
1364 /*
1365 * PSK parameters:
1366 *
1367 * opaque psk_identity_hint<0..2^16-1>;
1368 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001369 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001370 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001371
1372 if( (*p) + len > end )
1373 {
1374 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1375 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1376 }
1377
1378 // TODO: Retrieve PSK identity hint and callback to app
1379 //
1380 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001381 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001382
1383 return( ret );
1384}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001385#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001386
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001387#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1388 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1389/*
1390 * Generate a pre-master secret and encrypt it with the server's RSA key
1391 */
1392static int ssl_write_encrypted_pms( ssl_context *ssl,
1393 size_t offset, size_t *olen,
1394 size_t pms_offset )
1395{
1396 int ret;
1397 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1398 unsigned char *p = ssl->handshake->premaster + pms_offset;
1399
1400 /*
1401 * Generate (part of) the pre-master as
1402 * struct {
1403 * ProtocolVersion client_version;
1404 * opaque random[46];
1405 * } PreMasterSecret;
1406 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001407 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1408 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001409
1410 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1411 {
1412 SSL_DEBUG_RET( 1, "f_rng", ret );
1413 return( ret );
1414 }
1415
1416 ssl->handshake->pmslen = 48;
1417
1418 /*
1419 * Now write it out, encrypted
1420 */
1421 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1422 POLARSSL_PK_RSA ) )
1423 {
1424 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1425 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1426 }
1427
1428 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1429 p, ssl->handshake->pmslen,
1430 ssl->out_msg + offset + len_bytes, olen,
1431 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1432 ssl->f_rng, ssl->p_rng ) ) != 0 )
1433 {
1434 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1435 return( ret );
1436 }
1437
1438#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1439 defined(POLARSSL_SSL_PROTO_TLS1_2)
1440 if( len_bytes == 2 )
1441 {
1442 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1443 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1444 *olen += 2;
1445 }
1446#endif
1447
1448 return( 0 );
1449}
1450#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1451 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001452
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001453#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001454#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001455 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1456 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001457static int ssl_parse_signature_algorithm( ssl_context *ssl,
1458 unsigned char **p,
1459 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001460 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001461 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001462{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001463 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001464 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001465 *pk_alg = POLARSSL_PK_NONE;
1466
1467 /* Only in TLS 1.2 */
1468 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1469 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001470 return( 0 );
1471 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001472
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001473 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001474 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1475
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001476 /*
1477 * Get hash algorithm
1478 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001479 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001480 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001481 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1482 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001483 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1484 }
1485
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001486 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001487 * Get signature algorithm
1488 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001489 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001490 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001491 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1492 "SignatureAlgorithm %d", (*p)[1] ) );
1493 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001494 }
1495
1496 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1497 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1498 *p += 2;
1499
1500 return( 0 );
1501}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001502#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001503 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1504 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001505#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001506
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001507
1508#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1509 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1510static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1511{
1512 int ret;
1513 const ecp_keypair *peer_key;
1514
1515 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1516 POLARSSL_PK_ECKEY ) )
1517 {
1518 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1519 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1520 }
1521
1522 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1523
1524 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1525 POLARSSL_ECDH_THEIRS ) ) != 0 )
1526 {
1527 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1528 return( ret );
1529 }
1530
1531 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1532 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001533 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001534 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1535 }
1536
1537 return( ret );
1538}
1539#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1540 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1541
Paul Bakker41c83d32013-03-20 14:39:14 +01001542static int ssl_parse_server_key_exchange( ssl_context *ssl )
1543{
Paul Bakker23986e52011-04-24 08:57:21 +00001544 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001545 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001546 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001547#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001548 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1549 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001550 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001551 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001552 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001553 size_t hashlen;
1554 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001555#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001556
1557 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1558
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001559#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001560 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 {
1562 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1563 ssl->state++;
1564 return( 0 );
1565 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001566 ((void) p);
1567 ((void) end);
1568#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001569
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001570#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1571 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1572 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1573 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1574 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001575 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1576 {
1577 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1578 return( ret );
1579 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001580
1581 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1582 ssl->state++;
1583 return( 0 );
1584 }
1585 ((void) p);
1586 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001587#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1588 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001589
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1591 {
1592 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1593 return( ret );
1594 }
1595
1596 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1597 {
1598 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001599 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 }
1601
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001602 /*
1603 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1604 * doesn't use a psk_identity_hint
1605 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001606 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1607 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001608 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1609 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001610 {
1611 ssl->record_read = 1;
1612 goto exit;
1613 }
1614
1615 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1616 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 }
1618
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001619 p = ssl->in_msg + 4;
1620 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001621 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001622
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001623#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1624 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1625 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1626 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1627 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1628 {
1629 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1630 {
1631 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1632 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1633 }
1634 } /* FALLTROUGH */
1635#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1636
1637#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1638 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1639 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1640 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1641 ; /* nothing more to do */
1642 else
1643#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1644 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1645#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1646 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1647 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1648 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001650 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001651 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001652 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001653 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1654 }
1655 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001656 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001657#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1658 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001659#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001660 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001661 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1662 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001663 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001664 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001665 {
1666 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1667 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001668 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1669 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1670 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001671 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001672 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001673#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001674 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001675 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001676 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001677 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001678 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001679 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001680
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001681#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001682 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1683 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001684 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001685 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1686 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001687 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001688 params_len = p - ( ssl->in_msg + 4 );
1689
Paul Bakker29e1f122013-04-16 13:07:56 +02001690 /*
1691 * Handle the digitally-signed structure
1692 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001693#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1694 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001695 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001696 if( ssl_parse_signature_algorithm( ssl, &p, end,
1697 &md_alg, &pk_alg ) != 0 )
1698 {
1699 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1700 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1701 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001702
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001703 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001704 {
1705 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1706 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1707 }
1708 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001709 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001710#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001711#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1712 defined(POLARSSL_SSL_PROTO_TLS1_1)
1713 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001714 {
1715 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001716
Paul Bakker9659dae2013-08-28 16:21:34 +02001717 /* Default hash for ECDSA is SHA-1 */
1718 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1719 md_alg = POLARSSL_MD_SHA1;
1720 }
1721 else
1722#endif
1723 {
1724 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001725 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001726 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001727
1728 /*
1729 * Read signature
1730 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001731 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001732 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001733
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001734 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001735 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001736 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001737 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001739
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001740 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001741
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001742 /*
1743 * Compute the hash that has been signed
1744 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001745#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1746 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001747 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001748 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001749 md5_context md5;
1750 sha1_context sha1;
1751
Paul Bakker5b4af392014-06-26 12:09:34 +02001752 md5_init( &md5 );
1753 sha1_init( &sha1 );
1754
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001755 hashlen = 36;
1756
Paul Bakker29e1f122013-04-16 13:07:56 +02001757 /*
1758 * digitally-signed struct {
1759 * opaque md5_hash[16];
1760 * opaque sha_hash[20];
1761 * };
1762 *
1763 * md5_hash
1764 * MD5(ClientHello.random + ServerHello.random
1765 * + ServerParams);
1766 * sha_hash
1767 * SHA(ClientHello.random + ServerHello.random
1768 * + ServerParams);
1769 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001770 md5_starts( &md5 );
1771 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001772 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001773 md5_finish( &md5, hash );
1774
1775 sha1_starts( &sha1 );
1776 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001777 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001778 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001779
1780 md5_free( &md5 );
1781 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001782 }
1783 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001784#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1785 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001786#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1787 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001788 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001789 {
1790 md_context_t ctx;
1791
Paul Bakker84bbeb52014-07-01 14:53:22 +02001792 md_init( &ctx );
1793
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001794 /* Info from md_alg will be used instead */
1795 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001796
1797 /*
1798 * digitally-signed struct {
1799 * opaque client_random[32];
1800 * opaque server_random[32];
1801 * ServerDHParams params;
1802 * };
1803 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001804 if( ( ret = md_init_ctx( &ctx,
1805 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001806 {
1807 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1808 return( ret );
1809 }
1810
1811 md_starts( &ctx );
1812 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001813 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001814 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001815 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001816 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001817 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001818#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1819 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001820 {
Paul Bakker577e0062013-08-28 11:57:20 +02001821 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001822 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001823 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001824
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001825 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1826 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001827
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001828 /*
1829 * Verify signature
1830 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001831 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001832 {
1833 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1834 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1835 }
1836
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001837 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1838 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001839 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001840 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001841 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001842 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001844#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001845 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1846 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001848exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 ssl->state++;
1850
1851 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
1852
1853 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854}
1855
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001856#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1857 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1858 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1859 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1860static int ssl_parse_certificate_request( ssl_context *ssl )
1861{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001862 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
1863
1864 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1865
1866 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1867 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1868 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1869 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1870 {
1871 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
1872 ssl->state++;
1873 return( 0 );
1874 }
1875
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001876 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1877 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001878}
1879#else
Paul Bakker5121ce52009-01-03 21:22:43 +00001880static int ssl_parse_certificate_request( ssl_context *ssl )
1881{
1882 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01001883 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001884 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001885 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001886 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
1888 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1889
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001890 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1891 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1892 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1893 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1894 {
1895 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
1896 ssl->state++;
1897 return( 0 );
1898 }
1899
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 /*
1901 * 0 . 0 handshake type
1902 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01001903 * 4 . 4 cert type count
1904 * 5 .. m-1 cert types
1905 * m .. m+1 sig alg length (TLS 1.2 only)
1906 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 * n .. n+1 length of all DNs
1908 * n+2 .. n+3 length of DN 1
1909 * n+4 .. ... Distinguished Name #1
1910 * ... .. ... length of DN 2, etc.
1911 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001912 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001914 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1915 {
1916 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1917 return( ret );
1918 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001919
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001920 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1921 {
1922 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1923 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1924 }
1925
1926 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 }
1928
1929 ssl->client_auth = 0;
1930 ssl->state++;
1931
1932 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
1933 ssl->client_auth++;
1934
1935 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
1936 ssl->client_auth ? "a" : "no" ) );
1937
Paul Bakker926af752012-11-23 13:38:07 +01001938 if( ssl->client_auth == 0 )
1939 goto exit;
1940
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001941 ssl->record_read = 0;
1942
Paul Bakker926af752012-11-23 13:38:07 +01001943 // TODO: handshake_failure alert for an anonymous server to request
1944 // client authentication
1945
1946 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001947
Paul Bakker926af752012-11-23 13:38:07 +01001948 // Retrieve cert types
1949 //
1950 cert_type_len = buf[4];
1951 n = cert_type_len;
1952
1953 if( ssl->in_hslen < 6 + n )
1954 {
1955 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1956 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1957 }
1958
Paul Bakker73d44312013-05-22 13:56:26 +02001959 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01001960 while( cert_type_len > 0 )
1961 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001962#if defined(POLARSSL_RSA_C)
1963 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001964 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01001965 {
1966 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
1967 break;
1968 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001969 else
1970#endif
1971#if defined(POLARSSL_ECDSA_C)
1972 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001973 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001974 {
1975 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
1976 break;
1977 }
1978 else
1979#endif
1980 {
1981 ; /* Unsupported cert type, ignore */
1982 }
Paul Bakker926af752012-11-23 13:38:07 +01001983
1984 cert_type_len--;
1985 p++;
1986 }
1987
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001988#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001989 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1990 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001991 /* Ignored, see comments about hash in write_certificate_verify */
1992 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001993 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
1994 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01001995
1996 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001997 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01001998 n += sig_alg_len;
1999
2000 if( ssl->in_hslen < 6 + n )
2001 {
2002 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2003 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2004 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002005 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002006#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002007
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002008 /* Ignore certificate_authorities, we only have one cert anyway */
2009 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002010 dn_len = ( ( buf[5 + m + n] << 8 )
2011 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002012
2013 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002014 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002015 {
2016 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2017 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2018 }
2019
2020exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2022
2023 return( 0 );
2024}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002025#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2026 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2027 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2028 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002029
2030static int ssl_parse_server_hello_done( ssl_context *ssl )
2031{
2032 int ret;
2033
2034 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2035
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002036 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 {
2038 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2039 {
2040 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2041 return( ret );
2042 }
2043
2044 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2045 {
2046 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002047 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 }
2049 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002050 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002051
2052 if( ssl->in_hslen != 4 ||
2053 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2054 {
2055 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002056 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 }
2058
2059 ssl->state++;
2060
2061 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2062
2063 return( 0 );
2064}
2065
2066static int ssl_write_client_key_exchange( ssl_context *ssl )
2067{
Paul Bakker23986e52011-04-24 08:57:21 +00002068 int ret;
2069 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002070 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
2072 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2073
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002074#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002075 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 /*
2078 * DHM key exchange -- send G^X mod P
2079 */
Paul Bakker48916f92012-09-16 19:57:18 +00002080 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
2082 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2083 ssl->out_msg[5] = (unsigned char)( n );
2084 i = 6;
2085
Paul Bakker29b64762012-09-25 09:36:44 +00002086 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002087 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 &ssl->out_msg[i], n,
2089 ssl->f_rng, ssl->p_rng );
2090 if( ret != 0 )
2091 {
2092 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2093 return( ret );
2094 }
2095
Paul Bakker48916f92012-09-16 19:57:18 +00002096 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2097 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002099 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002100
Paul Bakker48916f92012-09-16 19:57:18 +00002101 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2102 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002103 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002104 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 {
2106 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2107 return( ret );
2108 }
2109
Paul Bakker48916f92012-09-16 19:57:18 +00002110 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 }
2112 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002113#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002114#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002115 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2116 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2117 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002118 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002119 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2120 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2121 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002122 {
2123 /*
2124 * ECDH key exchange -- send client public value
2125 */
2126 i = 4;
2127
2128 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2129 &n,
2130 &ssl->out_msg[i], 1000,
2131 ssl->f_rng, ssl->p_rng );
2132 if( ret != 0 )
2133 {
2134 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2135 return( ret );
2136 }
2137
2138 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2139
2140 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2141 &ssl->handshake->pmslen,
2142 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002143 POLARSSL_MPI_MAX_SIZE,
2144 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002145 {
2146 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2147 return( ret );
2148 }
2149
2150 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2151 }
2152 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002153#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002154 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2155 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2156 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002157#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002158 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002159 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002160 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2161 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002162 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002163 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002164 * opaque psk_identity<0..2^16-1>;
2165 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002166 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002167 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2168
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002169 i = 4;
2170 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002171 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2172 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002173
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002174 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2175 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002176
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002177#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002178 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002179 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002180 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002181 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002182 else
2183#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002184#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2185 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2186 {
2187 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2188 return( ret );
2189 }
2190 else
2191#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002192#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002193 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002194 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002195 /*
2196 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2197 */
2198 n = ssl->handshake->dhm_ctx.len;
2199 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2200 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002201
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002202 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002203 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002204 &ssl->out_msg[i], n,
2205 ssl->f_rng, ssl->p_rng );
2206 if( ret != 0 )
2207 {
2208 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2209 return( ret );
2210 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002211 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002212 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002213#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002214#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002215 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002216 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002217 /*
2218 * ClientECDiffieHellmanPublic public;
2219 */
2220 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2221 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2222 ssl->f_rng, ssl->p_rng );
2223 if( ret != 0 )
2224 {
2225 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2226 return( ret );
2227 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002228
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002229 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2230 }
2231 else
2232#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2233 {
2234 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002235 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002236 }
2237
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002238 if( ( ret = ssl_psk_derive_premaster( ssl,
2239 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002240 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002241 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002242 return( ret );
2243 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002244 }
2245 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002246#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002247#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002248 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002250 i = 4;
2251 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002252 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 }
Paul Bakkered27a042013-04-18 22:46:23 +02002254 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002255#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002256 {
2257 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002258 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002259 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002260 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Paul Bakkerff60ee62010-03-16 21:09:09 +00002262 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2263 {
2264 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2265 return( ret );
2266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
2268 ssl->out_msglen = i + n;
2269 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2270 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2271
2272 ssl->state++;
2273
2274 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2275 {
2276 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2277 return( ret );
2278 }
2279
2280 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2281
2282 return( 0 );
2283}
2284
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002285#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2286 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002287 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2288 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002289static int ssl_write_certificate_verify( ssl_context *ssl )
2290{
Paul Bakkered27a042013-04-18 22:46:23 +02002291 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
2293 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2294
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002295 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002296 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002297 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002298 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002299 {
2300 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2301 ssl->state++;
2302 return( 0 );
2303 }
2304
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002305 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2306 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307}
2308#else
2309static int ssl_write_certificate_verify( ssl_context *ssl )
2310{
2311 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2312 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2313 size_t n = 0, offset = 0;
2314 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002315 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002316 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002317 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002318
2319 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2320
2321 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002322 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002323 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002324 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2325 {
2326 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2327 ssl->state++;
2328 return( 0 );
2329 }
2330
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002331 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 {
2333 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2334 ssl->state++;
2335 return( 0 );
2336 }
2337
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002338 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002339 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002340 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2341 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 }
2343
2344 /*
2345 * Make an RSA signature of the handshake digests
2346 */
Paul Bakker48916f92012-09-16 19:57:18 +00002347 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002348
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002349#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2350 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002351 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002352 {
Paul Bakker926af752012-11-23 13:38:07 +01002353 /*
2354 * digitally-signed struct {
2355 * opaque md5_hash[16];
2356 * opaque sha_hash[20];
2357 * };
2358 *
2359 * md5_hash
2360 * MD5(handshake_messages);
2361 *
2362 * sha_hash
2363 * SHA(handshake_messages);
2364 */
2365 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002366 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002367
2368 /*
2369 * For ECDSA, default hash is SHA-1 only
2370 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002371 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002372 {
2373 hash_start += 16;
2374 hashlen -= 16;
2375 md_alg = POLARSSL_MD_SHA1;
2376 }
Paul Bakker926af752012-11-23 13:38:07 +01002377 }
2378 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002379#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2380 POLARSSL_SSL_PROTO_TLS1_1 */
2381#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2382 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002383 {
2384 /*
2385 * digitally-signed struct {
2386 * opaque handshake_messages[handshake_messages_length];
2387 * };
2388 *
2389 * Taking shortcut here. We assume that the server always allows the
2390 * PRF Hash function and has sent it in the allowed signature
2391 * algorithms list received in the Certificate Request message.
2392 *
2393 * Until we encounter a server that does not, we will take this
2394 * shortcut.
2395 *
2396 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2397 * in order to satisfy 'weird' needs from the server side.
2398 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002399 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2400 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002401 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002402 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002403 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002404 }
2405 else
2406 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002407 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002408 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002409 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002410 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002411
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002412 /* Info from md_alg will be used instead */
2413 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002414 offset = 2;
2415 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002416 else
2417#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002418 {
2419 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002420 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002421 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002422
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002423 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002424 ssl->out_msg + 6 + offset, &n,
2425 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002426 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002427 SSL_DEBUG_RET( 1, "pk_sign", ret );
2428 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002429 }
Paul Bakker926af752012-11-23 13:38:07 +01002430
Paul Bakker1ef83d62012-04-11 12:09:53 +00002431 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2432 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
Paul Bakker1ef83d62012-04-11 12:09:53 +00002434 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2436 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2437
2438 ssl->state++;
2439
2440 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2441 {
2442 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2443 return( ret );
2444 }
2445
2446 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2447
Paul Bakkered27a042013-04-18 22:46:23 +02002448 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002449}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002450#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2451 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2452 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
Paul Bakkera503a632013-08-14 13:48:06 +02002454#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002455static int ssl_parse_new_session_ticket( ssl_context *ssl )
2456{
2457 int ret;
2458 uint32_t lifetime;
2459 size_t ticket_len;
2460 unsigned char *ticket;
2461
2462 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2463
2464 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2465 {
2466 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2467 return( ret );
2468 }
2469
2470 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2471 {
2472 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2473 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2474 }
2475
2476 /*
2477 * struct {
2478 * uint32 ticket_lifetime_hint;
2479 * opaque ticket<0..2^16-1>;
2480 * } NewSessionTicket;
2481 *
2482 * 0 . 0 handshake message type
2483 * 1 . 3 handshake message length
2484 * 4 . 7 ticket_lifetime_hint
2485 * 8 . 9 ticket_len (n)
2486 * 10 . 9+n ticket content
2487 */
2488 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2489 ssl->in_hslen < 10 )
2490 {
2491 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2492 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2493 }
2494
2495 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2496 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2497
2498 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2499
2500 if( ticket_len + 10 != ssl->in_hslen )
2501 {
2502 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2503 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2504 }
2505
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002506 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2507
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002508 /* We're not waiting for a NewSessionTicket message any more */
2509 ssl->handshake->new_session_ticket = 0;
2510
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002511 /*
2512 * Zero-length ticket means the server changed his mind and doesn't want
2513 * to send a ticket after all, so just forget it
2514 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002515 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002516 return( 0 );
2517
Paul Bakker34617722014-06-13 17:20:13 +02002518 polarssl_zeroize( ssl->session_negotiate->ticket,
2519 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002520 polarssl_free( ssl->session_negotiate->ticket );
2521 ssl->session_negotiate->ticket = NULL;
2522 ssl->session_negotiate->ticket_len = 0;
2523
2524 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2525 {
2526 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2527 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2528 }
2529
2530 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2531
2532 ssl->session_negotiate->ticket = ticket;
2533 ssl->session_negotiate->ticket_len = ticket_len;
2534 ssl->session_negotiate->ticket_lifetime = lifetime;
2535
2536 /*
2537 * RFC 5077 section 3.4:
2538 * "If the client receives a session ticket from the server, then it
2539 * discards any Session ID that was sent in the ServerHello."
2540 */
2541 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2542 ssl->session_negotiate->length = 0;
2543
2544 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2545
2546 return( 0 );
2547}
Paul Bakkera503a632013-08-14 13:48:06 +02002548#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002549
Paul Bakker5121ce52009-01-03 21:22:43 +00002550/*
Paul Bakker1961b702013-01-25 14:49:24 +01002551 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 */
Paul Bakker1961b702013-01-25 14:49:24 +01002553int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002554{
2555 int ret = 0;
2556
Paul Bakker1961b702013-01-25 14:49:24 +01002557 if( ssl->state == SSL_HANDSHAKE_OVER )
2558 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Paul Bakker1961b702013-01-25 14:49:24 +01002560 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2561
2562 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2563 return( ret );
2564
2565 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 {
Paul Bakker1961b702013-01-25 14:49:24 +01002567 case SSL_HELLO_REQUEST:
2568 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 break;
2570
Paul Bakker1961b702013-01-25 14:49:24 +01002571 /*
2572 * ==> ClientHello
2573 */
2574 case SSL_CLIENT_HELLO:
2575 ret = ssl_write_client_hello( ssl );
2576 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Paul Bakker1961b702013-01-25 14:49:24 +01002578 /*
2579 * <== ServerHello
2580 * Certificate
2581 * ( ServerKeyExchange )
2582 * ( CertificateRequest )
2583 * ServerHelloDone
2584 */
2585 case SSL_SERVER_HELLO:
2586 ret = ssl_parse_server_hello( ssl );
2587 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
Paul Bakker1961b702013-01-25 14:49:24 +01002589 case SSL_SERVER_CERTIFICATE:
2590 ret = ssl_parse_certificate( ssl );
2591 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Paul Bakker1961b702013-01-25 14:49:24 +01002593 case SSL_SERVER_KEY_EXCHANGE:
2594 ret = ssl_parse_server_key_exchange( ssl );
2595 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
Paul Bakker1961b702013-01-25 14:49:24 +01002597 case SSL_CERTIFICATE_REQUEST:
2598 ret = ssl_parse_certificate_request( ssl );
2599 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Paul Bakker1961b702013-01-25 14:49:24 +01002601 case SSL_SERVER_HELLO_DONE:
2602 ret = ssl_parse_server_hello_done( ssl );
2603 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Paul Bakker1961b702013-01-25 14:49:24 +01002605 /*
2606 * ==> ( Certificate/Alert )
2607 * ClientKeyExchange
2608 * ( CertificateVerify )
2609 * ChangeCipherSpec
2610 * Finished
2611 */
2612 case SSL_CLIENT_CERTIFICATE:
2613 ret = ssl_write_certificate( ssl );
2614 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Paul Bakker1961b702013-01-25 14:49:24 +01002616 case SSL_CLIENT_KEY_EXCHANGE:
2617 ret = ssl_write_client_key_exchange( ssl );
2618 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Paul Bakker1961b702013-01-25 14:49:24 +01002620 case SSL_CERTIFICATE_VERIFY:
2621 ret = ssl_write_certificate_verify( ssl );
2622 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Paul Bakker1961b702013-01-25 14:49:24 +01002624 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2625 ret = ssl_write_change_cipher_spec( ssl );
2626 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
Paul Bakker1961b702013-01-25 14:49:24 +01002628 case SSL_CLIENT_FINISHED:
2629 ret = ssl_write_finished( ssl );
2630 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Paul Bakker1961b702013-01-25 14:49:24 +01002632 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002633 * <== ( NewSessionTicket )
2634 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002635 * Finished
2636 */
2637 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002638#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002639 if( ssl->handshake->new_session_ticket != 0 )
2640 ret = ssl_parse_new_session_ticket( ssl );
2641 else
Paul Bakkera503a632013-08-14 13:48:06 +02002642#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002643 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002644 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Paul Bakker1961b702013-01-25 14:49:24 +01002646 case SSL_SERVER_FINISHED:
2647 ret = ssl_parse_finished( ssl );
2648 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Paul Bakker1961b702013-01-25 14:49:24 +01002650 case SSL_FLUSH_BUFFERS:
2651 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2652 ssl->state = SSL_HANDSHAKE_WRAPUP;
2653 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Paul Bakker1961b702013-01-25 14:49:24 +01002655 case SSL_HANDSHAKE_WRAPUP:
2656 ssl_handshake_wrapup( ssl );
2657 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002658
Paul Bakker1961b702013-01-25 14:49:24 +01002659 default:
2660 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2661 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
2664 return( ret );
2665}
Paul Bakker9af723c2014-05-01 13:03:14 +02002666#endif /* POLARSSL_SSL_CLI_C */