blob: 37853bc6067bf795c789266c6ec9fd02d19357e4 [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 Bakkerb96f1542010-07-18 20:36:00 +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
123 if( ssl->renegotiation != SSL_RENEGOTIATION )
124 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
Paul Bakker5121ce52009-01-03 21:22:43 +0000503static int ssl_write_client_hello( ssl_context *ssl )
504{
Paul Bakker23986e52011-04-24 08:57:21 +0000505 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100506 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200508 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200509#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200511#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200512 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200513 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
515 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
516
Paul Bakkera9a028e2013-11-21 17:31:06 +0100517 if( ssl->f_rng == NULL )
518 {
519 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
520 return( POLARSSL_ERR_SSL_NO_RNG );
521 }
522
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100523#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000524 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100525#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000526 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000527 ssl->major_ver = ssl->min_major_ver;
528 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000529 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
Paul Bakker490ecc82011-10-06 13:04:09 +0000531 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
532 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200533 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
534 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000535 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
537 /*
538 * 0 . 0 handshake type
539 * 1 . 3 handshake length
540 * 4 . 5 highest version supported
541 * 6 . 9 current UNIX time
542 * 10 . 37 random bytes
543 */
544 buf = ssl->out_msg;
545 p = buf + 4;
546
547 *p++ = (unsigned char) ssl->max_major_ver;
548 *p++ = (unsigned char) ssl->max_minor_ver;
549
550 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
551 buf[4], buf[5] ) );
552
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200553#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 t = time( NULL );
555 *p++ = (unsigned char)( t >> 24 );
556 *p++ = (unsigned char)( t >> 16 );
557 *p++ = (unsigned char)( t >> 8 );
558 *p++ = (unsigned char)( t );
559
560 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200561#else
562 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
563 return( ret );
564
565 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200566#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Paul Bakkera3d195c2011-11-27 21:07:34 +0000568 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
569 return( ret );
570
571 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Paul Bakker48916f92012-09-16 19:57:18 +0000573 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
575 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
576
577 /*
578 * 38 . 38 session id length
579 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000580 * 40+n . 41+n ciphersuitelist length
581 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000582 * .. . .. compression methods length
583 * .. . .. compression methods
584 * .. . .. extensions length
585 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 */
Paul Bakker48916f92012-09-16 19:57:18 +0000587 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100589 if( n < 16 || n > 32 ||
590#if defined(POLARSSL_SSL_RENEGOTIATION)
591 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
592#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000593 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200594 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200596 }
597
Paul Bakkera503a632013-08-14 13:48:06 +0200598#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200599 /*
600 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
601 * generate and include a Session ID in the TLS ClientHello."
602 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100603#if defined(POLARSSL_SSL_RENEGOTIATION)
604 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000605#endif
Manuel Pégourié-Gonnard51bccd32015-03-10 16:09:08 +0000606 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000607 if( ssl->session_negotiate->ticket != NULL &&
608 ssl->session_negotiate->ticket_len != 0 )
609 {
610 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200611
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000612 if( ret != 0 )
613 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200614
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000615 ssl->session_negotiate->length = n = 32;
616 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200617 }
Paul Bakkera503a632013-08-14 13:48:06 +0200618#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620 *p++ = (unsigned char) n;
621
622 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000623 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
625 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
626 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
627
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200628 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200629 n = 0;
630 q = p;
631
632 // Skip writing ciphersuite length for now
633 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
Paul Bakker2fbefde2013-06-29 16:01:15 +0200635 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200637 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
638
639 if( ciphersuite_info == NULL )
640 continue;
641
642 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
643 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
644 continue;
645
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100646 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
647 ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
648 continue;
649
Paul Bakkere3166ce2011-01-27 17:40:50 +0000650 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200651 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Paul Bakker2fbefde2013-06-29 16:01:15 +0200653 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200654 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
655 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 }
657
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +0000658 /*
659 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
660 */
661#if defined(POLARSSL_SSL_RENEGOTIATION)
662 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
663#endif
664 {
665 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
666 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
667 n++;
668 }
669
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200670 /* Some versions of OpenSSL don't handle it correctly if not at end */
671#if defined(POLARSSL_SSL_FALLBACK_SCSV)
672 if( ssl->fallback == SSL_IS_FALLBACK )
673 {
674 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
675 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
676 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
677 n++;
678 }
679#endif
680
Paul Bakker2fbefde2013-06-29 16:01:15 +0200681 *q++ = (unsigned char)( n >> 7 );
682 *q++ = (unsigned char)( n << 1 );
683
684 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
685
686
Paul Bakker2770fbd2012-07-03 13:30:23 +0000687#if defined(POLARSSL_ZLIB_SUPPORT)
688 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
689 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000690 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000691
692 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000693 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000694 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000695#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000697 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000698
699 *p++ = 1;
700 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200701#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
Paul Bakkerd3edc862013-03-20 16:07:17 +0100703 // First write extensions, then the total length
704 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200705#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100706 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
707 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200708#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000709
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100710#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100711 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
712 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100713#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000714
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100715#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
716 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100717 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
718 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200719#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000720
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200721#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100722 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
723 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100724
Paul Bakkerd3edc862013-03-20 16:07:17 +0100725 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
726 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100727#endif
728
Paul Bakker05decb22013-08-15 13:33:48 +0200729#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200730 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
731 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200732#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200733
Paul Bakker1f2bc622013-08-15 13:45:55 +0200734#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200735 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
736 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200737#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200738
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100739#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
740 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
741 ext_len += olen;
742#endif
743
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200744#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
745 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
746 ext_len += olen;
747#endif
748
Paul Bakkera503a632013-08-14 13:48:06 +0200749#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200750 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
751 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200752#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200753
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200754#if defined(POLARSSL_SSL_ALPN)
755 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
756 ext_len += olen;
757#endif
758
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100759 /* olen unused if all extensions are disabled */
760 ((void) olen);
761
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000762 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
763 ext_len ) );
764
Paul Bakkera7036632014-04-30 10:15:38 +0200765 if( ext_len > 0 )
766 {
767 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
768 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
769 p += ext_len;
770 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100771
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 ssl->out_msglen = p - buf;
773 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
774 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
775
776 ssl->state++;
777
778 if( ( ret = ssl_write_record( ssl ) ) != 0 )
779 {
780 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
781 return( ret );
782 }
783
784 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
785
786 return( 0 );
787}
788
Paul Bakker48916f92012-09-16 19:57:18 +0000789static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200790 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000791 size_t len )
792{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000793 int ret;
794
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100795#if defined(POLARSSL_SSL_RENEGOTIATION)
796 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000797 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100798 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000799 if( len != 1 + ssl->verify_data_len * 2 ||
800 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100801 safer_memcmp( buf + 1,
802 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
803 safer_memcmp( buf + 1 + ssl->verify_data_len,
804 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000805 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100806 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000807
808 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
809 return( ret );
810
Paul Bakker48916f92012-09-16 19:57:18 +0000811 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
812 }
813 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100814 else
815#endif /* POLARSSL_SSL_RENEGOTIATION */
816 {
817 if( len != 1 || buf[0] != 0x00 )
818 {
819 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
820
821 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
822 return( ret );
823
824 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
825 }
826
827 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
828 }
Paul Bakker48916f92012-09-16 19:57:18 +0000829
830 return( 0 );
831}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200832
Paul Bakker05decb22013-08-15 13:33:48 +0200833#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200834static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200835 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200836 size_t len )
837{
838 /*
839 * server should use the extension only if we did,
840 * and if so the server's value should match ours (and len is always 1)
841 */
842 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
843 len != 1 ||
844 buf[0] != ssl->mfl_code )
845 {
846 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
847 }
848
849 return( 0 );
850}
Paul Bakker05decb22013-08-15 13:33:48 +0200851#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000852
Paul Bakker1f2bc622013-08-15 13:45:55 +0200853#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200854static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
855 const unsigned char *buf,
856 size_t len )
857{
858 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
859 len != 0 )
860 {
861 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
862 }
863
864 ((void) buf);
865
866 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
867
868 return( 0 );
869}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200870#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200871
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100872#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
873static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
874 const unsigned char *buf,
875 size_t len )
876{
877 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
878 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
879 len != 0 )
880 {
881 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
882 }
883
884 ((void) buf);
885
886 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
887
888 return( 0 );
889}
890#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
891
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200892#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
893static int ssl_parse_extended_ms_ext( ssl_context *ssl,
894 const unsigned char *buf,
895 size_t len )
896{
897 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200898 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200899 len != 0 )
900 {
901 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
902 }
903
904 ((void) buf);
905
906 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
907
908 return( 0 );
909}
910#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
911
Paul Bakkera503a632013-08-14 13:48:06 +0200912#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200913static int ssl_parse_session_ticket_ext( ssl_context *ssl,
914 const unsigned char *buf,
915 size_t len )
916{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200917 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
918 len != 0 )
919 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200920 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200921 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200922
923 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200924
925 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200926
927 return( 0 );
928}
Paul Bakkera503a632013-08-14 13:48:06 +0200929#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200930
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200931#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200932static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
933 const unsigned char *buf,
934 size_t len )
935{
936 size_t list_size;
937 const unsigned char *p;
938
939 list_size = buf[0];
940 if( list_size + 1 != len )
941 {
942 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
943 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
944 }
945
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +0200946 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200947 while( list_size > 0 )
948 {
949 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
950 p[0] == POLARSSL_ECP_PF_COMPRESSED )
951 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200952 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200953 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
954 return( 0 );
955 }
956
957 list_size--;
958 p++;
959 }
960
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +0200961 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
962 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200963}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200964#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200965
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200966#if defined(POLARSSL_SSL_ALPN)
967static int ssl_parse_alpn_ext( ssl_context *ssl,
968 const unsigned char *buf, size_t len )
969{
970 size_t list_len, name_len;
971 const char **p;
972
973 /* If we didn't send it, the server shouldn't send it */
974 if( ssl->alpn_list == NULL )
975 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
976
977 /*
978 * opaque ProtocolName<1..2^8-1>;
979 *
980 * struct {
981 * ProtocolName protocol_name_list<2..2^16-1>
982 * } ProtocolNameList;
983 *
984 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
985 */
986
987 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
988 if( len < 4 )
989 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
990
991 list_len = ( buf[0] << 8 ) | buf[1];
992 if( list_len != len - 2 )
993 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
994
995 name_len = buf[2];
996 if( name_len != list_len - 1 )
997 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
998
999 /* Check that the server chosen protocol was in our list and save it */
1000 for( p = ssl->alpn_list; *p != NULL; p++ )
1001 {
1002 if( name_len == strlen( *p ) &&
1003 memcmp( buf + 3, *p, name_len ) == 0 )
1004 {
1005 ssl->alpn_chosen = *p;
1006 return( 0 );
1007 }
1008 }
1009
1010 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1011}
1012#endif /* POLARSSL_SSL_ALPN */
1013
Paul Bakker5121ce52009-01-03 21:22:43 +00001014static int ssl_parse_server_hello( ssl_context *ssl )
1015{
Paul Bakker2770fbd2012-07-03 13:30:23 +00001016 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +00001017 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001018 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001019 unsigned char *buf, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001020#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001021 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001022#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001023 int handshake_failure = 0;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001024 const ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001025#if defined(POLARSSL_DEBUG_C)
1026 uint32_t t;
1027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
1029 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1030
1031 /*
1032 * 0 . 0 handshake type
1033 * 1 . 3 handshake length
1034 * 4 . 5 protocol version
1035 * 6 . 9 UNIX time()
1036 * 10 . 37 random bytes
1037 */
1038 buf = ssl->in_msg;
1039
1040 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1041 {
1042 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1043 return( ret );
1044 }
1045
1046 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1047 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001048#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001049 if( ssl->renegotiation == SSL_RENEGOTIATION )
1050 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001051 ssl->renego_records_seen++;
1052
1053 if( ssl->renego_max_records >= 0 &&
1054 ssl->renego_records_seen > ssl->renego_max_records )
1055 {
1056 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1057 "but not honored by server" ) );
1058 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1059 }
1060
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001061 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1062 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1063 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001064#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001065
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001067 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 }
1069
1070 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1071 buf[4], buf[5] ) );
1072
1073 if( ssl->in_hslen < 42 ||
1074 buf[0] != SSL_HS_SERVER_HELLO ||
1075 buf[4] != SSL_MAJOR_VERSION_3 )
1076 {
1077 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001078 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 }
1080
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001081 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 {
1083 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001084 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 }
1086
1087 ssl->minor_ver = buf[5];
1088
Paul Bakker1d29fb52012-09-28 13:28:45 +00001089 if( ssl->minor_ver < ssl->min_minor_ver )
1090 {
1091 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001092 " [%d:%d] < [%d:%d]", ssl->major_ver,
1093 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001094
1095 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1096 SSL_ALERT_MSG_PROTOCOL_VERSION );
1097
1098 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1099 }
1100
Paul Bakker1504af52012-02-11 16:17:43 +00001101#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001102 t = ( (uint32_t) buf[6] << 24 )
1103 | ( (uint32_t) buf[7] << 16 )
1104 | ( (uint32_t) buf[8] << 8 )
1105 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001106 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001107#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
Paul Bakker48916f92012-09-16 19:57:18 +00001109 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
1111 n = buf[38];
1112
Paul Bakker5121ce52009-01-03 21:22:43 +00001113 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1114
Paul Bakker48916f92012-09-16 19:57:18 +00001115 if( n > 32 )
1116 {
1117 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1118 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1119 }
1120
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 /*
1122 * 38 . 38 session id length
1123 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001124 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001125 * 41+n . 41+n chosen compression alg.
1126 * 42+n . 43+n extensions length
1127 * 44+n . 44+n+m extensions
1128 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001129 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 {
1131 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001132 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
Paul Bakker48916f92012-09-16 19:57:18 +00001134 if( ( ext_len > 0 && ext_len < 4 ) ||
1135 ssl->in_hslen != 44 + n + ext_len )
1136 {
1137 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1138 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1139 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001140 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001141 else if( ssl->in_hslen == 42 + n )
1142 {
1143 ext_len = 0;
1144 }
1145 else
1146 {
1147 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1148 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
1151 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001152 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Paul Bakker380da532012-04-18 16:10:25 +00001154 /*
1155 * Initialize update checksum functions
1156 */
Paul Bakker68884e32013-01-07 18:20:04 +01001157 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1158
1159 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1160 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001161 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001162 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1163 }
Paul Bakker380da532012-04-18 16:10:25 +00001164
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001165 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1166
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1168 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1169
1170 /*
1171 * Check if the session can be resumed
1172 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001173 if( ssl->handshake->resume == 0 || n == 0 ||
1174#if defined(POLARSSL_SSL_RENEGOTIATION)
1175 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1176#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001177 ssl->session_negotiate->ciphersuite != i ||
1178 ssl->session_negotiate->compression != comp ||
1179 ssl->session_negotiate->length != n ||
1180 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 {
1182 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001183 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001184#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001185 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001186#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001187 ssl->session_negotiate->ciphersuite = i;
1188 ssl->session_negotiate->compression = comp;
1189 ssl->session_negotiate->length = n;
1190 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 }
1192 else
1193 {
1194 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001195
1196 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1197 {
1198 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1199 return( ret );
1200 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 }
1202
1203 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001204 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001205
Paul Bakkere3166ce2011-01-27 17:40:50 +00001206 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1208
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001209 suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1210 if( suite_info == NULL ||
1211 ( ssl->arc4_disabled &&
1212 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) )
1213 {
1214 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1215 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1216 }
1217
1218
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 i = 0;
1220 while( 1 )
1221 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001222 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 {
1224 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001225 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 }
1227
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001228 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1229 ssl->session_negotiate->ciphersuite )
1230 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001232 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 }
1234
Paul Bakker2770fbd2012-07-03 13:30:23 +00001235 if( comp != SSL_COMPRESS_NULL
1236#if defined(POLARSSL_ZLIB_SUPPORT)
1237 && comp != SSL_COMPRESS_DEFLATE
1238#endif
1239 )
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 }
Paul Bakker48916f92012-09-16 19:57:18 +00001244 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Paul Bakker48916f92012-09-16 19:57:18 +00001246 ext = buf + 44 + n;
1247
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001248 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1249
Paul Bakker48916f92012-09-16 19:57:18 +00001250 while( ext_len )
1251 {
1252 unsigned int ext_id = ( ( ext[0] << 8 )
1253 | ( ext[1] ) );
1254 unsigned int ext_size = ( ( ext[2] << 8 )
1255 | ( ext[3] ) );
1256
1257 if( ext_size + 4 > ext_len )
1258 {
1259 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1260 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1261 }
1262
1263 switch( ext_id )
1264 {
1265 case TLS_EXT_RENEGOTIATION_INFO:
1266 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001267#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001268 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001269#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001270
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001271 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1272 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001273 return( ret );
1274
1275 break;
1276
Paul Bakker05decb22013-08-15 13:33:48 +02001277#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001278 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1279 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1280
1281 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1282 ext + 4, ext_size ) ) != 0 )
1283 {
1284 return( ret );
1285 }
1286
1287 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001288#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001289
Paul Bakker1f2bc622013-08-15 13:45:55 +02001290#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001291 case TLS_EXT_TRUNCATED_HMAC:
1292 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1293
1294 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1295 ext + 4, ext_size ) ) != 0 )
1296 {
1297 return( ret );
1298 }
1299
1300 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001301#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001302
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001303#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1304 case TLS_EXT_ENCRYPT_THEN_MAC:
1305 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1306
1307 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1308 ext + 4, ext_size ) ) != 0 )
1309 {
1310 return( ret );
1311 }
1312
1313 break;
1314#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1315
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001316#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1317 case TLS_EXT_EXTENDED_MASTER_SECRET:
1318 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1319
1320 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1321 ext + 4, ext_size ) ) != 0 )
1322 {
1323 return( ret );
1324 }
1325
1326 break;
1327#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1328
Paul Bakkera503a632013-08-14 13:48:06 +02001329#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001330 case TLS_EXT_SESSION_TICKET:
1331 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1332
1333 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1334 ext + 4, ext_size ) ) != 0 )
1335 {
1336 return( ret );
1337 }
1338
1339 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001340#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001341
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001342#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001343 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1344 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1345
1346 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1347 ext + 4, ext_size ) ) != 0 )
1348 {
1349 return( ret );
1350 }
1351
1352 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001353#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001354
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001355#if defined(POLARSSL_SSL_ALPN)
1356 case TLS_EXT_ALPN:
1357 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1358
1359 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1360 return( ret );
1361
1362 break;
1363#endif /* POLARSSL_SSL_ALPN */
1364
Paul Bakker48916f92012-09-16 19:57:18 +00001365 default:
1366 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1367 ext_id ) );
1368 }
1369
1370 ext_len -= 4 + ext_size;
1371 ext += 4 + ext_size;
1372
1373 if( ext_len > 0 && ext_len < 4 )
1374 {
1375 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1376 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1377 }
1378 }
1379
1380 /*
1381 * Renegotiation security checks
1382 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001383 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1384 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001385 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001386 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1387 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001388 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001389#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001390 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1391 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1392 renegotiation_info_seen == 0 )
1393 {
1394 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1395 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001396 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001397 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1398 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1399 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001400 {
1401 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001402 handshake_failure = 1;
1403 }
1404 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1405 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1406 renegotiation_info_seen == 1 )
1407 {
1408 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1409 handshake_failure = 1;
1410 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001411#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001412
1413 if( handshake_failure == 1 )
1414 {
1415 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1416 return( ret );
1417
Paul Bakker48916f92012-09-16 19:57:18 +00001418 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
1421 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1422
1423 return( 0 );
1424}
1425
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001426#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1427 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001428static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1429 unsigned char *end )
1430{
1431 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1432
Paul Bakker29e1f122013-04-16 13:07:56 +02001433 /*
1434 * Ephemeral DH parameters:
1435 *
1436 * struct {
1437 * opaque dh_p<1..2^16-1>;
1438 * opaque dh_g<1..2^16-1>;
1439 * opaque dh_Ys<1..2^16-1>;
1440 * } ServerDHParams;
1441 */
1442 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1443 {
1444 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1445 return( ret );
1446 }
1447
1448 if( ssl->handshake->dhm_ctx.len < 64 ||
1449 ssl->handshake->dhm_ctx.len > 512 )
1450 {
1451 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1452 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1453 }
1454
1455 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1456 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1457 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001458
1459 return( ret );
1460}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001461#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1462 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001463
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001464#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001465 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001466 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1467 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1468 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1469static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1470{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001471 const ecp_curve_info *curve_info;
1472
1473 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1474 if( curve_info == NULL )
1475 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001476 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1477 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001478 }
1479
1480 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001481
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001482#if defined(POLARSSL_SSL_ECP_SET_CURVES)
1483 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1484#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001485 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1486 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001487#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001488 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001489
1490 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1491
1492 return( 0 );
1493}
Paul Bakker9af723c2014-05-01 13:03:14 +02001494#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1495 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1496 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1497 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1498 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001499
1500#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1501 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001502 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001503static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1504 unsigned char **p,
1505 unsigned char *end )
1506{
1507 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1508
Paul Bakker29e1f122013-04-16 13:07:56 +02001509 /*
1510 * Ephemeral ECDH parameters:
1511 *
1512 * struct {
1513 * ECParameters curve_params;
1514 * ECPoint public;
1515 * } ServerECDHParams;
1516 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001517 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1518 (const unsigned char **) p, end ) ) != 0 )
1519 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001520 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001521 return( ret );
1522 }
1523
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001524 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001525 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001526 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001527 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1528 }
1529
Paul Bakker29e1f122013-04-16 13:07:56 +02001530 return( ret );
1531}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001532#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001533 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1534 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001535
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001536#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001537static int ssl_parse_server_psk_hint( ssl_context *ssl,
1538 unsigned char **p,
1539 unsigned char *end )
1540{
1541 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001542 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001543 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001544
1545 /*
1546 * PSK parameters:
1547 *
1548 * opaque psk_identity_hint<0..2^16-1>;
1549 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001550 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001551 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001552
1553 if( (*p) + len > end )
1554 {
1555 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1556 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1557 }
1558
1559 // TODO: Retrieve PSK identity hint and callback to app
1560 //
1561 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001562 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001563
1564 return( ret );
1565}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001566#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001567
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001568#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1569 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1570/*
1571 * Generate a pre-master secret and encrypt it with the server's RSA key
1572 */
1573static int ssl_write_encrypted_pms( ssl_context *ssl,
1574 size_t offset, size_t *olen,
1575 size_t pms_offset )
1576{
1577 int ret;
1578 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1579 unsigned char *p = ssl->handshake->premaster + pms_offset;
1580
1581 /*
1582 * Generate (part of) the pre-master as
1583 * struct {
1584 * ProtocolVersion client_version;
1585 * opaque random[46];
1586 * } PreMasterSecret;
1587 */
1588 p[0] = (unsigned char) ssl->max_major_ver;
1589 p[1] = (unsigned char) ssl->max_minor_ver;
1590
1591 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1592 {
1593 SSL_DEBUG_RET( 1, "f_rng", ret );
1594 return( ret );
1595 }
1596
1597 ssl->handshake->pmslen = 48;
1598
1599 /*
1600 * Now write it out, encrypted
1601 */
1602 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1603 POLARSSL_PK_RSA ) )
1604 {
1605 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1606 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1607 }
1608
1609 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1610 p, ssl->handshake->pmslen,
1611 ssl->out_msg + offset + len_bytes, olen,
1612 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1613 ssl->f_rng, ssl->p_rng ) ) != 0 )
1614 {
1615 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1616 return( ret );
1617 }
1618
1619#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1620 defined(POLARSSL_SSL_PROTO_TLS1_2)
1621 if( len_bytes == 2 )
1622 {
1623 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1624 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1625 *olen += 2;
1626 }
1627#endif
1628
1629 return( 0 );
1630}
1631#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1632 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001633
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001634#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001635#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001636 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1637 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001638static int ssl_parse_signature_algorithm( ssl_context *ssl,
1639 unsigned char **p,
1640 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001641 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001642 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001643{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001644 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001645 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001646 *pk_alg = POLARSSL_PK_NONE;
1647
1648 /* Only in TLS 1.2 */
1649 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1650 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001651 return( 0 );
1652 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001653
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001654 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001655 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1656
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001657 /*
1658 * Get hash algorithm
1659 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001660 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001661 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001662 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1663 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001664 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1665 }
1666
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001667 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001668 * Get signature algorithm
1669 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001670 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001671 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001672 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1673 "SignatureAlgorithm %d", (*p)[1] ) );
1674 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001675 }
1676
1677 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1678 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1679 *p += 2;
1680
1681 return( 0 );
1682}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001683#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001684 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1685 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001686#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001687
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001688
1689#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1690 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1691static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1692{
1693 int ret;
1694 const ecp_keypair *peer_key;
1695
1696 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1697 POLARSSL_PK_ECKEY ) )
1698 {
1699 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1700 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1701 }
1702
1703 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1704
1705 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1706 POLARSSL_ECDH_THEIRS ) ) != 0 )
1707 {
1708 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1709 return( ret );
1710 }
1711
1712 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1713 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001714 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001715 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1716 }
1717
1718 return( ret );
1719}
1720#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1721 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1722
Paul Bakker41c83d32013-03-20 14:39:14 +01001723static int ssl_parse_server_key_exchange( ssl_context *ssl )
1724{
Paul Bakker23986e52011-04-24 08:57:21 +00001725 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001726 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001727 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001728#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001729 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1730 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001731 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001732 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001733 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001734 size_t hashlen;
1735 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001736#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001737
1738 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1739
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001740#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001741 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001742 {
1743 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1744 ssl->state++;
1745 return( 0 );
1746 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001747 ((void) p);
1748 ((void) end);
1749#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001751#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1752 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1753 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1754 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1755 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001756 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1757 {
1758 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1759 return( ret );
1760 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001761
1762 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1763 ssl->state++;
1764 return( 0 );
1765 }
1766 ((void) p);
1767 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001768#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1769 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001770
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1772 {
1773 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1774 return( ret );
1775 }
1776
1777 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1778 {
1779 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001780 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 }
1782
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001783 /*
1784 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1785 * doesn't use a psk_identity_hint
1786 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1788 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001789 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1790 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001791 {
1792 ssl->record_read = 1;
1793 goto exit;
1794 }
1795
1796 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1797 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001798 }
1799
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001800 p = ssl->in_msg + 4;
1801 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001802 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001803
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001804#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1805 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1806 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1807 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1808 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1809 {
1810 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1811 {
1812 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1813 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1814 }
1815 } /* FALLTROUGH */
1816#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1817
1818#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1819 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1820 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1821 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1822 ; /* nothing more to do */
1823 else
1824#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1825 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1826#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1827 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1828 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1829 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001831 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001832 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001833 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001834 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1835 }
1836 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001837 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001838#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1839 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001840#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001841 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001842 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1843 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001844 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001845 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001846 {
1847 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1848 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001849 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1850 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1851 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001852 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001853 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001854#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001855 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001856 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001857 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001858 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001859 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001860 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001861
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001862#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001863 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1864 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001865 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001866 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1867 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001868 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001869 params_len = p - ( ssl->in_msg + 4 );
1870
Paul Bakker29e1f122013-04-16 13:07:56 +02001871 /*
1872 * Handle the digitally-signed structure
1873 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001874#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1875 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001876 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001877 if( ssl_parse_signature_algorithm( ssl, &p, end,
1878 &md_alg, &pk_alg ) != 0 )
1879 {
1880 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1881 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1882 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001883
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001884 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001885 {
1886 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1887 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1888 }
1889 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001890 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001891#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001892#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1893 defined(POLARSSL_SSL_PROTO_TLS1_1)
1894 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001895 {
1896 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001897
Paul Bakker9659dae2013-08-28 16:21:34 +02001898 /* Default hash for ECDSA is SHA-1 */
1899 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1900 md_alg = POLARSSL_MD_SHA1;
1901 }
1902 else
1903#endif
1904 {
1905 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001906 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02001907 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001908
1909 /*
1910 * Read signature
1911 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001912 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001913 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001914
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001915 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001916 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001917 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001918 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1919 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001921 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001922
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001923 /*
1924 * Compute the hash that has been signed
1925 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001926#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1927 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001928 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001929 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001930 md5_context md5;
1931 sha1_context sha1;
1932
Paul Bakker5b4af392014-06-26 12:09:34 +02001933 md5_init( &md5 );
1934 sha1_init( &sha1 );
1935
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001936 hashlen = 36;
1937
Paul Bakker29e1f122013-04-16 13:07:56 +02001938 /*
1939 * digitally-signed struct {
1940 * opaque md5_hash[16];
1941 * opaque sha_hash[20];
1942 * };
1943 *
1944 * md5_hash
1945 * MD5(ClientHello.random + ServerHello.random
1946 * + ServerParams);
1947 * sha_hash
1948 * SHA(ClientHello.random + ServerHello.random
1949 * + ServerParams);
1950 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001951 md5_starts( &md5 );
1952 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001953 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001954 md5_finish( &md5, hash );
1955
1956 sha1_starts( &sha1 );
1957 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001958 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001959 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001960
1961 md5_free( &md5 );
1962 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001963 }
1964 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001965#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1966 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001967#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1968 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001969 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001970 {
1971 md_context_t ctx;
1972
Paul Bakker84bbeb52014-07-01 14:53:22 +02001973 md_init( &ctx );
1974
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001975 /* Info from md_alg will be used instead */
1976 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001977
1978 /*
1979 * digitally-signed struct {
1980 * opaque client_random[32];
1981 * opaque server_random[32];
1982 * ServerDHParams params;
1983 * };
1984 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001985 if( ( ret = md_init_ctx( &ctx,
1986 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001987 {
1988 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1989 return( ret );
1990 }
1991
1992 md_starts( &ctx );
1993 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001994 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001995 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02001996 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001997 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001998 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001999#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2000 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002001 {
Paul Bakker577e0062013-08-28 11:57:20 +02002002 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002003 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002004 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002005
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002006 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2007 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002008
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002009 /*
2010 * Verify signature
2011 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002012 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002013 {
2014 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2015 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2016 }
2017
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002018 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2019 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002020 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002021 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002022 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002023 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002025#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002026 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2027 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002028
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002029exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 ssl->state++;
2031
2032 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2033
2034 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035}
2036
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002037#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2038 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2039 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2040 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2041static int ssl_parse_certificate_request( ssl_context *ssl )
2042{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002043 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2044
2045 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2046
2047 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2048 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2049 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2050 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2051 {
2052 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2053 ssl->state++;
2054 return( 0 );
2055 }
2056
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002057 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2058 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002059}
2060#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002061static int ssl_parse_certificate_request( ssl_context *ssl )
2062{
2063 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002064 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002065 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002066 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002067 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002068
2069 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2070
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002071 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2072 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2073 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2074 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2075 {
2076 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2077 ssl->state++;
2078 return( 0 );
2079 }
2080
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 /*
2082 * 0 . 0 handshake type
2083 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002084 * 4 . 4 cert type count
2085 * 5 .. m-1 cert types
2086 * m .. m+1 sig alg length (TLS 1.2 only)
2087 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 * n .. n+1 length of all DNs
2089 * n+2 .. n+3 length of DN 1
2090 * n+4 .. ... Distinguished Name #1
2091 * ... .. ... length of DN 2, etc.
2092 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002093 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002095 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2096 {
2097 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2098 return( ret );
2099 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002100
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002101 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2102 {
2103 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2104 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2105 }
2106
2107 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
2109
2110 ssl->client_auth = 0;
2111 ssl->state++;
2112
2113 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2114 ssl->client_auth++;
2115
2116 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2117 ssl->client_auth ? "a" : "no" ) );
2118
Paul Bakker926af752012-11-23 13:38:07 +01002119 if( ssl->client_auth == 0 )
2120 goto exit;
2121
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002122 ssl->record_read = 0;
2123
Paul Bakker926af752012-11-23 13:38:07 +01002124 // TODO: handshake_failure alert for an anonymous server to request
2125 // client authentication
2126
2127 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002128
Paul Bakker926af752012-11-23 13:38:07 +01002129 // Retrieve cert types
2130 //
2131 cert_type_len = buf[4];
2132 n = cert_type_len;
2133
2134 if( ssl->in_hslen < 6 + n )
2135 {
2136 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2137 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2138 }
2139
Paul Bakker73d44312013-05-22 13:56:26 +02002140 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002141 while( cert_type_len > 0 )
2142 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002143#if defined(POLARSSL_RSA_C)
2144 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002145 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002146 {
2147 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2148 break;
2149 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002150 else
2151#endif
2152#if defined(POLARSSL_ECDSA_C)
2153 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002154 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002155 {
2156 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2157 break;
2158 }
2159 else
2160#endif
2161 {
2162 ; /* Unsupported cert type, ignore */
2163 }
Paul Bakker926af752012-11-23 13:38:07 +01002164
2165 cert_type_len--;
2166 p++;
2167 }
2168
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002169#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002170 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2171 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002172 /* Ignored, see comments about hash in write_certificate_verify */
2173 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002174 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2175 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002176
2177 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002178 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002179 n += sig_alg_len;
2180
2181 if( ssl->in_hslen < 6 + n )
2182 {
2183 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2184 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2185 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002186 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002187#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002188
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002189 /* Ignore certificate_authorities, we only have one cert anyway */
2190 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002191 dn_len = ( ( buf[5 + m + n] << 8 )
2192 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002193
2194 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002195 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002196 {
2197 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2198 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2199 }
2200
2201exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2203
2204 return( 0 );
2205}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002206#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2207 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2208 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2209 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002210
2211static int ssl_parse_server_hello_done( ssl_context *ssl )
2212{
2213 int ret;
2214
2215 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2216
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002217 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 {
2219 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2220 {
2221 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2222 return( ret );
2223 }
2224
2225 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2226 {
2227 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002228 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 }
2230 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002231 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002232
2233 if( ssl->in_hslen != 4 ||
2234 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2235 {
2236 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002237 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 }
2239
2240 ssl->state++;
2241
2242 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2243
2244 return( 0 );
2245}
2246
2247static int ssl_write_client_key_exchange( ssl_context *ssl )
2248{
Paul Bakker23986e52011-04-24 08:57:21 +00002249 int ret;
2250 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002251 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002252
2253 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2254
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002255#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002256 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 /*
2259 * DHM key exchange -- send G^X mod P
2260 */
Paul Bakker48916f92012-09-16 19:57:18 +00002261 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002262
2263 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2264 ssl->out_msg[5] = (unsigned char)( n );
2265 i = 6;
2266
Paul Bakker29b64762012-09-25 09:36:44 +00002267 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002268 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 &ssl->out_msg[i], n,
2270 ssl->f_rng, ssl->p_rng );
2271 if( ret != 0 )
2272 {
2273 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2274 return( ret );
2275 }
2276
Paul Bakker48916f92012-09-16 19:57:18 +00002277 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2278 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002280 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Paul Bakker48916f92012-09-16 19:57:18 +00002282 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2283 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002284 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002285 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 {
2287 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2288 return( ret );
2289 }
2290
Paul Bakker48916f92012-09-16 19:57:18 +00002291 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 }
2293 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002294#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002295#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002296 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2297 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2298 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002299 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002300 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2301 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2302 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002303 {
2304 /*
2305 * ECDH key exchange -- send client public value
2306 */
2307 i = 4;
2308
2309 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2310 &n,
2311 &ssl->out_msg[i], 1000,
2312 ssl->f_rng, ssl->p_rng );
2313 if( ret != 0 )
2314 {
2315 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2316 return( ret );
2317 }
2318
2319 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2320
2321 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2322 &ssl->handshake->pmslen,
2323 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002324 POLARSSL_MPI_MAX_SIZE,
2325 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002326 {
2327 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2328 return( ret );
2329 }
2330
2331 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2332 }
2333 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002334#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002335 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2336 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2337 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002338#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002339 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002340 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002341 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2342 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002343 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002344 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002345 * opaque psk_identity<0..2^16-1>;
2346 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002347 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002348 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2349
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002350 i = 4;
2351 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002352 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2353 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002354
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002355 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2356 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002357
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002358#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002359 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002360 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002361 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002362 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002363 else
2364#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002365#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2366 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2367 {
2368 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2369 return( ret );
2370 }
2371 else
2372#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002374 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002375 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002376 /*
2377 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2378 */
2379 n = ssl->handshake->dhm_ctx.len;
2380 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2381 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002382
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002383 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002384 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002385 &ssl->out_msg[i], n,
2386 ssl->f_rng, ssl->p_rng );
2387 if( ret != 0 )
2388 {
2389 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2390 return( ret );
2391 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002392 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002393 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002395#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002396 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002397 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002398 /*
2399 * ClientECDiffieHellmanPublic public;
2400 */
2401 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2402 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2403 ssl->f_rng, ssl->p_rng );
2404 if( ret != 0 )
2405 {
2406 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2407 return( ret );
2408 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002409
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002410 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2411 }
2412 else
2413#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2414 {
2415 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002416 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002417 }
2418
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002419 if( ( ret = ssl_psk_derive_premaster( ssl,
2420 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002421 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002422 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002423 return( ret );
2424 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002425 }
2426 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002427#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002428#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002429 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002431 i = 4;
2432 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002433 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 }
Paul Bakkered27a042013-04-18 22:46:23 +02002435 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002436#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002437 {
2438 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002439 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002440 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 ssl->out_msglen = i + n;
2444 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2445 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2446
2447 ssl->state++;
2448
2449 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2450 {
2451 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2452 return( ret );
2453 }
2454
2455 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2456
2457 return( 0 );
2458}
2459
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002460#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2461 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002462 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2463 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002464static int ssl_write_certificate_verify( ssl_context *ssl )
2465{
Paul Bakkered27a042013-04-18 22:46:23 +02002466 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002467 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2470
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002471 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2472 {
2473 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2474 return( ret );
2475 }
2476
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002477 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002478 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002479 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002480 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002481 {
2482 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2483 ssl->state++;
2484 return( 0 );
2485 }
2486
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002487 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2488 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002489}
2490#else
2491static int ssl_write_certificate_verify( ssl_context *ssl )
2492{
2493 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2494 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2495 size_t n = 0, offset = 0;
2496 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002497 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002498 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002499 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002500
2501 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2502
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002503 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2504 {
2505 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2506 return( ret );
2507 }
2508
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002509 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002510 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002511 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002512 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2513 {
2514 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2515 ssl->state++;
2516 return( 0 );
2517 }
2518
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002519 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 {
2521 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2522 ssl->state++;
2523 return( 0 );
2524 }
2525
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002526 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002528 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2529 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 }
2531
2532 /*
2533 * Make an RSA signature of the handshake digests
2534 */
Paul Bakker48916f92012-09-16 19:57:18 +00002535 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002537#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2538 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002539 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002540 {
Paul Bakker926af752012-11-23 13:38:07 +01002541 /*
2542 * digitally-signed struct {
2543 * opaque md5_hash[16];
2544 * opaque sha_hash[20];
2545 * };
2546 *
2547 * md5_hash
2548 * MD5(handshake_messages);
2549 *
2550 * sha_hash
2551 * SHA(handshake_messages);
2552 */
2553 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002554 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002555
2556 /*
2557 * For ECDSA, default hash is SHA-1 only
2558 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002559 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002560 {
2561 hash_start += 16;
2562 hashlen -= 16;
2563 md_alg = POLARSSL_MD_SHA1;
2564 }
Paul Bakker926af752012-11-23 13:38:07 +01002565 }
2566 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002567#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2568 POLARSSL_SSL_PROTO_TLS1_1 */
2569#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2570 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002571 {
2572 /*
2573 * digitally-signed struct {
2574 * opaque handshake_messages[handshake_messages_length];
2575 * };
2576 *
2577 * Taking shortcut here. We assume that the server always allows the
2578 * PRF Hash function and has sent it in the allowed signature
2579 * algorithms list received in the Certificate Request message.
2580 *
2581 * Until we encounter a server that does not, we will take this
2582 * shortcut.
2583 *
2584 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2585 * in order to satisfy 'weird' needs from the server side.
2586 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002587 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2588 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002589 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002590 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002591 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002592 }
2593 else
2594 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002595 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002596 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002597 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002598 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002599
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002600 /* Info from md_alg will be used instead */
2601 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002602 offset = 2;
2603 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002604 else
2605#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002606 {
2607 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002608 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002609 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002610
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002611 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002612 ssl->out_msg + 6 + offset, &n,
2613 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002614 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002615 SSL_DEBUG_RET( 1, "pk_sign", ret );
2616 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002617 }
Paul Bakker926af752012-11-23 13:38:07 +01002618
Paul Bakker1ef83d62012-04-11 12:09:53 +00002619 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2620 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
Paul Bakker1ef83d62012-04-11 12:09:53 +00002622 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2624 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2625
2626 ssl->state++;
2627
2628 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2629 {
2630 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2631 return( ret );
2632 }
2633
2634 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2635
Paul Bakkered27a042013-04-18 22:46:23 +02002636 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002638#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2639 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2640 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
Paul Bakkera503a632013-08-14 13:48:06 +02002642#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002643static int ssl_parse_new_session_ticket( ssl_context *ssl )
2644{
2645 int ret;
2646 uint32_t lifetime;
2647 size_t ticket_len;
2648 unsigned char *ticket;
2649
2650 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2651
2652 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2653 {
2654 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2655 return( ret );
2656 }
2657
2658 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2659 {
2660 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2661 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2662 }
2663
2664 /*
2665 * struct {
2666 * uint32 ticket_lifetime_hint;
2667 * opaque ticket<0..2^16-1>;
2668 * } NewSessionTicket;
2669 *
2670 * 0 . 0 handshake message type
2671 * 1 . 3 handshake message length
2672 * 4 . 7 ticket_lifetime_hint
2673 * 8 . 9 ticket_len (n)
2674 * 10 . 9+n ticket content
2675 */
2676 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2677 ssl->in_hslen < 10 )
2678 {
2679 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2680 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2681 }
2682
2683 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2684 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2685
2686 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2687
2688 if( ticket_len + 10 != ssl->in_hslen )
2689 {
2690 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2691 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2692 }
2693
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002694 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2695
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002696 /* We're not waiting for a NewSessionTicket message any more */
2697 ssl->handshake->new_session_ticket = 0;
2698
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002699 /*
2700 * Zero-length ticket means the server changed his mind and doesn't want
2701 * to send a ticket after all, so just forget it
2702 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002703 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002704 return( 0 );
2705
Paul Bakker34617722014-06-13 17:20:13 +02002706 polarssl_zeroize( ssl->session_negotiate->ticket,
2707 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002708 polarssl_free( ssl->session_negotiate->ticket );
2709 ssl->session_negotiate->ticket = NULL;
2710 ssl->session_negotiate->ticket_len = 0;
2711
2712 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2713 {
2714 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2715 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2716 }
2717
2718 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2719
2720 ssl->session_negotiate->ticket = ticket;
2721 ssl->session_negotiate->ticket_len = ticket_len;
2722 ssl->session_negotiate->ticket_lifetime = lifetime;
2723
2724 /*
2725 * RFC 5077 section 3.4:
2726 * "If the client receives a session ticket from the server, then it
2727 * discards any Session ID that was sent in the ServerHello."
2728 */
2729 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2730 ssl->session_negotiate->length = 0;
2731
2732 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2733
2734 return( 0 );
2735}
Paul Bakkera503a632013-08-14 13:48:06 +02002736#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002737
Paul Bakker5121ce52009-01-03 21:22:43 +00002738/*
Paul Bakker1961b702013-01-25 14:49:24 +01002739 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 */
Paul Bakker1961b702013-01-25 14:49:24 +01002741int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002742{
2743 int ret = 0;
2744
Paul Bakker1961b702013-01-25 14:49:24 +01002745 if( ssl->state == SSL_HANDSHAKE_OVER )
2746 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002747
Paul Bakker1961b702013-01-25 14:49:24 +01002748 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2749
2750 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2751 return( ret );
2752
2753 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 {
Paul Bakker1961b702013-01-25 14:49:24 +01002755 case SSL_HELLO_REQUEST:
2756 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 break;
2758
Paul Bakker1961b702013-01-25 14:49:24 +01002759 /*
2760 * ==> ClientHello
2761 */
2762 case SSL_CLIENT_HELLO:
2763 ret = ssl_write_client_hello( ssl );
2764 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
Paul Bakker1961b702013-01-25 14:49:24 +01002766 /*
2767 * <== ServerHello
2768 * Certificate
2769 * ( ServerKeyExchange )
2770 * ( CertificateRequest )
2771 * ServerHelloDone
2772 */
2773 case SSL_SERVER_HELLO:
2774 ret = ssl_parse_server_hello( ssl );
2775 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
Paul Bakker1961b702013-01-25 14:49:24 +01002777 case SSL_SERVER_CERTIFICATE:
2778 ret = ssl_parse_certificate( ssl );
2779 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
Paul Bakker1961b702013-01-25 14:49:24 +01002781 case SSL_SERVER_KEY_EXCHANGE:
2782 ret = ssl_parse_server_key_exchange( ssl );
2783 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Paul Bakker1961b702013-01-25 14:49:24 +01002785 case SSL_CERTIFICATE_REQUEST:
2786 ret = ssl_parse_certificate_request( ssl );
2787 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
Paul Bakker1961b702013-01-25 14:49:24 +01002789 case SSL_SERVER_HELLO_DONE:
2790 ret = ssl_parse_server_hello_done( ssl );
2791 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Paul Bakker1961b702013-01-25 14:49:24 +01002793 /*
2794 * ==> ( Certificate/Alert )
2795 * ClientKeyExchange
2796 * ( CertificateVerify )
2797 * ChangeCipherSpec
2798 * Finished
2799 */
2800 case SSL_CLIENT_CERTIFICATE:
2801 ret = ssl_write_certificate( ssl );
2802 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Paul Bakker1961b702013-01-25 14:49:24 +01002804 case SSL_CLIENT_KEY_EXCHANGE:
2805 ret = ssl_write_client_key_exchange( ssl );
2806 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
Paul Bakker1961b702013-01-25 14:49:24 +01002808 case SSL_CERTIFICATE_VERIFY:
2809 ret = ssl_write_certificate_verify( ssl );
2810 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
Paul Bakker1961b702013-01-25 14:49:24 +01002812 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2813 ret = ssl_write_change_cipher_spec( ssl );
2814 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Paul Bakker1961b702013-01-25 14:49:24 +01002816 case SSL_CLIENT_FINISHED:
2817 ret = ssl_write_finished( ssl );
2818 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Paul Bakker1961b702013-01-25 14:49:24 +01002820 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002821 * <== ( NewSessionTicket )
2822 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002823 * Finished
2824 */
2825 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002826#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002827 if( ssl->handshake->new_session_ticket != 0 )
2828 ret = ssl_parse_new_session_ticket( ssl );
2829 else
Paul Bakkera503a632013-08-14 13:48:06 +02002830#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002831 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002832 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Paul Bakker1961b702013-01-25 14:49:24 +01002834 case SSL_SERVER_FINISHED:
2835 ret = ssl_parse_finished( ssl );
2836 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
Paul Bakker1961b702013-01-25 14:49:24 +01002838 case SSL_FLUSH_BUFFERS:
2839 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2840 ssl->state = SSL_HANDSHAKE_WRAPUP;
2841 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Paul Bakker1961b702013-01-25 14:49:24 +01002843 case SSL_HANDSHAKE_WRAPUP:
2844 ssl_handshake_wrapup( ssl );
2845 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002846
Paul Bakker1961b702013-01-25 14:49:24 +01002847 default:
2848 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2849 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
2852 return( ret );
2853}
Paul Bakker9af723c2014-05-01 13:03:14 +02002854#endif /* POLARSSL_SSL_CLI_C */