blob: ab9eeed87326b8f05e23088adb258b4a544f16e3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/debug.h"
32#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <string.h>
35
Paul Bakker7dc4c442014-02-01 22:50:26 +010036#if defined(POLARSSL_PLATFORM_C)
37#include "polarssl/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020038#else
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <stdlib.h>
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020040#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakkerfa6a6202013-10-28 18:48:30 +010044#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020045#include <basetsd.h>
46typedef UINT32 uint32_t;
47#else
48#include <inttypes.h>
49#endif
50
51#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000052#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Paul Bakker34617722014-06-13 17:20:13 +020055#if defined(POLARSSL_SSL_SESSION_TICKETS)
56/* Implementation that should never be optimized out by the compiler */
57static void polarssl_zeroize( void *v, size_t n ) {
58 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
59}
60#endif
61
Paul Bakker0be444a2013-08-27 21:55:01 +020062#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010063static void ssl_write_hostname_ext( ssl_context *ssl,
64 unsigned char *buf,
65 size_t *olen )
66{
67 unsigned char *p = buf;
68
69 *olen = 0;
70
Paul Bakker66d5d072014-06-17 16:39:18 +020071 if( ssl->hostname == NULL )
Paul Bakkerd3edc862013-03-20 16:07:17 +010072 return;
73
74 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
75 ssl->hostname ) );
76
77 /*
78 * struct {
79 * NameType name_type;
80 * select (name_type) {
81 * case host_name: HostName;
82 * } name;
83 * } ServerName;
84 *
85 * enum {
86 * host_name(0), (255)
87 * } NameType;
88 *
89 * opaque HostName<1..2^16-1>;
90 *
91 * struct {
92 * ServerName server_name_list<1..2^16-1>
93 * } ServerNameList;
94 */
95 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
96 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
97
98 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
99 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
100
101 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
102 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
103
104 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
105 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
106 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
107
108 memcpy( p, ssl->hostname, ssl->hostname_len );
109
110 *olen = ssl->hostname_len + 9;
111}
Paul Bakker0be444a2013-08-27 21:55:01 +0200112#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100113
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100114#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100115static void ssl_write_renegotiation_ext( ssl_context *ssl,
116 unsigned char *buf,
117 size_t *olen )
118{
119 unsigned char *p = buf;
120
121 *olen = 0;
122
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +0000123 if( ssl->renegotiation != SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakkerd3edc862013-03-20 16:07:17 +0100124 return;
125
126 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
127
128 /*
129 * Secure renegotiation
130 */
131 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
132 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
133
134 *p++ = 0x00;
135 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
136 *p++ = ssl->verify_data_len & 0xFF;
137
138 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
139
140 *olen = 5 + ssl->verify_data_len;
141}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100142#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100143
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100144/*
145 * Only if we handle at least one key exchange that needs signatures.
146 */
147#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
148 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100149static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
150 unsigned char *buf,
151 size_t *olen )
152{
153 unsigned char *p = buf;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100154 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200155#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
156 unsigned char *sig_alg_list = buf + 6;
157#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100158
159 *olen = 0;
160
161 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
162 return;
163
164 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
165
166 /*
167 * Prepare signature_algorithms extension (TLS 1.2)
168 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200169#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200170#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100171 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
172 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
173 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
174 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
175#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200176#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100177 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
178 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
179 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
180 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
181#endif
182#if defined(POLARSSL_SHA1_C)
183 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
184 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
185#endif
186#if defined(POLARSSL_MD5_C)
187 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
188 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
189#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200190#endif /* POLARSSL_RSA_C */
191#if defined(POLARSSL_ECDSA_C)
192#if defined(POLARSSL_SHA512_C)
193 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
194 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
195 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
196 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
197#endif
198#if defined(POLARSSL_SHA256_C)
199 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
200 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
201 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
202 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
203#endif
204#if defined(POLARSSL_SHA1_C)
205 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
206 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
207#endif
208#if defined(POLARSSL_MD5_C)
209 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
210 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
211#endif
212#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100213
214 /*
215 * enum {
216 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
217 * sha512(6), (255)
218 * } HashAlgorithm;
219 *
220 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
221 * SignatureAlgorithm;
222 *
223 * struct {
224 * HashAlgorithm hash;
225 * SignatureAlgorithm signature;
226 * } SignatureAndHashAlgorithm;
227 *
228 * SignatureAndHashAlgorithm
229 * supported_signature_algorithms<2..2^16-2>;
230 */
231 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
232 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
233
234 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
235 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
236
237 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
238 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
239
Paul Bakkerd3edc862013-03-20 16:07:17 +0100240 *olen = 6 + sig_alg_len;
241}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100242#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
243 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100244
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200245#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100246static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
247 unsigned char *buf,
248 size_t *olen )
249{
250 unsigned char *p = buf;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100251 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100252 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100253 const ecp_curve_info *info;
254#if defined(POLARSSL_SSL_SET_CURVES)
255 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100256#else
257 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100258#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100259
260 *olen = 0;
261
262 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
263
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100264#if defined(POLARSSL_SSL_SET_CURVES)
265 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200266 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100267 info = ecp_curve_info_from_grp_id( *grp_id );
268#else
269 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
270 {
271#endif
272
273 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
274 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200275 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200276
277 if( elliptic_curve_len == 0 )
278 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100279
280 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
281 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
282
283 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
284 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
285
286 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
287 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
288
Paul Bakkerd3edc862013-03-20 16:07:17 +0100289 *olen = 6 + elliptic_curve_len;
290}
291
292static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
293 unsigned char *buf,
294 size_t *olen )
295{
296 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200297 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100298
299 *olen = 0;
300
301 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
302
303 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
304 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
305
306 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100307 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200308
309 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100310 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
311
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200312 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100313}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200314#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100315
Paul Bakker05decb22013-08-15 13:33:48 +0200316#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200317static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
318 unsigned char *buf,
319 size_t *olen )
320{
321 unsigned char *p = buf;
322
323 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
324 *olen = 0;
325 return;
326 }
327
328 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
329
330 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
331 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
332
333 *p++ = 0x00;
334 *p++ = 1;
335
336 *p++ = ssl->mfl_code;
337
338 *olen = 5;
339}
Paul Bakker05decb22013-08-15 13:33:48 +0200340#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200341
Paul Bakker1f2bc622013-08-15 13:45:55 +0200342#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200343static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
344 unsigned char *buf, size_t *olen )
345{
346 unsigned char *p = buf;
347
348 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
349 {
350 *olen = 0;
351 return;
352 }
353
354 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
355
356 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
357 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
358
359 *p++ = 0x00;
360 *p++ = 0x00;
361
362 *olen = 4;
363}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200364#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200365
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100366#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
367static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
368 unsigned char *buf, size_t *olen )
369{
370 unsigned char *p = buf;
371
372 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
373 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
374 {
375 *olen = 0;
376 return;
377 }
378
379 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
380 "extension" ) );
381
382 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
383 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
384
385 *p++ = 0x00;
386 *p++ = 0x00;
387
388 *olen = 4;
389}
390#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
391
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200392#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
393static void ssl_write_extended_ms_ext( ssl_context *ssl,
394 unsigned char *buf, size_t *olen )
395{
396 unsigned char *p = buf;
397
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200398 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
399 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200400 {
401 *olen = 0;
402 return;
403 }
404
405 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
406 "extension" ) );
407
408 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
409 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
410
411 *p++ = 0x00;
412 *p++ = 0x00;
413
414 *olen = 4;
415}
416#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
417
Paul Bakkera503a632013-08-14 13:48:06 +0200418#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200419static void ssl_write_session_ticket_ext( ssl_context *ssl,
420 unsigned char *buf, size_t *olen )
421{
422 unsigned char *p = buf;
423 size_t tlen = ssl->session_negotiate->ticket_len;
424
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200425 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
426 {
427 *olen = 0;
428 return;
429 }
430
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200431 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
432
433 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
434 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
435
436 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
437 *p++ = (unsigned char)( ( tlen ) & 0xFF );
438
439 *olen = 4;
440
441 if( ssl->session_negotiate->ticket == NULL ||
442 ssl->session_negotiate->ticket_len == 0 )
443 {
444 return;
445 }
446
447 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
448
449 memcpy( p, ssl->session_negotiate->ticket, tlen );
450
451 *olen += tlen;
452}
Paul Bakkera503a632013-08-14 13:48:06 +0200453#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200454
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200455#if defined(POLARSSL_SSL_ALPN)
456static void ssl_write_alpn_ext( ssl_context *ssl,
457 unsigned char *buf, size_t *olen )
458{
459 unsigned char *p = buf;
460 const char **cur;
461
462 if( ssl->alpn_list == NULL )
463 {
464 *olen = 0;
465 return;
466 }
467
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200468 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200469
470 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
471 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
472
473 /*
474 * opaque ProtocolName<1..2^8-1>;
475 *
476 * struct {
477 * ProtocolName protocol_name_list<2..2^16-1>
478 * } ProtocolNameList;
479 */
480
481 /* Skip writing extension and list length for now */
482 p += 4;
483
484 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
485 {
486 *p = (unsigned char)( strlen( *cur ) & 0xFF );
487 memcpy( p + 1, *cur, *p );
488 p += 1 + *p;
489 }
490
491 *olen = p - buf;
492
493 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
494 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
495 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
496
497 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
498 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
499 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
500}
501#endif /* POLARSSL_SSL_ALPN */
502
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200503/*
504 * Generate random bytes for ClientHello
505 */
506static int ssl_generate_random( ssl_context *ssl )
507{
508 int ret;
509 unsigned char *p = ssl->handshake->randbytes;
510#if defined(POLARSSL_HAVE_TIME)
511 time_t t;
512#endif
513
Manuel Pégourié-Gonnardfb2d2232014-07-22 15:59:14 +0200514 /*
515 * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
516 */
517#if defined(POLARSSL_SSL_PROTO_DTLS)
518 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
519 ssl->handshake->verify_cookie != NULL )
520 {
521 return( 0 );
522 }
523#endif
524
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200525#if defined(POLARSSL_HAVE_TIME)
526 t = time( NULL );
527 *p++ = (unsigned char)( t >> 24 );
528 *p++ = (unsigned char)( t >> 16 );
529 *p++ = (unsigned char)( t >> 8 );
530 *p++ = (unsigned char)( t );
531
532 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
533#else
534 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
535 return( ret );
536
537 p += 4;
538#endif /* POLARSSL_HAVE_TIME */
539
540 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
541 return( ret );
542
543 return( 0 );
544}
545
Paul Bakker5121ce52009-01-03 21:22:43 +0000546static int ssl_write_client_hello( ssl_context *ssl )
547{
Paul Bakker23986e52011-04-24 08:57:21 +0000548 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100549 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200551 unsigned char *p, *q;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200552 unsigned char offer_compress;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200553 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200554 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
557
Paul Bakkera9a028e2013-11-21 17:31:06 +0100558 if( ssl->f_rng == NULL )
559 {
560 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
561 return( POLARSSL_ERR_SSL_NO_RNG );
562 }
563
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100564#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000565 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100566#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000567 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000568 ssl->major_ver = ssl->min_major_ver;
569 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000570 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Paul Bakker490ecc82011-10-06 13:04:09 +0000572 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
573 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200574 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
575 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000576 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578 /*
579 * 0 . 0 handshake type
580 * 1 . 3 handshake length
581 * 4 . 5 highest version supported
582 * 6 . 9 current UNIX time
583 * 10 . 37 random bytes
584 */
585 buf = ssl->out_msg;
586 p = buf + 4;
587
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +0100588 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
589 ssl->transport, p );
590 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
592 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
593 buf[4], buf[5] ) );
594
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200595 if( ( ret = ssl_generate_random( ssl ) ) != 0 )
596 {
597 SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200598 return( ret );
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200599 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200600
Manuel Pégourié-Gonnardb760f002014-07-22 15:53:27 +0200601 memcpy( p, ssl->handshake->randbytes, 32 );
602 SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
603 p += 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
605 /*
606 * 38 . 38 session id length
607 * 39 . 39+n session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100608 * 39+n . 39+n DTLS only: cookie length (1 byte)
609 * 40+n . .. DTSL only: cookie
610 * .. . .. ciphersuitelist length (2 bytes)
611 * .. . .. ciphersuitelist
612 * .. . .. compression methods length (1 byte)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000613 * .. . .. compression methods
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100614 * .. . .. extensions length (2 bytes)
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000615 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 */
Paul Bakker48916f92012-09-16 19:57:18 +0000617 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100619 if( n < 16 || n > 32 ||
620#if defined(POLARSSL_SSL_RENEGOTIATION)
621 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
622#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000623 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200624 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200626 }
627
Paul Bakkera503a632013-08-14 13:48:06 +0200628#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200629 /*
630 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
631 * generate and include a Session ID in the TLS ClientHello."
632 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100633#if defined(POLARSSL_SSL_RENEGOTIATION)
634 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200635 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000636#endif
637 if( ssl->session_negotiate->ticket != NULL &&
638 ssl->session_negotiate->ticket_len != 0 )
639 {
640 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200641
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000642 if( ret != 0 )
643 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200644
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000645 ssl->session_negotiate->length = n = 32;
646 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200647 }
Paul Bakkera503a632013-08-14 13:48:06 +0200648#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
650 *p++ = (unsigned char) n;
651
652 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000653 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
655 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
656 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
657
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100658 /*
659 * DTLS cookie
660 */
661#if defined(POLARSSL_SSL_PROTO_DTLS)
662 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
663 {
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +0200664 if( ssl->handshake->verify_cookie == NULL )
665 {
666 SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
667 *p++ = 0;
668 }
669 else
670 {
671 SSL_DEBUG_BUF( 3, "client hello, cookie",
672 ssl->handshake->verify_cookie,
673 ssl->handshake->verify_cookie_len );
674
675 *p++ = ssl->handshake->verify_cookie_len;
676 memcpy( p, ssl->handshake->verify_cookie,
677 ssl->handshake->verify_cookie_len );
678 p += ssl->handshake->verify_cookie_len;
679 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100680 }
681#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000682
Paul Bakker48916f92012-09-16 19:57:18 +0000683 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100684 * Ciphersuite list
Paul Bakker48916f92012-09-16 19:57:18 +0000685 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +0100686 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
687
688 /* Skip writing ciphersuite length for now */
689 n = 0;
690 q = p;
691 p += 2;
692
Paul Bakker2fbefde2013-06-29 16:01:15 +0200693 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200695 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
696
697 if( ciphersuite_info == NULL )
698 continue;
699
700 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
701 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
702 continue;
703
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100704#if defined(POLARSSL_SSL_PROTO_DTLS)
705 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
706 ( ciphersuite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
707 continue;
708#endif
709
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100710 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
711 ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
712 continue;
713
Paul Bakkere3166ce2011-01-27 17:40:50 +0000714 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200715 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
Paul Bakker2fbefde2013-06-29 16:01:15 +0200717 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200718 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
719 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 }
721
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +0000722 /*
723 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
724 */
725#if defined(POLARSSL_SSL_RENEGOTIATION)
726 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
727#endif
728 {
729 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
730 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
731 n++;
732 }
733
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200734 /* Some versions of OpenSSL don't handle it correctly if not at end */
735#if defined(POLARSSL_SSL_FALLBACK_SCSV)
736 if( ssl->fallback == SSL_IS_FALLBACK )
737 {
738 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
739 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
740 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
741 n++;
742 }
743#endif
744
Paul Bakker2fbefde2013-06-29 16:01:15 +0200745 *q++ = (unsigned char)( n >> 7 );
746 *q++ = (unsigned char)( n << 1 );
747
748 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
749
Paul Bakker2770fbd2012-07-03 13:30:23 +0000750#if defined(POLARSSL_ZLIB_SUPPORT)
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200751 offer_compress = 1;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000752#else
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200753 offer_compress = 0;
754#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +0200756 /*
757 * We don't support compression with DTLS right now: is many records come
758 * in the same datagram, uncompressing one could overwrite the next one.
759 * We don't want to add complexity for handling that case unless there is
760 * an actual need for it.
761 */
762#if defined(POLARSSL_SSL_PROTO_DTLS)
763 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
764 offer_compress = 0;
765#endif
766
767 if( offer_compress )
768 {
769 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
770 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
771 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
772
773 *p++ = 2;
774 *p++ = SSL_COMPRESS_DEFLATE;
775 *p++ = SSL_COMPRESS_NULL;
776 }
777 else
778 {
779 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
780 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
781 SSL_COMPRESS_NULL ) );
782
783 *p++ = 1;
784 *p++ = SSL_COMPRESS_NULL;
785 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Paul Bakkerd3edc862013-03-20 16:07:17 +0100787 // First write extensions, then the total length
788 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200789#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100790 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
791 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200792#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100794#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100795 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
796 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100797#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000798
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100799#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
800 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100801 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
802 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200803#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000804
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200805#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100806 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
807 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100808
Paul Bakkerd3edc862013-03-20 16:07:17 +0100809 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
810 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100811#endif
812
Paul Bakker05decb22013-08-15 13:33:48 +0200813#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200814 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
815 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200816#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200817
Paul Bakker1f2bc622013-08-15 13:45:55 +0200818#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200819 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
820 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200821#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200822
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100823#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
824 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
825 ext_len += olen;
826#endif
827
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200828#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
829 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
830 ext_len += olen;
831#endif
832
Paul Bakkera503a632013-08-14 13:48:06 +0200833#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200834 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
835 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200836#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200837
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200838#if defined(POLARSSL_SSL_ALPN)
839 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
840 ext_len += olen;
841#endif
842
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100843 /* olen unused if all extensions are disabled */
844 ((void) olen);
845
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000846 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
847 ext_len ) );
848
Paul Bakkera7036632014-04-30 10:15:38 +0200849 if( ext_len > 0 )
850 {
851 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
852 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
853 p += ext_len;
854 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100855
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 ssl->out_msglen = p - buf;
857 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
858 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
859
860 ssl->state++;
861
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +0200862#if defined(POLARSSL_SSL_PROTO_DTLS)
863 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
864 ssl_send_flight_completed( ssl );
865#endif
866
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 if( ( ret = ssl_write_record( ssl ) ) != 0 )
868 {
869 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
870 return( ret );
871 }
872
873 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
874
875 return( 0 );
876}
877
Paul Bakker48916f92012-09-16 19:57:18 +0000878static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200879 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000880 size_t len )
881{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000882 int ret;
883
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100884#if defined(POLARSSL_SSL_RENEGOTIATION)
885 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000886 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100887 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000888 if( len != 1 + ssl->verify_data_len * 2 ||
889 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100890 safer_memcmp( buf + 1,
891 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
892 safer_memcmp( buf + 1 + ssl->verify_data_len,
893 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000894 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100895 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000896
897 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
898 return( ret );
899
Paul Bakker48916f92012-09-16 19:57:18 +0000900 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
901 }
902 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100903 else
904#endif /* POLARSSL_SSL_RENEGOTIATION */
905 {
906 if( len != 1 || buf[0] != 0x00 )
907 {
908 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
909
910 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
911 return( ret );
912
913 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
914 }
915
916 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
917 }
Paul Bakker48916f92012-09-16 19:57:18 +0000918
919 return( 0 );
920}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200921
Paul Bakker05decb22013-08-15 13:33:48 +0200922#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200923static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200924 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200925 size_t len )
926{
927 /*
928 * server should use the extension only if we did,
929 * and if so the server's value should match ours (and len is always 1)
930 */
931 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
932 len != 1 ||
933 buf[0] != ssl->mfl_code )
934 {
935 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
936 }
937
938 return( 0 );
939}
Paul Bakker05decb22013-08-15 13:33:48 +0200940#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000941
Paul Bakker1f2bc622013-08-15 13:45:55 +0200942#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200943static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
944 const unsigned char *buf,
945 size_t len )
946{
947 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
948 len != 0 )
949 {
950 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
951 }
952
953 ((void) buf);
954
955 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
956
957 return( 0 );
958}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200959#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200960
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100961#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
962static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
963 const unsigned char *buf,
964 size_t len )
965{
966 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
967 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
968 len != 0 )
969 {
970 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
971 }
972
973 ((void) buf);
974
975 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
976
977 return( 0 );
978}
979#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
980
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200981#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
982static int ssl_parse_extended_ms_ext( ssl_context *ssl,
983 const unsigned char *buf,
984 size_t len )
985{
986 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200987 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200988 len != 0 )
989 {
990 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
991 }
992
993 ((void) buf);
994
995 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
996
997 return( 0 );
998}
999#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1000
Paul Bakkera503a632013-08-14 13:48:06 +02001001#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001002static int ssl_parse_session_ticket_ext( ssl_context *ssl,
1003 const unsigned char *buf,
1004 size_t len )
1005{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001006 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
1007 len != 0 )
1008 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001009 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001010 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001011
1012 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001013
1014 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001015
1016 return( 0 );
1017}
Paul Bakkera503a632013-08-14 13:48:06 +02001018#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001019
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001020#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001021static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
1022 const unsigned char *buf,
1023 size_t len )
1024{
1025 size_t list_size;
1026 const unsigned char *p;
1027
1028 list_size = buf[0];
1029 if( list_size + 1 != len )
1030 {
1031 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1032 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1033 }
1034
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001035 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001036 while( list_size > 0 )
1037 {
1038 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
1039 p[0] == POLARSSL_ECP_PF_COMPRESSED )
1040 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001041 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001042 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1043 return( 0 );
1044 }
1045
1046 list_size--;
1047 p++;
1048 }
1049
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +02001050 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1051 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001052}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001053#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001054
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001055#if defined(POLARSSL_SSL_ALPN)
1056static int ssl_parse_alpn_ext( ssl_context *ssl,
1057 const unsigned char *buf, size_t len )
1058{
1059 size_t list_len, name_len;
1060 const char **p;
1061
1062 /* If we didn't send it, the server shouldn't send it */
1063 if( ssl->alpn_list == NULL )
1064 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1065
1066 /*
1067 * opaque ProtocolName<1..2^8-1>;
1068 *
1069 * struct {
1070 * ProtocolName protocol_name_list<2..2^16-1>
1071 * } ProtocolNameList;
1072 *
1073 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1074 */
1075
1076 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1077 if( len < 4 )
1078 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1079
1080 list_len = ( buf[0] << 8 ) | buf[1];
1081 if( list_len != len - 2 )
1082 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1083
1084 name_len = buf[2];
1085 if( name_len != list_len - 1 )
1086 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1087
1088 /* Check that the server chosen protocol was in our list and save it */
1089 for( p = ssl->alpn_list; *p != NULL; p++ )
1090 {
1091 if( name_len == strlen( *p ) &&
1092 memcmp( buf + 3, *p, name_len ) == 0 )
1093 {
1094 ssl->alpn_chosen = *p;
1095 return( 0 );
1096 }
1097 }
1098
1099 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1100}
1101#endif /* POLARSSL_SSL_ALPN */
1102
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001103/*
1104 * Parse HelloVerifyRequest. Only called after verifying the HS type.
1105 */
1106#if defined(POLARSSL_SSL_PROTO_DTLS)
1107static int ssl_parse_hello_verify_request( ssl_context *ssl )
1108{
Manuel Pégourié-Gonnard069eb792014-09-10 20:08:29 +02001109 const unsigned char *p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001110 int major_ver, minor_ver;
1111 unsigned char cookie_len;
1112
1113 SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
1114
1115 /*
1116 * struct {
1117 * ProtocolVersion server_version;
1118 * opaque cookie<0..2^8-1>;
1119 * } HelloVerifyRequest;
1120 */
1121 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
1122 ssl_read_version( &major_ver, &minor_ver, ssl->transport, p );
1123 p += 2;
1124
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02001125 /*
1126 * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
1127 * even is lower than our min version.
1128 */
1129 if( major_ver < SSL_MAJOR_VERSION_3 ||
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001130 minor_ver < SSL_MINOR_VERSION_2 ||
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02001131 major_ver > ssl->max_major_ver ||
1132 minor_ver > ssl->max_minor_ver )
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001133 {
1134 SSL_DEBUG_MSG( 1, ( "bad server version" ) );
1135
1136 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1137 SSL_ALERT_MSG_PROTOCOL_VERSION );
1138
1139 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1140 }
1141
1142 cookie_len = *p++;
1143 SSL_DEBUG_BUF( 3, "cookie", (unsigned char *) p, cookie_len );
1144
1145 polarssl_free( ssl->handshake->verify_cookie );
1146
1147 ssl->handshake->verify_cookie = polarssl_malloc( cookie_len );
1148 if( ssl->handshake->verify_cookie == NULL )
1149 {
1150 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", cookie_len ) );
1151 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1152 }
1153
1154 memcpy( ssl->handshake->verify_cookie, p, cookie_len );
1155 ssl->handshake->verify_cookie_len = cookie_len;
1156
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001157 /* Start over at ClientHello */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001158 ssl->state = SSL_CLIENT_HELLO;
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02001159 ssl_reset_checksum( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001160
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001161 ssl_recv_flight_completed( ssl );
1162
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001163 SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
1164
1165 return( 0 );
1166}
1167#endif /* POLARSSL_SSL_PROTO_DTLS */
1168
Paul Bakker5121ce52009-01-03 21:22:43 +00001169static int ssl_parse_server_hello( ssl_context *ssl )
1170{
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001171 int ret, i;
Paul Bakker23986e52011-04-24 08:57:21 +00001172 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001173 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001174 unsigned char *buf, *ext;
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001175 unsigned char comp, accept_comp;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001176#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001177 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001178#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001179 int handshake_failure = 0;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001180 const ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001181#if defined(POLARSSL_DEBUG_C)
1182 uint32_t t;
1183#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001184
1185 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1186
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 buf = ssl->in_msg;
1188
1189 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1190 {
1191 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1192 return( ret );
1193 }
1194
1195 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1196 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001197#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00001198 if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001199 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001200 ssl->renego_records_seen++;
1201
1202 if( ssl->renego_max_records >= 0 &&
1203 ssl->renego_records_seen > ssl->renego_max_records )
1204 {
1205 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1206 "but not honored by server" ) );
1207 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1208 }
1209
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001210 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1211 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1212 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001213#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001214
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001216 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001217 }
1218
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02001219#if defined(POLARSSL_SSL_PROTO_DTLS)
1220 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1221 {
1222 if( buf[0] == SSL_HS_HELLO_VERIFY_REQUEST )
1223 {
1224 SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
1225 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1226 return( ssl_parse_hello_verify_request( ssl ) );
1227 }
1228 else
1229 {
1230 /* We made it through the verification process */
1231 polarssl_free( ssl->handshake->verify_cookie );
1232 ssl->handshake->verify_cookie = NULL;
1233 ssl->handshake->verify_cookie_len = 0;
1234 }
1235 }
1236#endif /* POLARSSL_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001238 if( ssl->in_hslen < 38 + ssl_hs_hdr_len( ssl ) ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001239 buf[0] != SSL_HS_SERVER_HELLO )
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 {
1241 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001242 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 }
1244
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001245 /*
1246 * 0 . 1 server_version
1247 * 2 . 33 random (maybe including 4 bytes of Unix time)
1248 * 34 . 34 session_id length = n
1249 * 35 . 34+n session_id
1250 * 35+n . 36+n cipher_suite
1251 * 37+n . 37+n compression_method
1252 *
1253 * 38+n . 39+n extensions length (optional)
1254 * 40+n . .. extensions
1255 */
1256 buf += ssl_hs_hdr_len( ssl );
1257
1258 SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001259 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001260 ssl->transport, buf + 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001262 if( ssl->major_ver < ssl->min_major_ver ||
1263 ssl->minor_ver < ssl->min_minor_ver ||
1264 ssl->major_ver > ssl->max_major_ver ||
1265 ssl->minor_ver > ssl->max_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001266 {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001267 SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
1268 " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
1269 ssl->min_major_ver, ssl->min_minor_ver,
1270 ssl->major_ver, ssl->minor_ver,
1271 ssl->max_major_ver, ssl->max_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001272
1273 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1274 SSL_ALERT_MSG_PROTOCOL_VERSION );
1275
1276 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1277 }
1278
Paul Bakker1504af52012-02-11 16:17:43 +00001279#if defined(POLARSSL_DEBUG_C)
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001280 t = ( (uint32_t) buf[2] << 24 )
1281 | ( (uint32_t) buf[3] << 16 )
1282 | ( (uint32_t) buf[4] << 8 )
1283 | ( (uint32_t) buf[5] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001284 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001285#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001287 memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001288
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001289 n = buf[34];
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001291 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Paul Bakker48916f92012-09-16 19:57:18 +00001293 if( n > 32 )
1294 {
1295 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1296 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1297 }
1298
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001299 if( ssl->in_hslen > 39 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 {
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001301 ext_len = ( ( buf[38 + n] << 8 )
1302 | ( buf[39 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
Paul Bakker48916f92012-09-16 19:57:18 +00001304 if( ( ext_len > 0 && ext_len < 4 ) ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001305 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
Paul Bakker48916f92012-09-16 19:57:18 +00001306 {
1307 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1308 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 }
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001311 else if( ssl->in_hslen == 38 + n )
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001312 {
1313 ext_len = 0;
1314 }
1315 else
1316 {
1317 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1318 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001321 /* ciphersuite (used later) */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001322 i = ( buf[35 + n] << 8 ) | buf[36 + n];
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001323
1324 /*
1325 * Read and check compression
1326 */
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001327 comp = buf[37 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001329#if defined(POLARSSL_ZLIB_SUPPORT)
1330 accept_comp = 1;
1331#else
1332 accept_comp = 0;
1333#endif
1334
1335 /* See comments in ssl_write_client_hello() */
1336#if defined(POLARSSL_SSL_PROTO_DTLS)
1337 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1338 accept_comp = 0;
1339#endif
1340
1341 if( ( accept_comp == 0 && comp != SSL_COMPRESS_NULL ) ||
1342 ( comp != SSL_COMPRESS_NULL && comp != SSL_COMPRESS_DEFLATE ) )
1343 {
1344 SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
1345 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1346 }
1347
Paul Bakker380da532012-04-18 16:10:25 +00001348 /*
1349 * Initialize update checksum functions
1350 */
Paul Bakker68884e32013-01-07 18:20:04 +01001351 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1352
1353 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1354 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001355 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001356 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1357 }
Paul Bakker380da532012-04-18 16:10:25 +00001358
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001359 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1360
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001362 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
1364 /*
1365 * Check if the session can be resumed
1366 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001367 if( ssl->handshake->resume == 0 || n == 0 ||
1368#if defined(POLARSSL_SSL_RENEGOTIATION)
1369 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1370#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001371 ssl->session_negotiate->ciphersuite != i ||
1372 ssl->session_negotiate->compression != comp ||
1373 ssl->session_negotiate->length != n ||
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001374 memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 {
1376 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001377 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001378#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001379 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001380#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001381 ssl->session_negotiate->ciphersuite = i;
1382 ssl->session_negotiate->compression = comp;
1383 ssl->session_negotiate->length = n;
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001384 memcpy( ssl->session_negotiate->id, buf + 35, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 }
1386 else
1387 {
1388 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001389
1390 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1391 {
1392 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1393 return( ret );
1394 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 }
1396
1397 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001398 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Paul Bakkere3166ce2011-01-27 17:40:50 +00001400 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001401 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001403 suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1404 if( suite_info == NULL ||
1405 ( ssl->arc4_disabled &&
1406 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) )
1407 {
1408 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1409 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1410 }
1411
1412
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 i = 0;
1414 while( 1 )
1415 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001416 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 {
1418 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001419 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 }
1421
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001422 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1423 ssl->session_negotiate->ciphersuite )
1424 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001426 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 }
1428
Paul Bakker2770fbd2012-07-03 13:30:23 +00001429 if( comp != SSL_COMPRESS_NULL
1430#if defined(POLARSSL_ZLIB_SUPPORT)
1431 && comp != SSL_COMPRESS_DEFLATE
1432#endif
1433 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 {
1435 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001436 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 }
Paul Bakker48916f92012-09-16 19:57:18 +00001438 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001439
Manuel Pégourié-Gonnard0b3400d2014-09-10 21:23:41 +02001440 ext = buf + 40 + n;
Paul Bakker48916f92012-09-16 19:57:18 +00001441
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001442 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1443
Paul Bakker48916f92012-09-16 19:57:18 +00001444 while( ext_len )
1445 {
1446 unsigned int ext_id = ( ( ext[0] << 8 )
1447 | ( ext[1] ) );
1448 unsigned int ext_size = ( ( ext[2] << 8 )
1449 | ( ext[3] ) );
1450
1451 if( ext_size + 4 > ext_len )
1452 {
1453 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1454 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1455 }
1456
1457 switch( ext_id )
1458 {
1459 case TLS_EXT_RENEGOTIATION_INFO:
1460 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001461#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001462 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001463#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001464
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001465 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1466 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001467 return( ret );
1468
1469 break;
1470
Paul Bakker05decb22013-08-15 13:33:48 +02001471#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001472 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1473 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1474
1475 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1476 ext + 4, ext_size ) ) != 0 )
1477 {
1478 return( ret );
1479 }
1480
1481 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001482#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001483
Paul Bakker1f2bc622013-08-15 13:45:55 +02001484#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001485 case TLS_EXT_TRUNCATED_HMAC:
1486 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1487
1488 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1489 ext + 4, ext_size ) ) != 0 )
1490 {
1491 return( ret );
1492 }
1493
1494 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001495#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001496
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001497#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1498 case TLS_EXT_ENCRYPT_THEN_MAC:
1499 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1500
1501 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1502 ext + 4, ext_size ) ) != 0 )
1503 {
1504 return( ret );
1505 }
1506
1507 break;
1508#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1509
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001510#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1511 case TLS_EXT_EXTENDED_MASTER_SECRET:
1512 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1513
1514 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1515 ext + 4, ext_size ) ) != 0 )
1516 {
1517 return( ret );
1518 }
1519
1520 break;
1521#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1522
Paul Bakkera503a632013-08-14 13:48:06 +02001523#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001524 case TLS_EXT_SESSION_TICKET:
1525 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1526
1527 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1528 ext + 4, ext_size ) ) != 0 )
1529 {
1530 return( ret );
1531 }
1532
1533 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001534#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001535
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001536#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001537 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1538 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1539
1540 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1541 ext + 4, ext_size ) ) != 0 )
1542 {
1543 return( ret );
1544 }
1545
1546 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001547#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001548
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001549#if defined(POLARSSL_SSL_ALPN)
1550 case TLS_EXT_ALPN:
1551 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1552
1553 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1554 return( ret );
1555
1556 break;
1557#endif /* POLARSSL_SSL_ALPN */
1558
Paul Bakker48916f92012-09-16 19:57:18 +00001559 default:
1560 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1561 ext_id ) );
1562 }
1563
1564 ext_len -= 4 + ext_size;
1565 ext += 4 + ext_size;
1566
1567 if( ext_len > 0 && ext_len < 4 )
1568 {
1569 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1570 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1571 }
1572 }
1573
1574 /*
1575 * Renegotiation security checks
1576 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001577 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1578 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001579 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001580 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1581 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001582 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001583#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00001584 else if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001585 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1586 renegotiation_info_seen == 0 )
1587 {
1588 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1589 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001590 }
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00001591 else if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001592 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1593 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001594 {
1595 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001596 handshake_failure = 1;
1597 }
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00001598 else if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001599 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1600 renegotiation_info_seen == 1 )
1601 {
1602 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1603 handshake_failure = 1;
1604 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001605#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001606
1607 if( handshake_failure == 1 )
1608 {
1609 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1610 return( ret );
1611
Paul Bakker48916f92012-09-16 19:57:18 +00001612 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1613 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
1615 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1616
1617 return( 0 );
1618}
1619
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001620#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1621 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001622static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1623 unsigned char *end )
1624{
1625 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1626
Paul Bakker29e1f122013-04-16 13:07:56 +02001627 /*
1628 * Ephemeral DH parameters:
1629 *
1630 * struct {
1631 * opaque dh_p<1..2^16-1>;
1632 * opaque dh_g<1..2^16-1>;
1633 * opaque dh_Ys<1..2^16-1>;
1634 * } ServerDHParams;
1635 */
1636 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1637 {
1638 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1639 return( ret );
1640 }
1641
1642 if( ssl->handshake->dhm_ctx.len < 64 ||
1643 ssl->handshake->dhm_ctx.len > 512 )
1644 {
1645 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1646 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1647 }
1648
1649 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1650 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1651 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001652
1653 return( ret );
1654}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001655#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1656 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001657
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001658#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001659 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001660 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1661 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1662 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1663static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1664{
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001665 const ecp_curve_info *curve_info;
1666
1667 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1668 if( curve_info == NULL )
1669 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001670 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1671 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01001672 }
1673
1674 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001675
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001676#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1677 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1678#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001679 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1680 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001681#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001682 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001683
1684 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1685
1686 return( 0 );
1687}
Paul Bakker9af723c2014-05-01 13:03:14 +02001688#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1689 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1690 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1691 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1692 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001693
1694#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1695 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001696 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001697static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1698 unsigned char **p,
1699 unsigned char *end )
1700{
1701 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1702
Paul Bakker29e1f122013-04-16 13:07:56 +02001703 /*
1704 * Ephemeral ECDH parameters:
1705 *
1706 * struct {
1707 * ECParameters curve_params;
1708 * ECPoint public;
1709 * } ServerECDHParams;
1710 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001711 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1712 (const unsigned char **) p, end ) ) != 0 )
1713 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001714 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001715 return( ret );
1716 }
1717
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001718 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001719 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001720 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001721 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1722 }
1723
Paul Bakker29e1f122013-04-16 13:07:56 +02001724 return( ret );
1725}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001726#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001727 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1728 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001729
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001730#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001731static int ssl_parse_server_psk_hint( ssl_context *ssl,
1732 unsigned char **p,
1733 unsigned char *end )
1734{
1735 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001736 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001737 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001738
1739 /*
1740 * PSK parameters:
1741 *
1742 * opaque psk_identity_hint<0..2^16-1>;
1743 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001744 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001745 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001746
1747 if( (*p) + len > end )
1748 {
1749 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1750 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1751 }
1752
1753 // TODO: Retrieve PSK identity hint and callback to app
1754 //
1755 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001756 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001757
1758 return( ret );
1759}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001760#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001761
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001762#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1763 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1764/*
1765 * Generate a pre-master secret and encrypt it with the server's RSA key
1766 */
1767static int ssl_write_encrypted_pms( ssl_context *ssl,
1768 size_t offset, size_t *olen,
1769 size_t pms_offset )
1770{
1771 int ret;
1772 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1773 unsigned char *p = ssl->handshake->premaster + pms_offset;
1774
1775 /*
1776 * Generate (part of) the pre-master as
1777 * struct {
1778 * ProtocolVersion client_version;
1779 * opaque random[46];
1780 * } PreMasterSecret;
1781 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001782 ssl_write_version( ssl->max_major_ver, ssl->max_minor_ver,
1783 ssl->transport, p );
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001784
1785 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1786 {
1787 SSL_DEBUG_RET( 1, "f_rng", ret );
1788 return( ret );
1789 }
1790
1791 ssl->handshake->pmslen = 48;
1792
1793 /*
1794 * Now write it out, encrypted
1795 */
1796 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1797 POLARSSL_PK_RSA ) )
1798 {
1799 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1800 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1801 }
1802
1803 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1804 p, ssl->handshake->pmslen,
1805 ssl->out_msg + offset + len_bytes, olen,
1806 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1807 ssl->f_rng, ssl->p_rng ) ) != 0 )
1808 {
1809 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1810 return( ret );
1811 }
1812
1813#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1814 defined(POLARSSL_SSL_PROTO_TLS1_2)
1815 if( len_bytes == 2 )
1816 {
1817 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1818 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1819 *olen += 2;
1820 }
1821#endif
1822
1823 return( 0 );
1824}
1825#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1826 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001827
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001828#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001829#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001830 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1831 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001832static int ssl_parse_signature_algorithm( ssl_context *ssl,
1833 unsigned char **p,
1834 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001835 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001836 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001837{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001838 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001839 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001840 *pk_alg = POLARSSL_PK_NONE;
1841
1842 /* Only in TLS 1.2 */
1843 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1844 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001845 return( 0 );
1846 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001847
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001848 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001849 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1850
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001851 /*
1852 * Get hash algorithm
1853 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001854 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001855 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001856 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1857 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001858 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1859 }
1860
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001861 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001862 * Get signature algorithm
1863 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001864 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001865 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001866 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1867 "SignatureAlgorithm %d", (*p)[1] ) );
1868 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001869 }
1870
1871 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1872 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1873 *p += 2;
1874
1875 return( 0 );
1876}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001877#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001878 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1879 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001880#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001881
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001882
1883#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1884 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1885static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1886{
1887 int ret;
1888 const ecp_keypair *peer_key;
1889
1890 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1891 POLARSSL_PK_ECKEY ) )
1892 {
1893 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1894 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1895 }
1896
1897 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1898
1899 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1900 POLARSSL_ECDH_THEIRS ) ) != 0 )
1901 {
1902 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1903 return( ret );
1904 }
1905
1906 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1907 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001908 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001909 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1910 }
1911
1912 return( ret );
1913}
1914#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1915 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1916
Paul Bakker41c83d32013-03-20 14:39:14 +01001917static int ssl_parse_server_key_exchange( ssl_context *ssl )
1918{
Paul Bakker23986e52011-04-24 08:57:21 +00001919 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001920 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001921 unsigned char *p, *end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001922
1923 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1924
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001925#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001926 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 {
1928 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1929 ssl->state++;
1930 return( 0 );
1931 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001932 ((void) p);
1933 ((void) end);
1934#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001936#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1937 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1938 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1939 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1940 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001941 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1942 {
1943 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1944 return( ret );
1945 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001946
1947 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1948 ssl->state++;
1949 return( 0 );
1950 }
1951 ((void) p);
1952 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001953#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1954 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001955
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1957 {
1958 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1959 return( ret );
1960 }
1961
1962 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1963 {
1964 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001965 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 }
1967
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001968 /*
1969 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1970 * doesn't use a psk_identity_hint
1971 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1973 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001974 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1975 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001976 {
1977 ssl->record_read = 1;
1978 goto exit;
1979 }
1980
1981 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1982 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 }
1984
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001985 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001986 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00001987 SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001988
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001989#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1990 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1991 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1992 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1993 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1994 {
1995 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1996 {
1997 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1998 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1999 }
2000 } /* FALLTROUGH */
2001#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
2002
2003#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2004 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2005 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2006 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2007 ; /* nothing more to do */
2008 else
2009#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
2010 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
2011#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2012 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2013 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2014 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002015 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002016 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002017 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002018 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002019 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2020 }
2021 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002022 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002023#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2024 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002025#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002026 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002027 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2028 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002029 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002030 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002031 {
2032 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2033 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002034 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2035 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2036 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002037 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002038 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002039#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002040 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002041 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002042 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002043 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002044 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002045 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002046
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002047#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002048 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2049 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02002050 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002051 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2052 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002053 {
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002054 size_t sig_len, hashlen;
2055 unsigned char hash[64];
2056 md_type_t md_alg = POLARSSL_MD_NONE;
2057 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002058 unsigned char *params = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardd92d6a12014-09-10 15:25:02 +00002059 size_t params_len = p - params;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002060
Paul Bakker29e1f122013-04-16 13:07:56 +02002061 /*
2062 * Handle the digitally-signed structure
2063 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002064#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2065 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002066 {
Paul Bakker9659dae2013-08-28 16:21:34 +02002067 if( ssl_parse_signature_algorithm( ssl, &p, end,
2068 &md_alg, &pk_alg ) != 0 )
2069 {
2070 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2071 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2072 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002073
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002074 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002075 {
2076 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2077 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2078 }
2079 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002080 else
Paul Bakker9af723c2014-05-01 13:03:14 +02002081#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002082#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2083 defined(POLARSSL_SSL_PROTO_TLS1_1)
2084 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002085 {
2086 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002087
Paul Bakker9659dae2013-08-28 16:21:34 +02002088 /* Default hash for ECDSA is SHA-1 */
2089 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
2090 md_alg = POLARSSL_MD_SHA1;
2091 }
2092 else
2093#endif
2094 {
2095 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002096 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02002097 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002098
2099 /*
2100 * Read signature
2101 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002102 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002103 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002104
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002105 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002106 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002107 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01002108 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2109 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002110
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002111 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002112
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002113 /*
2114 * Compute the hash that has been signed
2115 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002116#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2117 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002118 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002119 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002120 md5_context md5;
2121 sha1_context sha1;
2122
Paul Bakker5b4af392014-06-26 12:09:34 +02002123 md5_init( &md5 );
2124 sha1_init( &sha1 );
2125
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002126 hashlen = 36;
2127
Paul Bakker29e1f122013-04-16 13:07:56 +02002128 /*
2129 * digitally-signed struct {
2130 * opaque md5_hash[16];
2131 * opaque sha_hash[20];
2132 * };
2133 *
2134 * md5_hash
2135 * MD5(ClientHello.random + ServerHello.random
2136 * + ServerParams);
2137 * sha_hash
2138 * SHA(ClientHello.random + ServerHello.random
2139 * + ServerParams);
2140 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002141 md5_starts( &md5 );
2142 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002143 md5_update( &md5, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002144 md5_finish( &md5, hash );
2145
2146 sha1_starts( &sha1 );
2147 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002148 sha1_update( &sha1, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002149 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02002150
2151 md5_free( &md5 );
2152 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02002153 }
2154 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002155#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2156 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002157#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2158 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002159 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002160 {
2161 md_context_t ctx;
2162
Paul Bakker84bbeb52014-07-01 14:53:22 +02002163 md_init( &ctx );
2164
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002165 /* Info from md_alg will be used instead */
2166 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02002167
2168 /*
2169 * digitally-signed struct {
2170 * opaque client_random[32];
2171 * opaque server_random[32];
2172 * ServerDHParams params;
2173 * };
2174 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002175 if( ( ret = md_init_ctx( &ctx,
2176 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002177 {
2178 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2179 return( ret );
2180 }
2181
2182 md_starts( &ctx );
2183 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardf4830b52014-09-10 15:15:51 +00002184 md_update( &ctx, params, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002185 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002186 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002187 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002188 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002189#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2190 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002191 {
Paul Bakker577e0062013-08-28 11:57:20 +02002192 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002193 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002194 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002195
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002196 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2197 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002198
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002199 /*
2200 * Verify signature
2201 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002202 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002203 {
2204 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2205 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2206 }
2207
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002208 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2209 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002210 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002211 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002212 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002215#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002216 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2217 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002218
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002219exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 ssl->state++;
2221
2222 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2223
2224 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225}
2226
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002227#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2228 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2229 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2230 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2231static int ssl_parse_certificate_request( ssl_context *ssl )
2232{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002233 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2234
2235 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2236
2237 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2238 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2239 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2240 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2241 {
2242 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2243 ssl->state++;
2244 return( 0 );
2245 }
2246
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002247 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2248 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002249}
2250#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002251static int ssl_parse_certificate_request( ssl_context *ssl )
2252{
2253 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002254 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002255 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002256 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002257 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002258
2259 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2260
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002261 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2262 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2263 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2264 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2265 {
2266 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2267 ssl->state++;
2268 return( 0 );
2269 }
2270
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002271 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002273 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2274 {
2275 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2276 return( ret );
2277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002279 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2280 {
2281 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2282 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2283 }
2284
2285 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 }
2287
2288 ssl->client_auth = 0;
2289 ssl->state++;
2290
2291 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2292 ssl->client_auth++;
2293
2294 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2295 ssl->client_auth ? "a" : "no" ) );
2296
Paul Bakker926af752012-11-23 13:38:07 +01002297 if( ssl->client_auth == 0 )
2298 goto exit;
2299
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002300 ssl->record_read = 0;
2301
Paul Bakker926af752012-11-23 13:38:07 +01002302 // TODO: handshake_failure alert for an anonymous server to request
2303 // client authentication
2304
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002305 /*
2306 * struct {
2307 * ClientCertificateType certificate_types<1..2^8-1>;
2308 * SignatureAndHashAlgorithm
2309 * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
2310 * DistinguishedName certificate_authorities<0..2^16-1>;
2311 * } CertificateRequest;
2312 */
Paul Bakker926af752012-11-23 13:38:07 +01002313 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002314
Paul Bakker926af752012-11-23 13:38:07 +01002315 // Retrieve cert types
2316 //
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002317 cert_type_len = buf[ssl_hs_hdr_len( ssl )];
Paul Bakker926af752012-11-23 13:38:07 +01002318 n = cert_type_len;
2319
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002320 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002321 {
2322 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2323 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2324 }
2325
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002326 p = buf + ssl_hs_hdr_len( ssl ) + 1;
Paul Bakker926af752012-11-23 13:38:07 +01002327 while( cert_type_len > 0 )
2328 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002329#if defined(POLARSSL_RSA_C)
2330 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002331 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002332 {
2333 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2334 break;
2335 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002336 else
2337#endif
2338#if defined(POLARSSL_ECDSA_C)
2339 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002340 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002341 {
2342 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2343 break;
2344 }
2345 else
2346#endif
2347 {
2348 ; /* Unsupported cert type, ignore */
2349 }
Paul Bakker926af752012-11-23 13:38:07 +01002350
2351 cert_type_len--;
2352 p++;
2353 }
2354
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002355#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002356 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2357 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002358 /* Ignored, see comments about hash in write_certificate_verify */
2359 // TODO: should check the signature part against our pk_key though
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002360 size_t sig_alg_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
2361 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002362
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002363 p = buf + ssl_hs_hdr_len( ssl ) + 3 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002364 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002365 n += sig_alg_len;
2366
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002367 if( ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 2 + n )
Paul Bakker926af752012-11-23 13:38:07 +01002368 {
2369 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2370 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2371 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002372 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002373#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002374
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002375 /* Ignore certificate_authorities, we only have one cert anyway */
2376 // TODO: should not send cert if no CA matches
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002377 dn_len = ( ( buf[ssl_hs_hdr_len( ssl ) + 1 + m + n] << 8 )
2378 | ( buf[ssl_hs_hdr_len( ssl ) + 2 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002379
2380 n += dn_len;
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002381 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) + 3 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002382 {
2383 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2384 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2385 }
2386
2387exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2389
2390 return( 0 );
2391}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002392#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2393 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2394 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2395 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
2397static int ssl_parse_server_hello_done( ssl_context *ssl )
2398{
2399 int ret;
2400
2401 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2402
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002403 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 {
2405 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2406 {
2407 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2408 return( ret );
2409 }
2410
2411 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2412 {
2413 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002414 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 }
2416 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002417 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002418
Manuel Pégourié-Gonnard04c1b4e2014-09-10 19:25:43 +02002419 if( ssl->in_hslen != ssl_hs_hdr_len( ssl ) ||
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2421 {
2422 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002423 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 }
2425
2426 ssl->state++;
2427
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002428#if defined(POLARSSL_SSL_PROTO_DTLS)
2429 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2430 ssl_recv_flight_completed( ssl );
2431#endif
2432
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2434
2435 return( 0 );
2436}
2437
2438static int ssl_write_client_key_exchange( ssl_context *ssl )
2439{
Paul Bakker23986e52011-04-24 08:57:21 +00002440 int ret;
2441 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002442 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
2444 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2445
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002446#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002447 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 /*
2450 * DHM key exchange -- send G^X mod P
2451 */
Paul Bakker48916f92012-09-16 19:57:18 +00002452 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
2454 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2455 ssl->out_msg[5] = (unsigned char)( n );
2456 i = 6;
2457
Paul Bakker29b64762012-09-25 09:36:44 +00002458 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002459 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 &ssl->out_msg[i], n,
2461 ssl->f_rng, ssl->p_rng );
2462 if( ret != 0 )
2463 {
2464 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2465 return( ret );
2466 }
2467
Paul Bakker48916f92012-09-16 19:57:18 +00002468 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2469 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002471 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
Paul Bakker48916f92012-09-16 19:57:18 +00002473 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2474 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002475 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002476 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002477 {
2478 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2479 return( ret );
2480 }
2481
Paul Bakker48916f92012-09-16 19:57:18 +00002482 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 }
2484 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002485#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002486#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002487 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2488 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2489 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002490 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002491 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2492 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2493 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002494 {
2495 /*
2496 * ECDH key exchange -- send client public value
2497 */
2498 i = 4;
2499
2500 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2501 &n,
2502 &ssl->out_msg[i], 1000,
2503 ssl->f_rng, ssl->p_rng );
2504 if( ret != 0 )
2505 {
2506 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2507 return( ret );
2508 }
2509
2510 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2511
2512 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2513 &ssl->handshake->pmslen,
2514 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002515 POLARSSL_MPI_MAX_SIZE,
2516 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002517 {
2518 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2519 return( ret );
2520 }
2521
2522 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2523 }
2524 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002525#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002526 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2527 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2528 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002529#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002530 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002531 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002532 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2533 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002534 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002535 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002536 * opaque psk_identity<0..2^16-1>;
2537 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002538 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002539 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2540
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002541 i = 4;
2542 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002543 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2544 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002545
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002546 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2547 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002548
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002549#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002550 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002551 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002552 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002553 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002554 else
2555#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002556#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2557 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2558 {
2559 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2560 return( ret );
2561 }
2562 else
2563#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002564#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002565 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002566 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002567 /*
2568 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2569 */
2570 n = ssl->handshake->dhm_ctx.len;
2571 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2572 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002573
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002574 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002575 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002576 &ssl->out_msg[i], n,
2577 ssl->f_rng, ssl->p_rng );
2578 if( ret != 0 )
2579 {
2580 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2581 return( ret );
2582 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002583 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002584 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002585#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002586#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002587 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002588 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002589 /*
2590 * ClientECDiffieHellmanPublic public;
2591 */
2592 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2593 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2594 ssl->f_rng, ssl->p_rng );
2595 if( ret != 0 )
2596 {
2597 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2598 return( ret );
2599 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002600
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002601 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2602 }
2603 else
2604#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2605 {
2606 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002607 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002608 }
2609
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002610 if( ( ret = ssl_psk_derive_premaster( ssl,
2611 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002612 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002613 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002614 return( ret );
2615 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002616 }
2617 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002618#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002619#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002620 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002622 i = 4;
2623 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002624 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
Paul Bakkered27a042013-04-18 22:46:23 +02002626 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002627#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002628 {
2629 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002630 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002631 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002632 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 ssl->out_msglen = i + n;
2635 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2636 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2637
2638 ssl->state++;
2639
2640 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2641 {
2642 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2643 return( ret );
2644 }
2645
2646 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2647
2648 return( 0 );
2649}
2650
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002651#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2652 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002653 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2654 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002655static int ssl_write_certificate_verify( ssl_context *ssl )
2656{
Paul Bakkered27a042013-04-18 22:46:23 +02002657 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002658 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
2660 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2661
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002662 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2663 {
2664 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2665 return( ret );
2666 }
2667
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002668 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002669 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002670 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002671 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002672 {
2673 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2674 ssl->state++;
2675 return( 0 );
2676 }
2677
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002678 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2679 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002680}
2681#else
2682static int ssl_write_certificate_verify( ssl_context *ssl )
2683{
2684 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2685 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2686 size_t n = 0, offset = 0;
2687 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002688 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002689 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002690 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002691
2692 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2693
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002694 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2695 {
2696 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2697 return( ret );
2698 }
2699
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002700 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002701 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002702 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002703 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2704 {
2705 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2706 ssl->state++;
2707 return( 0 );
2708 }
2709
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002710 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 {
2712 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2713 ssl->state++;
2714 return( 0 );
2715 }
2716
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002717 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002719 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2720 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 }
2722
2723 /*
2724 * Make an RSA signature of the handshake digests
2725 */
Paul Bakker48916f92012-09-16 19:57:18 +00002726 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002728#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2729 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002730 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002731 {
Paul Bakker926af752012-11-23 13:38:07 +01002732 /*
2733 * digitally-signed struct {
2734 * opaque md5_hash[16];
2735 * opaque sha_hash[20];
2736 * };
2737 *
2738 * md5_hash
2739 * MD5(handshake_messages);
2740 *
2741 * sha_hash
2742 * SHA(handshake_messages);
2743 */
2744 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002745 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002746
2747 /*
2748 * For ECDSA, default hash is SHA-1 only
2749 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002750 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002751 {
2752 hash_start += 16;
2753 hashlen -= 16;
2754 md_alg = POLARSSL_MD_SHA1;
2755 }
Paul Bakker926af752012-11-23 13:38:07 +01002756 }
2757 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002758#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2759 POLARSSL_SSL_PROTO_TLS1_1 */
2760#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2761 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002762 {
2763 /*
2764 * digitally-signed struct {
2765 * opaque handshake_messages[handshake_messages_length];
2766 * };
2767 *
2768 * Taking shortcut here. We assume that the server always allows the
2769 * PRF Hash function and has sent it in the allowed signature
2770 * algorithms list received in the Certificate Request message.
2771 *
2772 * Until we encounter a server that does not, we will take this
2773 * shortcut.
2774 *
2775 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2776 * in order to satisfy 'weird' needs from the server side.
2777 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002778 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2779 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002780 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002781 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002782 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002783 }
2784 else
2785 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002786 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002787 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002788 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002789 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002790
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002791 /* Info from md_alg will be used instead */
2792 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002793 offset = 2;
2794 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002795 else
2796#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002797 {
2798 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002799 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002800 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002801
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002802 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002803 ssl->out_msg + 6 + offset, &n,
2804 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002805 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002806 SSL_DEBUG_RET( 1, "pk_sign", ret );
2807 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002808 }
Paul Bakker926af752012-11-23 13:38:07 +01002809
Paul Bakker1ef83d62012-04-11 12:09:53 +00002810 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2811 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Paul Bakker1ef83d62012-04-11 12:09:53 +00002813 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2815 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2816
2817 ssl->state++;
2818
2819 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2820 {
2821 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2822 return( ret );
2823 }
2824
2825 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2826
Paul Bakkered27a042013-04-18 22:46:23 +02002827 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002828}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002829#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2830 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2831 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Paul Bakkera503a632013-08-14 13:48:06 +02002833#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002834static int ssl_parse_new_session_ticket( ssl_context *ssl )
2835{
2836 int ret;
2837 uint32_t lifetime;
2838 size_t ticket_len;
2839 unsigned char *ticket;
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002840 const unsigned char *msg;
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002841
2842 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2843
2844 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2845 {
2846 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2847 return( ret );
2848 }
2849
2850 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2851 {
2852 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2853 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2854 }
2855
2856 /*
2857 * struct {
2858 * uint32 ticket_lifetime_hint;
2859 * opaque ticket<0..2^16-1>;
2860 * } NewSessionTicket;
2861 *
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002862 * 0 . 3 ticket_lifetime_hint
2863 * 4 . 5 ticket_len (n)
2864 * 6 . 5+n ticket content
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002865 */
2866 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002867 ssl->in_hslen < 6 + ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002868 {
2869 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2870 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2871 }
2872
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002873 msg = ssl->in_msg + ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002874
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002875 lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
2876 ( msg[2] << 8 ) | ( msg[3] );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002877
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002878 ticket_len = ( msg[4] << 8 ) | ( msg[5] );
2879
2880 if( ticket_len + 6 + ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002881 {
2882 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2883 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2884 }
2885
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002886 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2887
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002888 /* We're not waiting for a NewSessionTicket message any more */
2889 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002890 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002891
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002892 /*
2893 * Zero-length ticket means the server changed his mind and doesn't want
2894 * to send a ticket after all, so just forget it
2895 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002896 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002897 return( 0 );
2898
Paul Bakker34617722014-06-13 17:20:13 +02002899 polarssl_zeroize( ssl->session_negotiate->ticket,
2900 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002901 polarssl_free( ssl->session_negotiate->ticket );
2902 ssl->session_negotiate->ticket = NULL;
2903 ssl->session_negotiate->ticket_len = 0;
2904
2905 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2906 {
2907 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2908 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2909 }
2910
Manuel Pégourié-Gonnard000d5ae2014-09-10 21:52:12 +02002911 memcpy( ticket, msg + 6, ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002912
2913 ssl->session_negotiate->ticket = ticket;
2914 ssl->session_negotiate->ticket_len = ticket_len;
2915 ssl->session_negotiate->ticket_lifetime = lifetime;
2916
2917 /*
2918 * RFC 5077 section 3.4:
2919 * "If the client receives a session ticket from the server, then it
2920 * discards any Session ID that was sent in the ServerHello."
2921 */
2922 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2923 ssl->session_negotiate->length = 0;
2924
2925 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2926
2927 return( 0 );
2928}
Paul Bakkera503a632013-08-14 13:48:06 +02002929#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002930
Paul Bakker5121ce52009-01-03 21:22:43 +00002931/*
Paul Bakker1961b702013-01-25 14:49:24 +01002932 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 */
Paul Bakker1961b702013-01-25 14:49:24 +01002934int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002935{
2936 int ret = 0;
2937
Paul Bakker1961b702013-01-25 14:49:24 +01002938 if( ssl->state == SSL_HANDSHAKE_OVER )
2939 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Paul Bakker1961b702013-01-25 14:49:24 +01002941 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2942
2943 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2944 return( ret );
2945
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002946#if defined(POLARSSL_SSL_PROTO_DTLS)
2947 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2948 ssl->handshake != NULL &&
2949 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2950 {
2951 if( ( ret = ssl_resend( ssl ) ) != 0 )
2952 return( ret );
2953 }
2954#endif
2955
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02002956 /* Change state now, so that it is right in ssl_read_record(), used
2957 * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
2958#if defined(POLARSSL_SSL_SESSION_TICKETS)
2959 if( ssl->state == SSL_SERVER_CHANGE_CIPHER_SPEC &&
2960 ssl->handshake->new_session_ticket != 0 )
2961 {
2962 ssl->state = SSL_SERVER_NEW_SESSION_TICKET;
2963 }
2964#endif
2965
Paul Bakker1961b702013-01-25 14:49:24 +01002966 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002967 {
Paul Bakker1961b702013-01-25 14:49:24 +01002968 case SSL_HELLO_REQUEST:
2969 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002970 break;
2971
Paul Bakker1961b702013-01-25 14:49:24 +01002972 /*
2973 * ==> ClientHello
2974 */
2975 case SSL_CLIENT_HELLO:
2976 ret = ssl_write_client_hello( ssl );
2977 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
Paul Bakker1961b702013-01-25 14:49:24 +01002979 /*
2980 * <== ServerHello
2981 * Certificate
2982 * ( ServerKeyExchange )
2983 * ( CertificateRequest )
2984 * ServerHelloDone
2985 */
2986 case SSL_SERVER_HELLO:
2987 ret = ssl_parse_server_hello( ssl );
2988 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002989
Paul Bakker1961b702013-01-25 14:49:24 +01002990 case SSL_SERVER_CERTIFICATE:
2991 ret = ssl_parse_certificate( ssl );
2992 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002993
Paul Bakker1961b702013-01-25 14:49:24 +01002994 case SSL_SERVER_KEY_EXCHANGE:
2995 ret = ssl_parse_server_key_exchange( ssl );
2996 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002997
Paul Bakker1961b702013-01-25 14:49:24 +01002998 case SSL_CERTIFICATE_REQUEST:
2999 ret = ssl_parse_certificate_request( ssl );
3000 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
Paul Bakker1961b702013-01-25 14:49:24 +01003002 case SSL_SERVER_HELLO_DONE:
3003 ret = ssl_parse_server_hello_done( ssl );
3004 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003005
Paul Bakker1961b702013-01-25 14:49:24 +01003006 /*
3007 * ==> ( Certificate/Alert )
3008 * ClientKeyExchange
3009 * ( CertificateVerify )
3010 * ChangeCipherSpec
3011 * Finished
3012 */
3013 case SSL_CLIENT_CERTIFICATE:
3014 ret = ssl_write_certificate( ssl );
3015 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Paul Bakker1961b702013-01-25 14:49:24 +01003017 case SSL_CLIENT_KEY_EXCHANGE:
3018 ret = ssl_write_client_key_exchange( ssl );
3019 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
Paul Bakker1961b702013-01-25 14:49:24 +01003021 case SSL_CERTIFICATE_VERIFY:
3022 ret = ssl_write_certificate_verify( ssl );
3023 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003024
Paul Bakker1961b702013-01-25 14:49:24 +01003025 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3026 ret = ssl_write_change_cipher_spec( ssl );
3027 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003028
Paul Bakker1961b702013-01-25 14:49:24 +01003029 case SSL_CLIENT_FINISHED:
3030 ret = ssl_write_finished( ssl );
3031 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Paul Bakker1961b702013-01-25 14:49:24 +01003033 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003034 * <== ( NewSessionTicket )
3035 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003036 * Finished
3037 */
Paul Bakkera503a632013-08-14 13:48:06 +02003038#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003039 case SSL_SERVER_NEW_SESSION_TICKET:
3040 ret = ssl_parse_new_session_ticket( ssl );
3041 break;
Paul Bakkera503a632013-08-14 13:48:06 +02003042#endif
Manuel Pégourié-Gonnardcd32a502014-09-20 13:54:12 +02003043
3044 case SSL_SERVER_CHANGE_CIPHER_SPEC:
3045 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003046 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Paul Bakker1961b702013-01-25 14:49:24 +01003048 case SSL_SERVER_FINISHED:
3049 ret = ssl_parse_finished( ssl );
3050 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003051
Paul Bakker1961b702013-01-25 14:49:24 +01003052 case SSL_FLUSH_BUFFERS:
3053 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3054 ssl->state = SSL_HANDSHAKE_WRAPUP;
3055 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Paul Bakker1961b702013-01-25 14:49:24 +01003057 case SSL_HANDSHAKE_WRAPUP:
3058 ssl_handshake_wrapup( ssl );
3059 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003060
Paul Bakker1961b702013-01-25 14:49:24 +01003061 default:
3062 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3063 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3064 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
3066 return( ret );
3067}
Paul Bakker9af723c2014-05-01 13:03:14 +02003068#endif /* POLARSSL_SSL_CLI_C */