blob: 53ed93766f8fbea0fdd4e9e11e25dd77ec1f0a93 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Paul Bakker68884e32013-01-07 18:20:04 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/debug.h"
31#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020033#if defined(POLARSSL_MEMORY_C)
34#include "polarssl/memory.h"
35#else
36#define polarssl_malloc malloc
37#define polarssl_free free
38#endif
39
Paul Bakker5121ce52009-01-03 21:22:43 +000040#include <stdlib.h>
41#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020042
Paul Bakkerfa6a6202013-10-28 18:48:30 +010043#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020044#include <basetsd.h>
45typedef UINT32 uint32_t;
46#else
47#include <inttypes.h>
48#endif
49
50#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakker0be444a2013-08-27 21:55:01 +020054#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010055static void ssl_write_hostname_ext( ssl_context *ssl,
56 unsigned char *buf,
57 size_t *olen )
58{
59 unsigned char *p = buf;
60
61 *olen = 0;
62
63 if ( ssl->hostname == NULL )
64 return;
65
66 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
67 ssl->hostname ) );
68
69 /*
70 * struct {
71 * NameType name_type;
72 * select (name_type) {
73 * case host_name: HostName;
74 * } name;
75 * } ServerName;
76 *
77 * enum {
78 * host_name(0), (255)
79 * } NameType;
80 *
81 * opaque HostName<1..2^16-1>;
82 *
83 * struct {
84 * ServerName server_name_list<1..2^16-1>
85 * } ServerNameList;
86 */
87 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
88 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
89
90 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
91 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
92
93 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
94 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
95
96 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
97 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
98 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
99
100 memcpy( p, ssl->hostname, ssl->hostname_len );
101
102 *olen = ssl->hostname_len + 9;
103}
Paul Bakker0be444a2013-08-27 21:55:01 +0200104#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100105
106static void ssl_write_renegotiation_ext( ssl_context *ssl,
107 unsigned char *buf,
108 size_t *olen )
109{
110 unsigned char *p = buf;
111
112 *olen = 0;
113
114 if( ssl->renegotiation != SSL_RENEGOTIATION )
115 return;
116
117 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
118
119 /*
120 * Secure renegotiation
121 */
122 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
123 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
124
125 *p++ = 0x00;
126 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
127 *p++ = ssl->verify_data_len & 0xFF;
128
129 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
130
131 *olen = 5 + ssl->verify_data_len;
132}
133
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200134#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100135static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
136 unsigned char *buf,
137 size_t *olen )
138{
139 unsigned char *p = buf;
Manuel Pégourié-Gonnard9c9812a2013-08-23 12:18:46 +0200140 unsigned char *sig_alg_list = buf + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100141 size_t sig_alg_len = 0;
142
143 *olen = 0;
144
145 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
146 return;
147
148 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
149
150 /*
151 * Prepare signature_algorithms extension (TLS 1.2)
152 */
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200153#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200154#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100155 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
156 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
157 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
158 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
159#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200160#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100161 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
162 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
163 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
164 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
165#endif
166#if defined(POLARSSL_SHA1_C)
167 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
168 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
169#endif
170#if defined(POLARSSL_MD5_C)
171 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
172 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
173#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200174#endif /* POLARSSL_RSA_C */
175#if defined(POLARSSL_ECDSA_C)
176#if defined(POLARSSL_SHA512_C)
177 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
178 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
179 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
180 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
181#endif
182#if defined(POLARSSL_SHA256_C)
183 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
184 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
185 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
186 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
187#endif
188#if defined(POLARSSL_SHA1_C)
189 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
190 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
191#endif
192#if defined(POLARSSL_MD5_C)
193 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
194 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
195#endif
196#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100197
198 /*
199 * enum {
200 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
201 * sha512(6), (255)
202 * } HashAlgorithm;
203 *
204 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
205 * SignatureAlgorithm;
206 *
207 * struct {
208 * HashAlgorithm hash;
209 * SignatureAlgorithm signature;
210 * } SignatureAndHashAlgorithm;
211 *
212 * SignatureAndHashAlgorithm
213 * supported_signature_algorithms<2..2^16-2>;
214 */
215 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
216 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
217
218 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
219 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
220
221 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
222 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
223
Paul Bakkerd3edc862013-03-20 16:07:17 +0100224 *olen = 6 + sig_alg_len;
225}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200226#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100227
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200228#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100229static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
230 unsigned char *buf,
231 size_t *olen )
232{
233 unsigned char *p = buf;
234 unsigned char elliptic_curve_list[20];
235 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnarda79d1232013-09-17 15:42:35 +0200236 const ecp_curve_info *curve;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200237 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100238
239 *olen = 0;
240
241 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
242
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200243 for( curve = ecp_curve_list();
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200244 curve->grp_id != POLARSSL_ECP_DP_NONE;
245 curve++ )
246 {
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200247 elliptic_curve_list[elliptic_curve_len++] = curve->tls_id >> 8;
248 elliptic_curve_list[elliptic_curve_len++] = curve->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200249 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200250
251 if( elliptic_curve_len == 0 )
252 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100253
254 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
255 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
256
257 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
258 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
259
260 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
261 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
262
263 memcpy( p, elliptic_curve_list, elliptic_curve_len );
264
265 *olen = 6 + elliptic_curve_len;
266}
267
268static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
269 unsigned char *buf,
270 size_t *olen )
271{
272 unsigned char *p = buf;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +0200273 ((void) ssl);
Paul Bakkerd3edc862013-03-20 16:07:17 +0100274
275 *olen = 0;
276
277 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
278
279 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
280 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
281
282 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100283 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200284
285 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100286 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
287
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200288 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100289}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200290#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100291
Paul Bakker05decb22013-08-15 13:33:48 +0200292#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200293static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
294 unsigned char *buf,
295 size_t *olen )
296{
297 unsigned char *p = buf;
298
299 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
300 *olen = 0;
301 return;
302 }
303
304 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
305
306 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
307 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
308
309 *p++ = 0x00;
310 *p++ = 1;
311
312 *p++ = ssl->mfl_code;
313
314 *olen = 5;
315}
Paul Bakker05decb22013-08-15 13:33:48 +0200316#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200317
Paul Bakker1f2bc622013-08-15 13:45:55 +0200318#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200319static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
320 unsigned char *buf, size_t *olen )
321{
322 unsigned char *p = buf;
323
324 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
325 {
326 *olen = 0;
327 return;
328 }
329
330 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
331
332 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
333 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
334
335 *p++ = 0x00;
336 *p++ = 0x00;
337
338 *olen = 4;
339}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200340#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200341
Paul Bakkera503a632013-08-14 13:48:06 +0200342#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200343static void ssl_write_session_ticket_ext( ssl_context *ssl,
344 unsigned char *buf, size_t *olen )
345{
346 unsigned char *p = buf;
347 size_t tlen = ssl->session_negotiate->ticket_len;
348
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200349 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
350 {
351 *olen = 0;
352 return;
353 }
354
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200355 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
356
357 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
358 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
359
360 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
361 *p++ = (unsigned char)( ( tlen ) & 0xFF );
362
363 *olen = 4;
364
365 if( ssl->session_negotiate->ticket == NULL ||
366 ssl->session_negotiate->ticket_len == 0 )
367 {
368 return;
369 }
370
371 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
372
373 memcpy( p, ssl->session_negotiate->ticket, tlen );
374
375 *olen += tlen;
376}
Paul Bakkera503a632013-08-14 13:48:06 +0200377#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200378
Paul Bakker5121ce52009-01-03 21:22:43 +0000379static int ssl_write_client_hello( ssl_context *ssl )
380{
Paul Bakker23986e52011-04-24 08:57:21 +0000381 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100382 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200384 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200385#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200387#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200388 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200389 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
392
Paul Bakkera9a028e2013-11-21 17:31:06 +0100393 if( ssl->f_rng == NULL )
394 {
395 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
396 return( POLARSSL_ERR_SSL_NO_RNG );
397 }
398
Paul Bakker48916f92012-09-16 19:57:18 +0000399 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
400 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000401 ssl->major_ver = ssl->min_major_ver;
402 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000403 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
Paul Bakker490ecc82011-10-06 13:04:09 +0000405 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
406 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200407 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
408 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000409 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411 /*
412 * 0 . 0 handshake type
413 * 1 . 3 handshake length
414 * 4 . 5 highest version supported
415 * 6 . 9 current UNIX time
416 * 10 . 37 random bytes
417 */
418 buf = ssl->out_msg;
419 p = buf + 4;
420
421 *p++ = (unsigned char) ssl->max_major_ver;
422 *p++ = (unsigned char) ssl->max_minor_ver;
423
424 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
425 buf[4], buf[5] ) );
426
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200427#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 t = time( NULL );
429 *p++ = (unsigned char)( t >> 24 );
430 *p++ = (unsigned char)( t >> 16 );
431 *p++ = (unsigned char)( t >> 8 );
432 *p++ = (unsigned char)( t );
433
434 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200435#else
436 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
437 return( ret );
438
439 p += 4;
440#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000441
Paul Bakkera3d195c2011-11-27 21:07:34 +0000442 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
443 return( ret );
444
445 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
Paul Bakker48916f92012-09-16 19:57:18 +0000447 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
450
451 /*
452 * 38 . 38 session id length
453 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000454 * 40+n . 41+n ciphersuitelist length
455 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000456 * .. . .. compression methods length
457 * .. . .. compression methods
458 * .. . .. extensions length
459 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 */
Paul Bakker48916f92012-09-16 19:57:18 +0000461 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
Paul Bakker0a597072012-09-25 21:55:46 +0000463 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
464 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200465 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200467 }
468
Paul Bakkera503a632013-08-14 13:48:06 +0200469#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200470 /*
471 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
472 * generate and include a Session ID in the TLS ClientHello."
473 */
474 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
475 ssl->session_negotiate->ticket != NULL &&
476 ssl->session_negotiate->ticket_len != 0 )
477 {
478 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
479
480 if( ret != 0 )
481 return( ret );
482
483 ssl->session_negotiate->length = n = 32;
484 }
Paul Bakkera503a632013-08-14 13:48:06 +0200485#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487 *p++ = (unsigned char) n;
488
489 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000490 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
493 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
494
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200495 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200496 n = 0;
497 q = p;
498
499 // Skip writing ciphersuite length for now
500 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
Paul Bakker48916f92012-09-16 19:57:18 +0000502 /*
503 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
504 */
505 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
506 {
507 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
508 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
Paul Bakker2fbefde2013-06-29 16:01:15 +0200509 n++;
Paul Bakker48916f92012-09-16 19:57:18 +0000510 }
511
Paul Bakker2fbefde2013-06-29 16:01:15 +0200512 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200514 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
515
516 if( ciphersuite_info == NULL )
517 continue;
518
519 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
520 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
521 continue;
522
Paul Bakkere3166ce2011-01-27 17:40:50 +0000523 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200524 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
Paul Bakker2fbefde2013-06-29 16:01:15 +0200526 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200527 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
528 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 }
530
Paul Bakker2fbefde2013-06-29 16:01:15 +0200531 *q++ = (unsigned char)( n >> 7 );
532 *q++ = (unsigned char)( n << 1 );
533
534 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
535
536
Paul Bakker2770fbd2012-07-03 13:30:23 +0000537#if defined(POLARSSL_ZLIB_SUPPORT)
538 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
539 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000540 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000541
542 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000543 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000544 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000545#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000547 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 *p++ = 1;
550 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000551#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Paul Bakkerd3edc862013-03-20 16:07:17 +0100553 // First write extensions, then the total length
554 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200555#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100556 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
557 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200558#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Paul Bakkerd3edc862013-03-20 16:07:17 +0100560 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
561 ext_len += olen;
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000562
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200563#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100564 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
565 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200566#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000567
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200568#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100569 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
570 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100571
Paul Bakkerd3edc862013-03-20 16:07:17 +0100572 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
573 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100574#endif
575
Paul Bakker05decb22013-08-15 13:33:48 +0200576#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200577 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
578 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200579#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200580
Paul Bakker1f2bc622013-08-15 13:45:55 +0200581#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200582 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
583 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200584#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200585
Paul Bakkera503a632013-08-14 13:48:06 +0200586#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200587 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
588 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200589#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200590
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000591 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
592 ext_len ) );
593
594 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
595 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
Paul Bakkerd3edc862013-03-20 16:07:17 +0100596 p += ext_len;
Paul Bakker41c83d32013-03-20 14:39:14 +0100597
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 ssl->out_msglen = p - buf;
599 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
600 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
601
602 ssl->state++;
603
604 if( ( ret = ssl_write_record( ssl ) ) != 0 )
605 {
606 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
607 return( ret );
608 }
609
610 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
611
612 return( 0 );
613}
614
Paul Bakker48916f92012-09-16 19:57:18 +0000615static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200616 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000617 size_t len )
618{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000619 int ret;
620
Paul Bakker48916f92012-09-16 19:57:18 +0000621 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
622 {
623 if( len != 1 || buf[0] != 0x0 )
624 {
625 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000626
627 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
628 return( ret );
629
Paul Bakker48916f92012-09-16 19:57:18 +0000630 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
631 }
632
633 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
634 }
635 else
636 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100637 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000638 if( len != 1 + ssl->verify_data_len * 2 ||
639 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100640 safer_memcmp( buf + 1,
641 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
642 safer_memcmp( buf + 1 + ssl->verify_data_len,
643 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000644 {
645 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000646
647 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
648 return( ret );
649
Paul Bakker48916f92012-09-16 19:57:18 +0000650 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
651 }
652 }
653
654 return( 0 );
655}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200656
Paul Bakker05decb22013-08-15 13:33:48 +0200657#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200658static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200659 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200660 size_t len )
661{
662 /*
663 * server should use the extension only if we did,
664 * and if so the server's value should match ours (and len is always 1)
665 */
666 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
667 len != 1 ||
668 buf[0] != ssl->mfl_code )
669 {
670 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
671 }
672
673 return( 0 );
674}
Paul Bakker05decb22013-08-15 13:33:48 +0200675#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000676
Paul Bakker1f2bc622013-08-15 13:45:55 +0200677#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200678static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
679 const unsigned char *buf,
680 size_t len )
681{
682 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
683 len != 0 )
684 {
685 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
686 }
687
688 ((void) buf);
689
690 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
691
692 return( 0 );
693}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200694#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200695
Paul Bakkera503a632013-08-14 13:48:06 +0200696#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200697static int ssl_parse_session_ticket_ext( ssl_context *ssl,
698 const unsigned char *buf,
699 size_t len )
700{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200701 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
702 len != 0 )
703 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200704 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200705 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200706
707 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +0200708
709 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200710
711 return( 0 );
712}
Paul Bakkera503a632013-08-14 13:48:06 +0200713#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200714
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200715#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200716static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
717 const unsigned char *buf,
718 size_t len )
719{
720 size_t list_size;
721 const unsigned char *p;
722
723 list_size = buf[0];
724 if( list_size + 1 != len )
725 {
726 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
727 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
728 }
729
730 p = buf + 2;
731 while( list_size > 0 )
732 {
733 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
734 p[0] == POLARSSL_ECP_PF_COMPRESSED )
735 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200736 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200737 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
738 return( 0 );
739 }
740
741 list_size--;
742 p++;
743 }
744
745 return( 0 );
746}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200747#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200748
Paul Bakker5121ce52009-01-03 21:22:43 +0000749static int ssl_parse_server_hello( ssl_context *ssl )
750{
Paul Bakker2770fbd2012-07-03 13:30:23 +0000751 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +0000752 size_t n;
Paul Bakker48916f92012-09-16 19:57:18 +0000753 size_t ext_len = 0;
754 unsigned char *buf, *ext;
755 int renegotiation_info_seen = 0;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000756 int handshake_failure = 0;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200757#if defined(POLARSSL_DEBUG_C)
758 uint32_t t;
759#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
761 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
762
763 /*
764 * 0 . 0 handshake type
765 * 1 . 3 handshake length
766 * 4 . 5 protocol version
767 * 6 . 9 UNIX time()
768 * 10 . 37 random bytes
769 */
770 buf = ssl->in_msg;
771
772 if( ( ret = ssl_read_record( ssl ) ) != 0 )
773 {
774 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
775 return( ret );
776 }
777
778 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
779 {
780 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000781 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000782 }
783
784 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
785 buf[4], buf[5] ) );
786
787 if( ssl->in_hslen < 42 ||
788 buf[0] != SSL_HS_SERVER_HELLO ||
789 buf[4] != SSL_MAJOR_VERSION_3 )
790 {
791 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000792 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000793 }
794
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000795 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +0000796 {
797 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000798 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000799 }
800
801 ssl->minor_ver = buf[5];
802
Paul Bakker1d29fb52012-09-28 13:28:45 +0000803 if( ssl->minor_ver < ssl->min_minor_ver )
804 {
805 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
806 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
807 buf[4], buf[5] ) );
808
809 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
810 SSL_ALERT_MSG_PROTOCOL_VERSION );
811
812 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
813 }
814
Paul Bakker1504af52012-02-11 16:17:43 +0000815#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200816 t = ( (uint32_t) buf[6] << 24 )
817 | ( (uint32_t) buf[7] << 16 )
818 | ( (uint32_t) buf[8] << 8 )
819 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +0200820 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +0000821#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000822
Paul Bakker48916f92012-09-16 19:57:18 +0000823 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
825 n = buf[38];
826
Paul Bakker5121ce52009-01-03 21:22:43 +0000827 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
828
Paul Bakker48916f92012-09-16 19:57:18 +0000829 if( n > 32 )
830 {
831 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
832 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
833 }
834
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 /*
836 * 38 . 38 session id length
837 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000838 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 * 41+n . 41+n chosen compression alg.
840 * 42+n . 43+n extensions length
841 * 44+n . 44+n+m extensions
842 */
Paul Bakker48916f92012-09-16 19:57:18 +0000843 if( ssl->in_hslen > 42 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000844 {
845 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +0000846 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000847
Paul Bakker48916f92012-09-16 19:57:18 +0000848 if( ( ext_len > 0 && ext_len < 4 ) ||
849 ssl->in_hslen != 44 + n + ext_len )
850 {
851 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
852 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
853 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000854 }
855
856 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +0000857 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Paul Bakker380da532012-04-18 16:10:25 +0000859 /*
860 * Initialize update checksum functions
861 */
Paul Bakker68884e32013-01-07 18:20:04 +0100862 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
Paul Bakker41c83d32013-03-20 14:39:14 +0100863 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker68884e32013-01-07 18:20:04 +0100864
865 if( ssl->transform_negotiate->ciphersuite_info == NULL )
866 {
867 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200868 ssl->ciphersuite_list[ssl->minor_ver][i] ) );
Paul Bakker68884e32013-01-07 18:20:04 +0100869 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
870 }
Paul Bakker380da532012-04-18 16:10:25 +0000871
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
873 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
874
875 /*
876 * Check if the session can be resumed
877 */
Paul Bakker0a597072012-09-25 21:55:46 +0000878 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
879 ssl->handshake->resume == 0 || n == 0 ||
Paul Bakker48916f92012-09-16 19:57:18 +0000880 ssl->session_negotiate->ciphersuite != i ||
881 ssl->session_negotiate->compression != comp ||
882 ssl->session_negotiate->length != n ||
883 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000884 {
885 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +0000886 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200887#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +0000888 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200889#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000890 ssl->session_negotiate->ciphersuite = i;
891 ssl->session_negotiate->compression = comp;
892 ssl->session_negotiate->length = n;
893 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 }
895 else
896 {
897 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000898
899 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
900 {
901 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
902 return( ret );
903 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 }
905
906 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +0000907 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Paul Bakkere3166ce2011-01-27 17:40:50 +0000909 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
911
912 i = 0;
913 while( 1 )
914 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200915 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000916 {
917 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000918 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 }
920
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200921 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
922 ssl->session_negotiate->ciphersuite )
923 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000924 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200925 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 }
927
Paul Bakker2770fbd2012-07-03 13:30:23 +0000928 if( comp != SSL_COMPRESS_NULL
929#if defined(POLARSSL_ZLIB_SUPPORT)
930 && comp != SSL_COMPRESS_DEFLATE
931#endif
932 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 {
934 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +0000935 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +0000936 }
Paul Bakker48916f92012-09-16 19:57:18 +0000937 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +0000938
Paul Bakker48916f92012-09-16 19:57:18 +0000939 ext = buf + 44 + n;
940
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200941 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
942
Paul Bakker48916f92012-09-16 19:57:18 +0000943 while( ext_len )
944 {
945 unsigned int ext_id = ( ( ext[0] << 8 )
946 | ( ext[1] ) );
947 unsigned int ext_size = ( ( ext[2] << 8 )
948 | ( ext[3] ) );
949
950 if( ext_size + 4 > ext_len )
951 {
952 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
953 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
954 }
955
956 switch( ext_id )
957 {
958 case TLS_EXT_RENEGOTIATION_INFO:
959 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
960 renegotiation_info_seen = 1;
961
962 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size ) ) != 0 )
963 return( ret );
964
965 break;
966
Paul Bakker05decb22013-08-15 13:33:48 +0200967#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200968 case TLS_EXT_MAX_FRAGMENT_LENGTH:
969 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
970
971 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
972 ext + 4, ext_size ) ) != 0 )
973 {
974 return( ret );
975 }
976
977 break;
Paul Bakker05decb22013-08-15 13:33:48 +0200978#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200979
Paul Bakker1f2bc622013-08-15 13:45:55 +0200980#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200981 case TLS_EXT_TRUNCATED_HMAC:
982 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
983
984 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
985 ext + 4, ext_size ) ) != 0 )
986 {
987 return( ret );
988 }
989
990 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200991#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200992
Paul Bakkera503a632013-08-14 13:48:06 +0200993#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200994 case TLS_EXT_SESSION_TICKET:
995 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
996
997 if( ( ret = ssl_parse_session_ticket_ext( ssl,
998 ext + 4, ext_size ) ) != 0 )
999 {
1000 return( ret );
1001 }
1002
1003 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001004#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001005
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001006#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001007 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1008 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1009
1010 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1011 ext + 4, ext_size ) ) != 0 )
1012 {
1013 return( ret );
1014 }
1015
1016 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001017#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001018
Paul Bakker48916f92012-09-16 19:57:18 +00001019 default:
1020 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1021 ext_id ) );
1022 }
1023
1024 ext_len -= 4 + ext_size;
1025 ext += 4 + ext_size;
1026
1027 if( ext_len > 0 && ext_len < 4 )
1028 {
1029 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1030 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1031 }
1032 }
1033
1034 /*
1035 * Renegotiation security checks
1036 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001037 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1038 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001039 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001040 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1041 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001042 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001043 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1044 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1045 renegotiation_info_seen == 0 )
1046 {
1047 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1048 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001049 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001050 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1051 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1052 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001053 {
1054 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001055 handshake_failure = 1;
1056 }
1057 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1058 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1059 renegotiation_info_seen == 1 )
1060 {
1061 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1062 handshake_failure = 1;
1063 }
1064
1065 if( handshake_failure == 1 )
1066 {
1067 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1068 return( ret );
1069
Paul Bakker48916f92012-09-16 19:57:18 +00001070 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1071 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
1073 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1074
1075 return( 0 );
1076}
1077
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001078#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1079 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001080static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1081 unsigned char *end )
1082{
1083 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1084
Paul Bakker29e1f122013-04-16 13:07:56 +02001085 /*
1086 * Ephemeral DH parameters:
1087 *
1088 * struct {
1089 * opaque dh_p<1..2^16-1>;
1090 * opaque dh_g<1..2^16-1>;
1091 * opaque dh_Ys<1..2^16-1>;
1092 * } ServerDHParams;
1093 */
1094 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1095 {
1096 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1097 return( ret );
1098 }
1099
1100 if( ssl->handshake->dhm_ctx.len < 64 ||
1101 ssl->handshake->dhm_ctx.len > 512 )
1102 {
1103 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1104 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1105 }
1106
1107 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1108 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1109 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001110
1111 return( ret );
1112}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001113#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1114 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001115
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001116#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001117 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1118 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001119static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1120 unsigned char **p,
1121 unsigned char *end )
1122{
1123 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1124
Paul Bakker29e1f122013-04-16 13:07:56 +02001125 /*
1126 * Ephemeral ECDH parameters:
1127 *
1128 * struct {
1129 * ECParameters curve_params;
1130 * ECPoint public;
1131 * } ServerECDHParams;
1132 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001133 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1134 (const unsigned char **) p, end ) ) != 0 )
1135 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001136 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001137 return( ret );
1138 }
1139
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001140 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
1141 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
1142
Paul Bakker29e1f122013-04-16 13:07:56 +02001143 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1144 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
1145 {
1146 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDH length)" ) );
1147 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1148 }
1149
1150 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
Paul Bakker29e1f122013-04-16 13:07:56 +02001151
1152 return( ret );
1153}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001154#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001155 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1156 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001157
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001158#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001159static int ssl_parse_server_psk_hint( ssl_context *ssl,
1160 unsigned char **p,
1161 unsigned char *end )
1162{
1163 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001164 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001165 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001166
1167 /*
1168 * PSK parameters:
1169 *
1170 * opaque psk_identity_hint<0..2^16-1>;
1171 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001172 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001173 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001174
1175 if( (*p) + len > end )
1176 {
1177 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1178 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1179 }
1180
1181 // TODO: Retrieve PSK identity hint and callback to app
1182 //
1183 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001184 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001185
1186 return( ret );
1187}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001188#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001189
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001190#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1191 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1192/*
1193 * Generate a pre-master secret and encrypt it with the server's RSA key
1194 */
1195static int ssl_write_encrypted_pms( ssl_context *ssl,
1196 size_t offset, size_t *olen,
1197 size_t pms_offset )
1198{
1199 int ret;
1200 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1201 unsigned char *p = ssl->handshake->premaster + pms_offset;
1202
1203 /*
1204 * Generate (part of) the pre-master as
1205 * struct {
1206 * ProtocolVersion client_version;
1207 * opaque random[46];
1208 * } PreMasterSecret;
1209 */
1210 p[0] = (unsigned char) ssl->max_major_ver;
1211 p[1] = (unsigned char) ssl->max_minor_ver;
1212
1213 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1214 {
1215 SSL_DEBUG_RET( 1, "f_rng", ret );
1216 return( ret );
1217 }
1218
1219 ssl->handshake->pmslen = 48;
1220
1221 /*
1222 * Now write it out, encrypted
1223 */
1224 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1225 POLARSSL_PK_RSA ) )
1226 {
1227 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1228 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1229 }
1230
1231 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1232 p, ssl->handshake->pmslen,
1233 ssl->out_msg + offset + len_bytes, olen,
1234 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1235 ssl->f_rng, ssl->p_rng ) ) != 0 )
1236 {
1237 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1238 return( ret );
1239 }
1240
1241#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1242 defined(POLARSSL_SSL_PROTO_TLS1_2)
1243 if( len_bytes == 2 )
1244 {
1245 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1246 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1247 *olen += 2;
1248 }
1249#endif
1250
1251 return( 0 );
1252}
1253#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1254 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001255
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001256#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001257#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001258 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1259 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001260static int ssl_parse_signature_algorithm( ssl_context *ssl,
1261 unsigned char **p,
1262 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001263 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001264 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001265{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001266 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001267 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001268 *pk_alg = POLARSSL_PK_NONE;
1269
1270 /* Only in TLS 1.2 */
1271 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1272 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001273 return( 0 );
1274 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001275
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001276 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001277 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1278
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001279 /*
1280 * Get hash algorithm
1281 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001282 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001283 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001284 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1285 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001286 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1287 }
1288
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001289 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001290 * Get signature algorithm
1291 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001292 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001293 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001294 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1295 "SignatureAlgorithm %d", (*p)[1] ) );
1296 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001297 }
1298
1299 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1300 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1301 *p += 2;
1302
1303 return( 0 );
1304}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001305#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001306 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1307 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001308#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001309
Paul Bakker41c83d32013-03-20 14:39:14 +01001310static int ssl_parse_server_key_exchange( ssl_context *ssl )
1311{
Paul Bakker23986e52011-04-24 08:57:21 +00001312 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001313 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001314 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001315#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001316 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1317 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001318 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001319 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001320 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001321 size_t hashlen;
1322 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001323#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
1325 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1326
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001327#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001328 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 {
1330 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1331 ssl->state++;
1332 return( 0 );
1333 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001334 ((void) p);
1335 ((void) end);
1336#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1339 {
1340 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1341 return( ret );
1342 }
1343
1344 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1345 {
1346 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001347 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 }
1349
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001350 /*
1351 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1352 * doesn't use a psk_identity_hint
1353 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1355 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001356 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1357 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001358 {
1359 ssl->record_read = 1;
1360 goto exit;
1361 }
1362
1363 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1364 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 }
1366
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001367 p = ssl->in_msg + 4;
1368 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001369 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001370
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001371#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1372 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1373 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1374 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1375 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1376 {
1377 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1378 {
1379 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1380 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1381 }
1382 } /* FALLTROUGH */
1383#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1384
1385#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1386 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1387 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1388 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1389 ; /* nothing more to do */
1390 else
1391#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1392 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1393#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1394 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1395 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1396 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001398 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001399 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001400 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001401 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1402 }
1403 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001404 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001405#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1406 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001407#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001408 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001409 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1410 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001411 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001412 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001413 {
1414 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1415 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001416 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1417 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1418 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001419 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001420 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001421#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001422 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001423 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01001424 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001425 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001426 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1427 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001428
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001429#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001430 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1431 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001432 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001433 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1434 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001435 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001436 params_len = p - ( ssl->in_msg + 4 );
1437
Paul Bakker29e1f122013-04-16 13:07:56 +02001438 /*
1439 * Handle the digitally-signed structure
1440 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001441#if defined(POLARSSL_SSL_PROTO_TLS1_2)
1442 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001443 {
Paul Bakker9659dae2013-08-28 16:21:34 +02001444 if( ssl_parse_signature_algorithm( ssl, &p, end,
1445 &md_alg, &pk_alg ) != 0 )
1446 {
1447 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1448 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1449 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001450
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001451 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00001452 {
1453 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1454 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1455 }
1456 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001457 else
Paul Bakker577e0062013-08-28 11:57:20 +02001458#endif
Paul Bakker9659dae2013-08-28 16:21:34 +02001459#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1460 defined(POLARSSL_SSL_PROTO_TLS1_1)
1461 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02001462 {
1463 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00001464
Paul Bakker9659dae2013-08-28 16:21:34 +02001465 /* Default hash for ECDSA is SHA-1 */
1466 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
1467 md_alg = POLARSSL_MD_SHA1;
1468 }
1469 else
1470#endif
1471 {
1472 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1473 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1474 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001475
1476 /*
1477 * Read signature
1478 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001479 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00001480 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001481
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001482 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01001483 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001484 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001485 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1486 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001487
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001488 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02001489
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001490 /*
1491 * Compute the hash that has been signed
1492 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001493#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
1494 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001495 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001496 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001497 md5_context md5;
1498 sha1_context sha1;
1499
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001500 hashlen = 36;
1501
Paul Bakker29e1f122013-04-16 13:07:56 +02001502 /*
1503 * digitally-signed struct {
1504 * opaque md5_hash[16];
1505 * opaque sha_hash[20];
1506 * };
1507 *
1508 * md5_hash
1509 * MD5(ClientHello.random + ServerHello.random
1510 * + ServerParams);
1511 * sha_hash
1512 * SHA(ClientHello.random + ServerHello.random
1513 * + ServerParams);
1514 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001515 md5_starts( &md5 );
1516 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001517 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001518 md5_finish( &md5, hash );
1519
1520 sha1_starts( &sha1 );
1521 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001522 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001523 sha1_finish( &sha1, hash + 16 );
Paul Bakker29e1f122013-04-16 13:07:56 +02001524 }
1525 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001526#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
1527 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02001528#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1529 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02001530 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001531 {
1532 md_context_t ctx;
1533
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02001534 /* Info from md_alg will be used instead */
1535 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02001536
1537 /*
1538 * digitally-signed struct {
1539 * opaque client_random[32];
1540 * opaque server_random[32];
1541 * ServerDHParams params;
1542 * };
1543 */
1544 if( ( ret = md_init_ctx( &ctx, md_info_from_type( md_alg ) ) ) != 0 )
1545 {
1546 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
1547 return( ret );
1548 }
1549
1550 md_starts( &ctx );
1551 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001552 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02001553 md_finish( &ctx, hash );
Paul Bakker04376b12013-08-16 14:45:26 +02001554 md_free_ctx( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02001555 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001556 else
Paul Bakker9659dae2013-08-28 16:21:34 +02001557#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1558 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001559 {
Paul Bakker577e0062013-08-28 11:57:20 +02001560 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1561 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1562 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001563
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02001564 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
1565 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02001566
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001567 /*
1568 * Verify signature
1569 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02001570 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001571 {
1572 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1573 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1574 }
1575
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001576 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
1577 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001578 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001579 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001580 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00001581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001582 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001583#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001584 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1585 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001586
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001587exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 ssl->state++;
1589
1590 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
1591
1592 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001593}
1594
1595static int ssl_parse_certificate_request( ssl_context *ssl )
1596{
1597 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01001598 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001599 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001600 size_t cert_type_len = 0, dn_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
1602 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
1603
1604 /*
1605 * 0 . 0 handshake type
1606 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01001607 * 4 . 4 cert type count
1608 * 5 .. m-1 cert types
1609 * m .. m+1 sig alg length (TLS 1.2 only)
1610 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 * n .. n+1 length of all DNs
1612 * n+2 .. n+3 length of DN 1
1613 * n+4 .. ... Distinguished Name #1
1614 * ... .. ... length of DN 2, etc.
1615 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001616 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001618 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1619 {
1620 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1621 return( ret );
1622 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001624 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1625 {
1626 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1627 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1628 }
1629
1630 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001631 }
1632
1633 ssl->client_auth = 0;
1634 ssl->state++;
1635
1636 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
1637 ssl->client_auth++;
1638
1639 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
1640 ssl->client_auth ? "a" : "no" ) );
1641
Paul Bakker926af752012-11-23 13:38:07 +01001642 if( ssl->client_auth == 0 )
1643 goto exit;
1644
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001645 ssl->record_read = 0;
1646
Paul Bakker926af752012-11-23 13:38:07 +01001647 // TODO: handshake_failure alert for an anonymous server to request
1648 // client authentication
1649
1650 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001651
Paul Bakker926af752012-11-23 13:38:07 +01001652 // Retrieve cert types
1653 //
1654 cert_type_len = buf[4];
1655 n = cert_type_len;
1656
1657 if( ssl->in_hslen < 6 + n )
1658 {
1659 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1660 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1661 }
1662
Paul Bakker73d44312013-05-22 13:56:26 +02001663 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01001664 while( cert_type_len > 0 )
1665 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001666#if defined(POLARSSL_RSA_C)
1667 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001668 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01001669 {
1670 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
1671 break;
1672 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001673 else
1674#endif
1675#if defined(POLARSSL_ECDSA_C)
1676 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001677 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001678 {
1679 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
1680 break;
1681 }
1682 else
1683#endif
1684 {
1685 ; /* Unsupported cert type, ignore */
1686 }
Paul Bakker926af752012-11-23 13:38:07 +01001687
1688 cert_type_len--;
1689 p++;
1690 }
1691
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001692#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001693 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
1694 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001695 /* Ignored, see comments about hash in write_certificate_verify */
1696 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001697 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
1698 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01001699
1700 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001701 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01001702 n += sig_alg_len;
1703
1704 if( ssl->in_hslen < 6 + n )
1705 {
1706 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1707 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1708 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001709 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001710#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01001711
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001712 /* Ignore certificate_authorities, we only have one cert anyway */
1713 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001714 dn_len = ( ( buf[5 + m + n] << 8 )
1715 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01001716
1717 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01001718 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01001719 {
1720 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
1721 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
1722 }
1723
1724exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00001725 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
1726
1727 return( 0 );
1728}
1729
1730static int ssl_parse_server_hello_done( ssl_context *ssl )
1731{
1732 int ret;
1733
1734 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
1735
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001736 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001737 {
1738 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1739 {
1740 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1741 return( ret );
1742 }
1743
1744 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1745 {
1746 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001747 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748 }
1749 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001750 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
1752 if( ssl->in_hslen != 4 ||
1753 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
1754 {
1755 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001756 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 }
1758
1759 ssl->state++;
1760
1761 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
1762
1763 return( 0 );
1764}
1765
1766static int ssl_write_client_key_exchange( ssl_context *ssl )
1767{
Paul Bakker23986e52011-04-24 08:57:21 +00001768 int ret;
1769 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01001770 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
1772 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
1773
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001774#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01001775 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 /*
1778 * DHM key exchange -- send G^X mod P
1779 */
Paul Bakker48916f92012-09-16 19:57:18 +00001780 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
1782 ssl->out_msg[4] = (unsigned char)( n >> 8 );
1783 ssl->out_msg[5] = (unsigned char)( n );
1784 i = 6;
1785
Paul Bakker29b64762012-09-25 09:36:44 +00001786 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001787 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 &ssl->out_msg[i], n,
1789 ssl->f_rng, ssl->p_rng );
1790 if( ret != 0 )
1791 {
1792 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
1793 return( ret );
1794 }
1795
Paul Bakker48916f92012-09-16 19:57:18 +00001796 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1797 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
Paul Bakker48916f92012-09-16 19:57:18 +00001799 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001800
Paul Bakker48916f92012-09-16 19:57:18 +00001801 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
1802 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02001803 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02001804 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 {
1806 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1807 return( ret );
1808 }
1809
Paul Bakker48916f92012-09-16 19:57:18 +00001810 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00001811 }
1812 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001813#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001814#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1815 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1816 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
1817 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01001818 {
1819 /*
1820 * ECDH key exchange -- send client public value
1821 */
1822 i = 4;
1823
1824 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
1825 &n,
1826 &ssl->out_msg[i], 1000,
1827 ssl->f_rng, ssl->p_rng );
1828 if( ret != 0 )
1829 {
1830 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
1831 return( ret );
1832 }
1833
1834 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
1835
1836 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
1837 &ssl->handshake->pmslen,
1838 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001839 POLARSSL_MPI_MAX_SIZE,
1840 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001841 {
1842 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1843 return( ret );
1844 }
1845
1846 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1847 }
1848 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001849#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1850 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001851#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001852 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001853 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001854 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1855 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001856 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001857 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001858 * opaque psk_identity<0..2^16-1>;
1859 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001860 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001861 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
1862
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001863 i = 4;
1864 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001865 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
1866 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001867
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001868 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
1869 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001870
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001871#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001872 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001873 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001874 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001875 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001876 else
1877#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001878#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1879 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1880 {
1881 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
1882 return( ret );
1883 }
1884 else
1885#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001886#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001887 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001888 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001889 /*
1890 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
1891 */
1892 n = ssl->handshake->dhm_ctx.len;
1893 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
1894 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001895
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001896 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02001897 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001898 &ssl->out_msg[i], n,
1899 ssl->f_rng, ssl->p_rng );
1900 if( ret != 0 )
1901 {
1902 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
1903 return( ret );
1904 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001905 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001906 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001907#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001908#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001909 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001910 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001911 /*
1912 * ClientECDiffieHellmanPublic public;
1913 */
1914 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
1915 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
1916 ssl->f_rng, ssl->p_rng );
1917 if( ret != 0 )
1918 {
1919 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
1920 return( ret );
1921 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001922
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02001923 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
1924 }
1925 else
1926#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1927 {
1928 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1929 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001930 }
1931
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001932 if( ( ret = ssl_psk_derive_premaster( ssl,
1933 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001934 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001935 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001936 return( ret );
1937 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001938 }
1939 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001940#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001941#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02001942 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001944 i = 4;
1945 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00001946 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 }
Paul Bakkered27a042013-04-18 22:46:23 +02001948 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001949#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02001950 {
1951 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001952 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkered27a042013-04-18 22:46:23 +02001953 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1954 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001955
Paul Bakkerff60ee62010-03-16 21:09:09 +00001956 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1957 {
1958 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1959 return( ret );
1960 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001961
1962 ssl->out_msglen = i + n;
1963 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1964 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
1965
1966 ssl->state++;
1967
1968 if( ( ret = ssl_write_record( ssl ) ) != 0 )
1969 {
1970 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
1971 return( ret );
1972 }
1973
1974 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
1975
1976 return( 0 );
1977}
1978
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001979#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1980 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001981 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1982 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001983static int ssl_write_certificate_verify( ssl_context *ssl )
1984{
Paul Bakkered27a042013-04-18 22:46:23 +02001985 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1986 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
1988 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1989
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001990 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001991 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001992 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001993 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02001994 {
1995 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
1996 ssl->state++;
1997 return( 0 );
1998 }
1999
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002000 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002001 return( ret );
2002}
2003#else
2004static int ssl_write_certificate_verify( ssl_context *ssl )
2005{
2006 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2007 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2008 size_t n = 0, offset = 0;
2009 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002010 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002011 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002012 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002013
2014 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2015
2016 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002017 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002018 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002019 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2020 {
2021 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2022 ssl->state++;
2023 return( 0 );
2024 }
2025
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002026 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 {
2028 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2029 ssl->state++;
2030 return( 0 );
2031 }
2032
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002033 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002035 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2036 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 }
2038
2039 /*
2040 * Make an RSA signature of the handshake digests
2041 */
Paul Bakker48916f92012-09-16 19:57:18 +00002042 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002044#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2045 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002046 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002047 {
Paul Bakker926af752012-11-23 13:38:07 +01002048 /*
2049 * digitally-signed struct {
2050 * opaque md5_hash[16];
2051 * opaque sha_hash[20];
2052 * };
2053 *
2054 * md5_hash
2055 * MD5(handshake_messages);
2056 *
2057 * sha_hash
2058 * SHA(handshake_messages);
2059 */
2060 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002061 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002062
2063 /*
2064 * For ECDSA, default hash is SHA-1 only
2065 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002066 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002067 {
2068 hash_start += 16;
2069 hashlen -= 16;
2070 md_alg = POLARSSL_MD_SHA1;
2071 }
Paul Bakker926af752012-11-23 13:38:07 +01002072 }
2073 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002074#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2075 POLARSSL_SSL_PROTO_TLS1_1 */
2076#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2077 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002078 {
2079 /*
2080 * digitally-signed struct {
2081 * opaque handshake_messages[handshake_messages_length];
2082 * };
2083 *
2084 * Taking shortcut here. We assume that the server always allows the
2085 * PRF Hash function and has sent it in the allowed signature
2086 * algorithms list received in the Certificate Request message.
2087 *
2088 * Until we encounter a server that does not, we will take this
2089 * shortcut.
2090 *
2091 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2092 * in order to satisfy 'weird' needs from the server side.
2093 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002094 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2095 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002096 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002097 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002098 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002099 }
2100 else
2101 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002102 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002103 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002104 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002105 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002106
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002107 /* Info from md_alg will be used instead */
2108 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002109 offset = 2;
2110 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002111 else
2112#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002113 {
2114 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002115 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002116 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002117
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002118 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002119 ssl->out_msg + 6 + offset, &n,
2120 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002121 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002122 SSL_DEBUG_RET( 1, "pk_sign", ret );
2123 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002124 }
Paul Bakker926af752012-11-23 13:38:07 +01002125
Paul Bakker1ef83d62012-04-11 12:09:53 +00002126 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2127 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002128
Paul Bakker1ef83d62012-04-11 12:09:53 +00002129 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2131 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2132
2133 ssl->state++;
2134
2135 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2136 {
2137 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2138 return( ret );
2139 }
2140
2141 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2142
Paul Bakkered27a042013-04-18 22:46:23 +02002143 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002144}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002145#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2146 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2147 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Paul Bakkera503a632013-08-14 13:48:06 +02002149#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002150static int ssl_parse_new_session_ticket( ssl_context *ssl )
2151{
2152 int ret;
2153 uint32_t lifetime;
2154 size_t ticket_len;
2155 unsigned char *ticket;
2156
2157 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2158
2159 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2160 {
2161 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2162 return( ret );
2163 }
2164
2165 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2166 {
2167 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2168 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2169 }
2170
2171 /*
2172 * struct {
2173 * uint32 ticket_lifetime_hint;
2174 * opaque ticket<0..2^16-1>;
2175 * } NewSessionTicket;
2176 *
2177 * 0 . 0 handshake message type
2178 * 1 . 3 handshake message length
2179 * 4 . 7 ticket_lifetime_hint
2180 * 8 . 9 ticket_len (n)
2181 * 10 . 9+n ticket content
2182 */
2183 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2184 ssl->in_hslen < 10 )
2185 {
2186 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2187 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2188 }
2189
2190 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2191 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2192
2193 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2194
2195 if( ticket_len + 10 != ssl->in_hslen )
2196 {
2197 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2198 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2199 }
2200
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002201 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2202
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002203 /* We're not waiting for a NewSessionTicket message any more */
2204 ssl->handshake->new_session_ticket = 0;
2205
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002206 /*
2207 * Zero-length ticket means the server changed his mind and doesn't want
2208 * to send a ticket after all, so just forget it
2209 */
2210 if( ticket_len == 0)
2211 return( 0 );
2212
2213 polarssl_free( ssl->session_negotiate->ticket );
2214 ssl->session_negotiate->ticket = NULL;
2215 ssl->session_negotiate->ticket_len = 0;
2216
2217 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2218 {
2219 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2220 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2221 }
2222
2223 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2224
2225 ssl->session_negotiate->ticket = ticket;
2226 ssl->session_negotiate->ticket_len = ticket_len;
2227 ssl->session_negotiate->ticket_lifetime = lifetime;
2228
2229 /*
2230 * RFC 5077 section 3.4:
2231 * "If the client receives a session ticket from the server, then it
2232 * discards any Session ID that was sent in the ServerHello."
2233 */
2234 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2235 ssl->session_negotiate->length = 0;
2236
2237 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2238
2239 return( 0 );
2240}
Paul Bakkera503a632013-08-14 13:48:06 +02002241#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002242
Paul Bakker5121ce52009-01-03 21:22:43 +00002243/*
Paul Bakker1961b702013-01-25 14:49:24 +01002244 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 */
Paul Bakker1961b702013-01-25 14:49:24 +01002246int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002247{
2248 int ret = 0;
2249
Paul Bakker1961b702013-01-25 14:49:24 +01002250 if( ssl->state == SSL_HANDSHAKE_OVER )
2251 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252
Paul Bakker1961b702013-01-25 14:49:24 +01002253 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2254
2255 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2256 return( ret );
2257
2258 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 {
Paul Bakker1961b702013-01-25 14:49:24 +01002260 case SSL_HELLO_REQUEST:
2261 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 break;
2263
Paul Bakker1961b702013-01-25 14:49:24 +01002264 /*
2265 * ==> ClientHello
2266 */
2267 case SSL_CLIENT_HELLO:
2268 ret = ssl_write_client_hello( ssl );
2269 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
Paul Bakker1961b702013-01-25 14:49:24 +01002271 /*
2272 * <== ServerHello
2273 * Certificate
2274 * ( ServerKeyExchange )
2275 * ( CertificateRequest )
2276 * ServerHelloDone
2277 */
2278 case SSL_SERVER_HELLO:
2279 ret = ssl_parse_server_hello( ssl );
2280 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Paul Bakker1961b702013-01-25 14:49:24 +01002282 case SSL_SERVER_CERTIFICATE:
2283 ret = ssl_parse_certificate( ssl );
2284 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
Paul Bakker1961b702013-01-25 14:49:24 +01002286 case SSL_SERVER_KEY_EXCHANGE:
2287 ret = ssl_parse_server_key_exchange( ssl );
2288 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002289
Paul Bakker1961b702013-01-25 14:49:24 +01002290 case SSL_CERTIFICATE_REQUEST:
2291 ret = ssl_parse_certificate_request( ssl );
2292 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
Paul Bakker1961b702013-01-25 14:49:24 +01002294 case SSL_SERVER_HELLO_DONE:
2295 ret = ssl_parse_server_hello_done( ssl );
2296 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
Paul Bakker1961b702013-01-25 14:49:24 +01002298 /*
2299 * ==> ( Certificate/Alert )
2300 * ClientKeyExchange
2301 * ( CertificateVerify )
2302 * ChangeCipherSpec
2303 * Finished
2304 */
2305 case SSL_CLIENT_CERTIFICATE:
2306 ret = ssl_write_certificate( ssl );
2307 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002308
Paul Bakker1961b702013-01-25 14:49:24 +01002309 case SSL_CLIENT_KEY_EXCHANGE:
2310 ret = ssl_write_client_key_exchange( ssl );
2311 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
Paul Bakker1961b702013-01-25 14:49:24 +01002313 case SSL_CERTIFICATE_VERIFY:
2314 ret = ssl_write_certificate_verify( ssl );
2315 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
Paul Bakker1961b702013-01-25 14:49:24 +01002317 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2318 ret = ssl_write_change_cipher_spec( ssl );
2319 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002320
Paul Bakker1961b702013-01-25 14:49:24 +01002321 case SSL_CLIENT_FINISHED:
2322 ret = ssl_write_finished( ssl );
2323 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
Paul Bakker1961b702013-01-25 14:49:24 +01002325 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002326 * <== ( NewSessionTicket )
2327 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002328 * Finished
2329 */
2330 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002331#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002332 if( ssl->handshake->new_session_ticket != 0 )
2333 ret = ssl_parse_new_session_ticket( ssl );
2334 else
Paul Bakkera503a632013-08-14 13:48:06 +02002335#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002336 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002337 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
Paul Bakker1961b702013-01-25 14:49:24 +01002339 case SSL_SERVER_FINISHED:
2340 ret = ssl_parse_finished( ssl );
2341 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002342
Paul Bakker1961b702013-01-25 14:49:24 +01002343 case SSL_FLUSH_BUFFERS:
2344 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2345 ssl->state = SSL_HANDSHAKE_WRAPUP;
2346 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002347
Paul Bakker1961b702013-01-25 14:49:24 +01002348 case SSL_HANDSHAKE_WRAPUP:
2349 ssl_handshake_wrapup( ssl );
2350 break;
Paul Bakker48916f92012-09-16 19:57:18 +00002351
Paul Bakker1961b702013-01-25 14:49:24 +01002352 default:
2353 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2354 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2355 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
2357 return( ret );
2358}
Paul Bakker5121ce52009-01-03 21:22:43 +00002359#endif