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