blob: a899affcae6ef8f10f6d4f06d64bf7bd0600ac57 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Paul Bakker68884e32013-01-07 18:20:04 +01004 * Copyright (C) 2006-2013, 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
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/debug.h"
31#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020033#if defined(POLARSSL_MEMORY_C)
34#include "polarssl/memory.h"
35#else
36#define polarssl_malloc malloc
37#define polarssl_free free
38#endif
39
Paul Bakker5121ce52009-01-03 21:22:43 +000040#include <stdlib.h>
41#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020042
43#ifdef _MSC_VER
44#include <basetsd.h>
45typedef UINT32 uint32_t;
46#else
47#include <inttypes.h>
48#endif
49
50#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakker0be444a2013-08-27 21:55:01 +020054#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010055static void ssl_write_hostname_ext( ssl_context *ssl,
56 unsigned char *buf,
57 size_t *olen )
58{
59 unsigned char *p = buf;
60
61 *olen = 0;
62
63 if ( ssl->hostname == NULL )
64 return;
65
66 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
67 ssl->hostname ) );
68
69 /*
70 * struct {
71 * NameType name_type;
72 * select (name_type) {
73 * case host_name: HostName;
74 * } name;
75 * } ServerName;
76 *
77 * enum {
78 * host_name(0), (255)
79 * } NameType;
80 *
81 * opaque HostName<1..2^16-1>;
82 *
83 * struct {
84 * ServerName server_name_list<1..2^16-1>
85 * } ServerNameList;
86 */
87 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
88 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
89
90 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
91 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
92
93 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
94 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
95
96 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
97 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
98 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
99
100 memcpy( p, ssl->hostname, ssl->hostname_len );
101
102 *olen = ssl->hostname_len + 9;
103}
Paul Bakker0be444a2013-08-27 21:55:01 +0200104#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100105
106static void ssl_write_renegotiation_ext( ssl_context *ssl,
107 unsigned char *buf,
108 size_t *olen )
109{
110 unsigned char *p = buf;
111
112 *olen = 0;
113
114 if( ssl->renegotiation != SSL_RENEGOTIATION )
115 return;
116
117 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
118
119 /*
120 * Secure renegotiation
121 */
122 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
123 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
124
125 *p++ = 0x00;
126 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
127 *p++ = ssl->verify_data_len & 0xFF;
128
129 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
130
131 *olen = 5 + ssl->verify_data_len;
132}
133
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200134#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100135static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
136 unsigned char *buf,
137 size_t *olen )
138{
139 unsigned char *p = buf;
Manuel Pégourié-Gonnard9c9812a2013-08-23 12:18:46 +0200140 unsigned char *sig_alg_list = buf + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100141 size_t sig_alg_len = 0;
142
143 *olen = 0;
144
145 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
146 return;
147
148 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
149
150 /*
151 * Prepare signature_algorithms extension (TLS 1.2)
152 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200153#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200154#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100155 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
156 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
157 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
158 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
159#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200160#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100161 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
162 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
163 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
164 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
165#endif
166#if defined(POLARSSL_SHA1_C)
167 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
168 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
169#endif
170#if defined(POLARSSL_MD5_C)
171 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
172 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
173#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200174#endif /* POLARSSL_RSA_C */
175#if defined(POLARSSL_ECDSA_C)
176#if defined(POLARSSL_SHA512_C)
177 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
178 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
179 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
180 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
181#endif
182#if defined(POLARSSL_SHA256_C)
183 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
184 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
185 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
186 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
187#endif
188#if defined(POLARSSL_SHA1_C)
189 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
190 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
191#endif
192#if defined(POLARSSL_MD5_C)
193 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
194 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
195#endif
196#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100197
198 /*
199 * enum {
200 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
201 * sha512(6), (255)
202 * } HashAlgorithm;
203 *
204 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
205 * SignatureAlgorithm;
206 *
207 * struct {
208 * HashAlgorithm hash;
209 * SignatureAlgorithm signature;
210 * } SignatureAndHashAlgorithm;
211 *
212 * SignatureAndHashAlgorithm
213 * supported_signature_algorithms<2..2^16-2>;
214 */
215 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
216 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
217
218 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
219 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
220
221 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
222 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
223
Paul Bakkerd3edc862013-03-20 16:07:17 +0100224 *olen = 6 + sig_alg_len;
225}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200226#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100227
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200228#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100229static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
230 unsigned char *buf,
231 size_t *olen )
232{
233 unsigned char *p = buf;
234 unsigned char elliptic_curve_list[20];
235 size_t elliptic_curve_len = 0;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200236 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100237
238 *olen = 0;
239
240 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
241
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200242#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100243 elliptic_curve_list[elliptic_curve_len++] = 0x00;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200244 elliptic_curve_list[elliptic_curve_len++] = ecp_named_curve_from_grp_id( POLARSSL_ECP_DP_SECP521R1 );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200245#endif
246#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100247 elliptic_curve_list[elliptic_curve_len++] = 0x00;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200248 elliptic_curve_list[elliptic_curve_len++] = ecp_named_curve_from_grp_id( POLARSSL_ECP_DP_SECP384R1 );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200249#endif
250#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100251 elliptic_curve_list[elliptic_curve_len++] = 0x00;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200252 elliptic_curve_list[elliptic_curve_len++] = ecp_named_curve_from_grp_id( POLARSSL_ECP_DP_SECP256R1 );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200253#endif
254#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100255 elliptic_curve_list[elliptic_curve_len++] = 0x00;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200256 elliptic_curve_list[elliptic_curve_len++] = ecp_named_curve_from_grp_id( POLARSSL_ECP_DP_SECP224R1 );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200257#endif
258#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100259 elliptic_curve_list[elliptic_curve_len++] = 0x00;
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200260 elliptic_curve_list[elliptic_curve_len++] = ecp_named_curve_from_grp_id( POLARSSL_ECP_DP_SECP192R1 );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200261#endif
262
263 if( elliptic_curve_len == 0 )
264 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100265
266 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
267 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
268
269 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
270 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
271
272 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
273 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
274
275 memcpy( p, elliptic_curve_list, elliptic_curve_len );
276
277 *olen = 6 + elliptic_curve_len;
278}
279
280static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
281 unsigned char *buf,
282 size_t *olen )
283{
284 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200285 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100286
287 *olen = 0;
288
289 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
290
291 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
292 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
293
294 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100295 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200296
297 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100298 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
299
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200300 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100301}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200302#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100303
Paul Bakker05decb22013-08-15 13:33:48 +0200304#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200305static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
306 unsigned char *buf,
307 size_t *olen )
308{
309 unsigned char *p = buf;
310
311 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
312 *olen = 0;
313 return;
314 }
315
316 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
317
318 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
319 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
320
321 *p++ = 0x00;
322 *p++ = 1;
323
324 *p++ = ssl->mfl_code;
325
326 *olen = 5;
327}
Paul Bakker05decb22013-08-15 13:33:48 +0200328#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200329
Paul Bakker1f2bc622013-08-15 13:45:55 +0200330#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200331static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
332 unsigned char *buf, size_t *olen )
333{
334 unsigned char *p = buf;
335
336 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
337 {
338 *olen = 0;
339 return;
340 }
341
342 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
343
344 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
345 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
346
347 *p++ = 0x00;
348 *p++ = 0x00;
349
350 *olen = 4;
351}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200352#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200353
Paul Bakkera503a632013-08-14 13:48:06 +0200354#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200355static void ssl_write_session_ticket_ext( ssl_context *ssl,
356 unsigned char *buf, size_t *olen )
357{
358 unsigned char *p = buf;
359 size_t tlen = ssl->session_negotiate->ticket_len;
360
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200361 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
362 {
363 *olen = 0;
364 return;
365 }
366
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200367 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
368
369 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
370 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
371
372 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
373 *p++ = (unsigned char)( ( tlen ) & 0xFF );
374
375 *olen = 4;
376
377 if( ssl->session_negotiate->ticket == NULL ||
378 ssl->session_negotiate->ticket_len == 0 )
379 {
380 return;
381 }
382
383 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
384
385 memcpy( p, ssl->session_negotiate->ticket, tlen );
386
387 *olen += tlen;
388}
Paul Bakkera503a632013-08-14 13:48:06 +0200389#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200390
Paul Bakker5121ce52009-01-03 21:22:43 +0000391static int ssl_write_client_hello( ssl_context *ssl )
392{
Paul Bakker23986e52011-04-24 08:57:21 +0000393 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100394 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200396 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200397#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200399#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200400 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200401 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
403 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
404
Paul Bakker48916f92012-09-16 19:57:18 +0000405 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
406 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000407 ssl->major_ver = ssl->min_major_ver;
408 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000409 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
Paul Bakker490ecc82011-10-06 13:04:09 +0000411 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
412 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200413 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
414 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000415 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417 /*
418 * 0 . 0 handshake type
419 * 1 . 3 handshake length
420 * 4 . 5 highest version supported
421 * 6 . 9 current UNIX time
422 * 10 . 37 random bytes
423 */
424 buf = ssl->out_msg;
425 p = buf + 4;
426
427 *p++ = (unsigned char) ssl->max_major_ver;
428 *p++ = (unsigned char) ssl->max_minor_ver;
429
430 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
431 buf[4], buf[5] ) );
432
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200433#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 t = time( NULL );
435 *p++ = (unsigned char)( t >> 24 );
436 *p++ = (unsigned char)( t >> 16 );
437 *p++ = (unsigned char)( t >> 8 );
438 *p++ = (unsigned char)( t );
439
440 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200441#else
442 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
443 return( ret );
444
445 p += 4;
446#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
Paul Bakkera3d195c2011-11-27 21:07:34 +0000448 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
449 return( ret );
450
451 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
Paul Bakker48916f92012-09-16 19:57:18 +0000453 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
455 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
456
457 /*
458 * 38 . 38 session id length
459 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000460 * 40+n . 41+n ciphersuitelist length
461 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000462 * .. . .. compression methods length
463 * .. . .. compression methods
464 * .. . .. extensions length
465 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 */
Paul Bakker48916f92012-09-16 19:57:18 +0000467 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Paul Bakker0a597072012-09-25 21:55:46 +0000469 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
470 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200471 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200473 }
474
Paul Bakkera503a632013-08-14 13:48:06 +0200475#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200476 /*
477 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
478 * generate and include a Session ID in the TLS ClientHello."
479 */
480 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
481 ssl->session_negotiate->ticket != NULL &&
482 ssl->session_negotiate->ticket_len != 0 )
483 {
484 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
485
486 if( ret != 0 )
487 return( ret );
488
489 ssl->session_negotiate->length = n = 32;
490 }
Paul Bakkera503a632013-08-14 13:48:06 +0200491#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493 *p++ = (unsigned char) n;
494
495 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000496 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
499 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
500
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200501 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200502 n = 0;
503 q = p;
504
505 // Skip writing ciphersuite length for now
506 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Paul Bakker48916f92012-09-16 19:57:18 +0000508 /*
509 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
510 */
511 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
512 {
513 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
514 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200515 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000516 }
517
Paul Bakker2fbefde2013-06-29 16:01:15 +0200518 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200520 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
521
522 if( ciphersuite_info == NULL )
523 continue;
524
525 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
526 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
527 continue;
528
Paul Bakkere3166ce2011-01-27 17:40:50 +0000529 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200530 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Paul Bakker2fbefde2013-06-29 16:01:15 +0200532 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200533 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
534 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 }
536
Paul Bakker2fbefde2013-06-29 16:01:15 +0200537 *q++ = (unsigned char)( n >> 7 );
538 *q++ = (unsigned char)( n << 1 );
539
540 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
541
542
Paul Bakker2770fbd2012-07-03 13:30:23 +0000543#if defined(POLARSSL_ZLIB_SUPPORT)
544 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
545 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000546 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000547
548 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000549 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000550 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000551#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000553 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555 *p++ = 1;
556 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000557#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
Paul Bakkerd3edc862013-03-20 16:07:17 +0100559 // First write extensions, then the total length
560 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200561#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100562 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
563 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200564#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Paul Bakkerd3edc862013-03-20 16:07:17 +0100566 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
567 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000568
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200569#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100570 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
571 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200572#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000573
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200574#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100575 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
576 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100577
Paul Bakkerd3edc862013-03-20 16:07:17 +0100578 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
579 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100580#endif
581
Paul Bakker05decb22013-08-15 13:33:48 +0200582#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200583 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
584 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200585#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200586
Paul Bakker1f2bc622013-08-15 13:45:55 +0200587#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200588 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
589 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200590#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200591
Paul Bakkera503a632013-08-14 13:48:06 +0200592#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200593 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
594 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200595#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200596
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000597 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
598 ext_len ) );
599
600 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
601 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100602 p += ext_len;
Paul Bakker41c83d32013-03-20 14:39:14 +0100603
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 ssl->out_msglen = p - buf;
605 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
606 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
607
608 ssl->state++;
609
610 if( ( ret = ssl_write_record( ssl ) ) != 0 )
611 {
612 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
613 return( ret );
614 }
615
616 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
617
618 return( 0 );
619}
620
Paul Bakker48916f92012-09-16 19:57:18 +0000621static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200622 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000623 size_t len )
624{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000625 int ret;
626
Paul Bakker48916f92012-09-16 19:57:18 +0000627 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
628 {
629 if( len != 1 || buf[0] != 0x0 )
630 {
631 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000632
633 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
634 return( ret );
635
Paul Bakker48916f92012-09-16 19:57:18 +0000636 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
637 }
638
639 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
640 }
641 else
642 {
643 if( len != 1 + ssl->verify_data_len * 2 ||
644 buf[0] != ssl->verify_data_len * 2 ||
645 memcmp( buf + 1, ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
646 memcmp( buf + 1 + ssl->verify_data_len,
647 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
648 {
649 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000650
651 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
652 return( ret );
653
Paul Bakker48916f92012-09-16 19:57:18 +0000654 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
655 }
656 }
657
658 return( 0 );
659}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200660
Paul Bakker05decb22013-08-15 13:33:48 +0200661#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200662static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200663 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200664 size_t len )
665{
666 /*
667 * server should use the extension only if we did,
668 * and if so the server's value should match ours (and len is always 1)
669 */
670 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
671 len != 1 ||
672 buf[0] != ssl->mfl_code )
673 {
674 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
675 }
676
677 return( 0 );
678}
Paul Bakker05decb22013-08-15 13:33:48 +0200679#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000680
Paul Bakker1f2bc622013-08-15 13:45:55 +0200681#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200682static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
683 const unsigned char *buf,
684 size_t len )
685{
686 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
687 len != 0 )
688 {
689 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
690 }
691
692 ((void) buf);
693
694 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
695
696 return( 0 );
697}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200698#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200699
Paul Bakkera503a632013-08-14 13:48:06 +0200700#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200701static int ssl_parse_session_ticket_ext( ssl_context *ssl,
702 const unsigned char *buf,
703 size_t len )
704{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200705 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
706 len != 0 )
707 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200708 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200709 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200710
711 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200712
713 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200714
715 return( 0 );
716}
Paul Bakkera503a632013-08-14 13:48:06 +0200717#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200718
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200719#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200720static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
721 const unsigned char *buf,
722 size_t len )
723{
724 size_t list_size;
725 const unsigned char *p;
726
727 list_size = buf[0];
728 if( list_size + 1 != len )
729 {
730 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
731 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
732 }
733
734 p = buf + 2;
735 while( list_size > 0 )
736 {
737 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
738 p[0] == POLARSSL_ECP_PF_COMPRESSED )
739 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200740 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200741 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
742 return( 0 );
743 }
744
745 list_size--;
746 p++;
747 }
748
749 return( 0 );
750}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200751#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200752
Paul Bakker5121ce52009-01-03 21:22:43 +0000753static int ssl_parse_server_hello( ssl_context *ssl )
754{
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200755 uint32_t t;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000756 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +0000757 size_t n;
Paul Bakker48916f92012-09-16 19:57:18 +0000758 size_t ext_len = 0;
759 unsigned char *buf, *ext;
760 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000761 int handshake_failure = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
764
765 /*
766 * 0 . 0 handshake type
767 * 1 . 3 handshake length
768 * 4 . 5 protocol version
769 * 6 . 9 UNIX time()
770 * 10 . 37 random bytes
771 */
772 buf = ssl->in_msg;
773
774 if( ( ret = ssl_read_record( ssl ) ) != 0 )
775 {
776 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
777 return( ret );
778 }
779
780 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
781 {
782 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000783 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000784 }
785
786 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
787 buf[4], buf[5] ) );
788
789 if( ssl->in_hslen < 42 ||
790 buf[0] != SSL_HS_SERVER_HELLO ||
791 buf[4] != SSL_MAJOR_VERSION_3 )
792 {
793 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000794 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000795 }
796
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000797 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +0000798 {
799 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000800 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000801 }
802
803 ssl->minor_ver = buf[5];
804
Paul Bakker1d29fb52012-09-28 13:28:45 +0000805 if( ssl->minor_ver < ssl->min_minor_ver )
806 {
807 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
808 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
809 buf[4], buf[5] ) );
810
811 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
812 SSL_ALERT_MSG_PROTOCOL_VERSION );
813
814 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
815 }
816
Paul Bakker1504af52012-02-11 16:17:43 +0000817#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200818 t = ( (uint32_t) buf[6] << 24 )
819 | ( (uint32_t) buf[7] << 16 )
820 | ( (uint32_t) buf[8] << 8 )
821 | ( (uint32_t) buf[9] );
Paul Bakker87e5cda2012-01-14 18:14:15 +0000822#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000823
Paul Bakker48916f92012-09-16 19:57:18 +0000824 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000825
826 n = buf[38];
827
828 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
829 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
830
Paul Bakker48916f92012-09-16 19:57:18 +0000831 if( n > 32 )
832 {
833 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
834 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
835 }
836
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 /*
838 * 38 . 38 session id length
839 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000840 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 * 41+n . 41+n chosen compression alg.
842 * 42+n . 43+n extensions length
843 * 44+n . 44+n+m extensions
844 */
Paul Bakker48916f92012-09-16 19:57:18 +0000845 if( ssl->in_hslen > 42 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000846 {
847 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +0000848 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000849
Paul Bakker48916f92012-09-16 19:57:18 +0000850 if( ( ext_len > 0 && ext_len < 4 ) ||
851 ssl->in_hslen != 44 + n + ext_len )
852 {
853 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
854 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
855 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 }
857
858 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +0000859 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakker380da532012-04-18 16:10:25 +0000861 /*
862 * Initialize update checksum functions
863 */
Paul Bakker68884e32013-01-07 18:20:04 +0100864 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
Paul Bakker41c83d32013-03-20 14:39:14 +0100865 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker68884e32013-01-07 18:20:04 +0100866
867 if( ssl->transform_negotiate->ciphersuite_info == NULL )
868 {
869 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200870 ssl->ciphersuite_list[ssl->minor_ver][i] ) );
Paul Bakker68884e32013-01-07 18:20:04 +0100871 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
872 }
Paul Bakker380da532012-04-18 16:10:25 +0000873
Paul Bakker5121ce52009-01-03 21:22:43 +0000874 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
875 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
876
877 /*
878 * Check if the session can be resumed
879 */
Paul Bakker0a597072012-09-25 21:55:46 +0000880 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
881 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000882 ssl->session_negotiate->ciphersuite != i ||
883 ssl->session_negotiate->compression != comp ||
884 ssl->session_negotiate->length != n ||
885 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000886 {
887 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +0000888 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200889#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +0000890 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200891#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000892 ssl->session_negotiate->ciphersuite = i;
893 ssl->session_negotiate->compression = comp;
894 ssl->session_negotiate->length = n;
895 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000896 }
897 else
898 {
899 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000900
901 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
902 {
903 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
904 return( ret );
905 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000906 }
907
908 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +0000909 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Paul Bakkere3166ce2011-01-27 17:40:50 +0000911 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000912 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
913
914 i = 0;
915 while( 1 )
916 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200917 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000918 {
919 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000920 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 }
922
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200923 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
924 ssl->session_negotiate->ciphersuite )
925 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200927 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 }
929
Paul Bakker2770fbd2012-07-03 13:30:23 +0000930 if( comp != SSL_COMPRESS_NULL
931#if defined(POLARSSL_ZLIB_SUPPORT)
932 && comp != SSL_COMPRESS_DEFLATE
933#endif
934 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 {
936 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000937 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 }
Paul Bakker48916f92012-09-16 19:57:18 +0000939 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000940
Paul Bakker48916f92012-09-16 19:57:18 +0000941 ext = buf + 44 + n;
942
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200943 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
944
Paul Bakker48916f92012-09-16 19:57:18 +0000945 while( ext_len )
946 {
947 unsigned int ext_id = ( ( ext[0] << 8 )
948 | ( ext[1] ) );
949 unsigned int ext_size = ( ( ext[2] << 8 )
950 | ( ext[3] ) );
951
952 if( ext_size + 4 > ext_len )
953 {
954 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
955 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
956 }
957
958 switch( ext_id )
959 {
960 case TLS_EXT_RENEGOTIATION_INFO:
961 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
962 renegotiation_info_seen = 1;
963
964 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ) ) != 0 )
965 return( ret );
966
967 break;
968
Paul Bakker05decb22013-08-15 13:33:48 +0200969#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200970 case TLS_EXT_MAX_FRAGMENT_LENGTH:
971 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
972
973 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
974 ext + 4, ext_size ) ) != 0 )
975 {
976 return( ret );
977 }
978
979 break;
Paul Bakker05decb22013-08-15 13:33:48 +0200980#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200981
Paul Bakker1f2bc622013-08-15 13:45:55 +0200982#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200983 case TLS_EXT_TRUNCATED_HMAC:
984 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
985
986 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
987 ext + 4, ext_size ) ) != 0 )
988 {
989 return( ret );
990 }
991
992 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200993#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200994
Paul Bakkera503a632013-08-14 13:48:06 +0200995#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200996 case TLS_EXT_SESSION_TICKET:
997 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
998
999 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1000 ext + 4, ext_size ) ) != 0 )
1001 {
1002 return( ret );
1003 }
1004
1005 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001006#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001007
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001008#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001009 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1010 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1011
1012 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1013 ext + 4, ext_size ) ) != 0 )
1014 {
1015 return( ret );
1016 }
1017
1018 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001019#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001020
Paul Bakker48916f92012-09-16 19:57:18 +00001021 default:
1022 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1023 ext_id ) );
1024 }
1025
1026 ext_len -= 4 + ext_size;
1027 ext += 4 + ext_size;
1028
1029 if( ext_len > 0 && ext_len < 4 )
1030 {
1031 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1032 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1033 }
1034 }
1035
1036 /*
1037 * Renegotiation security checks
1038 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001039 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1040 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001041 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001042 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1043 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001044 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001045 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1046 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1047 renegotiation_info_seen == 0 )
1048 {
1049 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1050 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001051 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001052 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1053 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1054 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001055 {
1056 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001057 handshake_failure = 1;
1058 }
1059 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1060 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1061 renegotiation_info_seen == 1 )
1062 {
1063 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1064 handshake_failure = 1;
1065 }
1066
1067 if( handshake_failure == 1 )
1068 {
1069 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1070 return( ret );
1071
Paul Bakker48916f92012-09-16 19:57:18 +00001072 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
1075 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1076
1077 return( 0 );
1078}
1079
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001080#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1081 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001082static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1083 unsigned char *end )
1084{
1085 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1086
Paul Bakker29e1f122013-04-16 13:07:56 +02001087 /*
1088 * Ephemeral DH parameters:
1089 *
1090 * struct {
1091 * opaque dh_p<1..2^16-1>;
1092 * opaque dh_g<1..2^16-1>;
1093 * opaque dh_Ys<1..2^16-1>;
1094 * } ServerDHParams;
1095 */
1096 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1097 {
1098 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1099 return( ret );
1100 }
1101
1102 if( ssl->handshake->dhm_ctx.len < 64 ||
1103 ssl->handshake->dhm_ctx.len > 512 )
1104 {
1105 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1106 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1107 }
1108
1109 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1110 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1111 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001112
1113 return( ret );
1114}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001115#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1116 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001117
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001118#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1119 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001120static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1121 unsigned char **p,
1122 unsigned char *end )
1123{
1124 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1125
Paul Bakker29e1f122013-04-16 13:07:56 +02001126 /*
1127 * Ephemeral ECDH parameters:
1128 *
1129 * struct {
1130 * ECParameters curve_params;
1131 * ECPoint public;
1132 * } ServerECDHParams;
1133 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001134 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1135 (const unsigned char **) p, end ) ) != 0 )
1136 {
1137 SSL_DEBUG_RET( 2, ( "ecdh_read_params" ), ret );
1138 return( ret );
1139 }
1140
1141 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1142 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
1143 {
1144 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDH length)" ) );
1145 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1146 }
1147
1148 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
Paul Bakker29e1f122013-04-16 13:07:56 +02001149
1150 return( ret );
1151}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001152#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1153 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001154
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001155#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1156 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001157static int ssl_parse_server_psk_hint( ssl_context *ssl,
1158 unsigned char **p,
1159 unsigned char *end )
1160{
1161 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001162 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001163 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001164
1165 /*
1166 * PSK parameters:
1167 *
1168 * opaque psk_identity_hint<0..2^16-1>;
1169 */
1170 len = (*p)[1] << 8 | (*p)[0];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001171 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001172
1173 if( (*p) + len > end )
1174 {
1175 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1176 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1177 }
1178
1179 // TODO: Retrieve PSK identity hint and callback to app
1180 //
1181 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001182 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001183
1184 return( ret );
1185}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001186#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1187 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001188
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001189#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001190#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001191 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1192 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001193static int ssl_parse_signature_algorithm( ssl_context *ssl,
1194 unsigned char **p,
1195 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001196 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001197 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001198{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001199 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001200 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001201 *pk_alg = POLARSSL_PK_NONE;
1202
1203 /* Only in TLS 1.2 */
1204 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1205 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001206 return( 0 );
1207 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001208
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001209 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001210 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1211
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001212 /*
1213 * Get hash algorithm
1214 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001215 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001216 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001217 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1218 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001219 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1220 }
1221
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001222 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001223 * Get signature algorithm
1224 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001225 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001226 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001227 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1228 "SignatureAlgorithm %d", (*p)[1] ) );
1229 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001230 }
1231
1232 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1233 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1234 *p += 2;
1235
1236 return( 0 );
1237}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001238#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001239 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1240 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001241#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001242
Paul Bakker41c83d32013-03-20 14:39:14 +01001243static int ssl_parse_server_key_exchange( ssl_context *ssl )
1244{
Paul Bakker23986e52011-04-24 08:57:21 +00001245 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001246 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001248#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001249 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1250 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001251 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001252 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001253 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001254 size_t hashlen;
1255 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001256#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
1258 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1259
Paul Bakker41c83d32013-03-20 14:39:14 +01001260 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001261 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001262 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001263 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_PSK &&
1264 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 {
1266 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1267 ssl->state++;
1268 return( 0 );
1269 }
1270
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1272 {
1273 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1274 return( ret );
1275 }
1276
1277 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1278 {
1279 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001280 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 }
1282
1283 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1284 {
Paul Bakker188c8de2013-04-19 09:13:37 +02001285 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
1286 {
1287 ssl->record_read = 1;
1288 goto exit;
1289 }
1290
1291 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1292 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
1294
Paul Bakker1ef83d62012-04-11 12:09:53 +00001295 SSL_DEBUG_BUF( 3, "server key exchange", ssl->in_msg + 4, ssl->in_hslen - 4 );
1296
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001297 p = ssl->in_msg + 4;
1298 end = ssl->in_msg + ssl->in_hslen;
1299
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001300#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01001301 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001303 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001304 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001305 SSL_DEBUG_MSG( 1, ( "failed to parsebad server key exchange message" ) );
1306 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1307 }
1308 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001309 else
1310#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001311#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1312 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1313 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1314 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001315 {
1316 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1317 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001318 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1319 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1320 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001321 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001322 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001323#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1324 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001325#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1326 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker41c83d32013-03-20 14:39:14 +01001327 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001328 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001329 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001330 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1331 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1332 }
1333 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001334 else
1335#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
1336#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1337 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1338 {
1339 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1340 {
1341 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1342 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1343 }
1344 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
1345 {
1346 SSL_DEBUG_MSG( 1, ( "failed to parsebad server key exchange message" ) );
1347 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1348 }
1349 }
1350 else
1351#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1352 {
1353 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1354 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001355
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001356#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001357 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1358 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001359 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001360 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1361 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001362 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001363 params_len = p - ( ssl->in_msg + 4 );
1364
Paul Bakker29e1f122013-04-16 13:07:56 +02001365 /*
1366 * Handle the digitally-signed structure
1367 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001368#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1369 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001370 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001371 if( ssl_parse_signature_algorithm( ssl, &p, end,
1372 &md_alg, &pk_alg ) != 0 )
1373 {
1374 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1375 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1376 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001377
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001378 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001379 {
1380 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1381 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1382 }
1383 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001384 else
Paul Bakker577e0062013-08-28 11:57:20 +02001385#endif
Paul Bakker9659dae2013-08-28 16:21:34 +02001386#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1387 defined(POLARSSL_SSL_PROTO_TLS1_1)
1388 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001389 {
1390 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001391
Paul Bakker9659dae2013-08-28 16:21:34 +02001392 /* Default hash for ECDSA is SHA-1 */
1393 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1394 md_alg = POLARSSL_MD_SHA1;
1395 }
1396 else
1397#endif
1398 {
1399 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1400 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1401 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001402
1403 /*
1404 * Read signature
1405 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001406 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001407 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001408
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001409 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001410 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001411 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001412 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1413 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001414
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001415 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001416
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001417 /*
1418 * Compute the hash that has been signed
1419 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001420#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1421 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001422 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001423 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001424 md5_context md5;
1425 sha1_context sha1;
1426
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001427 hashlen = 36;
1428
Paul Bakker29e1f122013-04-16 13:07:56 +02001429 /*
1430 * digitally-signed struct {
1431 * opaque md5_hash[16];
1432 * opaque sha_hash[20];
1433 * };
1434 *
1435 * md5_hash
1436 * MD5(ClientHello.random + ServerHello.random
1437 * + ServerParams);
1438 * sha_hash
1439 * SHA(ClientHello.random + ServerHello.random
1440 * + ServerParams);
1441 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001442 md5_starts( &md5 );
1443 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001444 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001445 md5_finish( &md5, hash );
1446
1447 sha1_starts( &sha1 );
1448 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001449 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001450 sha1_finish( &sha1, hash + 16 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001451 }
1452 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001453#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1454 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001455#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1456 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001457 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001458 {
1459 md_context_t ctx;
1460
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001461 /* Info from md_alg will be used instead */
1462 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001463
1464 /*
1465 * digitally-signed struct {
1466 * opaque client_random[32];
1467 * opaque server_random[32];
1468 * ServerDHParams params;
1469 * };
1470 */
1471 if( ( ret = md_init_ctx( &ctx, md_info_from_type( md_alg ) ) ) != 0 )
1472 {
1473 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1474 return( ret );
1475 }
1476
1477 md_starts( &ctx );
1478 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001479 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001480 md_finish( &ctx, hash );
Paul Bakker04376b12013-08-16 14:45:26 +02001481 md_free_ctx( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001482 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001483 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001484#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1485 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001486 {
Paul Bakker577e0062013-08-28 11:57:20 +02001487 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1488 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1489 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001490
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001491 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1492 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001493
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001494 /*
1495 * Verify signature
1496 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001497 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001498 {
1499 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1500 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1501 }
1502
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001503 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1504 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001505 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001506 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001507 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001510#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001511 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1512 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001513
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001514exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001515 ssl->state++;
1516
1517 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
1518
1519 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001520}
1521
1522static int ssl_parse_certificate_request( ssl_context *ssl )
1523{
1524 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01001525 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001526 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001527 size_t cert_type_len = 0, dn_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001528
1529 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1530
1531 /*
1532 * 0 . 0 handshake type
1533 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01001534 * 4 . 4 cert type count
1535 * 5 .. m-1 cert types
1536 * m .. m+1 sig alg length (TLS 1.2 only)
1537 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001538 * n .. n+1 length of all DNs
1539 * n+2 .. n+3 length of DN 1
1540 * n+4 .. ... Distinguished Name #1
1541 * ... .. ... length of DN 2, etc.
1542 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001543 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001545 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1546 {
1547 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1548 return( ret );
1549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001550
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001551 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1552 {
1553 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1554 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1555 }
1556
1557 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 }
1559
1560 ssl->client_auth = 0;
1561 ssl->state++;
1562
1563 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
1564 ssl->client_auth++;
1565
1566 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
1567 ssl->client_auth ? "a" : "no" ) );
1568
Paul Bakker926af752012-11-23 13:38:07 +01001569 if( ssl->client_auth == 0 )
1570 goto exit;
1571
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001572 ssl->record_read = 0;
1573
Paul Bakker926af752012-11-23 13:38:07 +01001574 // TODO: handshake_failure alert for an anonymous server to request
1575 // client authentication
1576
1577 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001578
Paul Bakker926af752012-11-23 13:38:07 +01001579 // Retrieve cert types
1580 //
1581 cert_type_len = buf[4];
1582 n = cert_type_len;
1583
1584 if( ssl->in_hslen < 6 + n )
1585 {
1586 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1587 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1588 }
1589
Paul Bakker73d44312013-05-22 13:56:26 +02001590 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01001591 while( cert_type_len > 0 )
1592 {
1593 if( *p == SSL_CERT_TYPE_RSA_SIGN )
1594 {
1595 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
1596 break;
1597 }
1598
1599 cert_type_len--;
1600 p++;
1601 }
1602
1603 if( ssl->handshake->cert_type == 0 )
1604 {
1605 SSL_DEBUG_MSG( 1, ( "no known cert_type provided" ) );
1606 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1607 }
1608
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001609#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001610 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1611 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001612 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
1613 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01001614
1615 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001616 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01001617 n += sig_alg_len;
1618
1619 if( ssl->in_hslen < 6 + n )
1620 {
1621 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1622 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1623 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001624 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001625#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01001626
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001627 dn_len = ( ( buf[5 + m + n] << 8 )
1628 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01001629
1630 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001631 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01001632 {
1633 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1634 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1635 }
1636
1637exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001638 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1639
1640 return( 0 );
1641}
1642
1643static int ssl_parse_server_hello_done( ssl_context *ssl )
1644{
1645 int ret;
1646
1647 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
1648
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001649 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650 {
1651 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1652 {
1653 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1654 return( ret );
1655 }
1656
1657 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1658 {
1659 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001660 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001661 }
1662 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001663 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
1665 if( ssl->in_hslen != 4 ||
1666 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
1667 {
1668 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001669 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 }
1671
1672 ssl->state++;
1673
1674 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
1675
1676 return( 0 );
1677}
1678
1679static int ssl_write_client_key_exchange( ssl_context *ssl )
1680{
Paul Bakker23986e52011-04-24 08:57:21 +00001681 int ret;
1682 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01001683 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
1685 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
1686
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001687#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01001688 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001689 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001690 /*
1691 * DHM key exchange -- send G^X mod P
1692 */
Paul Bakker48916f92012-09-16 19:57:18 +00001693 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
1695 ssl->out_msg[4] = (unsigned char)( n >> 8 );
1696 ssl->out_msg[5] = (unsigned char)( n );
1697 i = 6;
1698
Paul Bakker29b64762012-09-25 09:36:44 +00001699 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
1700 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 &ssl->out_msg[i], n,
1702 ssl->f_rng, ssl->p_rng );
1703 if( ret != 0 )
1704 {
1705 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
1706 return( ret );
1707 }
1708
Paul Bakker48916f92012-09-16 19:57:18 +00001709 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1710 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00001711
Paul Bakker48916f92012-09-16 19:57:18 +00001712 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001713
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02001714 /* No blinding needed for DHE, but will be needed for fixed DH! */
Paul Bakker48916f92012-09-16 19:57:18 +00001715 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
1716 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02001717 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02001718 NULL, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001719 {
1720 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1721 return( ret );
1722 }
1723
Paul Bakker48916f92012-09-16 19:57:18 +00001724 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00001725 }
1726 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001727#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001728#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1729 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1730 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1731 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01001732 {
1733 /*
1734 * ECDH key exchange -- send client public value
1735 */
1736 i = 4;
1737
1738 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
1739 &n,
1740 &ssl->out_msg[i], 1000,
1741 ssl->f_rng, ssl->p_rng );
1742 if( ret != 0 )
1743 {
1744 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
1745 return( ret );
1746 }
1747
1748 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
1749
1750 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
1751 &ssl->handshake->pmslen,
1752 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001753 POLARSSL_MPI_MAX_SIZE,
1754 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001755 {
1756 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1757 return( ret );
1758 }
1759
1760 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1761 }
1762 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001763#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1764 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001765#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1766 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
1767 {
1768 unsigned char *p = ssl->handshake->premaster;
1769
1770 /*
1771 * PSK key exchange
1772 *
1773 * opaque psk_identity<0..2^16-1>;
1774 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001775 if( ssl->psk == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001776 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
1777
1778 if( sizeof(ssl->handshake->premaster) < 4 + 2 * ssl->psk_len )
1779 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1780
1781 n = ssl->psk_identity_len;
1782
1783 ssl->out_msg[4] = (unsigned char)( n >> 8 );
1784 ssl->out_msg[5] = (unsigned char)( n );
1785 i = 6;
1786
1787 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
1788
1789 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1790 *(p++) = (unsigned char)( ssl->psk_len );
1791 p += ssl->psk_len;
1792
1793 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1794 *(p++) = (unsigned char)( ssl->psk_len );
1795 memcpy( p, ssl->psk, ssl->psk_len );
1796 p += ssl->psk_len;
1797
1798 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
1799 }
1800 else
1801#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001802#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1803 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1804 {
1805 unsigned char *p = ssl->handshake->premaster;
1806
1807 /*
1808 * DHE_PSK key exchange
1809 *
1810 * opaque psk_identity<0..2^16-1>;
1811 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
1812 */
1813 if( ssl->psk == NULL )
1814 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
1815
1816 if( sizeof(ssl->handshake->premaster) < 4 + ssl->psk_identity_len +
1817 ssl->handshake->dhm_ctx.len )
1818 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1819
1820 i = 4;
1821 n = ssl->psk_identity_len;
1822 ssl->out_msg[4] = (unsigned char)( n >> 8 );
1823 ssl->out_msg[5] = (unsigned char)( n );
1824
1825 memcpy( ssl->out_msg + 6, ssl->psk_identity, ssl->psk_identity_len );
1826
1827 n = ssl->handshake->dhm_ctx.len;
1828 ssl->out_msg[6 + ssl->psk_identity_len] = (unsigned char)( n >> 8 );
1829 ssl->out_msg[7 + ssl->psk_identity_len] = (unsigned char)( n );
1830
1831 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
1832 mpi_size( &ssl->handshake->dhm_ctx.P ),
1833 &ssl->out_msg[8 + ssl->psk_identity_len], n,
1834 ssl->f_rng, ssl->p_rng );
1835 if( ret != 0 )
1836 {
1837 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
1838 return( ret );
1839 }
1840
1841 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1842 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1843
1844 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
1845 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02001846 /* No blinding needed since this is ephemeral DHM */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001847 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard032c34e2013-09-07 13:06:27 +02001848 p, &n, NULL, NULL ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001849 {
1850 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1851 return( ret );
1852 }
1853
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001854 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1855
1856 p += ssl->handshake->dhm_ctx.len;
1857
1858 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1859 *(p++) = (unsigned char)( ssl->psk_len );
1860 memcpy( p, ssl->psk, ssl->psk_len );
1861 p += ssl->psk_len;
1862
1863 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
1864 n = ssl->handshake->pmslen;
1865 }
1866 else
1867#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1868#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02001869 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 {
1871 /*
1872 * RSA key exchange -- send rsa_public(pkcs1 v1.5(premaster))
1873 */
Paul Bakker48916f92012-09-16 19:57:18 +00001874 ssl->handshake->premaster[0] = (unsigned char) ssl->max_major_ver;
1875 ssl->handshake->premaster[1] = (unsigned char) ssl->max_minor_ver;
1876 ssl->handshake->pmslen = 48;
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
Paul Bakker48916f92012-09-16 19:57:18 +00001878 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster + 2,
1879 ssl->handshake->pmslen - 2 );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001880 if( ret != 0 )
1881 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001883 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1884 POLARSSL_PK_RSA ) )
1885 {
1886 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1887 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1888 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001889
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001890 i = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 4 : 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001892 ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1893 ssl->handshake->premaster, ssl->handshake->pmslen,
1894 ssl->out_msg + i, &n, SSL_BUFFER_LEN,
1895 ssl->f_rng, ssl->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 if( ret != 0 )
1897 {
1898 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1899 return( ret );
1900 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001901
Paul Bakker577e0062013-08-28 11:57:20 +02001902#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1903 defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001904 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
1905 {
1906 ssl->out_msg[4] = (unsigned char)( n >> 8 );
1907 ssl->out_msg[5] = (unsigned char)( n );
1908 }
Paul Bakker577e0062013-08-28 11:57:20 +02001909#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001910
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 }
Paul Bakkered27a042013-04-18 22:46:23 +02001912 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001913#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02001914 {
1915 ((void) ciphersuite_info);
1916 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1917 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
Paul Bakkerff60ee62010-03-16 21:09:09 +00001919 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1920 {
1921 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1922 return( ret );
1923 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
1925 ssl->out_msglen = i + n;
1926 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1927 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
1928
1929 ssl->state++;
1930
1931 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1932 {
1933 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1934 return( ret );
1935 }
1936
1937 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
1938
1939 return( 0 );
1940}
1941
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001942#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1943 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
1944 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001945static int ssl_write_certificate_verify( ssl_context *ssl )
1946{
Paul Bakkered27a042013-04-18 22:46:23 +02001947 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1948 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001949
1950 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1951
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001952 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1953 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02001954 {
1955 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
1956 ssl->state++;
1957 return( 0 );
1958 }
1959
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001960 return( ret );
1961}
1962#else
1963static int ssl_write_certificate_verify( ssl_context *ssl )
1964{
1965 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1966 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
1967 size_t n = 0, offset = 0;
1968 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001969 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001970 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02001971 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001972
1973 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1974
1975 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1976 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1977 {
1978 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
1979 ssl->state++;
1980 return( 0 );
1981 }
1982
Paul Bakkered27a042013-04-18 22:46:23 +02001983 if( ssl->client_auth == 0 || ssl->own_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 {
1985 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
1986 ssl->state++;
1987 return( 0 );
1988 }
1989
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001990 if( ssl->pk_key == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001991 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00001992 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
1993 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 }
1995
1996 /*
1997 * Make an RSA signature of the handshake digests
1998 */
Paul Bakker48916f92012-09-16 19:57:18 +00001999 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002000
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002001#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2002 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002003 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002004 {
Paul Bakker926af752012-11-23 13:38:07 +01002005 /*
2006 * digitally-signed struct {
2007 * opaque md5_hash[16];
2008 * opaque sha_hash[20];
2009 * };
2010 *
2011 * md5_hash
2012 * MD5(handshake_messages);
2013 *
2014 * sha_hash
2015 * SHA(handshake_messages);
2016 */
2017 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002018 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002019
2020 /*
2021 * For ECDSA, default hash is SHA-1 only
2022 */
2023 if( pk_can_do( ssl->pk_key, POLARSSL_PK_ECDSA ) )
2024 {
2025 hash_start += 16;
2026 hashlen -= 16;
2027 md_alg = POLARSSL_MD_SHA1;
2028 }
Paul Bakker926af752012-11-23 13:38:07 +01002029 }
2030 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002031#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2032 POLARSSL_SSL_PROTO_TLS1_1 */
2033#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2034 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002035 {
2036 /*
2037 * digitally-signed struct {
2038 * opaque handshake_messages[handshake_messages_length];
2039 * };
2040 *
2041 * Taking shortcut here. We assume that the server always allows the
2042 * PRF Hash function and has sent it in the allowed signature
2043 * algorithms list received in the Certificate Request message.
2044 *
2045 * Until we encounter a server that does not, we will take this
2046 * shortcut.
2047 *
2048 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2049 * in order to satisfy 'weird' needs from the server side.
2050 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002051 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2052 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002053 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002054 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002055 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002056 }
2057 else
2058 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002059 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002060 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002061 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002062 ssl->out_msg[5] = ssl_sig_from_pk( ssl->pk_key );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002063
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002064 /* Info from md_alg will be used instead */
2065 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002066 offset = 2;
2067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002068 else
2069#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002070 {
2071 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002072 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002073 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002074
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002075 if( ( ret = pk_sign( ssl->pk_key, md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002076 ssl->out_msg + 6 + offset, &n,
2077 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002078 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002079 SSL_DEBUG_RET( 1, "pk_sign", ret );
2080 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002081 }
Paul Bakker926af752012-11-23 13:38:07 +01002082
Paul Bakker1ef83d62012-04-11 12:09:53 +00002083 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2084 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085
Paul Bakker1ef83d62012-04-11 12:09:53 +00002086 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2088 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2089
2090 ssl->state++;
2091
2092 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2093 {
2094 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2095 return( ret );
2096 }
2097
2098 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2099
Paul Bakkered27a042013-04-18 22:46:23 +02002100 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002102#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2103 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2104 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
Paul Bakkera503a632013-08-14 13:48:06 +02002106#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002107static int ssl_parse_new_session_ticket( ssl_context *ssl )
2108{
2109 int ret;
2110 uint32_t lifetime;
2111 size_t ticket_len;
2112 unsigned char *ticket;
2113
2114 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2115
2116 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2117 {
2118 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2119 return( ret );
2120 }
2121
2122 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2123 {
2124 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2125 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2126 }
2127
2128 /*
2129 * struct {
2130 * uint32 ticket_lifetime_hint;
2131 * opaque ticket<0..2^16-1>;
2132 * } NewSessionTicket;
2133 *
2134 * 0 . 0 handshake message type
2135 * 1 . 3 handshake message length
2136 * 4 . 7 ticket_lifetime_hint
2137 * 8 . 9 ticket_len (n)
2138 * 10 . 9+n ticket content
2139 */
2140 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2141 ssl->in_hslen < 10 )
2142 {
2143 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2144 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2145 }
2146
2147 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2148 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2149
2150 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2151
2152 if( ticket_len + 10 != ssl->in_hslen )
2153 {
2154 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2155 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2156 }
2157
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002158 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2159
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002160 /* We're not waiting for a NewSessionTicket message any more */
2161 ssl->handshake->new_session_ticket = 0;
2162
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002163 /*
2164 * Zero-length ticket means the server changed his mind and doesn't want
2165 * to send a ticket after all, so just forget it
2166 */
2167 if( ticket_len == 0)
2168 return( 0 );
2169
2170 polarssl_free( ssl->session_negotiate->ticket );
2171 ssl->session_negotiate->ticket = NULL;
2172 ssl->session_negotiate->ticket_len = 0;
2173
2174 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2175 {
2176 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2177 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2178 }
2179
2180 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2181
2182 ssl->session_negotiate->ticket = ticket;
2183 ssl->session_negotiate->ticket_len = ticket_len;
2184 ssl->session_negotiate->ticket_lifetime = lifetime;
2185
2186 /*
2187 * RFC 5077 section 3.4:
2188 * "If the client receives a session ticket from the server, then it
2189 * discards any Session ID that was sent in the ServerHello."
2190 */
2191 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2192 ssl->session_negotiate->length = 0;
2193
2194 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2195
2196 return( 0 );
2197}
Paul Bakkera503a632013-08-14 13:48:06 +02002198#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002199
Paul Bakker5121ce52009-01-03 21:22:43 +00002200/*
Paul Bakker1961b702013-01-25 14:49:24 +01002201 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 */
Paul Bakker1961b702013-01-25 14:49:24 +01002203int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002204{
2205 int ret = 0;
2206
Paul Bakker1961b702013-01-25 14:49:24 +01002207 if( ssl->state == SSL_HANDSHAKE_OVER )
2208 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209
Paul Bakker1961b702013-01-25 14:49:24 +01002210 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2211
2212 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2213 return( ret );
2214
2215 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 {
Paul Bakker1961b702013-01-25 14:49:24 +01002217 case SSL_HELLO_REQUEST:
2218 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 break;
2220
Paul Bakker1961b702013-01-25 14:49:24 +01002221 /*
2222 * ==> ClientHello
2223 */
2224 case SSL_CLIENT_HELLO:
2225 ret = ssl_write_client_hello( ssl );
2226 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002227
Paul Bakker1961b702013-01-25 14:49:24 +01002228 /*
2229 * <== ServerHello
2230 * Certificate
2231 * ( ServerKeyExchange )
2232 * ( CertificateRequest )
2233 * ServerHelloDone
2234 */
2235 case SSL_SERVER_HELLO:
2236 ret = ssl_parse_server_hello( ssl );
2237 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002238
Paul Bakker1961b702013-01-25 14:49:24 +01002239 case SSL_SERVER_CERTIFICATE:
2240 ret = ssl_parse_certificate( ssl );
2241 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002242
Paul Bakker1961b702013-01-25 14:49:24 +01002243 case SSL_SERVER_KEY_EXCHANGE:
2244 ret = ssl_parse_server_key_exchange( ssl );
2245 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
Paul Bakker1961b702013-01-25 14:49:24 +01002247 case SSL_CERTIFICATE_REQUEST:
2248 ret = ssl_parse_certificate_request( ssl );
2249 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002250
Paul Bakker1961b702013-01-25 14:49:24 +01002251 case SSL_SERVER_HELLO_DONE:
2252 ret = ssl_parse_server_hello_done( ssl );
2253 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Paul Bakker1961b702013-01-25 14:49:24 +01002255 /*
2256 * ==> ( Certificate/Alert )
2257 * ClientKeyExchange
2258 * ( CertificateVerify )
2259 * ChangeCipherSpec
2260 * Finished
2261 */
2262 case SSL_CLIENT_CERTIFICATE:
2263 ret = ssl_write_certificate( ssl );
2264 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
Paul Bakker1961b702013-01-25 14:49:24 +01002266 case SSL_CLIENT_KEY_EXCHANGE:
2267 ret = ssl_write_client_key_exchange( ssl );
2268 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002269
Paul Bakker1961b702013-01-25 14:49:24 +01002270 case SSL_CERTIFICATE_VERIFY:
2271 ret = ssl_write_certificate_verify( ssl );
2272 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Paul Bakker1961b702013-01-25 14:49:24 +01002274 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2275 ret = ssl_write_change_cipher_spec( ssl );
2276 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
Paul Bakker1961b702013-01-25 14:49:24 +01002278 case SSL_CLIENT_FINISHED:
2279 ret = ssl_write_finished( ssl );
2280 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Paul Bakker1961b702013-01-25 14:49:24 +01002282 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002283 * <== ( NewSessionTicket )
2284 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002285 * Finished
2286 */
2287 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002288#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002289 if( ssl->handshake->new_session_ticket != 0 )
2290 ret = ssl_parse_new_session_ticket( ssl );
2291 else
Paul Bakkera503a632013-08-14 13:48:06 +02002292#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002293 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002294 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002295
Paul Bakker1961b702013-01-25 14:49:24 +01002296 case SSL_SERVER_FINISHED:
2297 ret = ssl_parse_finished( ssl );
2298 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002299
Paul Bakker1961b702013-01-25 14:49:24 +01002300 case SSL_FLUSH_BUFFERS:
2301 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2302 ssl->state = SSL_HANDSHAKE_WRAPUP;
2303 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002304
Paul Bakker1961b702013-01-25 14:49:24 +01002305 case SSL_HANDSHAKE_WRAPUP:
2306 ssl_handshake_wrapup( ssl );
2307 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002308
Paul Bakker1961b702013-01-25 14:49:24 +01002309 default:
2310 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2311 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2312 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
2314 return( ret );
2315}
Paul Bakker5121ce52009-01-03 21:22:43 +00002316#endif