blob: b134b92177461fb99dd0e10af739c5cb8cb2f3d0 [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;
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +0200236 const ecp_curve_info *curve;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200237 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100238
239 *olen = 0;
240
241 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
242
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200243 for( curve = ecp_curve_list();
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200244 curve->grp_id != POLARSSL_ECP_DP_NONE;
245 curve++ )
246 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200247 elliptic_curve_list[elliptic_curve_len++] = curve->tls_id >> 8;
248 elliptic_curve_list[elliptic_curve_len++] = curve->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200249 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200250
251 if( elliptic_curve_len == 0 )
252 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100253
254 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
255 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
256
257 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
258 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
259
260 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
261 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
262
263 memcpy( p, elliptic_curve_list, elliptic_curve_len );
264
265 *olen = 6 + elliptic_curve_len;
266}
267
268static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
269 unsigned char *buf,
270 size_t *olen )
271{
272 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200273 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100274
275 *olen = 0;
276
277 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
278
279 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
280 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
281
282 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100283 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200284
285 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100286 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
287
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200288 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100289}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200290#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100291
Paul Bakker05decb22013-08-15 13:33:48 +0200292#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200293static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
294 unsigned char *buf,
295 size_t *olen )
296{
297 unsigned char *p = buf;
298
299 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
300 *olen = 0;
301 return;
302 }
303
304 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
305
306 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
307 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
308
309 *p++ = 0x00;
310 *p++ = 1;
311
312 *p++ = ssl->mfl_code;
313
314 *olen = 5;
315}
Paul Bakker05decb22013-08-15 13:33:48 +0200316#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200317
Paul Bakker1f2bc622013-08-15 13:45:55 +0200318#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200319static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
320 unsigned char *buf, size_t *olen )
321{
322 unsigned char *p = buf;
323
324 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
325 {
326 *olen = 0;
327 return;
328 }
329
330 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
331
332 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
333 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
334
335 *p++ = 0x00;
336 *p++ = 0x00;
337
338 *olen = 4;
339}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200340#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200341
Paul Bakkera503a632013-08-14 13:48:06 +0200342#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200343static void ssl_write_session_ticket_ext( ssl_context *ssl,
344 unsigned char *buf, size_t *olen )
345{
346 unsigned char *p = buf;
347 size_t tlen = ssl->session_negotiate->ticket_len;
348
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200349 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
350 {
351 *olen = 0;
352 return;
353 }
354
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200355 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
356
357 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
358 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
359
360 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
361 *p++ = (unsigned char)( ( tlen ) & 0xFF );
362
363 *olen = 4;
364
365 if( ssl->session_negotiate->ticket == NULL ||
366 ssl->session_negotiate->ticket_len == 0 )
367 {
368 return;
369 }
370
371 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
372
373 memcpy( p, ssl->session_negotiate->ticket, tlen );
374
375 *olen += tlen;
376}
Paul Bakkera503a632013-08-14 13:48:06 +0200377#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200378
Paul Bakker5121ce52009-01-03 21:22:43 +0000379static int ssl_write_client_hello( ssl_context *ssl )
380{
Paul Bakker23986e52011-04-24 08:57:21 +0000381 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100382 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200384 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200385#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200387#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200388 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200389 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
392
Paul Bakker48916f92012-09-16 19:57:18 +0000393 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
394 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000395 ssl->major_ver = ssl->min_major_ver;
396 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000397 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
Paul Bakker490ecc82011-10-06 13:04:09 +0000399 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
400 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200401 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
402 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000403 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405 /*
406 * 0 . 0 handshake type
407 * 1 . 3 handshake length
408 * 4 . 5 highest version supported
409 * 6 . 9 current UNIX time
410 * 10 . 37 random bytes
411 */
412 buf = ssl->out_msg;
413 p = buf + 4;
414
415 *p++ = (unsigned char) ssl->max_major_ver;
416 *p++ = (unsigned char) ssl->max_minor_ver;
417
418 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
419 buf[4], buf[5] ) );
420
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200421#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 t = time( NULL );
423 *p++ = (unsigned char)( t >> 24 );
424 *p++ = (unsigned char)( t >> 16 );
425 *p++ = (unsigned char)( t >> 8 );
426 *p++ = (unsigned char)( t );
427
428 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200429#else
430 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
431 return( ret );
432
433 p += 4;
434#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
Paul Bakkera3d195c2011-11-27 21:07:34 +0000436 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
437 return( ret );
438
439 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
Paul Bakker48916f92012-09-16 19:57:18 +0000441 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000442
443 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
444
445 /*
446 * 38 . 38 session id length
447 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000448 * 40+n . 41+n ciphersuitelist length
449 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000450 * .. . .. compression methods length
451 * .. . .. compression methods
452 * .. . .. extensions length
453 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 */
Paul Bakker48916f92012-09-16 19:57:18 +0000455 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
Paul Bakker0a597072012-09-25 21:55:46 +0000457 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
458 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200459 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200461 }
462
Paul Bakkera503a632013-08-14 13:48:06 +0200463#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200464 /*
465 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
466 * generate and include a Session ID in the TLS ClientHello."
467 */
468 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
469 ssl->session_negotiate->ticket != NULL &&
470 ssl->session_negotiate->ticket_len != 0 )
471 {
472 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
473
474 if( ret != 0 )
475 return( ret );
476
477 ssl->session_negotiate->length = n = 32;
478 }
Paul Bakkera503a632013-08-14 13:48:06 +0200479#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
481 *p++ = (unsigned char) n;
482
483 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000484 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
487 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
488
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200489 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200490 n = 0;
491 q = p;
492
493 // Skip writing ciphersuite length for now
494 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
Paul Bakker48916f92012-09-16 19:57:18 +0000496 /*
497 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
498 */
499 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
500 {
501 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
502 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200503 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000504 }
505
Paul Bakker2fbefde2013-06-29 16:01:15 +0200506 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200508 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
509
510 if( ciphersuite_info == NULL )
511 continue;
512
513 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
514 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
515 continue;
516
Paul Bakkere3166ce2011-01-27 17:40:50 +0000517 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200518 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
Paul Bakker2fbefde2013-06-29 16:01:15 +0200520 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200521 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
522 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 }
524
Paul Bakker2fbefde2013-06-29 16:01:15 +0200525 *q++ = (unsigned char)( n >> 7 );
526 *q++ = (unsigned char)( n << 1 );
527
528 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
529
530
Paul Bakker2770fbd2012-07-03 13:30:23 +0000531#if defined(POLARSSL_ZLIB_SUPPORT)
532 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
533 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000534 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000535
536 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000537 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000538 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000539#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000541 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
543 *p++ = 1;
544 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000545#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Paul Bakkerd3edc862013-03-20 16:07:17 +0100547 // First write extensions, then the total length
548 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200549#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100550 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
551 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200552#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Paul Bakkerd3edc862013-03-20 16:07:17 +0100554 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
555 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000556
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200557#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100558 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
559 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200560#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000561
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200562#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100563 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
564 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100565
Paul Bakkerd3edc862013-03-20 16:07:17 +0100566 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
567 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100568#endif
569
Paul Bakker05decb22013-08-15 13:33:48 +0200570#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200571 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
572 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200573#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200574
Paul Bakker1f2bc622013-08-15 13:45:55 +0200575#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200576 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
577 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200578#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200579
Paul Bakkera503a632013-08-14 13:48:06 +0200580#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200581 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
582 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200583#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200584
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000585 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
586 ext_len ) );
587
588 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
589 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100590 p += ext_len;
Paul Bakker41c83d32013-03-20 14:39:14 +0100591
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 ssl->out_msglen = p - buf;
593 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
594 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
595
596 ssl->state++;
597
598 if( ( ret = ssl_write_record( ssl ) ) != 0 )
599 {
600 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
601 return( ret );
602 }
603
604 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
605
606 return( 0 );
607}
608
Paul Bakker48916f92012-09-16 19:57:18 +0000609static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200610 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000611 size_t len )
612{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000613 int ret;
614
Paul Bakker48916f92012-09-16 19:57:18 +0000615 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
616 {
617 if( len != 1 || buf[0] != 0x0 )
618 {
619 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000620
621 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
622 return( ret );
623
Paul Bakker48916f92012-09-16 19:57:18 +0000624 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
625 }
626
627 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
628 }
629 else
630 {
631 if( len != 1 + ssl->verify_data_len * 2 ||
632 buf[0] != ssl->verify_data_len * 2 ||
633 memcmp( buf + 1, ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
634 memcmp( buf + 1 + ssl->verify_data_len,
635 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
636 {
637 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000638
639 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
640 return( ret );
641
Paul Bakker48916f92012-09-16 19:57:18 +0000642 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
643 }
644 }
645
646 return( 0 );
647}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200648
Paul Bakker05decb22013-08-15 13:33:48 +0200649#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200650static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200651 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200652 size_t len )
653{
654 /*
655 * server should use the extension only if we did,
656 * and if so the server's value should match ours (and len is always 1)
657 */
658 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
659 len != 1 ||
660 buf[0] != ssl->mfl_code )
661 {
662 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
663 }
664
665 return( 0 );
666}
Paul Bakker05decb22013-08-15 13:33:48 +0200667#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000668
Paul Bakker1f2bc622013-08-15 13:45:55 +0200669#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200670static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
671 const unsigned char *buf,
672 size_t len )
673{
674 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
675 len != 0 )
676 {
677 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
678 }
679
680 ((void) buf);
681
682 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
683
684 return( 0 );
685}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200686#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200687
Paul Bakkera503a632013-08-14 13:48:06 +0200688#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200689static int ssl_parse_session_ticket_ext( ssl_context *ssl,
690 const unsigned char *buf,
691 size_t len )
692{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200693 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
694 len != 0 )
695 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200696 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200697 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200698
699 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200700
701 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200702
703 return( 0 );
704}
Paul Bakkera503a632013-08-14 13:48:06 +0200705#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200706
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200707#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200708static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
709 const unsigned char *buf,
710 size_t len )
711{
712 size_t list_size;
713 const unsigned char *p;
714
715 list_size = buf[0];
716 if( list_size + 1 != len )
717 {
718 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
719 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
720 }
721
722 p = buf + 2;
723 while( list_size > 0 )
724 {
725 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
726 p[0] == POLARSSL_ECP_PF_COMPRESSED )
727 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200728 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200729 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
730 return( 0 );
731 }
732
733 list_size--;
734 p++;
735 }
736
737 return( 0 );
738}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200739#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200740
Paul Bakker5121ce52009-01-03 21:22:43 +0000741static int ssl_parse_server_hello( ssl_context *ssl )
742{
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200743 uint32_t t;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000744 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +0000745 size_t n;
Paul Bakker48916f92012-09-16 19:57:18 +0000746 size_t ext_len = 0;
747 unsigned char *buf, *ext;
748 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000749 int handshake_failure = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
751 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
752
753 /*
754 * 0 . 0 handshake type
755 * 1 . 3 handshake length
756 * 4 . 5 protocol version
757 * 6 . 9 UNIX time()
758 * 10 . 37 random bytes
759 */
760 buf = ssl->in_msg;
761
762 if( ( ret = ssl_read_record( ssl ) ) != 0 )
763 {
764 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
765 return( ret );
766 }
767
768 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
769 {
770 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000771 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 }
773
774 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
775 buf[4], buf[5] ) );
776
777 if( ssl->in_hslen < 42 ||
778 buf[0] != SSL_HS_SERVER_HELLO ||
779 buf[4] != SSL_MAJOR_VERSION_3 )
780 {
781 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000782 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000783 }
784
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000785 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 {
787 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000788 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000789 }
790
791 ssl->minor_ver = buf[5];
792
Paul Bakker1d29fb52012-09-28 13:28:45 +0000793 if( ssl->minor_ver < ssl->min_minor_ver )
794 {
795 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
796 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
797 buf[4], buf[5] ) );
798
799 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
800 SSL_ALERT_MSG_PROTOCOL_VERSION );
801
802 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
803 }
804
Paul Bakker1504af52012-02-11 16:17:43 +0000805#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200806 t = ( (uint32_t) buf[6] << 24 )
807 | ( (uint32_t) buf[7] << 16 )
808 | ( (uint32_t) buf[8] << 8 )
809 | ( (uint32_t) buf[9] );
Paul Bakker87e5cda2012-01-14 18:14:15 +0000810#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000811
Paul Bakker48916f92012-09-16 19:57:18 +0000812 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000813
814 n = buf[38];
815
816 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
817 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
818
Paul Bakker48916f92012-09-16 19:57:18 +0000819 if( n > 32 )
820 {
821 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
822 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
823 }
824
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 /*
826 * 38 . 38 session id length
827 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000828 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 * 41+n . 41+n chosen compression alg.
830 * 42+n . 43+n extensions length
831 * 44+n . 44+n+m extensions
832 */
Paul Bakker48916f92012-09-16 19:57:18 +0000833 if( ssl->in_hslen > 42 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 {
835 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +0000836 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000837
Paul Bakker48916f92012-09-16 19:57:18 +0000838 if( ( ext_len > 0 && ext_len < 4 ) ||
839 ssl->in_hslen != 44 + n + ext_len )
840 {
841 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
842 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
843 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000844 }
845
846 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +0000847 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +0000848
Paul Bakker380da532012-04-18 16:10:25 +0000849 /*
850 * Initialize update checksum functions
851 */
Paul Bakker68884e32013-01-07 18:20:04 +0100852 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
Paul Bakker41c83d32013-03-20 14:39:14 +0100853 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker68884e32013-01-07 18:20:04 +0100854
855 if( ssl->transform_negotiate->ciphersuite_info == NULL )
856 {
857 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200858 ssl->ciphersuite_list[ssl->minor_ver][i] ) );
Paul Bakker68884e32013-01-07 18:20:04 +0100859 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
860 }
Paul Bakker380da532012-04-18 16:10:25 +0000861
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
863 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
864
865 /*
866 * Check if the session can be resumed
867 */
Paul Bakker0a597072012-09-25 21:55:46 +0000868 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
869 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000870 ssl->session_negotiate->ciphersuite != i ||
871 ssl->session_negotiate->compression != comp ||
872 ssl->session_negotiate->length != n ||
873 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000874 {
875 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +0000876 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200877#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +0000878 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200879#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000880 ssl->session_negotiate->ciphersuite = i;
881 ssl->session_negotiate->compression = comp;
882 ssl->session_negotiate->length = n;
883 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000884 }
885 else
886 {
887 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000888
889 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
890 {
891 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
892 return( ret );
893 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 }
895
896 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +0000897 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000898
Paul Bakkere3166ce2011-01-27 17:40:50 +0000899 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000900 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
901
902 i = 0;
903 while( 1 )
904 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200905 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000906 {
907 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000908 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 }
910
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200911 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
912 ssl->session_negotiate->ciphersuite )
913 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200915 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000916 }
917
Paul Bakker2770fbd2012-07-03 13:30:23 +0000918 if( comp != SSL_COMPRESS_NULL
919#if defined(POLARSSL_ZLIB_SUPPORT)
920 && comp != SSL_COMPRESS_DEFLATE
921#endif
922 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000923 {
924 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000925 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 }
Paul Bakker48916f92012-09-16 19:57:18 +0000927 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Paul Bakker48916f92012-09-16 19:57:18 +0000929 ext = buf + 44 + n;
930
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200931 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
932
Paul Bakker48916f92012-09-16 19:57:18 +0000933 while( ext_len )
934 {
935 unsigned int ext_id = ( ( ext[0] << 8 )
936 | ( ext[1] ) );
937 unsigned int ext_size = ( ( ext[2] << 8 )
938 | ( ext[3] ) );
939
940 if( ext_size + 4 > ext_len )
941 {
942 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
943 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
944 }
945
946 switch( ext_id )
947 {
948 case TLS_EXT_RENEGOTIATION_INFO:
949 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
950 renegotiation_info_seen = 1;
951
952 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ) ) != 0 )
953 return( ret );
954
955 break;
956
Paul Bakker05decb22013-08-15 13:33:48 +0200957#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200958 case TLS_EXT_MAX_FRAGMENT_LENGTH:
959 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
960
961 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
962 ext + 4, ext_size ) ) != 0 )
963 {
964 return( ret );
965 }
966
967 break;
Paul Bakker05decb22013-08-15 13:33:48 +0200968#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200969
Paul Bakker1f2bc622013-08-15 13:45:55 +0200970#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200971 case TLS_EXT_TRUNCATED_HMAC:
972 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
973
974 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
975 ext + 4, ext_size ) ) != 0 )
976 {
977 return( ret );
978 }
979
980 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200981#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200982
Paul Bakkera503a632013-08-14 13:48:06 +0200983#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200984 case TLS_EXT_SESSION_TICKET:
985 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
986
987 if( ( ret = ssl_parse_session_ticket_ext( ssl,
988 ext + 4, ext_size ) ) != 0 )
989 {
990 return( ret );
991 }
992
993 break;
Paul Bakkera503a632013-08-14 13:48:06 +0200994#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200995
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200996#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200997 case TLS_EXT_SUPPORTED_POINT_FORMATS:
998 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
999
1000 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1001 ext + 4, ext_size ) ) != 0 )
1002 {
1003 return( ret );
1004 }
1005
1006 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001007#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001008
Paul Bakker48916f92012-09-16 19:57:18 +00001009 default:
1010 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1011 ext_id ) );
1012 }
1013
1014 ext_len -= 4 + ext_size;
1015 ext += 4 + ext_size;
1016
1017 if( ext_len > 0 && ext_len < 4 )
1018 {
1019 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1020 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1021 }
1022 }
1023
1024 /*
1025 * Renegotiation security checks
1026 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001027 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1028 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001029 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001030 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1031 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001032 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001033 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1034 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1035 renegotiation_info_seen == 0 )
1036 {
1037 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1038 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001039 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001040 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1041 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1042 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001043 {
1044 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001045 handshake_failure = 1;
1046 }
1047 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1048 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1049 renegotiation_info_seen == 1 )
1050 {
1051 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1052 handshake_failure = 1;
1053 }
1054
1055 if( handshake_failure == 1 )
1056 {
1057 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1058 return( ret );
1059
Paul Bakker48916f92012-09-16 19:57:18 +00001060 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1061 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
1063 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1064
1065 return( 0 );
1066}
1067
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001068#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1069 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001070static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1071 unsigned char *end )
1072{
1073 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1074
Paul Bakker29e1f122013-04-16 13:07:56 +02001075 /*
1076 * Ephemeral DH parameters:
1077 *
1078 * struct {
1079 * opaque dh_p<1..2^16-1>;
1080 * opaque dh_g<1..2^16-1>;
1081 * opaque dh_Ys<1..2^16-1>;
1082 * } ServerDHParams;
1083 */
1084 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1085 {
1086 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1087 return( ret );
1088 }
1089
1090 if( ssl->handshake->dhm_ctx.len < 64 ||
1091 ssl->handshake->dhm_ctx.len > 512 )
1092 {
1093 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1094 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1095 }
1096
1097 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1098 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1099 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001100
1101 return( ret );
1102}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001103#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1104 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001105
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001106#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1107 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001108static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1109 unsigned char **p,
1110 unsigned char *end )
1111{
1112 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1113
Paul Bakker29e1f122013-04-16 13:07:56 +02001114 /*
1115 * Ephemeral ECDH parameters:
1116 *
1117 * struct {
1118 * ECParameters curve_params;
1119 * ECPoint public;
1120 * } ServerECDHParams;
1121 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001122 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1123 (const unsigned char **) p, end ) ) != 0 )
1124 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001125 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001126 return( ret );
1127 }
1128
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001129 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
1130 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
1131
Paul Bakker29e1f122013-04-16 13:07:56 +02001132 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1133 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
1134 {
1135 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDH length)" ) );
1136 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1137 }
1138
1139 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
Paul Bakker29e1f122013-04-16 13:07:56 +02001140
1141 return( ret );
1142}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001143#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1144 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001145
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001146#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1147 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001148static int ssl_parse_server_psk_hint( ssl_context *ssl,
1149 unsigned char **p,
1150 unsigned char *end )
1151{
1152 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001153 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001154 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001155
1156 /*
1157 * PSK parameters:
1158 *
1159 * opaque psk_identity_hint<0..2^16-1>;
1160 */
1161 len = (*p)[1] << 8 | (*p)[0];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001162 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001163
1164 if( (*p) + len > end )
1165 {
1166 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1167 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1168 }
1169
1170 // TODO: Retrieve PSK identity hint and callback to app
1171 //
1172 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001173 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001174
1175 return( ret );
1176}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001177#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1178 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001179
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001180#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001181#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001182 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1183 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001184static int ssl_parse_signature_algorithm( ssl_context *ssl,
1185 unsigned char **p,
1186 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001187 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001188 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001189{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001190 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001191 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001192 *pk_alg = POLARSSL_PK_NONE;
1193
1194 /* Only in TLS 1.2 */
1195 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1196 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001197 return( 0 );
1198 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001199
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001200 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001201 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1202
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001203 /*
1204 * Get hash algorithm
1205 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001206 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001207 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001208 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1209 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001210 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1211 }
1212
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001213 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001214 * Get signature algorithm
1215 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001216 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001217 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001218 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1219 "SignatureAlgorithm %d", (*p)[1] ) );
1220 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001221 }
1222
1223 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1224 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1225 *p += 2;
1226
1227 return( 0 );
1228}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001229#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001230 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1231 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001232#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001233
Paul Bakker41c83d32013-03-20 14:39:14 +01001234static int ssl_parse_server_key_exchange( ssl_context *ssl )
1235{
Paul Bakker23986e52011-04-24 08:57:21 +00001236 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001237 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001239#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001240 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1241 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001242 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001243 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001244 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001245 size_t hashlen;
1246 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001247#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
1249 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1250
Paul Bakker41c83d32013-03-20 14:39:14 +01001251 if( ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_RSA &&
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001252 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_RSA &&
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001253 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA &&
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001254 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_PSK &&
1255 ciphersuite_info->key_exchange != POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 {
1257 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1258 ssl->state++;
1259 return( 0 );
1260 }
1261
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1263 {
1264 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1265 return( ret );
1266 }
1267
1268 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1269 {
1270 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001271 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 }
1273
1274 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1275 {
Paul Bakker188c8de2013-04-19 09:13:37 +02001276 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
1277 {
1278 ssl->record_read = 1;
1279 goto exit;
1280 }
1281
1282 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1283 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 }
1285
Paul Bakker1ef83d62012-04-11 12:09:53 +00001286 SSL_DEBUG_BUF( 3, "server key exchange", ssl->in_msg + 4, ssl->in_hslen - 4 );
1287
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001288 p = ssl->in_msg + 4;
1289 end = ssl->in_msg + ssl->in_hslen;
1290
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001291#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01001292 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001294 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001295 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001296 SSL_DEBUG_MSG( 1, ( "failed to parsebad server key exchange message" ) );
1297 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1298 }
1299 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001300 else
1301#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001302#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1303 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1304 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1305 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001306 {
1307 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1308 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001309 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1310 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1311 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001312 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001313 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001314#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1315 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001316#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1317 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker41c83d32013-03-20 14:39:14 +01001318 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001319 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001320 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001321 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1322 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1323 }
1324 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001325 else
1326#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
1327#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1328 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1329 {
1330 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1331 {
1332 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1333 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1334 }
1335 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
1336 {
1337 SSL_DEBUG_MSG( 1, ( "failed to parsebad server key exchange message" ) );
1338 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1339 }
1340 }
1341 else
1342#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1343 {
1344 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1345 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001346
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001347#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001348 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1349 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001350 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001351 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1352 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001353 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001354 params_len = p - ( ssl->in_msg + 4 );
1355
Paul Bakker29e1f122013-04-16 13:07:56 +02001356 /*
1357 * Handle the digitally-signed structure
1358 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001359#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1360 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001361 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001362 if( ssl_parse_signature_algorithm( ssl, &p, end,
1363 &md_alg, &pk_alg ) != 0 )
1364 {
1365 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1366 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1367 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001368
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001369 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001370 {
1371 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1372 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1373 }
1374 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001375 else
Paul Bakker577e0062013-08-28 11:57:20 +02001376#endif
Paul Bakker9659dae2013-08-28 16:21:34 +02001377#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1378 defined(POLARSSL_SSL_PROTO_TLS1_1)
1379 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001380 {
1381 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001382
Paul Bakker9659dae2013-08-28 16:21:34 +02001383 /* Default hash for ECDSA is SHA-1 */
1384 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1385 md_alg = POLARSSL_MD_SHA1;
1386 }
1387 else
1388#endif
1389 {
1390 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1391 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1392 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001393
1394 /*
1395 * Read signature
1396 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001397 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001398 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001399
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001400 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001401 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001402 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001403 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001406 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001407
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001408 /*
1409 * Compute the hash that has been signed
1410 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001411#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1412 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001413 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001414 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001415 md5_context md5;
1416 sha1_context sha1;
1417
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001418 hashlen = 36;
1419
Paul Bakker29e1f122013-04-16 13:07:56 +02001420 /*
1421 * digitally-signed struct {
1422 * opaque md5_hash[16];
1423 * opaque sha_hash[20];
1424 * };
1425 *
1426 * md5_hash
1427 * MD5(ClientHello.random + ServerHello.random
1428 * + ServerParams);
1429 * sha_hash
1430 * SHA(ClientHello.random + ServerHello.random
1431 * + ServerParams);
1432 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001433 md5_starts( &md5 );
1434 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001435 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001436 md5_finish( &md5, hash );
1437
1438 sha1_starts( &sha1 );
1439 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001440 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001441 sha1_finish( &sha1, hash + 16 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001442 }
1443 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001444#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1445 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001446#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1447 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001448 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001449 {
1450 md_context_t ctx;
1451
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001452 /* Info from md_alg will be used instead */
1453 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001454
1455 /*
1456 * digitally-signed struct {
1457 * opaque client_random[32];
1458 * opaque server_random[32];
1459 * ServerDHParams params;
1460 * };
1461 */
1462 if( ( ret = md_init_ctx( &ctx, md_info_from_type( md_alg ) ) ) != 0 )
1463 {
1464 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1465 return( ret );
1466 }
1467
1468 md_starts( &ctx );
1469 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001470 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001471 md_finish( &ctx, hash );
Paul Bakker04376b12013-08-16 14:45:26 +02001472 md_free_ctx( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001473 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001474 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001475#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1476 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001477 {
Paul Bakker577e0062013-08-28 11:57:20 +02001478 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1479 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1480 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001481
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001482 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1483 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001484
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001485 /*
1486 * Verify signature
1487 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001488 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001489 {
1490 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1491 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1492 }
1493
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001494 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1495 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001496 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001497 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001498 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001501#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001502 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1503 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001505exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 ssl->state++;
1507
1508 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
1509
1510 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001511}
1512
1513static int ssl_parse_certificate_request( ssl_context *ssl )
1514{
1515 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01001516 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001517 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001518 size_t cert_type_len = 0, dn_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001519
1520 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1521
1522 /*
1523 * 0 . 0 handshake type
1524 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01001525 * 4 . 4 cert type count
1526 * 5 .. m-1 cert types
1527 * m .. m+1 sig alg length (TLS 1.2 only)
1528 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001529 * n .. n+1 length of all DNs
1530 * n+2 .. n+3 length of DN 1
1531 * n+4 .. ... Distinguished Name #1
1532 * ... .. ... length of DN 2, etc.
1533 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001534 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001536 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1537 {
1538 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1539 return( ret );
1540 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001541
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001542 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1543 {
1544 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1545 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1546 }
1547
1548 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001549 }
1550
1551 ssl->client_auth = 0;
1552 ssl->state++;
1553
1554 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
1555 ssl->client_auth++;
1556
1557 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
1558 ssl->client_auth ? "a" : "no" ) );
1559
Paul Bakker926af752012-11-23 13:38:07 +01001560 if( ssl->client_auth == 0 )
1561 goto exit;
1562
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001563 ssl->record_read = 0;
1564
Paul Bakker926af752012-11-23 13:38:07 +01001565 // TODO: handshake_failure alert for an anonymous server to request
1566 // client authentication
1567
1568 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001569
Paul Bakker926af752012-11-23 13:38:07 +01001570 // Retrieve cert types
1571 //
1572 cert_type_len = buf[4];
1573 n = cert_type_len;
1574
1575 if( ssl->in_hslen < 6 + n )
1576 {
1577 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1578 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1579 }
1580
Paul Bakker73d44312013-05-22 13:56:26 +02001581 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01001582 while( cert_type_len > 0 )
1583 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001584#if defined(POLARSSL_RSA_C)
1585 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
1586 pk_can_do( ssl->pk_key, POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01001587 {
1588 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
1589 break;
1590 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001591 else
1592#endif
1593#if defined(POLARSSL_ECDSA_C)
1594 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
1595 pk_can_do( ssl->pk_key, POLARSSL_PK_ECDSA ) )
1596 {
1597 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
1598 break;
1599 }
1600 else
1601#endif
1602 {
1603 ; /* Unsupported cert type, ignore */
1604 }
Paul Bakker926af752012-11-23 13:38:07 +01001605
1606 cert_type_len--;
1607 p++;
1608 }
1609
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001610 // TODO: shall we abort now or send an empty certificate list later?
1611
Paul Bakker926af752012-11-23 13:38:07 +01001612 if( ssl->handshake->cert_type == 0 )
1613 {
1614 SSL_DEBUG_MSG( 1, ( "no known cert_type provided" ) );
1615 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1616 }
1617
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001618#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001619 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1620 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001621 /* Ignored, see comments about hash in write_certificate_verify */
1622 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001623 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
1624 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01001625
1626 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001627 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01001628 n += sig_alg_len;
1629
1630 if( ssl->in_hslen < 6 + n )
1631 {
1632 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1633 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1634 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001635 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001636#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01001637
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001638 /* Ignore certificate_authorities, we only have one cert anyway */
1639 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001640 dn_len = ( ( buf[5 + m + n] << 8 )
1641 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01001642
1643 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001644 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01001645 {
1646 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1647 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1648 }
1649
1650exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001651 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1652
1653 return( 0 );
1654}
1655
1656static int ssl_parse_server_hello_done( ssl_context *ssl )
1657{
1658 int ret;
1659
1660 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
1661
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001662 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001663 {
1664 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1665 {
1666 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1667 return( ret );
1668 }
1669
1670 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1671 {
1672 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001673 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 }
1675 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001676 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
1678 if( ssl->in_hslen != 4 ||
1679 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
1680 {
1681 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001682 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683 }
1684
1685 ssl->state++;
1686
1687 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
1688
1689 return( 0 );
1690}
1691
1692static int ssl_write_client_key_exchange( ssl_context *ssl )
1693{
Paul Bakker23986e52011-04-24 08:57:21 +00001694 int ret;
1695 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01001696 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
1698 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
1699
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001700#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01001701 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 /*
1704 * DHM key exchange -- send G^X mod P
1705 */
Paul Bakker48916f92012-09-16 19:57:18 +00001706 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
1708 ssl->out_msg[4] = (unsigned char)( n >> 8 );
1709 ssl->out_msg[5] = (unsigned char)( n );
1710 i = 6;
1711
Paul Bakker29b64762012-09-25 09:36:44 +00001712 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
1713 mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00001714 &ssl->out_msg[i], n,
1715 ssl->f_rng, ssl->p_rng );
1716 if( ret != 0 )
1717 {
1718 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
1719 return( ret );
1720 }
1721
Paul Bakker48916f92012-09-16 19:57:18 +00001722 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1723 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
Paul Bakker48916f92012-09-16 19:57:18 +00001725 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001726
Paul Bakker48916f92012-09-16 19:57:18 +00001727 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
1728 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02001729 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02001730 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 {
1732 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1733 return( ret );
1734 }
1735
Paul Bakker48916f92012-09-16 19:57:18 +00001736 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00001737 }
1738 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001739#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001740#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1741 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1742 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1743 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01001744 {
1745 /*
1746 * ECDH key exchange -- send client public value
1747 */
1748 i = 4;
1749
1750 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
1751 &n,
1752 &ssl->out_msg[i], 1000,
1753 ssl->f_rng, ssl->p_rng );
1754 if( ret != 0 )
1755 {
1756 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
1757 return( ret );
1758 }
1759
1760 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
1761
1762 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
1763 &ssl->handshake->pmslen,
1764 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001765 POLARSSL_MPI_MAX_SIZE,
1766 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001767 {
1768 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1769 return( ret );
1770 }
1771
1772 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1773 }
1774 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001775#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1776 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001777#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1778 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
1779 {
1780 unsigned char *p = ssl->handshake->premaster;
1781
1782 /*
1783 * PSK key exchange
1784 *
1785 * opaque psk_identity<0..2^16-1>;
1786 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001787 if( ssl->psk == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001788 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
1789
1790 if( sizeof(ssl->handshake->premaster) < 4 + 2 * ssl->psk_len )
1791 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1792
1793 n = ssl->psk_identity_len;
1794
1795 ssl->out_msg[4] = (unsigned char)( n >> 8 );
1796 ssl->out_msg[5] = (unsigned char)( n );
1797 i = 6;
1798
1799 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
1800
1801 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1802 *(p++) = (unsigned char)( ssl->psk_len );
1803 p += ssl->psk_len;
1804
1805 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1806 *(p++) = (unsigned char)( ssl->psk_len );
1807 memcpy( p, ssl->psk, ssl->psk_len );
1808 p += ssl->psk_len;
1809
1810 ssl->handshake->pmslen = 4 + 2 * ssl->psk_len;
1811 }
1812 else
1813#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001814#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1815 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1816 {
1817 unsigned char *p = ssl->handshake->premaster;
1818
1819 /*
1820 * DHE_PSK key exchange
1821 *
1822 * opaque psk_identity<0..2^16-1>;
1823 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
1824 */
1825 if( ssl->psk == NULL )
1826 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
1827
1828 if( sizeof(ssl->handshake->premaster) < 4 + ssl->psk_identity_len +
1829 ssl->handshake->dhm_ctx.len )
1830 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1831
1832 i = 4;
1833 n = ssl->psk_identity_len;
1834 ssl->out_msg[4] = (unsigned char)( n >> 8 );
1835 ssl->out_msg[5] = (unsigned char)( n );
1836
1837 memcpy( ssl->out_msg + 6, ssl->psk_identity, ssl->psk_identity_len );
1838
1839 n = ssl->handshake->dhm_ctx.len;
1840 ssl->out_msg[6 + ssl->psk_identity_len] = (unsigned char)( n >> 8 );
1841 ssl->out_msg[7 + ssl->psk_identity_len] = (unsigned char)( n );
1842
1843 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
1844 mpi_size( &ssl->handshake->dhm_ctx.P ),
1845 &ssl->out_msg[8 + ssl->psk_identity_len], n,
1846 ssl->f_rng, ssl->p_rng );
1847 if( ret != 0 )
1848 {
1849 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
1850 return( ret );
1851 }
1852
1853 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1854 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1855
1856 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len >> 8 );
1857 *(p++) = (unsigned char)( ssl->handshake->dhm_ctx.len );
1858 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02001859 p, &n, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001860 {
1861 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1862 return( ret );
1863 }
1864
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001865 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1866
1867 p += ssl->handshake->dhm_ctx.len;
1868
1869 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1870 *(p++) = (unsigned char)( ssl->psk_len );
1871 memcpy( p, ssl->psk, ssl->psk_len );
1872 p += ssl->psk_len;
1873
1874 ssl->handshake->pmslen = 4 + ssl->handshake->dhm_ctx.len + ssl->psk_len;
1875 n = ssl->handshake->pmslen;
1876 }
1877 else
1878#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1879#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02001880 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 {
1882 /*
1883 * RSA key exchange -- send rsa_public(pkcs1 v1.5(premaster))
1884 */
Paul Bakker48916f92012-09-16 19:57:18 +00001885 ssl->handshake->premaster[0] = (unsigned char) ssl->max_major_ver;
1886 ssl->handshake->premaster[1] = (unsigned char) ssl->max_minor_ver;
1887 ssl->handshake->pmslen = 48;
Paul Bakker5121ce52009-01-03 21:22:43 +00001888
Paul Bakker48916f92012-09-16 19:57:18 +00001889 ret = ssl->f_rng( ssl->p_rng, ssl->handshake->premaster + 2,
1890 ssl->handshake->pmslen - 2 );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001891 if( ret != 0 )
1892 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001893
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001894 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1895 POLARSSL_PK_RSA ) )
1896 {
1897 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1898 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1899 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001900
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001901 i = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 4 : 6;
Paul Bakker5121ce52009-01-03 21:22:43 +00001902
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001903 ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1904 ssl->handshake->premaster, ssl->handshake->pmslen,
1905 ssl->out_msg + i, &n, SSL_BUFFER_LEN,
1906 ssl->f_rng, ssl->p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 if( ret != 0 )
1908 {
1909 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1910 return( ret );
1911 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001912
Paul Bakker577e0062013-08-28 11:57:20 +02001913#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1914 defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001915 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
1916 {
1917 ssl->out_msg[4] = (unsigned char)( n >> 8 );
1918 ssl->out_msg[5] = (unsigned char)( n );
1919 }
Paul Bakker577e0062013-08-28 11:57:20 +02001920#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001921
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 }
Paul Bakkered27a042013-04-18 22:46:23 +02001923 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001924#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02001925 {
1926 ((void) ciphersuite_info);
1927 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1928 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001929
Paul Bakkerff60ee62010-03-16 21:09:09 +00001930 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1931 {
1932 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1933 return( ret );
1934 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
1936 ssl->out_msglen = i + n;
1937 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1938 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
1939
1940 ssl->state++;
1941
1942 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1943 {
1944 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1945 return( ret );
1946 }
1947
1948 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
1949
1950 return( 0 );
1951}
1952
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001953#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1954 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001955 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1956 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001957static int ssl_write_certificate_verify( ssl_context *ssl )
1958{
Paul Bakkered27a042013-04-18 22:46:23 +02001959 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1960 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001961
1962 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1963
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001964 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1965 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02001966 {
1967 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
1968 ssl->state++;
1969 return( 0 );
1970 }
1971
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001972 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001973 return( ret );
1974}
1975#else
1976static int ssl_write_certificate_verify( ssl_context *ssl )
1977{
1978 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1979 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
1980 size_t n = 0, offset = 0;
1981 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001982 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001983 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02001984 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001985
1986 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1987
1988 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1989 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1990 {
1991 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
1992 ssl->state++;
1993 return( 0 );
1994 }
1995
Paul Bakkered27a042013-04-18 22:46:23 +02001996 if( ssl->client_auth == 0 || ssl->own_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 {
1998 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
1999 ssl->state++;
2000 return( 0 );
2001 }
2002
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002003 if( ssl->pk_key == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002005 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2006 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 }
2008
2009 /*
2010 * Make an RSA signature of the handshake digests
2011 */
Paul Bakker48916f92012-09-16 19:57:18 +00002012 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002014#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2015 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002016 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002017 {
Paul Bakker926af752012-11-23 13:38:07 +01002018 /*
2019 * digitally-signed struct {
2020 * opaque md5_hash[16];
2021 * opaque sha_hash[20];
2022 * };
2023 *
2024 * md5_hash
2025 * MD5(handshake_messages);
2026 *
2027 * sha_hash
2028 * SHA(handshake_messages);
2029 */
2030 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002031 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002032
2033 /*
2034 * For ECDSA, default hash is SHA-1 only
2035 */
2036 if( pk_can_do( ssl->pk_key, POLARSSL_PK_ECDSA ) )
2037 {
2038 hash_start += 16;
2039 hashlen -= 16;
2040 md_alg = POLARSSL_MD_SHA1;
2041 }
Paul Bakker926af752012-11-23 13:38:07 +01002042 }
2043 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002044#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2045 POLARSSL_SSL_PROTO_TLS1_1 */
2046#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2047 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002048 {
2049 /*
2050 * digitally-signed struct {
2051 * opaque handshake_messages[handshake_messages_length];
2052 * };
2053 *
2054 * Taking shortcut here. We assume that the server always allows the
2055 * PRF Hash function and has sent it in the allowed signature
2056 * algorithms list received in the Certificate Request message.
2057 *
2058 * Until we encounter a server that does not, we will take this
2059 * shortcut.
2060 *
2061 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2062 * in order to satisfy 'weird' needs from the server side.
2063 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002064 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2065 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002066 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002067 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002068 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002069 }
2070 else
2071 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002072 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002073 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002074 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002075 ssl->out_msg[5] = ssl_sig_from_pk( ssl->pk_key );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002076
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002077 /* Info from md_alg will be used instead */
2078 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002079 offset = 2;
2080 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002081 else
2082#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002083 {
2084 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002085 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002086 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002087
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002088 if( ( ret = pk_sign( ssl->pk_key, md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002089 ssl->out_msg + 6 + offset, &n,
2090 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002091 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002092 SSL_DEBUG_RET( 1, "pk_sign", ret );
2093 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002094 }
Paul Bakker926af752012-11-23 13:38:07 +01002095
Paul Bakker1ef83d62012-04-11 12:09:53 +00002096 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2097 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Paul Bakker1ef83d62012-04-11 12:09:53 +00002099 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2101 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2102
2103 ssl->state++;
2104
2105 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2106 {
2107 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2108 return( ret );
2109 }
2110
2111 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2112
Paul Bakkered27a042013-04-18 22:46:23 +02002113 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002114}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002115#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2116 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2117 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002118
Paul Bakkera503a632013-08-14 13:48:06 +02002119#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002120static int ssl_parse_new_session_ticket( ssl_context *ssl )
2121{
2122 int ret;
2123 uint32_t lifetime;
2124 size_t ticket_len;
2125 unsigned char *ticket;
2126
2127 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2128
2129 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2130 {
2131 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2132 return( ret );
2133 }
2134
2135 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2136 {
2137 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2138 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2139 }
2140
2141 /*
2142 * struct {
2143 * uint32 ticket_lifetime_hint;
2144 * opaque ticket<0..2^16-1>;
2145 * } NewSessionTicket;
2146 *
2147 * 0 . 0 handshake message type
2148 * 1 . 3 handshake message length
2149 * 4 . 7 ticket_lifetime_hint
2150 * 8 . 9 ticket_len (n)
2151 * 10 . 9+n ticket content
2152 */
2153 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2154 ssl->in_hslen < 10 )
2155 {
2156 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2157 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2158 }
2159
2160 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2161 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2162
2163 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2164
2165 if( ticket_len + 10 != ssl->in_hslen )
2166 {
2167 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2168 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2169 }
2170
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002171 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2172
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002173 /* We're not waiting for a NewSessionTicket message any more */
2174 ssl->handshake->new_session_ticket = 0;
2175
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002176 /*
2177 * Zero-length ticket means the server changed his mind and doesn't want
2178 * to send a ticket after all, so just forget it
2179 */
2180 if( ticket_len == 0)
2181 return( 0 );
2182
2183 polarssl_free( ssl->session_negotiate->ticket );
2184 ssl->session_negotiate->ticket = NULL;
2185 ssl->session_negotiate->ticket_len = 0;
2186
2187 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2188 {
2189 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2190 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2191 }
2192
2193 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2194
2195 ssl->session_negotiate->ticket = ticket;
2196 ssl->session_negotiate->ticket_len = ticket_len;
2197 ssl->session_negotiate->ticket_lifetime = lifetime;
2198
2199 /*
2200 * RFC 5077 section 3.4:
2201 * "If the client receives a session ticket from the server, then it
2202 * discards any Session ID that was sent in the ServerHello."
2203 */
2204 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2205 ssl->session_negotiate->length = 0;
2206
2207 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2208
2209 return( 0 );
2210}
Paul Bakkera503a632013-08-14 13:48:06 +02002211#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002212
Paul Bakker5121ce52009-01-03 21:22:43 +00002213/*
Paul Bakker1961b702013-01-25 14:49:24 +01002214 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 */
Paul Bakker1961b702013-01-25 14:49:24 +01002216int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002217{
2218 int ret = 0;
2219
Paul Bakker1961b702013-01-25 14:49:24 +01002220 if( ssl->state == SSL_HANDSHAKE_OVER )
2221 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002222
Paul Bakker1961b702013-01-25 14:49:24 +01002223 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2224
2225 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2226 return( ret );
2227
2228 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 {
Paul Bakker1961b702013-01-25 14:49:24 +01002230 case SSL_HELLO_REQUEST:
2231 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 break;
2233
Paul Bakker1961b702013-01-25 14:49:24 +01002234 /*
2235 * ==> ClientHello
2236 */
2237 case SSL_CLIENT_HELLO:
2238 ret = ssl_write_client_hello( ssl );
2239 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
Paul Bakker1961b702013-01-25 14:49:24 +01002241 /*
2242 * <== ServerHello
2243 * Certificate
2244 * ( ServerKeyExchange )
2245 * ( CertificateRequest )
2246 * ServerHelloDone
2247 */
2248 case SSL_SERVER_HELLO:
2249 ret = ssl_parse_server_hello( ssl );
2250 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002251
Paul Bakker1961b702013-01-25 14:49:24 +01002252 case SSL_SERVER_CERTIFICATE:
2253 ret = ssl_parse_certificate( ssl );
2254 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
Paul Bakker1961b702013-01-25 14:49:24 +01002256 case SSL_SERVER_KEY_EXCHANGE:
2257 ret = ssl_parse_server_key_exchange( ssl );
2258 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002259
Paul Bakker1961b702013-01-25 14:49:24 +01002260 case SSL_CERTIFICATE_REQUEST:
2261 ret = ssl_parse_certificate_request( ssl );
2262 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Paul Bakker1961b702013-01-25 14:49:24 +01002264 case SSL_SERVER_HELLO_DONE:
2265 ret = ssl_parse_server_hello_done( ssl );
2266 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Paul Bakker1961b702013-01-25 14:49:24 +01002268 /*
2269 * ==> ( Certificate/Alert )
2270 * ClientKeyExchange
2271 * ( CertificateVerify )
2272 * ChangeCipherSpec
2273 * Finished
2274 */
2275 case SSL_CLIENT_CERTIFICATE:
2276 ret = ssl_write_certificate( ssl );
2277 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
Paul Bakker1961b702013-01-25 14:49:24 +01002279 case SSL_CLIENT_KEY_EXCHANGE:
2280 ret = ssl_write_client_key_exchange( ssl );
2281 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
Paul Bakker1961b702013-01-25 14:49:24 +01002283 case SSL_CERTIFICATE_VERIFY:
2284 ret = ssl_write_certificate_verify( ssl );
2285 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002286
Paul Bakker1961b702013-01-25 14:49:24 +01002287 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2288 ret = ssl_write_change_cipher_spec( ssl );
2289 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002290
Paul Bakker1961b702013-01-25 14:49:24 +01002291 case SSL_CLIENT_FINISHED:
2292 ret = ssl_write_finished( ssl );
2293 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Paul Bakker1961b702013-01-25 14:49:24 +01002295 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002296 * <== ( NewSessionTicket )
2297 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002298 * Finished
2299 */
2300 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002301#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002302 if( ssl->handshake->new_session_ticket != 0 )
2303 ret = ssl_parse_new_session_ticket( ssl );
2304 else
Paul Bakkera503a632013-08-14 13:48:06 +02002305#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002306 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002307 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002308
Paul Bakker1961b702013-01-25 14:49:24 +01002309 case SSL_SERVER_FINISHED:
2310 ret = ssl_parse_finished( ssl );
2311 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
Paul Bakker1961b702013-01-25 14:49:24 +01002313 case SSL_FLUSH_BUFFERS:
2314 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2315 ssl->state = SSL_HANDSHAKE_WRAPUP;
2316 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002317
Paul Bakker1961b702013-01-25 14:49:24 +01002318 case SSL_HANDSHAKE_WRAPUP:
2319 ssl_handshake_wrapup( ssl );
2320 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002321
Paul Bakker1961b702013-01-25 14:49:24 +01002322 default:
2323 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2324 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002326
2327 return( ret );
2328}
Paul Bakker5121ce52009-01-03 21:22:43 +00002329#endif