blob: f0ce3778dbe90dadf39ed4f38ee50dacbcd22bad [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker7dc4c442014-02-01 22:50:26 +010037#if defined(POLARSSL_PLATFORM_C)
38#include "polarssl/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020039#else
40#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdlib.h>
45#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020046
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#include <basetsd.h>
49typedef UINT32 uint32_t;
50#else
51#include <inttypes.h>
52#endif
53
54#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakker34617722014-06-13 17:20:13 +020058#if defined(POLARSSL_SSL_SESSION_TICKETS)
59/* Implementation that should never be optimized out by the compiler */
60static void polarssl_zeroize( void *v, size_t n ) {
61 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
62}
63#endif
64
Paul Bakker0be444a2013-08-27 21:55:01 +020065#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010066static void ssl_write_hostname_ext( ssl_context *ssl,
67 unsigned char *buf,
68 size_t *olen )
69{
70 unsigned char *p = buf;
71
72 *olen = 0;
73
Paul Bakker66d5d072014-06-17 16:39:18 +020074 if( ssl->hostname == NULL )
Paul Bakkerd3edc862013-03-20 16:07:17 +010075 return;
76
77 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
78 ssl->hostname ) );
79
80 /*
81 * struct {
82 * NameType name_type;
83 * select (name_type) {
84 * case host_name: HostName;
85 * } name;
86 * } ServerName;
87 *
88 * enum {
89 * host_name(0), (255)
90 * } NameType;
91 *
92 * opaque HostName<1..2^16-1>;
93 *
94 * struct {
95 * ServerName server_name_list<1..2^16-1>
96 * } ServerNameList;
97 */
98 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
99 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
100
101 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
102 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
103
104 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
105 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
106
107 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
108 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
109 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
110
111 memcpy( p, ssl->hostname, ssl->hostname_len );
112
113 *olen = ssl->hostname_len + 9;
114}
Paul Bakker0be444a2013-08-27 21:55:01 +0200115#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100116
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100117#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100118static void ssl_write_renegotiation_ext( ssl_context *ssl,
119 unsigned char *buf,
120 size_t *olen )
121{
122 unsigned char *p = buf;
123
124 *olen = 0;
125
126 if( ssl->renegotiation != SSL_RENEGOTIATION )
127 return;
128
129 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
130
131 /*
132 * Secure renegotiation
133 */
134 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
135 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
136
137 *p++ = 0x00;
138 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
139 *p++ = ssl->verify_data_len & 0xFF;
140
141 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
142
143 *olen = 5 + ssl->verify_data_len;
144}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100145#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100146
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200147#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100148static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
149 unsigned char *buf,
150 size_t *olen )
151{
152 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100153 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200154#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
155 unsigned char *sig_alg_list = buf + 6;
156#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100157
158 *olen = 0;
159
160 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
161 return;
162
163 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
164
165 /*
166 * Prepare signature_algorithms extension (TLS 1.2)
167 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200168#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200169#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100170 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
171 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
172 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
173 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
174#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200175#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100176 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
177 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
178 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
179 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
180#endif
181#if defined(POLARSSL_SHA1_C)
182 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
183 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
184#endif
185#if defined(POLARSSL_MD5_C)
186 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
187 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
188#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200189#endif /* POLARSSL_RSA_C */
190#if defined(POLARSSL_ECDSA_C)
191#if defined(POLARSSL_SHA512_C)
192 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
193 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
194 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
195 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
196#endif
197#if defined(POLARSSL_SHA256_C)
198 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
199 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
200 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
201 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
202#endif
203#if defined(POLARSSL_SHA1_C)
204 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
205 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
206#endif
207#if defined(POLARSSL_MD5_C)
208 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
209 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
210#endif
211#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100212
213 /*
214 * enum {
215 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
216 * sha512(6), (255)
217 * } HashAlgorithm;
218 *
219 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
220 * SignatureAlgorithm;
221 *
222 * struct {
223 * HashAlgorithm hash;
224 * SignatureAlgorithm signature;
225 * } SignatureAndHashAlgorithm;
226 *
227 * SignatureAndHashAlgorithm
228 * supported_signature_algorithms<2..2^16-2>;
229 */
230 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
231 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
232
233 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
234 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
235
236 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
237 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
238
Paul Bakkerd3edc862013-03-20 16:07:17 +0100239 *olen = 6 + sig_alg_len;
240}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200241#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100242
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200243#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100244static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
245 unsigned char *buf,
246 size_t *olen )
247{
248 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100249 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100250 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100251 const ecp_curve_info *info;
252#if defined(POLARSSL_SSL_SET_CURVES)
253 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100254#else
255 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100256#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100257
258 *olen = 0;
259
260 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
261
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100262#if defined(POLARSSL_SSL_SET_CURVES)
263 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200264 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100265 info = ecp_curve_info_from_grp_id( *grp_id );
266#else
267 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
268 {
269#endif
270
271 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
272 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200273 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200274
275 if( elliptic_curve_len == 0 )
276 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100277
278 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
279 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
280
281 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
282 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
283
284 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
285 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
286
Paul Bakkerd3edc862013-03-20 16:07:17 +0100287 *olen = 6 + elliptic_curve_len;
288}
289
290static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
291 unsigned char *buf,
292 size_t *olen )
293{
294 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200295 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100296
297 *olen = 0;
298
299 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
300
301 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
302 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
303
304 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100305 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200306
307 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100308 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
309
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200310 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100311}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200312#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100313
Paul Bakker05decb22013-08-15 13:33:48 +0200314#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200315static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
316 unsigned char *buf,
317 size_t *olen )
318{
319 unsigned char *p = buf;
320
321 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
322 *olen = 0;
323 return;
324 }
325
326 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
327
328 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
329 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
330
331 *p++ = 0x00;
332 *p++ = 1;
333
334 *p++ = ssl->mfl_code;
335
336 *olen = 5;
337}
Paul Bakker05decb22013-08-15 13:33:48 +0200338#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200339
Paul Bakker1f2bc622013-08-15 13:45:55 +0200340#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200341static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
342 unsigned char *buf, size_t *olen )
343{
344 unsigned char *p = buf;
345
346 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
347 {
348 *olen = 0;
349 return;
350 }
351
352 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
353
354 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
355 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
356
357 *p++ = 0x00;
358 *p++ = 0x00;
359
360 *olen = 4;
361}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200362#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200363
Paul Bakkera503a632013-08-14 13:48:06 +0200364#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200365static void ssl_write_session_ticket_ext( ssl_context *ssl,
366 unsigned char *buf, size_t *olen )
367{
368 unsigned char *p = buf;
369 size_t tlen = ssl->session_negotiate->ticket_len;
370
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200371 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
372 {
373 *olen = 0;
374 return;
375 }
376
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200377 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
378
379 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
380 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
381
382 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
383 *p++ = (unsigned char)( ( tlen ) & 0xFF );
384
385 *olen = 4;
386
387 if( ssl->session_negotiate->ticket == NULL ||
388 ssl->session_negotiate->ticket_len == 0 )
389 {
390 return;
391 }
392
393 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
394
395 memcpy( p, ssl->session_negotiate->ticket, tlen );
396
397 *olen += tlen;
398}
Paul Bakkera503a632013-08-14 13:48:06 +0200399#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200400
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200401#if defined(POLARSSL_SSL_ALPN)
402static void ssl_write_alpn_ext( ssl_context *ssl,
403 unsigned char *buf, size_t *olen )
404{
405 unsigned char *p = buf;
406 const char **cur;
407
408 if( ssl->alpn_list == NULL )
409 {
410 *olen = 0;
411 return;
412 }
413
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200414 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200415
416 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
417 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
418
419 /*
420 * opaque ProtocolName<1..2^8-1>;
421 *
422 * struct {
423 * ProtocolName protocol_name_list<2..2^16-1>
424 * } ProtocolNameList;
425 */
426
427 /* Skip writing extension and list length for now */
428 p += 4;
429
430 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
431 {
432 *p = (unsigned char)( strlen( *cur ) & 0xFF );
433 memcpy( p + 1, *cur, *p );
434 p += 1 + *p;
435 }
436
437 *olen = p - buf;
438
439 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
440 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
441 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
442
443 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
444 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
445 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
446}
447#endif /* POLARSSL_SSL_ALPN */
448
Paul Bakker5121ce52009-01-03 21:22:43 +0000449static int ssl_write_client_hello( ssl_context *ssl )
450{
Paul Bakker23986e52011-04-24 08:57:21 +0000451 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100452 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200454 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200455#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200457#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200458 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200459 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
462
Paul Bakkera9a028e2013-11-21 17:31:06 +0100463 if( ssl->f_rng == NULL )
464 {
465 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
466 return( POLARSSL_ERR_SSL_NO_RNG );
467 }
468
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100469#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000470 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100471#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000472 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000473 ssl->major_ver = ssl->min_major_ver;
474 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000475 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
Paul Bakker490ecc82011-10-06 13:04:09 +0000477 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
478 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200479 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
480 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000481 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
483 /*
484 * 0 . 0 handshake type
485 * 1 . 3 handshake length
486 * 4 . 5 highest version supported
487 * 6 . 9 current UNIX time
488 * 10 . 37 random bytes
489 */
490 buf = ssl->out_msg;
491 p = buf + 4;
492
493 *p++ = (unsigned char) ssl->max_major_ver;
494 *p++ = (unsigned char) ssl->max_minor_ver;
495
496 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
497 buf[4], buf[5] ) );
498
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200499#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 t = time( NULL );
501 *p++ = (unsigned char)( t >> 24 );
502 *p++ = (unsigned char)( t >> 16 );
503 *p++ = (unsigned char)( t >> 8 );
504 *p++ = (unsigned char)( t );
505
506 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200507#else
508 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
509 return( ret );
510
511 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200512#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
Paul Bakkera3d195c2011-11-27 21:07:34 +0000514 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
515 return( ret );
516
517 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
Paul Bakker48916f92012-09-16 19:57:18 +0000519 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
522
523 /*
524 * 38 . 38 session id length
525 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000526 * 40+n . 41+n ciphersuitelist length
527 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000528 * .. . .. compression methods length
529 * .. . .. compression methods
530 * .. . .. extensions length
531 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 */
Paul Bakker48916f92012-09-16 19:57:18 +0000533 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100535 if( n < 16 || n > 32 ||
536#if defined(POLARSSL_SSL_RENEGOTIATION)
537 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
538#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000539 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200540 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200542 }
543
Paul Bakkera503a632013-08-14 13:48:06 +0200544#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200545 /*
546 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
547 * generate and include a Session ID in the TLS ClientHello."
548 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100549#if defined(POLARSSL_SSL_RENEGOTIATION)
550 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
551#endif
552 if( ssl->session_negotiate->ticket != NULL &&
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200553 ssl->session_negotiate->ticket_len != 0 )
554 {
555 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
556
557 if( ret != 0 )
558 return( ret );
559
560 ssl->session_negotiate->length = n = 32;
561 }
Paul Bakkera503a632013-08-14 13:48:06 +0200562#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 *p++ = (unsigned char) n;
565
566 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000567 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
570 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
571
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200572 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200573 n = 0;
574 q = p;
575
576 // Skip writing ciphersuite length for now
577 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Paul Bakker48916f92012-09-16 19:57:18 +0000579 /*
580 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
581 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100582#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000583 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100584#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000585 {
586 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
587 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200588 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000589 }
590
Paul Bakker2fbefde2013-06-29 16:01:15 +0200591 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200593 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
594
595 if( ciphersuite_info == NULL )
596 continue;
597
598 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
599 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
600 continue;
601
Paul Bakkere3166ce2011-01-27 17:40:50 +0000602 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200603 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Paul Bakker2fbefde2013-06-29 16:01:15 +0200605 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200606 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
607 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 }
609
Paul Bakker2fbefde2013-06-29 16:01:15 +0200610 *q++ = (unsigned char)( n >> 7 );
611 *q++ = (unsigned char)( n << 1 );
612
613 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
614
615
Paul Bakker2770fbd2012-07-03 13:30:23 +0000616#if defined(POLARSSL_ZLIB_SUPPORT)
617 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
618 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000619 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000620
621 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000622 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000623 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000624#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000626 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628 *p++ = 1;
629 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200630#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Paul Bakkerd3edc862013-03-20 16:07:17 +0100632 // First write extensions, then the total length
633 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200634#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100635 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
636 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200637#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100639#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100640 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
641 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100642#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000643
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200644#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100645 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
646 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200647#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000648
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200649#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100650 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
651 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100652
Paul Bakkerd3edc862013-03-20 16:07:17 +0100653 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
654 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100655#endif
656
Paul Bakker05decb22013-08-15 13:33:48 +0200657#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200658 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
659 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200660#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200661
Paul Bakker1f2bc622013-08-15 13:45:55 +0200662#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200663 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
664 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200665#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200666
Paul Bakkera503a632013-08-14 13:48:06 +0200667#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200668 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
669 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200670#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200671
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200672#if defined(POLARSSL_SSL_ALPN)
673 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
674 ext_len += olen;
675#endif
676
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000677 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
678 ext_len ) );
679
Paul Bakkera7036632014-04-30 10:15:38 +0200680 if( ext_len > 0 )
681 {
682 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
683 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
684 p += ext_len;
685 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100686
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 ssl->out_msglen = p - buf;
688 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
689 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
690
691 ssl->state++;
692
693 if( ( ret = ssl_write_record( ssl ) ) != 0 )
694 {
695 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
696 return( ret );
697 }
698
699 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
700
701 return( 0 );
702}
703
Paul Bakker48916f92012-09-16 19:57:18 +0000704static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200705 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000706 size_t len )
707{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000708 int ret;
709
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100710#if defined(POLARSSL_SSL_RENEGOTIATION)
711 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000712 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100713 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000714 if( len != 1 + ssl->verify_data_len * 2 ||
715 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100716 safer_memcmp( buf + 1,
717 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
718 safer_memcmp( buf + 1 + ssl->verify_data_len,
719 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000720 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100721 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000722
723 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
724 return( ret );
725
Paul Bakker48916f92012-09-16 19:57:18 +0000726 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
727 }
728 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100729 else
730#endif /* POLARSSL_SSL_RENEGOTIATION */
731 {
732 if( len != 1 || buf[0] != 0x00 )
733 {
734 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
735
736 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
737 return( ret );
738
739 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
740 }
741
742 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
743 }
Paul Bakker48916f92012-09-16 19:57:18 +0000744
745 return( 0 );
746}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200747
Paul Bakker05decb22013-08-15 13:33:48 +0200748#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200749static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200750 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200751 size_t len )
752{
753 /*
754 * server should use the extension only if we did,
755 * and if so the server's value should match ours (and len is always 1)
756 */
757 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
758 len != 1 ||
759 buf[0] != ssl->mfl_code )
760 {
761 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
762 }
763
764 return( 0 );
765}
Paul Bakker05decb22013-08-15 13:33:48 +0200766#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000767
Paul Bakker1f2bc622013-08-15 13:45:55 +0200768#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200769static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
770 const unsigned char *buf,
771 size_t len )
772{
773 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
774 len != 0 )
775 {
776 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
777 }
778
779 ((void) buf);
780
781 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
782
783 return( 0 );
784}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200785#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200786
Paul Bakkera503a632013-08-14 13:48:06 +0200787#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200788static int ssl_parse_session_ticket_ext( ssl_context *ssl,
789 const unsigned char *buf,
790 size_t len )
791{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200792 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
793 len != 0 )
794 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200795 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200796 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200797
798 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200799
800 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200801
802 return( 0 );
803}
Paul Bakkera503a632013-08-14 13:48:06 +0200804#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200805
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200806#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200807static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
808 const unsigned char *buf,
809 size_t len )
810{
811 size_t list_size;
812 const unsigned char *p;
813
814 list_size = buf[0];
815 if( list_size + 1 != len )
816 {
817 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
818 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
819 }
820
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200821 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200822 while( list_size > 0 )
823 {
824 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
825 p[0] == POLARSSL_ECP_PF_COMPRESSED )
826 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200827 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200828 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
829 return( 0 );
830 }
831
832 list_size--;
833 p++;
834 }
835
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200836 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
837 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200838}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200839#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200840
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200841#if defined(POLARSSL_SSL_ALPN)
842static int ssl_parse_alpn_ext( ssl_context *ssl,
843 const unsigned char *buf, size_t len )
844{
845 size_t list_len, name_len;
846 const char **p;
847
848 /* If we didn't send it, the server shouldn't send it */
849 if( ssl->alpn_list == NULL )
850 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
851
852 /*
853 * opaque ProtocolName<1..2^8-1>;
854 *
855 * struct {
856 * ProtocolName protocol_name_list<2..2^16-1>
857 * } ProtocolNameList;
858 *
859 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
860 */
861
862 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
863 if( len < 4 )
864 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
865
866 list_len = ( buf[0] << 8 ) | buf[1];
867 if( list_len != len - 2 )
868 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
869
870 name_len = buf[2];
871 if( name_len != list_len - 1 )
872 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
873
874 /* Check that the server chosen protocol was in our list and save it */
875 for( p = ssl->alpn_list; *p != NULL; p++ )
876 {
877 if( name_len == strlen( *p ) &&
878 memcmp( buf + 3, *p, name_len ) == 0 )
879 {
880 ssl->alpn_chosen = *p;
881 return( 0 );
882 }
883 }
884
885 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
886}
887#endif /* POLARSSL_SSL_ALPN */
888
Paul Bakker5121ce52009-01-03 21:22:43 +0000889static int ssl_parse_server_hello( ssl_context *ssl )
890{
Paul Bakker2770fbd2012-07-03 13:30:23 +0000891 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +0000892 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +0200893 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000894 unsigned char *buf, *ext;
895 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000896 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200897#if defined(POLARSSL_DEBUG_C)
898 uint32_t t;
899#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
901 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
902
903 /*
904 * 0 . 0 handshake type
905 * 1 . 3 handshake length
906 * 4 . 5 protocol version
907 * 6 . 9 UNIX time()
908 * 10 . 37 random bytes
909 */
910 buf = ssl->in_msg;
911
912 if( ( ret = ssl_read_record( ssl ) ) != 0 )
913 {
914 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
915 return( ret );
916 }
917
918 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
919 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100920#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +0200921 if( ssl->renegotiation == SSL_RENEGOTIATION )
922 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +0200923 ssl->renego_records_seen++;
924
925 if( ssl->renego_max_records >= 0 &&
926 ssl->renego_records_seen > ssl->renego_max_records )
927 {
928 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
929 "but not honored by server" ) );
930 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
931 }
932
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +0200933 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
934 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
935 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100936#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +0200937
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000939 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 }
941
942 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
943 buf[4], buf[5] ) );
944
945 if( ssl->in_hslen < 42 ||
946 buf[0] != SSL_HS_SERVER_HELLO ||
947 buf[4] != SSL_MAJOR_VERSION_3 )
948 {
949 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000950 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 }
952
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000953 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 {
955 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000956 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 }
958
959 ssl->minor_ver = buf[5];
960
Paul Bakker1d29fb52012-09-28 13:28:45 +0000961 if( ssl->minor_ver < ssl->min_minor_ver )
962 {
963 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200964 " [%d:%d] < [%d:%d]", ssl->major_ver,
965 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000966
967 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
968 SSL_ALERT_MSG_PROTOCOL_VERSION );
969
970 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
971 }
972
Paul Bakker1504af52012-02-11 16:17:43 +0000973#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200974 t = ( (uint32_t) buf[6] << 24 )
975 | ( (uint32_t) buf[7] << 16 )
976 | ( (uint32_t) buf[8] << 8 )
977 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200978 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +0000979#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Paul Bakker48916f92012-09-16 19:57:18 +0000981 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
983 n = buf[38];
984
Paul Bakker5121ce52009-01-03 21:22:43 +0000985 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
986
Paul Bakker48916f92012-09-16 19:57:18 +0000987 if( n > 32 )
988 {
989 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
990 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
991 }
992
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 /*
994 * 38 . 38 session id length
995 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000996 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +0000997 * 41+n . 41+n chosen compression alg.
998 * 42+n . 43+n extensions length
999 * 44+n . 44+n+m extensions
1000 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001001 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 {
1003 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001004 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001005
Paul Bakker48916f92012-09-16 19:57:18 +00001006 if( ( ext_len > 0 && ext_len < 4 ) ||
1007 ssl->in_hslen != 44 + n + ext_len )
1008 {
1009 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1010 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001012 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001013 else if( ssl->in_hslen == 42 + n )
1014 {
1015 ext_len = 0;
1016 }
1017 else
1018 {
1019 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1020 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1021 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
1023 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001024 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
Paul Bakker380da532012-04-18 16:10:25 +00001026 /*
1027 * Initialize update checksum functions
1028 */
Paul Bakker68884e32013-01-07 18:20:04 +01001029 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1030
1031 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1032 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001033 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001034 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1035 }
Paul Bakker380da532012-04-18 16:10:25 +00001036
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001037 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1038
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1040 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1041
1042 /*
1043 * Check if the session can be resumed
1044 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001045 if( ssl->handshake->resume == 0 || n == 0 ||
1046#if defined(POLARSSL_SSL_RENEGOTIATION)
1047 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1048#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001049 ssl->session_negotiate->ciphersuite != i ||
1050 ssl->session_negotiate->compression != comp ||
1051 ssl->session_negotiate->length != n ||
1052 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 {
1054 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001055 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001056#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001057 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001058#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001059 ssl->session_negotiate->ciphersuite = i;
1060 ssl->session_negotiate->compression = comp;
1061 ssl->session_negotiate->length = n;
1062 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 }
1064 else
1065 {
1066 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001067
1068 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1069 {
1070 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1071 return( ret );
1072 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 }
1074
1075 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001076 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Paul Bakkere3166ce2011-01-27 17:40:50 +00001078 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1080
1081 i = 0;
1082 while( 1 )
1083 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001084 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 {
1086 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001087 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 }
1089
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001090 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1091 ssl->session_negotiate->ciphersuite )
1092 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001093 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001094 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001095 }
1096
Paul Bakker2770fbd2012-07-03 13:30:23 +00001097 if( comp != SSL_COMPRESS_NULL
1098#if defined(POLARSSL_ZLIB_SUPPORT)
1099 && comp != SSL_COMPRESS_DEFLATE
1100#endif
1101 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 {
1103 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001104 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001105 }
Paul Bakker48916f92012-09-16 19:57:18 +00001106 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
Paul Bakker48916f92012-09-16 19:57:18 +00001108 ext = buf + 44 + n;
1109
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001110 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1111
Paul Bakker48916f92012-09-16 19:57:18 +00001112 while( ext_len )
1113 {
1114 unsigned int ext_id = ( ( ext[0] << 8 )
1115 | ( ext[1] ) );
1116 unsigned int ext_size = ( ( ext[2] << 8 )
1117 | ( ext[3] ) );
1118
1119 if( ext_size + 4 > ext_len )
1120 {
1121 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1122 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1123 }
1124
1125 switch( ext_id )
1126 {
1127 case TLS_EXT_RENEGOTIATION_INFO:
1128 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1129 renegotiation_info_seen = 1;
1130
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001131 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1132 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001133 return( ret );
1134
1135 break;
1136
Paul Bakker05decb22013-08-15 13:33:48 +02001137#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001138 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1139 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1140
1141 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1142 ext + 4, ext_size ) ) != 0 )
1143 {
1144 return( ret );
1145 }
1146
1147 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001148#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001149
Paul Bakker1f2bc622013-08-15 13:45:55 +02001150#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001151 case TLS_EXT_TRUNCATED_HMAC:
1152 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1153
1154 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1155 ext + 4, ext_size ) ) != 0 )
1156 {
1157 return( ret );
1158 }
1159
1160 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001161#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001162
Paul Bakkera503a632013-08-14 13:48:06 +02001163#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001164 case TLS_EXT_SESSION_TICKET:
1165 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1166
1167 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1168 ext + 4, ext_size ) ) != 0 )
1169 {
1170 return( ret );
1171 }
1172
1173 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001174#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001175
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001176#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001177 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1178 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1179
1180 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1181 ext + 4, ext_size ) ) != 0 )
1182 {
1183 return( ret );
1184 }
1185
1186 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001187#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001188
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001189#if defined(POLARSSL_SSL_ALPN)
1190 case TLS_EXT_ALPN:
1191 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1192
1193 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1194 return( ret );
1195
1196 break;
1197#endif /* POLARSSL_SSL_ALPN */
1198
Paul Bakker48916f92012-09-16 19:57:18 +00001199 default:
1200 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1201 ext_id ) );
1202 }
1203
1204 ext_len -= 4 + ext_size;
1205 ext += 4 + ext_size;
1206
1207 if( ext_len > 0 && ext_len < 4 )
1208 {
1209 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1210 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1211 }
1212 }
1213
1214 /*
1215 * Renegotiation security checks
1216 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001217 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1218 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001219 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001220 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1221 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001222 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001223#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001224 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1225 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1226 renegotiation_info_seen == 0 )
1227 {
1228 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1229 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001230 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001231 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1232 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1233 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001234 {
1235 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001236 handshake_failure = 1;
1237 }
1238 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1239 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1240 renegotiation_info_seen == 1 )
1241 {
1242 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1243 handshake_failure = 1;
1244 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001245#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001246
1247 if( handshake_failure == 1 )
1248 {
1249 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1250 return( ret );
1251
Paul Bakker48916f92012-09-16 19:57:18 +00001252 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1253 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
1255 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1256
1257 return( 0 );
1258}
1259
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001260#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1261 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001262static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1263 unsigned char *end )
1264{
1265 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1266
Paul Bakker29e1f122013-04-16 13:07:56 +02001267 /*
1268 * Ephemeral DH parameters:
1269 *
1270 * struct {
1271 * opaque dh_p<1..2^16-1>;
1272 * opaque dh_g<1..2^16-1>;
1273 * opaque dh_Ys<1..2^16-1>;
1274 * } ServerDHParams;
1275 */
1276 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1277 {
1278 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1279 return( ret );
1280 }
1281
1282 if( ssl->handshake->dhm_ctx.len < 64 ||
1283 ssl->handshake->dhm_ctx.len > 512 )
1284 {
1285 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1286 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1287 }
1288
1289 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1290 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1291 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001292
1293 return( ret );
1294}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001295#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1296 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001297
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001298#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001299 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001300 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1301 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1302 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1303static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1304{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001305 const ecp_curve_info *curve_info;
1306
1307 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1308 if( curve_info == NULL )
1309 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001310 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1311 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001312 }
1313
1314 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001315
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001316#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1317 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1318#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001319 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1320 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001321#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001322 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001323
1324 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1325
1326 return( 0 );
1327}
Paul Bakker9af723c2014-05-01 13:03:14 +02001328#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1329 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1330 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1331 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1332 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001333
1334#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1335 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001336 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001337static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1338 unsigned char **p,
1339 unsigned char *end )
1340{
1341 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1342
Paul Bakker29e1f122013-04-16 13:07:56 +02001343 /*
1344 * Ephemeral ECDH parameters:
1345 *
1346 * struct {
1347 * ECParameters curve_params;
1348 * ECPoint public;
1349 * } ServerECDHParams;
1350 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001351 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1352 (const unsigned char **) p, end ) ) != 0 )
1353 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001354 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001355 return( ret );
1356 }
1357
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001358 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001359 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001360 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001361 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1362 }
1363
Paul Bakker29e1f122013-04-16 13:07:56 +02001364 return( ret );
1365}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001366#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001367 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1368 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001369
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001370#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001371static int ssl_parse_server_psk_hint( ssl_context *ssl,
1372 unsigned char **p,
1373 unsigned char *end )
1374{
1375 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001376 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001377 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001378
1379 /*
1380 * PSK parameters:
1381 *
1382 * opaque psk_identity_hint<0..2^16-1>;
1383 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001384 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001385 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001386
1387 if( (*p) + len > end )
1388 {
1389 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1390 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1391 }
1392
1393 // TODO: Retrieve PSK identity hint and callback to app
1394 //
1395 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001396 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001397
1398 return( ret );
1399}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001400#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001401
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001402#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1403 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1404/*
1405 * Generate a pre-master secret and encrypt it with the server's RSA key
1406 */
1407static int ssl_write_encrypted_pms( ssl_context *ssl,
1408 size_t offset, size_t *olen,
1409 size_t pms_offset )
1410{
1411 int ret;
1412 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1413 unsigned char *p = ssl->handshake->premaster + pms_offset;
1414
1415 /*
1416 * Generate (part of) the pre-master as
1417 * struct {
1418 * ProtocolVersion client_version;
1419 * opaque random[46];
1420 * } PreMasterSecret;
1421 */
1422 p[0] = (unsigned char) ssl->max_major_ver;
1423 p[1] = (unsigned char) ssl->max_minor_ver;
1424
1425 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1426 {
1427 SSL_DEBUG_RET( 1, "f_rng", ret );
1428 return( ret );
1429 }
1430
1431 ssl->handshake->pmslen = 48;
1432
1433 /*
1434 * Now write it out, encrypted
1435 */
1436 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1437 POLARSSL_PK_RSA ) )
1438 {
1439 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1440 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1441 }
1442
1443 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1444 p, ssl->handshake->pmslen,
1445 ssl->out_msg + offset + len_bytes, olen,
1446 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1447 ssl->f_rng, ssl->p_rng ) ) != 0 )
1448 {
1449 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1450 return( ret );
1451 }
1452
1453#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1454 defined(POLARSSL_SSL_PROTO_TLS1_2)
1455 if( len_bytes == 2 )
1456 {
1457 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1458 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1459 *olen += 2;
1460 }
1461#endif
1462
1463 return( 0 );
1464}
1465#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1466 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001467
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001468#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001469#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001470 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1471 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001472static int ssl_parse_signature_algorithm( ssl_context *ssl,
1473 unsigned char **p,
1474 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001475 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001476 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001477{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001478 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001479 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001480 *pk_alg = POLARSSL_PK_NONE;
1481
1482 /* Only in TLS 1.2 */
1483 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1484 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001485 return( 0 );
1486 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001487
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001488 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001489 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1490
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001491 /*
1492 * Get hash algorithm
1493 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001494 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001495 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001496 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1497 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001498 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1499 }
1500
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001501 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001502 * Get signature algorithm
1503 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001504 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001505 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001506 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1507 "SignatureAlgorithm %d", (*p)[1] ) );
1508 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001509 }
1510
1511 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1512 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1513 *p += 2;
1514
1515 return( 0 );
1516}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001517#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001518 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1519 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001520#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001521
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001522
1523#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1524 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1525static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1526{
1527 int ret;
1528 const ecp_keypair *peer_key;
1529
1530 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1531 POLARSSL_PK_ECKEY ) )
1532 {
1533 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1534 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1535 }
1536
1537 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1538
1539 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1540 POLARSSL_ECDH_THEIRS ) ) != 0 )
1541 {
1542 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1543 return( ret );
1544 }
1545
1546 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1547 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001548 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001549 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1550 }
1551
1552 return( ret );
1553}
1554#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1555 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1556
Paul Bakker41c83d32013-03-20 14:39:14 +01001557static int ssl_parse_server_key_exchange( ssl_context *ssl )
1558{
Paul Bakker23986e52011-04-24 08:57:21 +00001559 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001560 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001561 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001562#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001563 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1564 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001565 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001566 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001567 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001568 size_t hashlen;
1569 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
1572 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1573
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001574#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001575 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001576 {
1577 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1578 ssl->state++;
1579 return( 0 );
1580 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001581 ((void) p);
1582 ((void) end);
1583#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001584
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001585#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1586 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1587 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1588 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1589 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001590 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1591 {
1592 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1593 return( ret );
1594 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001595
1596 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1597 ssl->state++;
1598 return( 0 );
1599 }
1600 ((void) p);
1601 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001602#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1603 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001604
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1606 {
1607 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1608 return( ret );
1609 }
1610
1611 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1612 {
1613 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001614 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 }
1616
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001617 /*
1618 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1619 * doesn't use a psk_identity_hint
1620 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1622 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001623 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1624 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001625 {
1626 ssl->record_read = 1;
1627 goto exit;
1628 }
1629
1630 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1631 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 }
1633
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001634 p = ssl->in_msg + 4;
1635 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001636 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001637
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001638#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1639 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1640 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1641 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1642 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1643 {
1644 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1645 {
1646 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1647 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1648 }
1649 } /* FALLTROUGH */
1650#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1651
1652#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1653 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1654 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1655 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1656 ; /* nothing more to do */
1657 else
1658#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1659 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1660#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1661 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1662 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1663 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001665 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001666 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001667 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001668 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1669 }
1670 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001671 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001672#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1673 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001674#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001675 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001676 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1677 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001678 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001679 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001680 {
1681 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1682 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001683 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1684 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1685 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001686 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001687 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001688#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001689 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001690 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001691 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001692 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001693 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001694 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001695
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001696#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001697 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1698 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001699 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001700 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1701 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001702 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001703 params_len = p - ( ssl->in_msg + 4 );
1704
Paul Bakker29e1f122013-04-16 13:07:56 +02001705 /*
1706 * Handle the digitally-signed structure
1707 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001708#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1709 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001710 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001711 if( ssl_parse_signature_algorithm( ssl, &p, end,
1712 &md_alg, &pk_alg ) != 0 )
1713 {
1714 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1715 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1716 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001717
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001718 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001719 {
1720 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1721 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1722 }
1723 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001724 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001725#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001726#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1727 defined(POLARSSL_SSL_PROTO_TLS1_1)
1728 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001729 {
1730 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001731
Paul Bakker9659dae2013-08-28 16:21:34 +02001732 /* Default hash for ECDSA is SHA-1 */
1733 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1734 md_alg = POLARSSL_MD_SHA1;
1735 }
1736 else
1737#endif
1738 {
1739 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001740 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001741 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001742
1743 /*
1744 * Read signature
1745 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001746 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001747 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001748
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001749 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001750 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001751 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001752 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1753 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001755 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001756
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001757 /*
1758 * Compute the hash that has been signed
1759 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001760#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1761 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001762 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001763 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001764 md5_context md5;
1765 sha1_context sha1;
1766
Paul Bakker5b4af392014-06-26 12:09:34 +02001767 md5_init( &md5 );
1768 sha1_init( &sha1 );
1769
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001770 hashlen = 36;
1771
Paul Bakker29e1f122013-04-16 13:07:56 +02001772 /*
1773 * digitally-signed struct {
1774 * opaque md5_hash[16];
1775 * opaque sha_hash[20];
1776 * };
1777 *
1778 * md5_hash
1779 * MD5(ClientHello.random + ServerHello.random
1780 * + ServerParams);
1781 * sha_hash
1782 * SHA(ClientHello.random + ServerHello.random
1783 * + ServerParams);
1784 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001785 md5_starts( &md5 );
1786 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001787 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001788 md5_finish( &md5, hash );
1789
1790 sha1_starts( &sha1 );
1791 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001792 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001793 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001794
1795 md5_free( &md5 );
1796 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001797 }
1798 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001799#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1800 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001801#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1802 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001803 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001804 {
1805 md_context_t ctx;
1806
Paul Bakker84bbeb52014-07-01 14:53:22 +02001807 md_init( &ctx );
1808
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001809 /* Info from md_alg will be used instead */
1810 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001811
1812 /*
1813 * digitally-signed struct {
1814 * opaque client_random[32];
1815 * opaque server_random[32];
1816 * ServerDHParams params;
1817 * };
1818 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001819 if( ( ret = md_init_ctx( &ctx,
1820 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001821 {
1822 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1823 return( ret );
1824 }
1825
1826 md_starts( &ctx );
1827 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001828 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001829 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001830 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001831 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001832 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001833#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1834 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001835 {
Paul Bakker577e0062013-08-28 11:57:20 +02001836 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001837 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001838 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001839
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001840 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1841 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001842
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001843 /*
1844 * Verify signature
1845 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001846 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001847 {
1848 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1849 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1850 }
1851
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001852 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1853 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001854 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001855 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001856 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001857 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001858 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001859#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001860 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1861 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001863exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 ssl->state++;
1865
1866 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
1867
1868 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001869}
1870
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001871#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1872 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1873 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1874 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1875static int ssl_parse_certificate_request( ssl_context *ssl )
1876{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001877 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
1878
1879 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1880
1881 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1882 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1883 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1884 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1885 {
1886 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
1887 ssl->state++;
1888 return( 0 );
1889 }
1890
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001891 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1892 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001893}
1894#else
Paul Bakker5121ce52009-01-03 21:22:43 +00001895static int ssl_parse_certificate_request( ssl_context *ssl )
1896{
1897 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01001898 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001899 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001900 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001901 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001902
1903 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1904
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001905 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1906 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1907 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1908 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1909 {
1910 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
1911 ssl->state++;
1912 return( 0 );
1913 }
1914
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 /*
1916 * 0 . 0 handshake type
1917 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01001918 * 4 . 4 cert type count
1919 * 5 .. m-1 cert types
1920 * m .. m+1 sig alg length (TLS 1.2 only)
1921 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 * n .. n+1 length of all DNs
1923 * n+2 .. n+3 length of DN 1
1924 * n+4 .. ... Distinguished Name #1
1925 * ... .. ... length of DN 2, etc.
1926 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001927 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001929 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1930 {
1931 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1932 return( ret );
1933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001934
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001935 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1936 {
1937 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1938 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1939 }
1940
1941 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 }
1943
1944 ssl->client_auth = 0;
1945 ssl->state++;
1946
1947 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
1948 ssl->client_auth++;
1949
1950 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
1951 ssl->client_auth ? "a" : "no" ) );
1952
Paul Bakker926af752012-11-23 13:38:07 +01001953 if( ssl->client_auth == 0 )
1954 goto exit;
1955
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001956 ssl->record_read = 0;
1957
Paul Bakker926af752012-11-23 13:38:07 +01001958 // TODO: handshake_failure alert for an anonymous server to request
1959 // client authentication
1960
1961 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001962
Paul Bakker926af752012-11-23 13:38:07 +01001963 // Retrieve cert types
1964 //
1965 cert_type_len = buf[4];
1966 n = cert_type_len;
1967
1968 if( ssl->in_hslen < 6 + n )
1969 {
1970 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1971 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1972 }
1973
Paul Bakker73d44312013-05-22 13:56:26 +02001974 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01001975 while( cert_type_len > 0 )
1976 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001977#if defined(POLARSSL_RSA_C)
1978 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001979 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01001980 {
1981 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
1982 break;
1983 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001984 else
1985#endif
1986#if defined(POLARSSL_ECDSA_C)
1987 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001988 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001989 {
1990 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
1991 break;
1992 }
1993 else
1994#endif
1995 {
1996 ; /* Unsupported cert type, ignore */
1997 }
Paul Bakker926af752012-11-23 13:38:07 +01001998
1999 cert_type_len--;
2000 p++;
2001 }
2002
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002003#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002004 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2005 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002006 /* Ignored, see comments about hash in write_certificate_verify */
2007 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002008 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2009 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002010
2011 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002012 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002013 n += sig_alg_len;
2014
2015 if( ssl->in_hslen < 6 + n )
2016 {
2017 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2018 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2019 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002020 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002021#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002022
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002023 /* Ignore certificate_authorities, we only have one cert anyway */
2024 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002025 dn_len = ( ( buf[5 + m + n] << 8 )
2026 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002027
2028 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002029 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002030 {
2031 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2032 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2033 }
2034
2035exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2037
2038 return( 0 );
2039}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002040#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2041 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2042 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2043 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002044
2045static int ssl_parse_server_hello_done( ssl_context *ssl )
2046{
2047 int ret;
2048
2049 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2050
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002051 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 {
2053 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2054 {
2055 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2056 return( ret );
2057 }
2058
2059 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2060 {
2061 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002062 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 }
2064 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002065 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002066
2067 if( ssl->in_hslen != 4 ||
2068 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2069 {
2070 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002071 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 }
2073
2074 ssl->state++;
2075
2076 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2077
2078 return( 0 );
2079}
2080
2081static int ssl_write_client_key_exchange( ssl_context *ssl )
2082{
Paul Bakker23986e52011-04-24 08:57:21 +00002083 int ret;
2084 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002085 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
2087 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2088
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002089#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002090 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 /*
2093 * DHM key exchange -- send G^X mod P
2094 */
Paul Bakker48916f92012-09-16 19:57:18 +00002095 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
2097 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2098 ssl->out_msg[5] = (unsigned char)( n );
2099 i = 6;
2100
Paul Bakker29b64762012-09-25 09:36:44 +00002101 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002102 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 &ssl->out_msg[i], n,
2104 ssl->f_rng, ssl->p_rng );
2105 if( ret != 0 )
2106 {
2107 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2108 return( ret );
2109 }
2110
Paul Bakker48916f92012-09-16 19:57:18 +00002111 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2112 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002114 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
Paul Bakker48916f92012-09-16 19:57:18 +00002116 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2117 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002118 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002119 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 {
2121 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2122 return( ret );
2123 }
2124
Paul Bakker48916f92012-09-16 19:57:18 +00002125 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
2127 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002128#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002129#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002130 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2131 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2132 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002133 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002134 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2135 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2136 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002137 {
2138 /*
2139 * ECDH key exchange -- send client public value
2140 */
2141 i = 4;
2142
2143 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2144 &n,
2145 &ssl->out_msg[i], 1000,
2146 ssl->f_rng, ssl->p_rng );
2147 if( ret != 0 )
2148 {
2149 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2150 return( ret );
2151 }
2152
2153 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2154
2155 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2156 &ssl->handshake->pmslen,
2157 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002158 POLARSSL_MPI_MAX_SIZE,
2159 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002160 {
2161 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2162 return( ret );
2163 }
2164
2165 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2166 }
2167 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002168#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002169 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2170 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2171 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002172#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002173 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002174 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002175 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2176 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002177 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002178 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002179 * opaque psk_identity<0..2^16-1>;
2180 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002181 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002182 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2183
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002184 i = 4;
2185 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002186 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2187 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002188
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002189 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2190 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002191
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002192#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002193 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002194 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002195 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002196 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002197 else
2198#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002199#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2200 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2201 {
2202 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2203 return( ret );
2204 }
2205 else
2206#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002207#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002208 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002209 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002210 /*
2211 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2212 */
2213 n = ssl->handshake->dhm_ctx.len;
2214 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2215 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002216
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002217 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002218 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002219 &ssl->out_msg[i], n,
2220 ssl->f_rng, ssl->p_rng );
2221 if( ret != 0 )
2222 {
2223 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2224 return( ret );
2225 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002226 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002227 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002228#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002229#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002230 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002231 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002232 /*
2233 * ClientECDiffieHellmanPublic public;
2234 */
2235 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2236 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2237 ssl->f_rng, ssl->p_rng );
2238 if( ret != 0 )
2239 {
2240 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2241 return( ret );
2242 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002243
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002244 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2245 }
2246 else
2247#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2248 {
2249 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002250 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002251 }
2252
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002253 if( ( ret = ssl_psk_derive_premaster( ssl,
2254 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002255 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002256 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002257 return( ret );
2258 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002259 }
2260 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002261#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002262#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002263 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002265 i = 4;
2266 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002267 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 }
Paul Bakkered27a042013-04-18 22:46:23 +02002269 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002270#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002271 {
2272 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002273 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002274 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
Paul Bakkerff60ee62010-03-16 21:09:09 +00002277 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2278 {
2279 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2280 return( ret );
2281 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
2283 ssl->out_msglen = i + n;
2284 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2285 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2286
2287 ssl->state++;
2288
2289 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2290 {
2291 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2292 return( ret );
2293 }
2294
2295 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2296
2297 return( 0 );
2298}
2299
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002300#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2301 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002302 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2303 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002304static int ssl_write_certificate_verify( ssl_context *ssl )
2305{
Paul Bakkered27a042013-04-18 22:46:23 +02002306 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002307
2308 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2309
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002310 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002311 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002312 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002313 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002314 {
2315 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2316 ssl->state++;
2317 return( 0 );
2318 }
2319
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002320 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2321 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002322}
2323#else
2324static int ssl_write_certificate_verify( ssl_context *ssl )
2325{
2326 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2327 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2328 size_t n = 0, offset = 0;
2329 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002330 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002331 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002332 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002333
2334 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2335
2336 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002337 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002338 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002339 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2340 {
2341 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2342 ssl->state++;
2343 return( 0 );
2344 }
2345
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002346 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 {
2348 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2349 ssl->state++;
2350 return( 0 );
2351 }
2352
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002353 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002355 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2356 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 }
2358
2359 /*
2360 * Make an RSA signature of the handshake digests
2361 */
Paul Bakker48916f92012-09-16 19:57:18 +00002362 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002364#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2365 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002366 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002367 {
Paul Bakker926af752012-11-23 13:38:07 +01002368 /*
2369 * digitally-signed struct {
2370 * opaque md5_hash[16];
2371 * opaque sha_hash[20];
2372 * };
2373 *
2374 * md5_hash
2375 * MD5(handshake_messages);
2376 *
2377 * sha_hash
2378 * SHA(handshake_messages);
2379 */
2380 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002381 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002382
2383 /*
2384 * For ECDSA, default hash is SHA-1 only
2385 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002386 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002387 {
2388 hash_start += 16;
2389 hashlen -= 16;
2390 md_alg = POLARSSL_MD_SHA1;
2391 }
Paul Bakker926af752012-11-23 13:38:07 +01002392 }
2393 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002394#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2395 POLARSSL_SSL_PROTO_TLS1_1 */
2396#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2397 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002398 {
2399 /*
2400 * digitally-signed struct {
2401 * opaque handshake_messages[handshake_messages_length];
2402 * };
2403 *
2404 * Taking shortcut here. We assume that the server always allows the
2405 * PRF Hash function and has sent it in the allowed signature
2406 * algorithms list received in the Certificate Request message.
2407 *
2408 * Until we encounter a server that does not, we will take this
2409 * shortcut.
2410 *
2411 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2412 * in order to satisfy 'weird' needs from the server side.
2413 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002414 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2415 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002416 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002417 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002418 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002419 }
2420 else
2421 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002422 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002423 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002424 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002425 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002426
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002427 /* Info from md_alg will be used instead */
2428 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002429 offset = 2;
2430 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002431 else
2432#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002433 {
2434 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002435 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002436 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002437
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002438 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002439 ssl->out_msg + 6 + offset, &n,
2440 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002441 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002442 SSL_DEBUG_RET( 1, "pk_sign", ret );
2443 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002444 }
Paul Bakker926af752012-11-23 13:38:07 +01002445
Paul Bakker1ef83d62012-04-11 12:09:53 +00002446 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2447 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Paul Bakker1ef83d62012-04-11 12:09:53 +00002449 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2451 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2452
2453 ssl->state++;
2454
2455 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2456 {
2457 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2458 return( ret );
2459 }
2460
2461 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2462
Paul Bakkered27a042013-04-18 22:46:23 +02002463 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002464}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002465#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2466 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2467 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Paul Bakkera503a632013-08-14 13:48:06 +02002469#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002470static int ssl_parse_new_session_ticket( ssl_context *ssl )
2471{
2472 int ret;
2473 uint32_t lifetime;
2474 size_t ticket_len;
2475 unsigned char *ticket;
2476
2477 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2478
2479 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2480 {
2481 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2482 return( ret );
2483 }
2484
2485 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2486 {
2487 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2488 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2489 }
2490
2491 /*
2492 * struct {
2493 * uint32 ticket_lifetime_hint;
2494 * opaque ticket<0..2^16-1>;
2495 * } NewSessionTicket;
2496 *
2497 * 0 . 0 handshake message type
2498 * 1 . 3 handshake message length
2499 * 4 . 7 ticket_lifetime_hint
2500 * 8 . 9 ticket_len (n)
2501 * 10 . 9+n ticket content
2502 */
2503 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2504 ssl->in_hslen < 10 )
2505 {
2506 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2507 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2508 }
2509
2510 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2511 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2512
2513 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2514
2515 if( ticket_len + 10 != ssl->in_hslen )
2516 {
2517 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2518 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2519 }
2520
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002521 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2522
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002523 /* We're not waiting for a NewSessionTicket message any more */
2524 ssl->handshake->new_session_ticket = 0;
2525
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002526 /*
2527 * Zero-length ticket means the server changed his mind and doesn't want
2528 * to send a ticket after all, so just forget it
2529 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002530 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002531 return( 0 );
2532
Paul Bakker34617722014-06-13 17:20:13 +02002533 polarssl_zeroize( ssl->session_negotiate->ticket,
2534 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002535 polarssl_free( ssl->session_negotiate->ticket );
2536 ssl->session_negotiate->ticket = NULL;
2537 ssl->session_negotiate->ticket_len = 0;
2538
2539 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2540 {
2541 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2542 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2543 }
2544
2545 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2546
2547 ssl->session_negotiate->ticket = ticket;
2548 ssl->session_negotiate->ticket_len = ticket_len;
2549 ssl->session_negotiate->ticket_lifetime = lifetime;
2550
2551 /*
2552 * RFC 5077 section 3.4:
2553 * "If the client receives a session ticket from the server, then it
2554 * discards any Session ID that was sent in the ServerHello."
2555 */
2556 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2557 ssl->session_negotiate->length = 0;
2558
2559 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2560
2561 return( 0 );
2562}
Paul Bakkera503a632013-08-14 13:48:06 +02002563#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002564
Paul Bakker5121ce52009-01-03 21:22:43 +00002565/*
Paul Bakker1961b702013-01-25 14:49:24 +01002566 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 */
Paul Bakker1961b702013-01-25 14:49:24 +01002568int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002569{
2570 int ret = 0;
2571
Paul Bakker1961b702013-01-25 14:49:24 +01002572 if( ssl->state == SSL_HANDSHAKE_OVER )
2573 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Paul Bakker1961b702013-01-25 14:49:24 +01002575 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2576
2577 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2578 return( ret );
2579
2580 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 {
Paul Bakker1961b702013-01-25 14:49:24 +01002582 case SSL_HELLO_REQUEST:
2583 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 break;
2585
Paul Bakker1961b702013-01-25 14:49:24 +01002586 /*
2587 * ==> ClientHello
2588 */
2589 case SSL_CLIENT_HELLO:
2590 ret = ssl_write_client_hello( ssl );
2591 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Paul Bakker1961b702013-01-25 14:49:24 +01002593 /*
2594 * <== ServerHello
2595 * Certificate
2596 * ( ServerKeyExchange )
2597 * ( CertificateRequest )
2598 * ServerHelloDone
2599 */
2600 case SSL_SERVER_HELLO:
2601 ret = ssl_parse_server_hello( ssl );
2602 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Paul Bakker1961b702013-01-25 14:49:24 +01002604 case SSL_SERVER_CERTIFICATE:
2605 ret = ssl_parse_certificate( ssl );
2606 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
Paul Bakker1961b702013-01-25 14:49:24 +01002608 case SSL_SERVER_KEY_EXCHANGE:
2609 ret = ssl_parse_server_key_exchange( ssl );
2610 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Paul Bakker1961b702013-01-25 14:49:24 +01002612 case SSL_CERTIFICATE_REQUEST:
2613 ret = ssl_parse_certificate_request( ssl );
2614 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Paul Bakker1961b702013-01-25 14:49:24 +01002616 case SSL_SERVER_HELLO_DONE:
2617 ret = ssl_parse_server_hello_done( ssl );
2618 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Paul Bakker1961b702013-01-25 14:49:24 +01002620 /*
2621 * ==> ( Certificate/Alert )
2622 * ClientKeyExchange
2623 * ( CertificateVerify )
2624 * ChangeCipherSpec
2625 * Finished
2626 */
2627 case SSL_CLIENT_CERTIFICATE:
2628 ret = ssl_write_certificate( ssl );
2629 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Paul Bakker1961b702013-01-25 14:49:24 +01002631 case SSL_CLIENT_KEY_EXCHANGE:
2632 ret = ssl_write_client_key_exchange( ssl );
2633 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Paul Bakker1961b702013-01-25 14:49:24 +01002635 case SSL_CERTIFICATE_VERIFY:
2636 ret = ssl_write_certificate_verify( ssl );
2637 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Paul Bakker1961b702013-01-25 14:49:24 +01002639 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2640 ret = ssl_write_change_cipher_spec( ssl );
2641 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
Paul Bakker1961b702013-01-25 14:49:24 +01002643 case SSL_CLIENT_FINISHED:
2644 ret = ssl_write_finished( ssl );
2645 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
Paul Bakker1961b702013-01-25 14:49:24 +01002647 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002648 * <== ( NewSessionTicket )
2649 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002650 * Finished
2651 */
2652 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002653#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002654 if( ssl->handshake->new_session_ticket != 0 )
2655 ret = ssl_parse_new_session_ticket( ssl );
2656 else
Paul Bakkera503a632013-08-14 13:48:06 +02002657#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002658 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002659 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Paul Bakker1961b702013-01-25 14:49:24 +01002661 case SSL_SERVER_FINISHED:
2662 ret = ssl_parse_finished( ssl );
2663 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
Paul Bakker1961b702013-01-25 14:49:24 +01002665 case SSL_FLUSH_BUFFERS:
2666 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2667 ssl->state = SSL_HANDSHAKE_WRAPUP;
2668 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002669
Paul Bakker1961b702013-01-25 14:49:24 +01002670 case SSL_HANDSHAKE_WRAPUP:
2671 ssl_handshake_wrapup( ssl );
2672 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002673
Paul Bakker1961b702013-01-25 14:49:24 +01002674 default:
2675 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2676 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
2679 return( ret );
2680}
Paul Bakker9af723c2014-05-01 13:03:14 +02002681#endif /* POLARSSL_SSL_CLI_C */