blob: e254f3a54b5950c084045100b5ae994bf6ef6065 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared 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/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker0be444a2013-08-27 21:55:01 +020038#include "polarssl/debug.h"
39#include "polarssl/ssl.h"
40
Paul Bakker6e339b52013-07-03 13:37:05 +020041#if defined(POLARSSL_MEMORY_C)
42#include "polarssl/memory.h"
43#else
44#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Paul Bakker5121ce52009-01-03 21:22:43 +000048#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000050#if defined _MSC_VER && !defined strcasecmp
51#define strcasecmp _stricmp
52#endif
53
Paul Bakker05decb22013-08-15 13:33:48 +020054#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020055/*
56 * Convert max_fragment_length codes to length.
57 * RFC 6066 says:
58 * enum{
59 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
60 * } MaxFragmentLength;
61 * and we add 0 -> extension unused
62 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020063static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020064{
65 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
66 512, /* SSL_MAX_FRAG_LEN_512 */
67 1024, /* SSL_MAX_FRAG_LEN_1024 */
68 2048, /* SSL_MAX_FRAG_LEN_2048 */
69 4096, /* SSL_MAX_FRAG_LEN_4096 */
70};
Paul Bakker05decb22013-08-15 13:33:48 +020071#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020072
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020073static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
74{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020075 ssl_session_free( dst );
76 memcpy( dst, src, sizeof( ssl_session ) );
77
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020079 if( src->peer_cert != NULL )
80 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020081 int ret;
82
Paul Bakkerc559c7a2013-09-18 14:13:26 +020083 if( ( dst->peer_cert = polarssl_malloc( sizeof(x509_crt) ) ) == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020084 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
85
Paul Bakkerb6b09562013-09-18 14:17:41 +020086 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020087
Paul Bakkerddf26b42013-09-18 13:46:23 +020088 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
89 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 {
91 polarssl_free( dst->peer_cert );
92 dst->peer_cert = NULL;
93 return( ret );
94 }
95 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020097
Paul Bakkera503a632013-08-14 13:48:06 +020098#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020099 if( src->ticket != NULL )
100 {
101 if( ( dst->ticket = polarssl_malloc( src->ticket_len ) ) == NULL )
102 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
103
104 memcpy( dst->ticket, src->ticket, src->ticket_len );
105 }
Paul Bakkera503a632013-08-14 13:48:06 +0200106#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200107
108 return( 0 );
109}
110
Paul Bakker05ef8352012-05-08 09:17:57 +0000111#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
112int (*ssl_hw_record_init)(ssl_context *ssl,
113 const unsigned char *key_enc, const unsigned char *key_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100114 size_t keylen,
Paul Bakker05ef8352012-05-08 09:17:57 +0000115 const unsigned char *iv_enc, const unsigned char *iv_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100116 size_t ivlen,
117 const unsigned char *mac_enc, const unsigned char *mac_dec,
118 size_t maclen) = NULL;
119int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000120int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
121int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
122int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
123int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
124#endif
125
Paul Bakker5121ce52009-01-03 21:22:43 +0000126/*
127 * Key material generation
128 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200129#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200130static int ssl3_prf( const unsigned char *secret, size_t slen,
131 const char *label,
132 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000133 unsigned char *dstbuf, size_t dlen )
134{
135 size_t i;
136 md5_context md5;
137 sha1_context sha1;
138 unsigned char padding[16];
139 unsigned char sha1sum[20];
140 ((void)label);
141
142 /*
143 * SSLv3:
144 * block =
145 * MD5( secret + SHA1( 'A' + secret + random ) ) +
146 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
147 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
148 * ...
149 */
150 for( i = 0; i < dlen / 16; i++ )
151 {
152 memset( padding, 'A' + i, 1 + i );
153
154 sha1_starts( &sha1 );
155 sha1_update( &sha1, padding, 1 + i );
156 sha1_update( &sha1, secret, slen );
157 sha1_update( &sha1, random, rlen );
158 sha1_finish( &sha1, sha1sum );
159
160 md5_starts( &md5 );
161 md5_update( &md5, secret, slen );
162 md5_update( &md5, sha1sum, 20 );
163 md5_finish( &md5, dstbuf + i * 16 );
164 }
165
166 memset( &md5, 0, sizeof( md5 ) );
167 memset( &sha1, 0, sizeof( sha1 ) );
168
169 memset( padding, 0, sizeof( padding ) );
170 memset( sha1sum, 0, sizeof( sha1sum ) );
171
172 return( 0 );
173}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200174#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000175
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200176#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200177static int tls1_prf( const unsigned char *secret, size_t slen,
178 const char *label,
179 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000180 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000181{
Paul Bakker23986e52011-04-24 08:57:21 +0000182 size_t nb, hs;
183 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200184 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 unsigned char tmp[128];
186 unsigned char h_i[20];
187
188 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000189 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191 hs = ( slen + 1 ) / 2;
192 S1 = secret;
193 S2 = secret + slen - hs;
194
195 nb = strlen( label );
196 memcpy( tmp + 20, label, nb );
197 memcpy( tmp + 20 + nb, random, rlen );
198 nb += rlen;
199
200 /*
201 * First compute P_md5(secret,label+random)[0..dlen]
202 */
203 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
204
205 for( i = 0; i < dlen; i += 16 )
206 {
207 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
208 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
209
210 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
211
212 for( j = 0; j < k; j++ )
213 dstbuf[i + j] = h_i[j];
214 }
215
216 /*
217 * XOR out with P_sha1(secret,label+random)[0..dlen]
218 */
219 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
220
221 for( i = 0; i < dlen; i += 20 )
222 {
223 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
224 sha1_hmac( S2, hs, tmp, 20, tmp );
225
226 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
227
228 for( j = 0; j < k; j++ )
229 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
230 }
231
232 memset( tmp, 0, sizeof( tmp ) );
233 memset( h_i, 0, sizeof( h_i ) );
234
235 return( 0 );
236}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200237#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200239#if defined(POLARSSL_SSL_PROTO_TLS1_2)
240#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200241static int tls_prf_sha256( const unsigned char *secret, size_t slen,
242 const char *label,
243 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000244 unsigned char *dstbuf, size_t dlen )
245{
246 size_t nb;
247 size_t i, j, k;
248 unsigned char tmp[128];
249 unsigned char h_i[32];
250
251 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
252 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
253
254 nb = strlen( label );
255 memcpy( tmp + 32, label, nb );
256 memcpy( tmp + 32 + nb, random, rlen );
257 nb += rlen;
258
259 /*
260 * Compute P_<hash>(secret, label + random)[0..dlen]
261 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200262 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000263
264 for( i = 0; i < dlen; i += 32 )
265 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200266 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
267 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000268
269 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
270
271 for( j = 0; j < k; j++ )
272 dstbuf[i + j] = h_i[j];
273 }
274
275 memset( tmp, 0, sizeof( tmp ) );
276 memset( h_i, 0, sizeof( h_i ) );
277
278 return( 0 );
279}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200280#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000281
Paul Bakker9e36f042013-06-30 14:34:05 +0200282#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200283static int tls_prf_sha384( const unsigned char *secret, size_t slen,
284 const char *label,
285 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000286 unsigned char *dstbuf, size_t dlen )
287{
288 size_t nb;
289 size_t i, j, k;
290 unsigned char tmp[128];
291 unsigned char h_i[48];
292
293 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
294 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
295
296 nb = strlen( label );
297 memcpy( tmp + 48, label, nb );
298 memcpy( tmp + 48 + nb, random, rlen );
299 nb += rlen;
300
301 /*
302 * Compute P_<hash>(secret, label + random)[0..dlen]
303 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200304 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000305
306 for( i = 0; i < dlen; i += 48 )
307 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200308 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
309 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000310
311 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
312
313 for( j = 0; j < k; j++ )
314 dstbuf[i + j] = h_i[j];
315 }
316
317 memset( tmp, 0, sizeof( tmp ) );
318 memset( h_i, 0, sizeof( h_i ) );
319
320 return( 0 );
321}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200322#endif /* POLARSSL_SHA512_C */
323#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000324
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200325static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200326
327#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
328 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200329static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200330#endif
Paul Bakker380da532012-04-18 16:10:25 +0000331
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200332#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000333static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000334static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200335#endif
336
337#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
338static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200340#endif
341
342#if defined(POLARSSL_SSL_PROTO_TLS1_2)
343#if defined(POLARSSL_SHA256_C)
344static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
345static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000346static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100348
Paul Bakker9e36f042013-06-30 14:34:05 +0200349#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200350static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100351static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000352static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100353#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200354#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +0000355
Paul Bakker5121ce52009-01-03 21:22:43 +0000356int ssl_derive_keys( ssl_context *ssl )
357{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200358 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 unsigned char keyblk[256];
361 unsigned char *key1;
362 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100363 unsigned char *mac_enc;
364 unsigned char *mac_dec;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000365 unsigned int iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100366 const cipher_info_t *cipher_info;
367 const md_info_t *md_info;
368
Paul Bakker48916f92012-09-16 19:57:18 +0000369 ssl_session *session = ssl->session_negotiate;
370 ssl_transform *transform = ssl->transform_negotiate;
371 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000372
373 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
374
Paul Bakker68884e32013-01-07 18:20:04 +0100375 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
376 if( cipher_info == NULL )
377 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200378 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100379 transform->ciphersuite_info->cipher ) );
380 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
381 }
382
383 md_info = md_info_from_type( transform->ciphersuite_info->mac );
384 if( md_info == NULL )
385 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200386 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100387 transform->ciphersuite_info->mac ) );
388 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
389 }
390
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000392 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000393 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200394#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000395 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000396 {
Paul Bakker48916f92012-09-16 19:57:18 +0000397 handshake->tls_prf = ssl3_prf;
398 handshake->calc_verify = ssl_calc_verify_ssl;
399 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000400 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200401 else
402#endif
403#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
404 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000405 {
Paul Bakker48916f92012-09-16 19:57:18 +0000406 handshake->tls_prf = tls1_prf;
407 handshake->calc_verify = ssl_calc_verify_tls;
408 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000409 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200410 else
411#endif
412#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200413#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
415 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = tls_prf_sha384;
418 handshake->calc_verify = ssl_calc_verify_tls_sha384;
419 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000421 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200422#endif
423#if defined(POLARSSL_SHA256_C)
424 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls_prf_sha256;
427 handshake->calc_verify = ssl_calc_verify_tls_sha256;
428 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200433 {
434 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200435 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200436 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000437
438 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 * SSLv3:
440 * master =
441 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
442 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
443 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200444 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200445 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 * master = PRF( premaster, "master secret", randbytes )[0..47]
447 */
Paul Bakker0a597072012-09-25 21:55:46 +0000448 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 {
Paul Bakker48916f92012-09-16 19:57:18 +0000450 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
451 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
Paul Bakker48916f92012-09-16 19:57:18 +0000453 handshake->tls_prf( handshake->premaster, handshake->pmslen,
454 "master secret",
455 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
Paul Bakker48916f92012-09-16 19:57:18 +0000457 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 }
459 else
460 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
461
462 /*
463 * Swap the client and server random values.
464 */
Paul Bakker48916f92012-09-16 19:57:18 +0000465 memcpy( tmp, handshake->randbytes, 64 );
466 memcpy( handshake->randbytes, tmp + 32, 32 );
467 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 memset( tmp, 0, sizeof( tmp ) );
469
470 /*
471 * SSLv3:
472 * key block =
473 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
474 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
475 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
476 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
477 * ...
478 *
479 * TLSv1:
480 * key block = PRF( master, "key expansion", randbytes )
481 */
Paul Bakker48916f92012-09-16 19:57:18 +0000482 handshake->tls_prf( session->master, 48, "key expansion",
483 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Paul Bakker48916f92012-09-16 19:57:18 +0000485 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
486 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
487 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
488 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
490
Paul Bakker48916f92012-09-16 19:57:18 +0000491 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493 /*
494 * Determine the appropriate key, IV and MAC length.
495 */
Paul Bakker68884e32013-01-07 18:20:04 +0100496
497 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 {
Paul Bakker68884e32013-01-07 18:20:04 +0100499 transform->keylen = cipher_info->key_length;
500 transform->keylen /= 8;
501 transform->minlen = 1;
502 transform->ivlen = 12;
503 transform->fixed_ivlen = 4;
504 transform->maclen = 0;
505 }
506 else
507 {
508 if( md_info->type != POLARSSL_MD_NONE )
509 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200510 int ret;
511
Paul Bakker61d113b2013-07-04 11:51:43 +0200512 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
513 {
514 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
515 return( ret );
516 }
517
518 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
519 {
520 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
521 return( ret );
522 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
Paul Bakker68884e32013-01-07 18:20:04 +0100524 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200525
Paul Bakker1f2bc622013-08-15 13:45:55 +0200526#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200527 /*
528 * If HMAC is to be truncated, we shall keep the leftmost bytes,
529 * (rfc 6066 page 13 or rfc 2104 section 4),
530 * so we only need to adjust the length here.
531 */
532 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
533 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200534#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100535 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
Paul Bakker68884e32013-01-07 18:20:04 +0100537 transform->keylen = cipher_info->key_length;
538 transform->keylen /= 8;
539 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Paul Bakker68884e32013-01-07 18:20:04 +0100541 transform->minlen = transform->keylen;
542 if( transform->minlen < transform->maclen )
543 {
544 if( cipher_info->mode == POLARSSL_MODE_STREAM )
545 transform->minlen = transform->maclen;
546 else
547 transform->minlen += transform->keylen;
548 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 }
550
551 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000552 transform->keylen, transform->minlen, transform->ivlen,
553 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555 /*
556 * Finally setup the cipher contexts, IVs and MAC secrets.
557 */
558 if( ssl->endpoint == SSL_IS_CLIENT )
559 {
Paul Bakker48916f92012-09-16 19:57:18 +0000560 key1 = keyblk + transform->maclen * 2;
561 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
Paul Bakker68884e32013-01-07 18:20:04 +0100563 mac_enc = keyblk;
564 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000566 /*
567 * This is not used in TLS v1.1.
568 */
Paul Bakker48916f92012-09-16 19:57:18 +0000569 iv_copy_len = ( transform->fixed_ivlen ) ?
570 transform->fixed_ivlen : transform->ivlen;
571 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
572 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000573 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 }
575 else
576 {
Paul Bakker48916f92012-09-16 19:57:18 +0000577 key1 = keyblk + transform->maclen * 2 + transform->keylen;
578 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
Paul Bakker68884e32013-01-07 18:20:04 +0100580 mac_enc = keyblk + transform->maclen;
581 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000583 /*
584 * This is not used in TLS v1.1.
585 */
Paul Bakker48916f92012-09-16 19:57:18 +0000586 iv_copy_len = ( transform->fixed_ivlen ) ?
587 transform->fixed_ivlen : transform->ivlen;
588 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
589 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000590 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 }
592
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200593#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100594 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
595 {
596 memcpy( transform->mac_enc, mac_enc, transform->maclen );
597 memcpy( transform->mac_dec, mac_dec, transform->maclen );
598 }
599 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200600#endif
601#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
602 defined(POLARSSL_SSL_PROTO_TLS1_2)
603 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100604 {
605 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
606 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
607 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200608 else
609#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200610 {
611 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200612 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200613 }
Paul Bakker68884e32013-01-07 18:20:04 +0100614
Paul Bakker05ef8352012-05-08 09:17:57 +0000615#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
616 if( ssl_hw_record_init != NULL)
617 {
618 int ret = 0;
619
620 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
621
Paul Bakker07eb38b2012-12-19 14:42:06 +0100622 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
623 transform->iv_enc, transform->iv_dec,
624 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100625 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100626 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000627 {
628 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
629 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
630 }
631 }
632#endif
633
Paul Bakker68884e32013-01-07 18:20:04 +0100634 switch( cipher_info->type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 {
Paul Bakker68884e32013-01-07 18:20:04 +0100636 case POLARSSL_CIPHER_ARC4_128:
Paul Bakker68884e32013-01-07 18:20:04 +0100637 case POLARSSL_CIPHER_DES_EDE3_CBC:
Paul Bakkercca5b812013-08-31 17:40:26 +0200638 case POLARSSL_CIPHER_CAMELLIA_128_CBC:
639 case POLARSSL_CIPHER_CAMELLIA_256_CBC:
Paul Bakker68884e32013-01-07 18:20:04 +0100640 case POLARSSL_CIPHER_AES_128_CBC:
641 case POLARSSL_CIPHER_AES_256_CBC:
Paul Bakkercca5b812013-08-31 17:40:26 +0200642 case POLARSSL_CIPHER_DES_CBC:
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +0200643 case POLARSSL_CIPHER_AES_128_GCM:
644 case POLARSSL_CIPHER_AES_256_GCM:
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200645 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
646 cipher_info ) ) != 0 )
647 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200648 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200649 return( ret );
650 }
651
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200652 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
653 cipher_info ) ) != 0 )
654 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200655 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200656 return( ret );
657 }
658
Paul Bakker45125bc2013-09-04 16:47:11 +0200659 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
660 cipher_info->key_length,
661 POLARSSL_ENCRYPT ) ) != 0 )
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200662 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200663 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
664 return( ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200665 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200666
Paul Bakker45125bc2013-09-04 16:47:11 +0200667 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
668 cipher_info->key_length,
669 POLARSSL_DECRYPT ) ) != 0 )
670 {
671 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
672 return( ret );
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200673 }
674
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200675#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200676 if( cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200677 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200678 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
679 POLARSSL_PADDING_NONE ) ) != 0 )
680 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200681 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200682 return( ret );
683 }
684
685 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
686 POLARSSL_PADDING_NONE ) ) != 0 )
687 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200688 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200689 return( ret );
690 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200691 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200692#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
Paul Bakker68884e32013-01-07 18:20:04 +0100695 case POLARSSL_CIPHER_NULL:
696 break;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000697
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000699 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 }
701
702 memset( keyblk, 0, sizeof( keyblk ) );
703
Paul Bakker2770fbd2012-07-03 13:30:23 +0000704#if defined(POLARSSL_ZLIB_SUPPORT)
705 // Initialize compression
706 //
Paul Bakker48916f92012-09-16 19:57:18 +0000707 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000708 {
709 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
710
Paul Bakker48916f92012-09-16 19:57:18 +0000711 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
712 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000713
Paul Bakker48916f92012-09-16 19:57:18 +0000714 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
715 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000716 {
717 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
718 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
719 }
720 }
721#endif /* POLARSSL_ZLIB_SUPPORT */
722
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
724
725 return( 0 );
726}
727
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200728#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000729void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000730{
731 md5_context md5;
732 sha1_context sha1;
733 unsigned char pad_1[48];
734 unsigned char pad_2[48];
735
Paul Bakker380da532012-04-18 16:10:25 +0000736 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
Paul Bakker48916f92012-09-16 19:57:18 +0000738 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
739 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
Paul Bakker380da532012-04-18 16:10:25 +0000741 memset( pad_1, 0x36, 48 );
742 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
Paul Bakker48916f92012-09-16 19:57:18 +0000744 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000745 md5_update( &md5, pad_1, 48 );
746 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
Paul Bakker380da532012-04-18 16:10:25 +0000748 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000749 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000750 md5_update( &md5, pad_2, 48 );
751 md5_update( &md5, hash, 16 );
752 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
Paul Bakker48916f92012-09-16 19:57:18 +0000754 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000755 sha1_update( &sha1, pad_1, 40 );
756 sha1_finish( &sha1, hash + 16 );
757
758 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000759 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000760 sha1_update( &sha1, pad_2, 40 );
761 sha1_update( &sha1, hash + 16, 20 );
762 sha1_finish( &sha1, hash + 16 );
763
764 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
765 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
766
767 return;
768}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200769#endif
Paul Bakker380da532012-04-18 16:10:25 +0000770
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200771#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000772void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
773{
774 md5_context md5;
775 sha1_context sha1;
776
777 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
778
Paul Bakker48916f92012-09-16 19:57:18 +0000779 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
780 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000781
Paul Bakker48916f92012-09-16 19:57:18 +0000782 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000783 sha1_finish( &sha1, hash + 16 );
784
785 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
786 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
787
788 return;
789}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200790#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000791
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200792#if defined(POLARSSL_SSL_PROTO_TLS1_2)
793#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000794void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
795{
Paul Bakker9e36f042013-06-30 14:34:05 +0200796 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000797
798 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
799
Paul Bakker9e36f042013-06-30 14:34:05 +0200800 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
801 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000802
803 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
804 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
805
806 return;
807}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200808#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000809
Paul Bakker9e36f042013-06-30 14:34:05 +0200810#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000811void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
812{
Paul Bakker9e36f042013-06-30 14:34:05 +0200813 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000814
815 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
816
Paul Bakker9e36f042013-06-30 14:34:05 +0200817 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
818 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000819
Paul Bakkerca4ab492012-04-18 14:23:57 +0000820 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
822
823 return;
824}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200825#endif /* POLARSSL_SHA512_C */
826#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000827
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200828#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200829 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) || \
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200830 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
831 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
832int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
833{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200834 unsigned char *p = ssl->handshake->premaster;
835 unsigned char *end = p + sizeof( ssl->handshake->premaster );
836
837 /*
838 * PMS = struct {
839 * opaque other_secret<0..2^16-1>;
840 * opaque psk<0..2^16-1>;
841 * };
842 * with "other_secret" depending on the particular key exchange
843 */
844#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
845 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
846 {
847 if( end - p < 2 + (int) ssl->psk_len )
848 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
849
850 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
851 *(p++) = (unsigned char)( ssl->psk_len );
852 p += ssl->psk_len;
853 }
854 else
855#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200856#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
857 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
858 {
859 /*
860 * other_secret already set by the ClientKeyExchange message,
861 * and is 48 bytes long
862 */
863 *p++ = 0;
864 *p++ = 48;
865 p += 48;
866 }
867 else
868#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200869#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
870 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
871 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200872 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200873 size_t len = ssl->handshake->dhm_ctx.len;
874
875 if( end - p < 2 + (int) len )
876 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
877
878 *(p++) = (unsigned char)( len >> 8 );
879 *(p++) = (unsigned char)( len );
880 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
881 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
882 {
883 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
884 return( ret );
885 }
886 p += len;
887
888 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
889 }
890 else
891#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
892#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
893 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
894 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200895 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200896 size_t zlen;
897
898 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
899 p + 2, end - (p + 2),
900 ssl->f_rng, ssl->p_rng ) ) != 0 )
901 {
902 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
903 return( ret );
904 }
905
906 *(p++) = (unsigned char)( zlen >> 8 );
907 *(p++) = (unsigned char)( zlen );
908 p += zlen;
909
910 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
911 }
912 else
913#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
914 {
915 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
916 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
917 }
918
919 /* opaque psk<0..2^16-1>; */
920 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
921 *(p++) = (unsigned char)( ssl->psk_len );
922 memcpy( p, ssl->psk, ssl->psk_len );
923 p += ssl->psk_len;
924
925 ssl->handshake->pmslen = p - ssl->handshake->premaster;
926
927 return( 0 );
928}
929#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200930 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED ||
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200931 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
932 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
933
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200934#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000935/*
936 * SSLv3.0 MAC functions
937 */
Paul Bakker68884e32013-01-07 18:20:04 +0100938static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
939 unsigned char *buf, size_t len,
940 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000941{
942 unsigned char header[11];
943 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100944 int padlen = 0;
945 int md_size = md_get_size( md_ctx->md_info );
946 int md_type = md_get_type( md_ctx->md_info );
947
948 if( md_type == POLARSSL_MD_MD5 )
949 padlen = 48;
950 else if( md_type == POLARSSL_MD_SHA1 )
951 padlen = 40;
952 else if( md_type == POLARSSL_MD_SHA256 )
953 padlen = 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
955 memcpy( header, ctr, 8 );
956 header[ 8] = (unsigned char) type;
957 header[ 9] = (unsigned char)( len >> 8 );
958 header[10] = (unsigned char)( len );
959
Paul Bakker68884e32013-01-07 18:20:04 +0100960 memset( padding, 0x36, padlen );
961 md_starts( md_ctx );
962 md_update( md_ctx, secret, md_size );
963 md_update( md_ctx, padding, padlen );
964 md_update( md_ctx, header, 11 );
965 md_update( md_ctx, buf, len );
966 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Paul Bakker68884e32013-01-07 18:20:04 +0100968 memset( padding, 0x5C, padlen );
969 md_starts( md_ctx );
970 md_update( md_ctx, secret, md_size );
971 md_update( md_ctx, padding, padlen );
972 md_update( md_ctx, buf + len, md_size );
973 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000974}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200975#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000976
Paul Bakker5121ce52009-01-03 21:22:43 +0000977/*
978 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200979 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000980static int ssl_encrypt_buf( ssl_context *ssl )
981{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200982 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
984 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
985
986 /*
987 * Add MAC then encrypt
988 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200989#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
991 {
Paul Bakker68884e32013-01-07 18:20:04 +0100992 ssl_mac( &ssl->transform_out->md_ctx_enc,
993 ssl->transform_out->mac_enc,
994 ssl->out_msg, ssl->out_msglen,
995 ssl->out_ctr, ssl->out_msgtype );
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 }
997 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200998#endif
999#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1000 defined(POLARSSL_SSL_PROTO_TLS1_2)
1001 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 {
Paul Bakker68884e32013-01-07 18:20:04 +01001003 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1004 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1005 ssl->out_msg, ssl->out_msglen );
1006 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1007 ssl->out_msg + ssl->out_msglen );
1008 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001010 else
1011#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001012 {
1013 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001014 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001015 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
1017 SSL_DEBUG_BUF( 4, "computed mac",
Paul Bakker48916f92012-09-16 19:57:18 +00001018 ssl->out_msg + ssl->out_msglen, ssl->transform_out->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001019
Paul Bakker48916f92012-09-16 19:57:18 +00001020 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Paul Bakker68884e32013-01-07 18:20:04 +01001022#if defined(POLARSSL_CIPHER_NULL_CIPHER)
1023 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
1024 {
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001025 ; /* Nothing to do */
Paul Bakker68884e32013-01-07 18:20:04 +01001026 }
1027 else
1028#endif /* POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001029#if defined(POLARSSL_ARC4_C)
Paul Bakker68884e32013-01-07 18:20:04 +01001030 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001032 int ret;
1033 size_t olen = 0;
1034
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1036 "including %d bytes of padding",
1037 ssl->out_msglen, 0 ) );
1038
1039 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1040 ssl->out_msg, ssl->out_msglen );
1041
Paul Bakker45125bc2013-09-04 16:47:11 +02001042 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001043 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001044 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1045 return( ret );
1046 }
1047
1048 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1049 ssl->transform_out->iv_enc,
1050 ssl->transform_out->ivlen ) ) != 0 )
1051 {
1052 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001053 return( ret );
1054 }
1055
1056 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1057 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1058 &olen ) ) != 0 )
1059 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001060 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001061 return( ret );
1062 }
1063
1064 if( ssl->out_msglen != olen )
1065 {
1066 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1067 ssl->out_msglen, olen ) );
1068 // TODO Real error number
1069 return( -1 );
1070 }
1071
1072 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001073 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001074 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001075 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001076 return( ret );
1077 }
1078
1079 if( 0 != olen )
1080 {
1081 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1082 0, olen ) );
1083 // TODO Real error number
1084 return( -1 );
1085 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 }
Paul Bakker68884e32013-01-07 18:20:04 +01001087 else
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001088#endif /* POLARSSL_ARC4_C */
Paul Bakker68884e32013-01-07 18:20:04 +01001089#if defined(POLARSSL_GCM_C)
1090 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
1091 ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001092 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001093 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001094 unsigned char *enc_msg;
1095 unsigned char add_data[13];
1096 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1097
Paul Bakkerca4ab492012-04-18 14:23:57 +00001098 enc_msglen = ssl->out_msglen;
1099
1100 memcpy( add_data, ssl->out_ctr, 8 );
1101 add_data[8] = ssl->out_msgtype;
1102 add_data[9] = ssl->major_ver;
1103 add_data[10] = ssl->minor_ver;
1104 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1105 add_data[12] = ssl->out_msglen & 0xFF;
1106
1107 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1108 add_data, 13 );
1109
Paul Bakker68884e32013-01-07 18:20:04 +01001110 /*
1111 * Generate IV
1112 */
1113 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001114 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001115 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1116 if( ret != 0 )
1117 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001118
Paul Bakker68884e32013-01-07 18:20:04 +01001119 memcpy( ssl->out_iv,
1120 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1121 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001122
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001123 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1124 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1125
Paul Bakker68884e32013-01-07 18:20:04 +01001126 /*
1127 * Fix pointer positions and message length with added IV
1128 */
1129 enc_msg = ssl->out_msg;
1130 enc_msglen = ssl->out_msglen;
1131 ssl->out_msglen += ssl->transform_out->ivlen -
1132 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001133
Paul Bakker68884e32013-01-07 18:20:04 +01001134 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1135 "including %d bytes of padding",
1136 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001137
Paul Bakker68884e32013-01-07 18:20:04 +01001138 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1139 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001140
Paul Bakker68884e32013-01-07 18:20:04 +01001141 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001142 * Encrypt
1143 */
1144 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1145 ssl->transform_out->iv_enc,
1146 ssl->transform_out->ivlen ) ) != 0 ||
1147 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1148 {
1149 return( ret );
1150 }
1151
1152 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1153 add_data, 13 ) ) != 0 )
1154 {
1155 return( ret );
1156 }
1157
1158 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1159 enc_msg, enc_msglen,
1160 enc_msg, &olen ) ) != 0 )
1161 {
1162 return( ret );
1163 }
1164 totlen = olen;
1165
1166 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1167 enc_msg + olen, &olen ) ) != 0 )
1168 {
1169 return( ret );
1170 }
1171 totlen += olen;
1172
1173 if( totlen != enc_msglen )
1174 {
1175 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1176 return( -1 );
1177 }
1178
1179 /*
1180 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001181 */
1182 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001183
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001184 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1185 enc_msg + enc_msglen, 16 ) ) != 0 )
1186 {
1187 return( ret );
1188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001190 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001191 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 else
Paul Bakker68884e32013-01-07 18:20:04 +01001193#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001194#if defined(POLARSSL_CIPHER_MODE_CBC)
1195 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1196 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001198 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001199 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001200 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001201
Paul Bakker48916f92012-09-16 19:57:18 +00001202 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1203 ssl->transform_out->ivlen;
1204 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 padlen = 0;
1206
1207 for( i = 0; i <= padlen; i++ )
1208 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1209
1210 ssl->out_msglen += padlen + 1;
1211
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001212 enc_msglen = ssl->out_msglen;
1213 enc_msg = ssl->out_msg;
1214
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001215#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001216 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001217 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1218 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001219 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001220 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001221 {
1222 /*
1223 * Generate IV
1224 */
Paul Bakker48916f92012-09-16 19:57:18 +00001225 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1226 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001227 if( ret != 0 )
1228 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001229
Paul Bakker92be97b2013-01-02 17:30:03 +01001230 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001231 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001232
1233 /*
1234 * Fix pointer positions and message length with added IV
1235 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001236 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001237 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001238 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001239 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001240#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001241
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001244 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
1246 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001247 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
Paul Bakker45125bc2013-09-04 16:47:11 +02001249 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001251 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1252 return( ret );
1253 }
1254
1255 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1256 ssl->transform_out->iv_enc,
1257 ssl->transform_out->ivlen ) ) != 0 )
1258 {
1259 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001260 return( ret );
1261 }
Paul Bakker68884e32013-01-07 18:20:04 +01001262
Paul Bakkercca5b812013-08-31 17:40:26 +02001263 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1264 enc_msg, enc_msglen, enc_msg,
1265 &olen ) ) != 0 )
1266 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001267 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001268 return( ret );
1269 }
Paul Bakker68884e32013-01-07 18:20:04 +01001270
Paul Bakkercca5b812013-08-31 17:40:26 +02001271 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001272
Paul Bakkercca5b812013-08-31 17:40:26 +02001273 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001274 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001275 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001276 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001277 return( ret );
1278 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001279
Paul Bakkercca5b812013-08-31 17:40:26 +02001280 if( enc_msglen != olen )
1281 {
1282 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1283 enc_msglen, olen ) );
1284 // TODO Real error number
1285 return( -1 );
1286 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001287
1288#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001289 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1290 {
1291 /*
1292 * Save IV in SSL3 and TLS1
1293 */
1294 memcpy( ssl->transform_out->iv_enc,
1295 ssl->transform_out->cipher_ctx_enc.iv,
1296 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001298#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001300 else
1301#endif /* POLARSSL_CIPHER_MODE_CBC */
1302 {
1303 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1304 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1305 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Paul Bakkerca4ab492012-04-18 14:23:57 +00001307 for( i = 8; i > 0; i-- )
1308 if( ++ssl->out_ctr[i - 1] != 0 )
1309 break;
1310
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1312
1313 return( 0 );
1314}
1315
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001316#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001317
Paul Bakker5121ce52009-01-03 21:22:43 +00001318static int ssl_decrypt_buf( ssl_context *ssl )
1319{
Paul Bakker45829992013-01-03 14:52:21 +01001320 size_t i, padlen = 0, correct = 1;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001321 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
1323 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1324
Paul Bakker48916f92012-09-16 19:57:18 +00001325 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 {
1327 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001328 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001329 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 }
1331
Paul Bakker68884e32013-01-07 18:20:04 +01001332#if defined(POLARSSL_CIPHER_NULL_CIPHER)
1333 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 {
Paul Bakker68884e32013-01-07 18:20:04 +01001335 padlen = 0;
1336 }
1337 else
1338#endif /* POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker40e46942009-01-03 21:51:57 +00001339#if defined(POLARSSL_ARC4_C)
Paul Bakker68884e32013-01-07 18:20:04 +01001340 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
1341 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001342 int ret;
1343 size_t olen = 0;
1344
Paul Bakker68884e32013-01-07 18:20:04 +01001345 padlen = 0;
1346
Paul Bakker45125bc2013-09-04 16:47:11 +02001347 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001348 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001349 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1350 return( ret );
1351 }
1352
1353 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1354 ssl->transform_in->iv_dec,
1355 ssl->transform_in->ivlen ) ) != 0 )
1356 {
1357 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001358 return( ret );
1359 }
1360
1361 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1362 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1363 &olen ) ) != 0 )
1364 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001365 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001366 return( ret );
1367 }
1368
1369 if( ssl->in_msglen != olen )
1370 {
1371 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1372 // TODO Real error number
1373 return( -1 );
1374 }
1375
1376 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001377 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001378 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001379 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001380 return( ret );
1381 }
1382
1383 if( 0 != olen )
1384 {
1385 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1386 // TODO Real error number
1387 return( -1 );
1388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 }
Paul Bakker68884e32013-01-07 18:20:04 +01001390 else
1391#endif /* POLARSSL_ARC4_C */
1392#if defined(POLARSSL_GCM_C)
1393 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
1394 ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001395 {
1396 unsigned char *dec_msg;
1397 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001398 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001399 unsigned char add_data[13];
1400 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1401
Paul Bakker68884e32013-01-07 18:20:04 +01001402 padlen = 0;
1403
1404 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1405 ssl->transform_in->fixed_ivlen );
1406 dec_msglen -= 16;
1407 dec_msg = ssl->in_msg;
1408 dec_msg_result = ssl->in_msg;
1409 ssl->in_msglen = dec_msglen;
1410
1411 memcpy( add_data, ssl->in_ctr, 8 );
1412 add_data[8] = ssl->in_msgtype;
1413 add_data[9] = ssl->major_ver;
1414 add_data[10] = ssl->minor_ver;
1415 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1416 add_data[12] = ssl->in_msglen & 0xFF;
1417
1418 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1419 add_data, 13 );
1420
1421 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1422 ssl->in_iv,
1423 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1424
1425 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1426 ssl->transform_in->ivlen );
1427 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1428
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001429 /*
1430 * Decrypt
1431 */
1432 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1433 ssl->transform_in->iv_dec,
1434 ssl->transform_in->ivlen ) ) != 0 ||
1435 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001436 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001437 return( ret );
1438 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001439
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001440 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1441 add_data, 13 ) ) != 0 )
1442 {
1443 return( ret );
1444 }
1445
1446 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1447 dec_msg, dec_msglen,
1448 dec_msg_result, &olen ) ) != 0 )
1449 {
1450 return( ret );
1451 }
1452 totlen = olen;
1453
1454 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1455 dec_msg_result + olen, &olen ) ) != 0 )
1456 {
1457 return( ret );
1458 }
1459 totlen += olen;
1460
1461 if( totlen != dec_msglen )
1462 {
1463 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1464 return( -1 );
1465 }
1466
1467 /*
1468 * Authenticate
1469 */
1470 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1471 dec_msg + dec_msglen, 16 ) ) != 0 )
1472 {
1473 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001474 return( POLARSSL_ERR_SSL_INVALID_MAC );
1475 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001476
Paul Bakkerca4ab492012-04-18 14:23:57 +00001477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 else
Paul Bakker68884e32013-01-07 18:20:04 +01001479#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001480#if defined(POLARSSL_CIPHER_MODE_CBC)
1481 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1482 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 {
Paul Bakker45829992013-01-03 14:52:21 +01001484 /*
1485 * Decrypt and check the padding
1486 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001487 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001488 unsigned char *dec_msg;
1489 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001490 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001491 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001492 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001493
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 /*
Paul Bakker45829992013-01-03 14:52:21 +01001495 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 */
Paul Bakker48916f92012-09-16 19:57:18 +00001497 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 {
1499 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001500 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001501 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 }
1503
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001504#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001505 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1506 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001507#endif
Paul Bakker45829992013-01-03 14:52:21 +01001508
1509 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1510 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1511 {
1512 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1513 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1514 return( POLARSSL_ERR_SSL_INVALID_MAC );
1515 }
1516
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001517 dec_msglen = ssl->in_msglen;
1518 dec_msg = ssl->in_msg;
1519 dec_msg_result = ssl->in_msg;
1520
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001521#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001522 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001523 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001524 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001525 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001526 {
Paul Bakker48916f92012-09-16 19:57:18 +00001527 dec_msglen -= ssl->transform_in->ivlen;
1528 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001529
Paul Bakker48916f92012-09-16 19:57:18 +00001530 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001531 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001532 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001533#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001534
Paul Bakker45125bc2013-09-04 16:47:11 +02001535 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001537 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1538 return( ret );
1539 }
1540
1541 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1542 ssl->transform_in->iv_dec,
1543 ssl->transform_in->ivlen ) ) != 0 )
1544 {
1545 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001546 return( ret );
1547 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001548
Paul Bakkercca5b812013-08-31 17:40:26 +02001549 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1550 dec_msg, dec_msglen, dec_msg_result,
1551 &olen ) ) != 0 )
1552 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001553 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001554 return( ret );
1555 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001556
Paul Bakkercca5b812013-08-31 17:40:26 +02001557 dec_msglen -= olen;
1558 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001559 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001560 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001561 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001562 return( ret );
1563 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001564
Paul Bakkercca5b812013-08-31 17:40:26 +02001565 if( dec_msglen != olen )
1566 {
1567 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1568 // TODO Real error number
1569 return( -1 );
1570 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001571
1572#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001573 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1574 {
1575 /*
1576 * Save IV in SSL3 and TLS1
1577 */
1578 memcpy( ssl->transform_in->iv_dec,
1579 ssl->transform_in->cipher_ctx_dec.iv,
1580 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001581 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001582#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001583
1584 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001585
1586 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1587 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001588#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001589 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1590 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001591#endif
Paul Bakker45829992013-01-03 14:52:21 +01001592 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001593 correct = 0;
1594 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001595
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001596#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1598 {
Paul Bakker48916f92012-09-16 19:57:18 +00001599 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001601#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1603 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001604 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001605#endif
Paul Bakker45829992013-01-03 14:52:21 +01001606 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 }
1608 }
1609 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001610#endif
1611#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1612 defined(POLARSSL_SSL_PROTO_TLS1_2)
1613 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 {
1615 /*
Paul Bakker45829992013-01-03 14:52:21 +01001616 * TLSv1+: always check the padding up to the first failure
1617 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001618 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001619 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001620 size_t padding_idx = ssl->in_msglen - padlen - 1;
1621
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001622 for( i = 1; i <= 256; i++ )
1623 {
1624 real_count &= ( i <= padlen );
1625 pad_count += real_count *
1626 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1627 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001628
1629 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001630
Paul Bakkerd66f0702013-01-31 16:57:45 +01001631#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001632 if( padlen > 0 && correct == 0)
1633 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001634#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001635 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001636 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001637 else
1638#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1639 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001640 {
1641 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001642 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001643 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001644 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001645 else
1646#endif /* POLARSSL_CIPHER_MODE_CBC */
1647 {
1648 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1649 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1650 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
1652 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1653 ssl->in_msg, ssl->in_msglen );
1654
1655 /*
1656 * Always compute the MAC (RFC4346, CBCTIME).
1657 */
Paul Bakker48916f92012-09-16 19:57:18 +00001658 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
1660 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1661 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
1662
Paul Bakker45829992013-01-03 14:52:21 +01001663 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001665#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1667 {
Paul Bakker68884e32013-01-07 18:20:04 +01001668 ssl_mac( &ssl->transform_in->md_ctx_dec,
1669 ssl->transform_in->mac_dec,
1670 ssl->in_msg, ssl->in_msglen,
1671 ssl->in_ctr, ssl->in_msgtype );
Paul Bakker5121ce52009-01-03 21:22:43 +00001672 }
1673 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001674#endif /* POLARSSL_SSL_PROTO_SSL3 */
1675#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1676 defined(POLARSSL_SSL_PROTO_TLS1_2)
1677 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 {
Paul Bakker45829992013-01-03 14:52:21 +01001679 /*
1680 * Process MAC and always update for padlen afterwards to make
1681 * total time independent of padlen
Paul Bakkere47b34b2013-02-27 14:48:00 +01001682 *
1683 * extra_run compensates MAC check for padlen
1684 *
1685 * Known timing attacks:
1686 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1687 *
1688 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1689 * correctly. (We round down instead of up, so -56 is the correct
1690 * value for our calculations instead of -55)
Paul Bakker45829992013-01-03 14:52:21 +01001691 */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001692 int j, extra_run = 0;
1693 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1694 ( 13 + ssl->in_msglen + 8 ) / 64;
1695
1696 extra_run &= correct * 0xFF;
1697
Paul Bakker68884e32013-01-07 18:20:04 +01001698 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1699 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1700 ssl->in_msglen );
1701 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1702 ssl->in_msg + ssl->in_msglen );
1703 for( j = 0; j < extra_run; j++ )
1704 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001705
Paul Bakker68884e32013-01-07 18:20:04 +01001706 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001708 else
1709#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1710 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001711 {
1712 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001713 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001714 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Paul Bakker48916f92012-09-16 19:57:18 +00001716 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001717 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
Paul Bakker48916f92012-09-16 19:57:18 +00001718 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001719
1720 if( memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Paul Bakker48916f92012-09-16 19:57:18 +00001721 ssl->transform_in->maclen ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001722 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001723#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001724 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001725#endif
Paul Bakker45829992013-01-03 14:52:21 +01001726 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 }
1728
1729 /*
Paul Bakker45829992013-01-03 14:52:21 +01001730 * Finally check the correct flag
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 */
Paul Bakker45829992013-01-03 14:52:21 +01001732 if( correct == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +00001733 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
1735 if( ssl->in_msglen == 0 )
1736 {
1737 ssl->nb_zero++;
1738
1739 /*
1740 * Three or more empty messages may be a DoS attack
1741 * (excessive CPU consumption).
1742 */
1743 if( ssl->nb_zero > 3 )
1744 {
1745 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1746 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001747 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748 }
1749 }
1750 else
1751 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001752
Paul Bakker23986e52011-04-24 08:57:21 +00001753 for( i = 8; i > 0; i-- )
1754 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001755 break;
1756
1757 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1758
1759 return( 0 );
1760}
1761
Paul Bakker2770fbd2012-07-03 13:30:23 +00001762#if defined(POLARSSL_ZLIB_SUPPORT)
1763/*
1764 * Compression/decompression functions
1765 */
1766static int ssl_compress_buf( ssl_context *ssl )
1767{
1768 int ret;
1769 unsigned char *msg_post = ssl->out_msg;
1770 size_t len_pre = ssl->out_msglen;
1771 unsigned char *msg_pre;
1772
1773 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1774
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001775 if( len_pre == 0 )
1776 return( 0 );
1777
Paul Bakker6e339b52013-07-03 13:37:05 +02001778 msg_pre = (unsigned char*) polarssl_malloc( len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001779 if( msg_pre == NULL )
1780 {
1781 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len_pre ) );
1782 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1783 }
1784
1785 memcpy( msg_pre, ssl->out_msg, len_pre );
1786
1787 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1788 ssl->out_msglen ) );
1789
1790 SSL_DEBUG_BUF( 4, "before compression: output payload",
1791 ssl->out_msg, ssl->out_msglen );
1792
Paul Bakker48916f92012-09-16 19:57:18 +00001793 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1794 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1795 ssl->transform_out->ctx_deflate.next_out = msg_post;
1796 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001797
Paul Bakker48916f92012-09-16 19:57:18 +00001798 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799 if( ret != Z_OK )
1800 {
1801 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1802 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1803 }
1804
Paul Bakker48916f92012-09-16 19:57:18 +00001805 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001806
Paul Bakker6e339b52013-07-03 13:37:05 +02001807 polarssl_free( msg_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001808
1809 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1810 ssl->out_msglen ) );
1811
1812 SSL_DEBUG_BUF( 4, "after compression: output payload",
1813 ssl->out_msg, ssl->out_msglen );
1814
1815 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1816
1817 return( 0 );
1818}
1819
1820static int ssl_decompress_buf( ssl_context *ssl )
1821{
1822 int ret;
1823 unsigned char *msg_post = ssl->in_msg;
1824 size_t len_pre = ssl->in_msglen;
1825 unsigned char *msg_pre;
1826
1827 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1828
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001829 if( len_pre == 0 )
1830 return( 0 );
1831
Paul Bakker6e339b52013-07-03 13:37:05 +02001832 msg_pre = (unsigned char*) polarssl_malloc( len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001833 if( msg_pre == NULL )
1834 {
1835 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len_pre ) );
1836 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1837 }
1838
1839 memcpy( msg_pre, ssl->in_msg, len_pre );
1840
1841 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1842 ssl->in_msglen ) );
1843
1844 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1845 ssl->in_msg, ssl->in_msglen );
1846
Paul Bakker48916f92012-09-16 19:57:18 +00001847 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1848 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1849 ssl->transform_in->ctx_inflate.next_out = msg_post;
1850 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001851
Paul Bakker48916f92012-09-16 19:57:18 +00001852 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001853 if( ret != Z_OK )
1854 {
1855 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1856 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1857 }
1858
Paul Bakker48916f92012-09-16 19:57:18 +00001859 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001860
Paul Bakker6e339b52013-07-03 13:37:05 +02001861 polarssl_free( msg_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001862
1863 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1864 ssl->in_msglen ) );
1865
1866 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1867 ssl->in_msg, ssl->in_msglen );
1868
1869 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1870
1871 return( 0 );
1872}
1873#endif /* POLARSSL_ZLIB_SUPPORT */
1874
Paul Bakker5121ce52009-01-03 21:22:43 +00001875/*
1876 * Fill the input message buffer
1877 */
Paul Bakker23986e52011-04-24 08:57:21 +00001878int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001879{
Paul Bakker23986e52011-04-24 08:57:21 +00001880 int ret;
1881 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
1883 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1884
1885 while( ssl->in_left < nb_want )
1886 {
1887 len = nb_want - ssl->in_left;
1888 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1889
1890 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1891 ssl->in_left, nb_want ) );
1892 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1893
Paul Bakker831a7552011-05-18 13:32:51 +00001894 if( ret == 0 )
1895 return( POLARSSL_ERR_SSL_CONN_EOF );
1896
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 if( ret < 0 )
1898 return( ret );
1899
1900 ssl->in_left += ret;
1901 }
1902
1903 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1904
1905 return( 0 );
1906}
1907
1908/*
1909 * Flush any data not yet written
1910 */
1911int ssl_flush_output( ssl_context *ssl )
1912{
1913 int ret;
1914 unsigned char *buf;
1915
1916 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1917
1918 while( ssl->out_left > 0 )
1919 {
1920 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1921 5 + ssl->out_msglen, ssl->out_left ) );
1922
Paul Bakker5bd42292012-12-19 14:40:42 +01001923 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001925
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1927
1928 if( ret <= 0 )
1929 return( ret );
1930
1931 ssl->out_left -= ret;
1932 }
1933
1934 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1935
1936 return( 0 );
1937}
1938
1939/*
1940 * Record layer functions
1941 */
1942int ssl_write_record( ssl_context *ssl )
1943{
Paul Bakker05ef8352012-05-08 09:17:57 +00001944 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001945 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001946
1947 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1948
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1950 {
1951 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1952 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1953 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1954
Paul Bakker48916f92012-09-16 19:57:18 +00001955 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 }
1957
Paul Bakker2770fbd2012-07-03 13:30:23 +00001958#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001959 if( ssl->transform_out != NULL &&
1960 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001961 {
1962 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1963 {
1964 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1965 return( ret );
1966 }
1967
1968 len = ssl->out_msglen;
1969 }
1970#endif /*POLARSSL_ZLIB_SUPPORT */
1971
Paul Bakker05ef8352012-05-08 09:17:57 +00001972#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1973 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001975 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
Paul Bakker05ef8352012-05-08 09:17:57 +00001977 ret = ssl_hw_record_write( ssl );
1978 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1979 {
1980 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1981 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1982 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001983
1984 if( ret == 0 )
1985 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001986 }
1987#endif
1988 if( !done )
1989 {
1990 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1991 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1992 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001993 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1994 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001995
Paul Bakker48916f92012-09-16 19:57:18 +00001996 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001997 {
1998 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1999 {
2000 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2001 return( ret );
2002 }
2003
2004 len = ssl->out_msglen;
2005 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2006 ssl->out_hdr[4] = (unsigned char)( len );
2007 }
2008
2009 ssl->out_left = 5 + ssl->out_msglen;
2010
2011 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2012 "version = [%d:%d], msglen = %d",
2013 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2014 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2015
2016 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002017 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 }
2019
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2021 {
2022 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2023 return( ret );
2024 }
2025
2026 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2027
2028 return( 0 );
2029}
2030
2031int ssl_read_record( ssl_context *ssl )
2032{
Paul Bakker05ef8352012-05-08 09:17:57 +00002033 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
2035 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2036
Paul Bakker68884e32013-01-07 18:20:04 +01002037 SSL_DEBUG_BUF( 4, "input record from network",
2038 ssl->in_hdr, 5 + ssl->in_msglen );
2039
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 if( ssl->in_hslen != 0 &&
2041 ssl->in_hslen < ssl->in_msglen )
2042 {
2043 /*
2044 * Get next Handshake message in the current record
2045 */
2046 ssl->in_msglen -= ssl->in_hslen;
2047
Paul Bakker8934a982011-08-05 11:11:53 +00002048 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002049 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002050
2051 ssl->in_hslen = 4;
2052 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2053
2054 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2055 " %d, type = %d, hslen = %d",
2056 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2057
2058 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2059 {
2060 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002061 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 }
2063
2064 if( ssl->in_msglen < ssl->in_hslen )
2065 {
2066 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002067 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
2069
Paul Bakker48916f92012-09-16 19:57:18 +00002070 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
2072 return( 0 );
2073 }
2074
2075 ssl->in_hslen = 0;
2076
2077 /*
2078 * Read the record header and validate it
2079 */
2080 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2081 {
2082 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2083 return( ret );
2084 }
2085
2086 ssl->in_msgtype = ssl->in_hdr[0];
2087 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2088
2089 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2090 "version = [%d:%d], msglen = %d",
2091 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2092 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2093
2094 if( ssl->in_hdr[1] != ssl->major_ver )
2095 {
2096 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002097 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 }
2099
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002100 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 {
2102 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002103 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 }
2105
2106 /*
2107 * Make sure the message length is acceptable
2108 */
Paul Bakker48916f92012-09-16 19:57:18 +00002109 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
2111 if( ssl->in_msglen < 1 ||
2112 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2113 {
2114 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002115 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 }
2117 }
2118 else
2119 {
Paul Bakker48916f92012-09-16 19:57:18 +00002120 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
2122 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002123 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
2125
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002126#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002128 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 {
2130 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002131 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002133#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002134
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002135#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2136 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 /*
2138 * TLS encrypted messages can have up to 256 bytes of padding
2139 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002140 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002141 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 {
2143 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002144 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002146#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 }
2148
2149 /*
2150 * Read and optionally decrypt the message contents
2151 */
2152 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2153 {
2154 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2155 return( ret );
2156 }
2157
2158 SSL_DEBUG_BUF( 4, "input record from network",
2159 ssl->in_hdr, 5 + ssl->in_msglen );
2160
Paul Bakker05ef8352012-05-08 09:17:57 +00002161#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2162 if( ssl_hw_record_read != NULL)
2163 {
2164 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2165
2166 ret = ssl_hw_record_read( ssl );
2167 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2168 {
2169 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2170 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2171 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002172
2173 if( ret == 0 )
2174 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002175 }
2176#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002177 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 {
2179 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2180 {
Paul Bakker40865c82013-01-31 17:13:13 +01002181#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2182 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2183 {
2184 ssl_send_alert_message( ssl,
2185 SSL_ALERT_LEVEL_FATAL,
2186 SSL_ALERT_MSG_BAD_RECORD_MAC );
2187 }
2188#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002189 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2190 return( ret );
2191 }
2192
2193 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2194 ssl->in_msg, ssl->in_msglen );
2195
2196 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2197 {
2198 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002199 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 }
2201 }
2202
Paul Bakker2770fbd2012-07-03 13:30:23 +00002203#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002204 if( ssl->transform_in != NULL &&
2205 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002206 {
2207 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2208 {
2209 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2210 return( ret );
2211 }
2212
2213 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2214 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2215 }
2216#endif /* POLARSSL_ZLIB_SUPPORT */
2217
Paul Bakker0a925182012-04-16 06:46:41 +00002218 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2219 ssl->in_msgtype != SSL_MSG_ALERT &&
2220 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2221 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2222 {
2223 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2224
Paul Bakker48916f92012-09-16 19:57:18 +00002225 if( ( ret = ssl_send_alert_message( ssl,
2226 SSL_ALERT_LEVEL_FATAL,
2227 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002228 {
2229 return( ret );
2230 }
2231
2232 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2233 }
2234
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2236 {
2237 ssl->in_hslen = 4;
2238 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2239
2240 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2241 " %d, type = %d, hslen = %d",
2242 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2243
2244 /*
2245 * Additional checks to validate the handshake header
2246 */
2247 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2248 {
2249 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002250 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 }
2252
2253 if( ssl->in_msglen < ssl->in_hslen )
2254 {
2255 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002256 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 }
2258
Paul Bakker48916f92012-09-16 19:57:18 +00002259 if( ssl->state != SSL_HANDSHAKE_OVER )
2260 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262
2263 if( ssl->in_msgtype == SSL_MSG_ALERT )
2264 {
2265 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2266 ssl->in_msg[0], ssl->in_msg[1] ) );
2267
2268 /*
2269 * Ignore non-fatal alerts, except close_notify
2270 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002271 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002273 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2274 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002275 /**
2276 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2277 * error identifier.
2278 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002279 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 }
2281
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002282 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2283 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 {
2285 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002286 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
2288 }
2289
2290 ssl->in_left = 0;
2291
2292 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2293
2294 return( 0 );
2295}
2296
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002297int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2298{
2299 int ret;
2300
2301 if( ( ret = ssl_send_alert_message( ssl,
2302 SSL_ALERT_LEVEL_FATAL,
2303 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2304 {
2305 return( ret );
2306 }
2307
2308 return( 0 );
2309}
2310
Paul Bakker0a925182012-04-16 06:46:41 +00002311int ssl_send_alert_message( ssl_context *ssl,
2312 unsigned char level,
2313 unsigned char message )
2314{
2315 int ret;
2316
2317 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2318
2319 ssl->out_msgtype = SSL_MSG_ALERT;
2320 ssl->out_msglen = 2;
2321 ssl->out_msg[0] = level;
2322 ssl->out_msg[1] = message;
2323
2324 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2325 {
2326 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2327 return( ret );
2328 }
2329
2330 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2331
2332 return( 0 );
2333}
2334
Paul Bakker5121ce52009-01-03 21:22:43 +00002335/*
2336 * Handshake functions
2337 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002338#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2339 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002340 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2341 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002342int ssl_write_certificate( ssl_context *ssl )
2343{
Paul Bakkered27a042013-04-18 22:46:23 +02002344 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002345 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002346
2347 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2348
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002349 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002350 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2351 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002352 {
2353 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2354 ssl->state++;
2355 return( 0 );
2356 }
2357
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002358 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002359 return( ret );
2360}
2361
2362int ssl_parse_certificate( ssl_context *ssl )
2363{
2364 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2365 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2366
2367 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2368
2369 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002370 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2371 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002372 {
2373 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2374 ssl->state++;
2375 return( 0 );
2376 }
2377
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002378 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002379 return( ret );
2380}
2381#else
2382int ssl_write_certificate( ssl_context *ssl )
2383{
2384 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2385 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002386 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002387 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2388
2389 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2390
2391 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002392 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2393 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394 {
2395 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2396 ssl->state++;
2397 return( 0 );
2398 }
2399
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 if( ssl->endpoint == SSL_IS_CLIENT )
2401 {
2402 if( ssl->client_auth == 0 )
2403 {
2404 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2405 ssl->state++;
2406 return( 0 );
2407 }
2408
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002409#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 /*
2411 * If using SSLv3 and got no cert, send an Alert message
2412 * (otherwise an empty Certificate message will be sent).
2413 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002414 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2416 {
2417 ssl->out_msglen = 2;
2418 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002419 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2420 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
2422 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2423 goto write_msg;
2424 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002425#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 }
2427 else /* SSL_IS_SERVER */
2428 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002429 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 {
2431 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002432 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 }
2434 }
2435
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002436 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
2438 /*
2439 * 0 . 0 handshake type
2440 * 1 . 3 handshake length
2441 * 4 . 6 length of all certs
2442 * 7 . 9 length of cert. 1
2443 * 10 . n-1 peer certificate
2444 * n . n+2 length of cert. 2
2445 * n+3 . ... upper level cert, etc.
2446 */
2447 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002448 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002449
Paul Bakker29087132010-03-21 21:03:34 +00002450 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 {
2452 n = crt->raw.len;
2453 if( i + 3 + n > SSL_MAX_CONTENT_LEN )
2454 {
2455 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2456 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002457 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 }
2459
2460 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2461 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2462 ssl->out_msg[i + 2] = (unsigned char)( n );
2463
2464 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2465 i += n; crt = crt->next;
2466 }
2467
2468 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2469 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2470 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2471
2472 ssl->out_msglen = i;
2473 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2474 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2475
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002476#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002477write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002478#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
2480 ssl->state++;
2481
2482 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2483 {
2484 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2485 return( ret );
2486 }
2487
2488 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2489
Paul Bakkered27a042013-04-18 22:46:23 +02002490 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002491}
2492
2493int ssl_parse_certificate( ssl_context *ssl )
2494{
Paul Bakkered27a042013-04-18 22:46:23 +02002495 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002496 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002497 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002498
2499 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2500
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002502 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2503 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002504 {
2505 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2506 ssl->state++;
2507 return( 0 );
2508 }
2509
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 if( ssl->endpoint == SSL_IS_SERVER &&
2511 ssl->authmode == SSL_VERIFY_NONE )
2512 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002513 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2515 ssl->state++;
2516 return( 0 );
2517 }
2518
2519 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2520 {
2521 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2522 return( ret );
2523 }
2524
2525 ssl->state++;
2526
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002527#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 /*
2529 * Check if the client sent an empty certificate
2530 */
2531 if( ssl->endpoint == SSL_IS_SERVER &&
2532 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2533 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002534 if( ssl->in_msglen == 2 &&
2535 ssl->in_msgtype == SSL_MSG_ALERT &&
2536 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2537 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 {
2539 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2540
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002541 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2543 return( 0 );
2544 else
Paul Bakker40e46942009-01-03 21:51:57 +00002545 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 }
2547 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002548#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002550#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2551 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 if( ssl->endpoint == SSL_IS_SERVER &&
2553 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2554 {
2555 if( ssl->in_hslen == 7 &&
2556 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2557 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2558 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2559 {
2560 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2561
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002562 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002564 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 else
2566 return( 0 );
2567 }
2568 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002569#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2570 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
2572 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2573 {
2574 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002575 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 }
2577
2578 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2579 {
2580 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002581 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 }
2583
2584 /*
2585 * Same message structure as in ssl_write_certificate()
2586 */
2587 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2588
2589 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2590 {
2591 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002592 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 }
2594
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002595 /* In case we tried to reuse a session but it failed */
2596 if( ssl->session_negotiate->peer_cert != NULL )
2597 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002598 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002599 polarssl_free( ssl->session_negotiate->peer_cert );
2600 }
2601
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002602 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2603 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 {
2605 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002606 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002607 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 }
2609
Paul Bakkerb6b09562013-09-18 14:17:41 +02002610 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
2612 i = 7;
2613
2614 while( i < ssl->in_hslen )
2615 {
2616 if( ssl->in_msg[i] != 0 )
2617 {
2618 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002619 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 }
2621
2622 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2623 | (unsigned int) ssl->in_msg[i + 2];
2624 i += 3;
2625
2626 if( n < 128 || i + n > ssl->in_hslen )
2627 {
2628 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002629 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 }
2631
Paul Bakkerddf26b42013-09-18 13:46:23 +02002632 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2633 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 if( ret != 0 )
2635 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002636 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 return( ret );
2638 }
2639
2640 i += n;
2641 }
2642
Paul Bakker48916f92012-09-16 19:57:18 +00002643 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
2645 if( ssl->authmode != SSL_VERIFY_NONE )
2646 {
2647 if( ssl->ca_chain == NULL )
2648 {
2649 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002650 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 }
2652
Paul Bakkerddf26b42013-09-18 13:46:23 +02002653 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2654 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2655 &ssl->session_negotiate->verify_result,
2656 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
2658 if( ret != 0 )
2659 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2660
2661 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2662 ret = 0;
2663 }
2664
2665 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2666
2667 return( ret );
2668}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002669#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2670 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2671 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
2673int ssl_write_change_cipher_spec( ssl_context *ssl )
2674{
2675 int ret;
2676
2677 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2678
2679 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2680 ssl->out_msglen = 1;
2681 ssl->out_msg[0] = 1;
2682
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 ssl->state++;
2684
2685 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2686 {
2687 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2688 return( ret );
2689 }
2690
2691 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2692
2693 return( 0 );
2694}
2695
2696int ssl_parse_change_cipher_spec( ssl_context *ssl )
2697{
2698 int ret;
2699
2700 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2701
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2703 {
2704 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2705 return( ret );
2706 }
2707
2708 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2709 {
2710 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002711 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 }
2713
2714 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2715 {
2716 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002717 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 }
2719
2720 ssl->state++;
2721
2722 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2723
2724 return( 0 );
2725}
2726
Paul Bakker41c83d32013-03-20 14:39:14 +01002727void ssl_optimize_checksum( ssl_context *ssl,
2728 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002729{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002730 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002731
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002732#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2733 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002734 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002735 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002736 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002737#endif
2738#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2739#if defined(POLARSSL_SHA512_C)
2740 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2741 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2742 else
2743#endif
2744#if defined(POLARSSL_SHA256_C)
2745 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002746 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002747 else
2748#endif
2749#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2750 /* Should never happen */
2751 return;
Paul Bakker380da532012-04-18 16:10:25 +00002752}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002753
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002754static void ssl_update_checksum_start( ssl_context *ssl,
2755 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002756{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002757#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2758 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002759 md5_update( &ssl->handshake->fin_md5 , buf, len );
2760 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002761#endif
2762#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2763#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002764 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002765#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002766#if defined(POLARSSL_SHA512_C)
2767 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002768#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002769#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002770}
2771
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002772#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2773 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002774static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2775 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002776{
Paul Bakker48916f92012-09-16 19:57:18 +00002777 md5_update( &ssl->handshake->fin_md5 , buf, len );
2778 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002779}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002780#endif
Paul Bakker380da532012-04-18 16:10:25 +00002781
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002782#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2783#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002784static void ssl_update_checksum_sha256( ssl_context *ssl,
2785 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002786{
Paul Bakker9e36f042013-06-30 14:34:05 +02002787 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002788}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002789#endif
Paul Bakker380da532012-04-18 16:10:25 +00002790
Paul Bakker9e36f042013-06-30 14:34:05 +02002791#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002792static void ssl_update_checksum_sha384( ssl_context *ssl,
2793 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002794{
Paul Bakker9e36f042013-06-30 14:34:05 +02002795 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002796}
Paul Bakker769075d2012-11-24 11:26:46 +01002797#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002798#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002799
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002800#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002801static void ssl_calc_finished_ssl(
2802 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002803{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002804 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002805 md5_context md5;
2806 sha1_context sha1;
2807
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 unsigned char padbuf[48];
2809 unsigned char md5sum[16];
2810 unsigned char sha1sum[20];
2811
Paul Bakker48916f92012-09-16 19:57:18 +00002812 ssl_session *session = ssl->session_negotiate;
2813 if( !session )
2814 session = ssl->session;
2815
Paul Bakker1ef83d62012-04-11 12:09:53 +00002816 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2817
Paul Bakker48916f92012-09-16 19:57:18 +00002818 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2819 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
2821 /*
2822 * SSLv3:
2823 * hash =
2824 * MD5( master + pad2 +
2825 * MD5( handshake + sender + master + pad1 ) )
2826 * + SHA1( master + pad2 +
2827 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 */
2829
Paul Bakker90995b52013-06-24 19:20:35 +02002830#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002831 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002832 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Paul Bakker90995b52013-06-24 19:20:35 +02002835#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002837 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002838#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Paul Bakker3c2122f2013-06-24 19:03:14 +02002840 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2841 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Paul Bakker1ef83d62012-04-11 12:09:53 +00002843 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Paul Bakker3c2122f2013-06-24 19:03:14 +02002845 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002846 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002847 md5_update( &md5, padbuf, 48 );
2848 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Paul Bakker3c2122f2013-06-24 19:03:14 +02002850 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002851 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002852 sha1_update( &sha1, padbuf, 40 );
2853 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
Paul Bakker1ef83d62012-04-11 12:09:53 +00002855 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Paul Bakker1ef83d62012-04-11 12:09:53 +00002857 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002858 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002859 md5_update( &md5, padbuf, 48 );
2860 md5_update( &md5, md5sum, 16 );
2861 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Paul Bakker1ef83d62012-04-11 12:09:53 +00002863 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002864 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865 sha1_update( &sha1, padbuf , 40 );
2866 sha1_update( &sha1, sha1sum, 20 );
2867 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871 memset( &md5, 0, sizeof( md5_context ) );
2872 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
2874 memset( padbuf, 0, sizeof( padbuf ) );
2875 memset( md5sum, 0, sizeof( md5sum ) );
2876 memset( sha1sum, 0, sizeof( sha1sum ) );
2877
2878 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2879}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002880#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002882#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002883static void ssl_calc_finished_tls(
2884 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002885{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002886 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002887 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002888 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002890 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakker48916f92012-09-16 19:57:18 +00002892 ssl_session *session = ssl->session_negotiate;
2893 if( !session )
2894 session = ssl->session;
2895
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Paul Bakker48916f92012-09-16 19:57:18 +00002898 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2899 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002900
Paul Bakker1ef83d62012-04-11 12:09:53 +00002901 /*
2902 * TLSv1:
2903 * hash = PRF( master, finished_label,
2904 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2905 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Paul Bakker90995b52013-06-24 19:20:35 +02002907#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002908 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2909 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002910#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911
Paul Bakker90995b52013-06-24 19:20:35 +02002912#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002913 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2914 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002915#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002916
2917 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002918 ? "client finished"
2919 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002920
2921 md5_finish( &md5, padbuf );
2922 sha1_finish( &sha1, padbuf + 16 );
2923
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002924 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002925 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002926
2927 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2928
2929 memset( &md5, 0, sizeof( md5_context ) );
2930 memset( &sha1, 0, sizeof( sha1_context ) );
2931
2932 memset( padbuf, 0, sizeof( padbuf ) );
2933
2934 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2935}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002936#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002937
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002938#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2939#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002940static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002941 ssl_context *ssl, unsigned char *buf, int from )
2942{
2943 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002944 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002945 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002946 unsigned char padbuf[32];
2947
Paul Bakker48916f92012-09-16 19:57:18 +00002948 ssl_session *session = ssl->session_negotiate;
2949 if( !session )
2950 session = ssl->session;
2951
Paul Bakker380da532012-04-18 16:10:25 +00002952 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002953
Paul Bakker9e36f042013-06-30 14:34:05 +02002954 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002955
2956 /*
2957 * TLSv1.2:
2958 * hash = PRF( master, finished_label,
2959 * Hash( handshake ) )[0.11]
2960 */
2961
Paul Bakker9e36f042013-06-30 14:34:05 +02002962#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002964 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002965#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002966
2967 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002968 ? "client finished"
2969 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002970
Paul Bakker9e36f042013-06-30 14:34:05 +02002971 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002972
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002973 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002974 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002975
2976 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2977
Paul Bakker9e36f042013-06-30 14:34:05 +02002978 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979
2980 memset( padbuf, 0, sizeof( padbuf ) );
2981
2982 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2983}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002984#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002985
Paul Bakker9e36f042013-06-30 14:34:05 +02002986#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002987static void ssl_calc_finished_tls_sha384(
2988 ssl_context *ssl, unsigned char *buf, int from )
2989{
2990 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002991 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002992 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002993 unsigned char padbuf[48];
2994
Paul Bakker48916f92012-09-16 19:57:18 +00002995 ssl_session *session = ssl->session_negotiate;
2996 if( !session )
2997 session = ssl->session;
2998
Paul Bakker380da532012-04-18 16:10:25 +00002999 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000
Paul Bakker9e36f042013-06-30 14:34:05 +02003001 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003002
3003 /*
3004 * TLSv1.2:
3005 * hash = PRF( master, finished_label,
3006 * Hash( handshake ) )[0.11]
3007 */
3008
Paul Bakker9e36f042013-06-30 14:34:05 +02003009#if !defined(POLARSSL_SHA512_ALT)
3010 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3011 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003012#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003013
3014 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003015 ? "client finished"
3016 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003017
Paul Bakker9e36f042013-06-30 14:34:05 +02003018 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003019
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003020 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003021 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003022
3023 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3024
Paul Bakker9e36f042013-06-30 14:34:05 +02003025 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003026
3027 memset( padbuf, 0, sizeof( padbuf ) );
3028
3029 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3030}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003031#endif /* POLARSSL_SHA512_C */
3032#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003033
Paul Bakker48916f92012-09-16 19:57:18 +00003034void ssl_handshake_wrapup( ssl_context *ssl )
3035{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003036 int resume = ssl->handshake->resume;
3037
Paul Bakker48916f92012-09-16 19:57:18 +00003038 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3039
3040 /*
3041 * Free our handshake params
3042 */
3043 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003044 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003045 ssl->handshake = NULL;
3046
3047 /*
3048 * Switch in our now active transform context
3049 */
3050 if( ssl->transform )
3051 {
3052 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003053 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003054 }
3055 ssl->transform = ssl->transform_negotiate;
3056 ssl->transform_negotiate = NULL;
3057
Paul Bakker0a597072012-09-25 21:55:46 +00003058 if( ssl->session )
3059 {
3060 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003061 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003062 }
3063 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003064 ssl->session_negotiate = NULL;
3065
Paul Bakker0a597072012-09-25 21:55:46 +00003066 /*
3067 * Add cache entry
3068 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003069 if( ssl->f_set_cache != NULL &&
3070 ssl->session->length != 0 &&
3071 resume == 0 )
3072 {
Paul Bakker0a597072012-09-25 21:55:46 +00003073 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3074 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003075 }
Paul Bakker0a597072012-09-25 21:55:46 +00003076
Paul Bakker48916f92012-09-16 19:57:18 +00003077 ssl->state++;
3078
3079 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3080}
3081
Paul Bakker1ef83d62012-04-11 12:09:53 +00003082int ssl_write_finished( ssl_context *ssl )
3083{
3084 int ret, hash_len;
3085
3086 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3087
Paul Bakker92be97b2013-01-02 17:30:03 +01003088 /*
3089 * Set the out_msg pointer to the correct location based on IV length
3090 */
3091 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3092 {
3093 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3094 ssl->transform_negotiate->fixed_ivlen;
3095 }
3096 else
3097 ssl->out_msg = ssl->out_iv;
3098
Paul Bakker48916f92012-09-16 19:57:18 +00003099 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003100
3101 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3103
Paul Bakker48916f92012-09-16 19:57:18 +00003104 ssl->verify_data_len = hash_len;
3105 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3106
Paul Bakker5121ce52009-01-03 21:22:43 +00003107 ssl->out_msglen = 4 + hash_len;
3108 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3109 ssl->out_msg[0] = SSL_HS_FINISHED;
3110
3111 /*
3112 * In case of session resuming, invert the client and server
3113 * ChangeCipherSpec messages order.
3114 */
Paul Bakker0a597072012-09-25 21:55:46 +00003115 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003116 {
3117 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003118 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003119 else
3120 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3121 }
3122 else
3123 ssl->state++;
3124
Paul Bakker48916f92012-09-16 19:57:18 +00003125 /*
3126 * Switch to our negotiated transform and session parameters for outbound data.
3127 */
3128 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3129 ssl->transform_out = ssl->transform_negotiate;
3130 ssl->session_out = ssl->session_negotiate;
3131 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003132
Paul Bakker07eb38b2012-12-19 14:42:06 +01003133#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3134 if( ssl_hw_record_activate != NULL)
3135 {
3136 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3137 {
3138 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3139 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3140 }
3141 }
3142#endif
3143
Paul Bakker5121ce52009-01-03 21:22:43 +00003144 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3145 {
3146 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3147 return( ret );
3148 }
3149
3150 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3151
3152 return( 0 );
3153}
3154
3155int ssl_parse_finished( ssl_context *ssl )
3156{
Paul Bakker23986e52011-04-24 08:57:21 +00003157 int ret;
3158 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003159 unsigned char buf[36];
3160
3161 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3162
Paul Bakker48916f92012-09-16 19:57:18 +00003163 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
Paul Bakker48916f92012-09-16 19:57:18 +00003165 /*
3166 * Switch to our negotiated transform and session parameters for inbound data.
3167 */
3168 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3169 ssl->transform_in = ssl->transform_negotiate;
3170 ssl->session_in = ssl->session_negotiate;
3171 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003172
Paul Bakker92be97b2013-01-02 17:30:03 +01003173 /*
3174 * Set the in_msg pointer to the correct location based on IV length
3175 */
3176 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3177 {
3178 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3179 ssl->transform_negotiate->fixed_ivlen;
3180 }
3181 else
3182 ssl->in_msg = ssl->in_iv;
3183
Paul Bakker07eb38b2012-12-19 14:42:06 +01003184#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3185 if( ssl_hw_record_activate != NULL)
3186 {
3187 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3188 {
3189 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3190 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3191 }
3192 }
3193#endif
3194
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3196 {
3197 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3198 return( ret );
3199 }
3200
3201 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3202 {
3203 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003204 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003205 }
3206
Paul Bakker1ef83d62012-04-11 12:09:53 +00003207 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3209
3210 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3211 ssl->in_hslen != 4 + hash_len )
3212 {
3213 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003214 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 }
3216
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 if( memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
3218 {
3219 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003220 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003221 }
3222
Paul Bakker48916f92012-09-16 19:57:18 +00003223 ssl->verify_data_len = hash_len;
3224 memcpy( ssl->peer_verify_data, buf, hash_len );
3225
Paul Bakker0a597072012-09-25 21:55:46 +00003226 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 {
3228 if( ssl->endpoint == SSL_IS_CLIENT )
3229 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3230
3231 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003232 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 }
3234 else
3235 ssl->state++;
3236
3237 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3238
3239 return( 0 );
3240}
3241
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003242static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003243{
3244 if( ssl->transform_negotiate )
3245 ssl_transform_free( ssl->transform_negotiate );
3246 else
Paul Bakker6e339b52013-07-03 13:37:05 +02003247 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker48916f92012-09-16 19:57:18 +00003248
3249 if( ssl->session_negotiate )
3250 ssl_session_free( ssl->session_negotiate );
3251 else
Paul Bakker6e339b52013-07-03 13:37:05 +02003252 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakker48916f92012-09-16 19:57:18 +00003253
3254 if( ssl->handshake )
3255 ssl_handshake_free( ssl->handshake );
3256 else
Paul Bakker6e339b52013-07-03 13:37:05 +02003257 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker48916f92012-09-16 19:57:18 +00003258
3259 if( ssl->handshake == NULL ||
3260 ssl->transform_negotiate == NULL ||
3261 ssl->session_negotiate == NULL )
3262 {
3263 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3264 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3265 }
3266
3267 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3268 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3269 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3270
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003271#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3272 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003273 md5_starts( &ssl->handshake->fin_md5 );
3274 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003275#endif
3276#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3277#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003278 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003279#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003280#if defined(POLARSSL_SHA512_C)
3281 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003282#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003283#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003284
3285 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003286 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003287
Paul Bakker61d113b2013-07-04 11:51:43 +02003288#if defined(POLARSSL_ECDH_C)
3289 ecdh_init( &ssl->handshake->ecdh_ctx );
3290#endif
3291
Paul Bakker48916f92012-09-16 19:57:18 +00003292 return( 0 );
3293}
3294
Paul Bakker5121ce52009-01-03 21:22:43 +00003295/*
3296 * Initialize an SSL context
3297 */
3298int ssl_init( ssl_context *ssl )
3299{
Paul Bakker48916f92012-09-16 19:57:18 +00003300 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 int len = SSL_BUFFER_LEN;
3302
3303 memset( ssl, 0, sizeof( ssl_context ) );
3304
Paul Bakker62f2dee2012-09-28 07:31:51 +00003305 /*
3306 * Sane defaults
3307 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003308 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3309 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3310 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3311 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003312
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003313 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003314
Paul Bakker62f2dee2012-09-28 07:31:51 +00003315#if defined(POLARSSL_DHM_C)
3316 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3317 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3318 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3319 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3320 {
3321 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3322 return( ret );
3323 }
3324#endif
3325
3326 /*
3327 * Prepare base structures
3328 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003329 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003331 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 ssl->in_msg = ssl->in_ctr + 13;
3333
3334 if( ssl->in_ctr == NULL )
3335 {
3336 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003337 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003338 }
3339
Paul Bakker6e339b52013-07-03 13:37:05 +02003340 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003341 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003342 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003343 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003344
3345 if( ssl->out_ctr == NULL )
3346 {
3347 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003348 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003349 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 }
3351
3352 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3353 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3354
Paul Bakker606b4ba2013-08-14 16:52:14 +02003355#if defined(POLARSSL_SSL_SESSION_TICKETS)
3356 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3357#endif
3358
Paul Bakker48916f92012-09-16 19:57:18 +00003359 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3360 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003361
3362 return( 0 );
3363}
3364
3365/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003366 * Reset an initialized and used SSL context for re-use while retaining
3367 * all application-set variables, function pointers and data.
3368 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003369int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003370{
Paul Bakker48916f92012-09-16 19:57:18 +00003371 int ret;
3372
Paul Bakker7eb013f2011-10-06 12:37:39 +00003373 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003374 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3375 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3376
3377 ssl->verify_data_len = 0;
3378 memset( ssl->own_verify_data, 0, 36 );
3379 memset( ssl->peer_verify_data, 0, 36 );
3380
Paul Bakker7eb013f2011-10-06 12:37:39 +00003381 ssl->in_offt = NULL;
3382
Paul Bakker92be97b2013-01-02 17:30:03 +01003383 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003384 ssl->in_msgtype = 0;
3385 ssl->in_msglen = 0;
3386 ssl->in_left = 0;
3387
3388 ssl->in_hslen = 0;
3389 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003390 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003391
Paul Bakker92be97b2013-01-02 17:30:03 +01003392 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003393 ssl->out_msgtype = 0;
3394 ssl->out_msglen = 0;
3395 ssl->out_left = 0;
3396
Paul Bakker48916f92012-09-16 19:57:18 +00003397 ssl->transform_in = NULL;
3398 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003399
3400 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3401 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003402
3403#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3404 if( ssl_hw_record_reset != NULL)
3405 {
3406 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003407 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003408 {
3409 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3410 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3411 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003412 }
3413#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003414
Paul Bakker48916f92012-09-16 19:57:18 +00003415 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003416 {
Paul Bakker48916f92012-09-16 19:57:18 +00003417 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003418 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003419 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003420 }
Paul Bakker48916f92012-09-16 19:57:18 +00003421
Paul Bakkerc0463502013-02-14 11:19:38 +01003422 if( ssl->session )
3423 {
3424 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003425 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003426 ssl->session = NULL;
3427 }
3428
Paul Bakker48916f92012-09-16 19:57:18 +00003429 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3430 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003431
3432 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003433}
3434
Paul Bakkera503a632013-08-14 13:48:06 +02003435#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003436/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003437 * Allocate and initialize ticket keys
3438 */
3439static int ssl_ticket_keys_init( ssl_context *ssl )
3440{
3441 int ret;
3442 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003443 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003444
3445 if( ssl->ticket_keys != NULL )
3446 return( 0 );
3447
3448 if( ( tkeys = polarssl_malloc( sizeof( ssl_ticket_keys ) ) ) == NULL )
3449 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3450
3451 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
3452 return( ret );
3453
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003454 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3455 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3456 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3457 {
3458 return( ret );
3459 }
3460
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003461 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
3462 return( ret );
3463
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003464 ssl->ticket_keys = tkeys;
3465
3466 return( 0 );
3467}
Paul Bakkera503a632013-08-14 13:48:06 +02003468#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003469
3470/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003471 * SSL set accessors
3472 */
3473void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3474{
3475 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003476
Paul Bakker606b4ba2013-08-14 16:52:14 +02003477#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003478 if( endpoint == SSL_IS_CLIENT )
3479 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003480#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003481}
3482
3483void ssl_set_authmode( ssl_context *ssl, int authmode )
3484{
3485 ssl->authmode = authmode;
3486}
3487
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003488#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003489void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003490 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003491 void *p_vrfy )
3492{
3493 ssl->f_vrfy = f_vrfy;
3494 ssl->p_vrfy = p_vrfy;
3495}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003496#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003497
Paul Bakker5121ce52009-01-03 21:22:43 +00003498void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003499 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003500 void *p_rng )
3501{
3502 ssl->f_rng = f_rng;
3503 ssl->p_rng = p_rng;
3504}
3505
3506void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003507 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003508 void *p_dbg )
3509{
3510 ssl->f_dbg = f_dbg;
3511 ssl->p_dbg = p_dbg;
3512}
3513
3514void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003515 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003516 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003517{
3518 ssl->f_recv = f_recv;
3519 ssl->f_send = f_send;
3520 ssl->p_recv = p_recv;
3521 ssl->p_send = p_send;
3522}
3523
Paul Bakker0a597072012-09-25 21:55:46 +00003524void ssl_set_session_cache( ssl_context *ssl,
3525 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3526 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003527{
Paul Bakker0a597072012-09-25 21:55:46 +00003528 ssl->f_get_cache = f_get_cache;
3529 ssl->p_get_cache = p_get_cache;
3530 ssl->f_set_cache = f_set_cache;
3531 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003532}
3533
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003534int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003535{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003536 int ret;
3537
3538 if( ssl == NULL ||
3539 session == NULL ||
3540 ssl->session_negotiate == NULL ||
3541 ssl->endpoint != SSL_IS_CLIENT )
3542 {
3543 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3544 }
3545
3546 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3547 return( ret );
3548
Paul Bakker0a597072012-09-25 21:55:46 +00003549 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003550
3551 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003552}
3553
Paul Bakkerb68cad62012-08-23 08:34:18 +00003554void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003555{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003556 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3557 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3558 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3559 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3560}
3561
3562void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3563 int major, int minor )
3564{
3565 if( major != SSL_MAJOR_VERSION_3 )
3566 return;
3567
3568 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3569 return;
3570
3571 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003572}
3573
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003574#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003575/* Add a new (empty) key_cert entry an return a pointer to it */
3576static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3577{
3578 ssl_key_cert *key_cert, *last;
3579
3580 if( ( key_cert = polarssl_malloc( sizeof( ssl_key_cert ) ) ) == NULL )
3581 return( NULL );
3582
3583 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3584
3585 /* Append the new key_cert to the (possibly empty) current list */
3586 if( ssl->key_cert == NULL )
3587 ssl->key_cert = key_cert;
3588 else
3589 {
3590 last = ssl->key_cert;
3591 while( last->next != NULL )
3592 last = last->next;
3593 last->next = key_cert;
3594 }
3595
3596 return key_cert;
3597}
3598
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003599void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003600 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003601{
3602 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003603 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 ssl->peer_cn = peer_cn;
3605}
3606
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003607int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003608 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003609{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003610 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3611
3612 if( key_cert == NULL )
3613 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3614
3615 key_cert->cert = own_cert;
3616 key_cert->key = pk_key;
3617
3618 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003619}
3620
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003621#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003622int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003623 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003624{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003625 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003626 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003627
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003628 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003629 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3630
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003631 if( ( key_cert->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
3632 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003633
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003634 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003635
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003636 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003637 if( ret != 0 )
3638 return( ret );
3639
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003640 if( ( ret = rsa_copy( key_cert->key->pk_ctx, rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003641 return( ret );
3642
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003643 key_cert->cert = own_cert;
3644 key_cert->key_own_alloc = 1;
3645
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003646 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003647}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003648#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003650int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003651 void *rsa_key,
3652 rsa_decrypt_func rsa_decrypt,
3653 rsa_sign_func rsa_sign,
3654 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003655{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003656 int ret;
3657 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003658
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003659 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003660 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3661
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003662 if( ( key_cert->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
3663 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003664
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003665 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003666
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003667 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3668 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3669 return( ret );
3670
3671 key_cert->cert = own_cert;
3672 key_cert->key_own_alloc = 1;
3673
3674 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003675}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003676#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003677
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003678#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003679 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003680 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
3681 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003682int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3683 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003684{
Paul Bakker6db455e2013-09-18 17:29:31 +02003685 if( psk == NULL || psk_identity == NULL )
3686 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3687
3688 if( ssl->psk != NULL )
3689 {
3690 polarssl_free( ssl->psk );
3691 polarssl_free( ssl->psk_identity );
3692 }
3693
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003694 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003695 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003696
3697 ssl->psk = polarssl_malloc( ssl->psk_len );
3698 ssl->psk_identity = polarssl_malloc( ssl->psk_identity_len );
3699
3700 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3701 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3702
3703 memcpy( ssl->psk, psk, ssl->psk_len );
3704 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003705
3706 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003707}
3708
3709void ssl_set_psk_cb( ssl_context *ssl,
3710 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3711 size_t),
3712 void *p_psk )
3713{
3714 ssl->f_psk = f_psk;
3715 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003716}
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003717#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003718 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003719 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3720 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003721
Paul Bakker48916f92012-09-16 19:57:18 +00003722#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003723int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003724{
3725 int ret;
3726
Paul Bakker48916f92012-09-16 19:57:18 +00003727 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 {
3729 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3730 return( ret );
3731 }
3732
Paul Bakker48916f92012-09-16 19:57:18 +00003733 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003734 {
3735 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3736 return( ret );
3737 }
3738
3739 return( 0 );
3740}
3741
Paul Bakker1b57b062011-01-06 15:48:19 +00003742int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3743{
3744 int ret;
3745
Paul Bakker48916f92012-09-16 19:57:18 +00003746 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003747 {
3748 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3749 return( ret );
3750 }
3751
Paul Bakker48916f92012-09-16 19:57:18 +00003752 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003753 {
3754 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3755 return( ret );
3756 }
3757
3758 return( 0 );
3759}
Paul Bakker48916f92012-09-16 19:57:18 +00003760#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003761
Paul Bakker0be444a2013-08-27 21:55:01 +02003762#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003763int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003764{
3765 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003766 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003767
3768 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003769
3770 if( ssl->hostname_len + 1 == 0 )
3771 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3772
Paul Bakker6e339b52013-07-03 13:37:05 +02003773 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003774
Paul Bakkerb15b8512012-01-13 13:44:06 +00003775 if( ssl->hostname == NULL )
3776 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3777
Paul Bakker3c2122f2013-06-24 19:03:14 +02003778 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003779 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003780
Paul Bakker40ea7de2009-05-03 10:18:48 +00003781 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003782
3783 return( 0 );
3784}
3785
Paul Bakker5701cdc2012-09-27 21:49:42 +00003786void ssl_set_sni( ssl_context *ssl,
3787 int (*f_sni)(void *, ssl_context *,
3788 const unsigned char *, size_t),
3789 void *p_sni )
3790{
3791 ssl->f_sni = f_sni;
3792 ssl->p_sni = p_sni;
3793}
Paul Bakker0be444a2013-08-27 21:55:01 +02003794#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003795
Paul Bakker490ecc82011-10-06 13:04:09 +00003796void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3797{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003798 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3799 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3800 {
3801 ssl->max_major_ver = major;
3802 ssl->max_minor_ver = minor;
3803 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003804}
3805
Paul Bakker1d29fb52012-09-28 13:28:45 +00003806void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3807{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003808 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3809 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3810 {
3811 ssl->min_major_ver = major;
3812 ssl->min_minor_ver = minor;
3813 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003814}
3815
Paul Bakker05decb22013-08-15 13:33:48 +02003816#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003817int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3818{
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003819 if( mfl_code >= sizeof( mfl_code_to_length ) ||
3820 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003821 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003822 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003823 }
3824
3825 ssl->mfl_code = mfl_code;
3826
3827 return( 0 );
3828}
Paul Bakker05decb22013-08-15 13:33:48 +02003829#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003830
Paul Bakker1f2bc622013-08-15 13:45:55 +02003831#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003832int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003833{
3834 if( ssl->endpoint != SSL_IS_CLIENT )
3835 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3836
Paul Bakker8c1ede62013-07-19 14:14:37 +02003837 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003838
3839 return( 0 );
3840}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003841#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003842
Paul Bakker48916f92012-09-16 19:57:18 +00003843void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3844{
3845 ssl->disable_renegotiation = renegotiation;
3846}
3847
3848void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3849{
3850 ssl->allow_legacy_renegotiation = allow_legacy;
3851}
3852
Paul Bakkera503a632013-08-14 13:48:06 +02003853#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003854int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3855{
3856 ssl->session_tickets = use_tickets;
3857
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003858 if( ssl->endpoint == SSL_IS_CLIENT )
3859 return( 0 );
3860
3861 if( ssl->f_rng == NULL )
3862 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3863
3864 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003865}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003866
3867void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3868{
3869 ssl->ticket_lifetime = lifetime;
3870}
Paul Bakkera503a632013-08-14 13:48:06 +02003871#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003872
Paul Bakker5121ce52009-01-03 21:22:43 +00003873/*
3874 * SSL get accessors
3875 */
Paul Bakker23986e52011-04-24 08:57:21 +00003876size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003877{
3878 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3879}
3880
Paul Bakkerff60ee62010-03-16 21:09:09 +00003881int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003882{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003883 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003884}
3885
Paul Bakkere3166ce2011-01-27 17:40:50 +00003886const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003887{
Paul Bakker926c8e42013-03-06 10:23:34 +01003888 if( ssl == NULL || ssl->session == NULL )
3889 return NULL;
3890
Paul Bakkere3166ce2011-01-27 17:40:50 +00003891 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003892}
3893
Paul Bakker43ca69c2011-01-15 17:35:19 +00003894const char *ssl_get_version( const ssl_context *ssl )
3895{
3896 switch( ssl->minor_ver )
3897 {
3898 case SSL_MINOR_VERSION_0:
3899 return( "SSLv3.0" );
3900
3901 case SSL_MINOR_VERSION_1:
3902 return( "TLSv1.0" );
3903
3904 case SSL_MINOR_VERSION_2:
3905 return( "TLSv1.1" );
3906
Paul Bakker1ef83d62012-04-11 12:09:53 +00003907 case SSL_MINOR_VERSION_3:
3908 return( "TLSv1.2" );
3909
Paul Bakker43ca69c2011-01-15 17:35:19 +00003910 default:
3911 break;
3912 }
3913 return( "unknown" );
3914}
3915
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003916#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003917const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003918{
3919 if( ssl == NULL || ssl->session == NULL )
3920 return NULL;
3921
3922 return ssl->session->peer_cert;
3923}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003924#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003925
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003926int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3927{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003928 if( ssl == NULL ||
3929 dst == NULL ||
3930 ssl->session == NULL ||
3931 ssl->endpoint != SSL_IS_CLIENT )
3932 {
3933 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3934 }
3935
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003936 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003937}
3938
Paul Bakker5121ce52009-01-03 21:22:43 +00003939/*
Paul Bakker1961b702013-01-25 14:49:24 +01003940 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003941 */
Paul Bakker1961b702013-01-25 14:49:24 +01003942int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003943{
Paul Bakker40e46942009-01-03 21:51:57 +00003944 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003945
Paul Bakker40e46942009-01-03 21:51:57 +00003946#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003947 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01003948 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003949#endif
3950
Paul Bakker40e46942009-01-03 21:51:57 +00003951#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003952 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003953 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003954#endif
3955
Paul Bakker1961b702013-01-25 14:49:24 +01003956 return( ret );
3957}
3958
3959/*
3960 * Perform the SSL handshake
3961 */
3962int ssl_handshake( ssl_context *ssl )
3963{
3964 int ret = 0;
3965
3966 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
3967
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02003968#if defined(POLARSSL_X509_CRT_PARSE_C)
3969 ssl->handshake->key_cert = ssl->key_cert;
3970#endif
3971
Paul Bakker1961b702013-01-25 14:49:24 +01003972 while( ssl->state != SSL_HANDSHAKE_OVER )
3973 {
3974 ret = ssl_handshake_step( ssl );
3975
3976 if( ret != 0 )
3977 break;
3978 }
3979
Paul Bakker5121ce52009-01-03 21:22:43 +00003980 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
3981
3982 return( ret );
3983}
3984
3985/*
Paul Bakker48916f92012-09-16 19:57:18 +00003986 * Renegotiate current connection
3987 */
3988int ssl_renegotiate( ssl_context *ssl )
3989{
3990 int ret;
3991
3992 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
3993
3994 if( ssl->state != SSL_HANDSHAKE_OVER )
3995 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3996
3997 ssl->state = SSL_HELLO_REQUEST;
3998 ssl->renegotiation = SSL_RENEGOTIATION;
3999
4000 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4001 return( ret );
4002
4003 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4004 {
4005 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4006 return( ret );
4007 }
4008
4009 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4010
4011 return( 0 );
4012}
4013
4014/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004015 * Receive application data decrypted from the SSL layer
4016 */
Paul Bakker23986e52011-04-24 08:57:21 +00004017int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004018{
Paul Bakker23986e52011-04-24 08:57:21 +00004019 int ret;
4020 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004021
4022 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4023
4024 if( ssl->state != SSL_HANDSHAKE_OVER )
4025 {
4026 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4027 {
4028 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4029 return( ret );
4030 }
4031 }
4032
4033 if( ssl->in_offt == NULL )
4034 {
4035 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4036 {
Paul Bakker831a7552011-05-18 13:32:51 +00004037 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4038 return( 0 );
4039
Paul Bakker5121ce52009-01-03 21:22:43 +00004040 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4041 return( ret );
4042 }
4043
4044 if( ssl->in_msglen == 0 &&
4045 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4046 {
4047 /*
4048 * OpenSSL sends empty messages to randomize the IV
4049 */
4050 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4051 {
Paul Bakker831a7552011-05-18 13:32:51 +00004052 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4053 return( 0 );
4054
Paul Bakker5121ce52009-01-03 21:22:43 +00004055 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4056 return( ret );
4057 }
4058 }
4059
Paul Bakker48916f92012-09-16 19:57:18 +00004060 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4061 {
4062 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4063
4064 if( ssl->endpoint == SSL_IS_CLIENT &&
4065 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4066 ssl->in_hslen != 4 ) )
4067 {
4068 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4069 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4070 }
4071
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004072 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4073 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4074 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004075 {
4076 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4077
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004078#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004079 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004080 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004081 /*
4082 * SSLv3 does not have a "no_renegotiation" alert
4083 */
4084 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4085 return( ret );
4086 }
4087 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004088#endif
4089#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4090 defined(POLARSSL_SSL_PROTO_TLS1_2)
4091 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004092 {
4093 if( ( ret = ssl_send_alert_message( ssl,
4094 SSL_ALERT_LEVEL_WARNING,
4095 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4096 {
4097 return( ret );
4098 }
Paul Bakker48916f92012-09-16 19:57:18 +00004099 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004100 else
4101#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004102 {
4103 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004104 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004105 }
Paul Bakker48916f92012-09-16 19:57:18 +00004106 }
4107 else
4108 {
4109 if( ( ret = ssl_renegotiate( ssl ) ) != 0 )
4110 {
4111 SSL_DEBUG_RET( 1, "ssl_renegotiate", ret );
4112 return( ret );
4113 }
4114
4115 return( POLARSSL_ERR_NET_WANT_READ );
4116 }
4117 }
4118 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004119 {
4120 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004121 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004122 }
4123
4124 ssl->in_offt = ssl->in_msg;
4125 }
4126
4127 n = ( len < ssl->in_msglen )
4128 ? len : ssl->in_msglen;
4129
4130 memcpy( buf, ssl->in_offt, n );
4131 ssl->in_msglen -= n;
4132
4133 if( ssl->in_msglen == 0 )
4134 /* all bytes consumed */
4135 ssl->in_offt = NULL;
4136 else
4137 /* more data available */
4138 ssl->in_offt += n;
4139
4140 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4141
Paul Bakker23986e52011-04-24 08:57:21 +00004142 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004143}
4144
4145/*
4146 * Send application data to be encrypted by the SSL layer
4147 */
Paul Bakker23986e52011-04-24 08:57:21 +00004148int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004149{
Paul Bakker23986e52011-04-24 08:57:21 +00004150 int ret;
4151 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004152 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004153
4154 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4155
4156 if( ssl->state != SSL_HANDSHAKE_OVER )
4157 {
4158 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4159 {
4160 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4161 return( ret );
4162 }
4163 }
4164
Paul Bakker05decb22013-08-15 13:33:48 +02004165#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004166 /*
4167 * Assume mfl_code is correct since it was checked when set
4168 */
4169 max_len = mfl_code_to_length[ssl->mfl_code];
4170
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004171 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004172 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004173 */
4174 if( ssl->session_out != NULL &&
4175 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4176 {
4177 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4178 }
Paul Bakker05decb22013-08-15 13:33:48 +02004179#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004180
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004181 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004182
Paul Bakker5121ce52009-01-03 21:22:43 +00004183 if( ssl->out_left != 0 )
4184 {
4185 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4186 {
4187 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4188 return( ret );
4189 }
4190 }
Paul Bakker887bd502011-06-08 13:10:54 +00004191 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004192 {
Paul Bakker887bd502011-06-08 13:10:54 +00004193 ssl->out_msglen = n;
4194 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4195 memcpy( ssl->out_msg, buf, n );
4196
4197 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4198 {
4199 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4200 return( ret );
4201 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004202 }
4203
4204 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4205
Paul Bakker23986e52011-04-24 08:57:21 +00004206 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004207}
4208
4209/*
4210 * Notify the peer that the connection is being closed
4211 */
4212int ssl_close_notify( ssl_context *ssl )
4213{
4214 int ret;
4215
4216 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4217
4218 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4219 {
4220 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4221 return( ret );
4222 }
4223
4224 if( ssl->state == SSL_HANDSHAKE_OVER )
4225 {
Paul Bakker48916f92012-09-16 19:57:18 +00004226 if( ( ret = ssl_send_alert_message( ssl,
4227 SSL_ALERT_LEVEL_WARNING,
4228 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004229 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004230 return( ret );
4231 }
4232 }
4233
4234 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4235
4236 return( ret );
4237}
4238
Paul Bakker48916f92012-09-16 19:57:18 +00004239void ssl_transform_free( ssl_transform *transform )
4240{
4241#if defined(POLARSSL_ZLIB_SUPPORT)
4242 deflateEnd( &transform->ctx_deflate );
4243 inflateEnd( &transform->ctx_inflate );
4244#endif
4245
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004246 cipher_free_ctx( &transform->cipher_ctx_enc );
4247 cipher_free_ctx( &transform->cipher_ctx_dec );
4248
Paul Bakker61d113b2013-07-04 11:51:43 +02004249 md_free_ctx( &transform->md_ctx_enc );
4250 md_free_ctx( &transform->md_ctx_dec );
4251
Paul Bakker48916f92012-09-16 19:57:18 +00004252 memset( transform, 0, sizeof( ssl_transform ) );
4253}
4254
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004255#if defined(POLARSSL_X509_CRT_PARSE_C)
4256static void ssl_key_cert_free( ssl_key_cert *key_cert )
4257{
4258 ssl_key_cert *cur = key_cert, *next;
4259
4260 while( cur != NULL )
4261 {
4262 next = cur->next;
4263
4264 if( cur->key_own_alloc )
4265 {
4266 pk_free( cur->key );
4267 polarssl_free( cur->key );
4268 }
4269 polarssl_free( cur );
4270
4271 cur = next;
4272 }
4273}
4274#endif /* POLARSSL_X509_CRT_PARSE_C */
4275
Paul Bakker48916f92012-09-16 19:57:18 +00004276void ssl_handshake_free( ssl_handshake_params *handshake )
4277{
4278#if defined(POLARSSL_DHM_C)
4279 dhm_free( &handshake->dhm_ctx );
4280#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004281#if defined(POLARSSL_ECDH_C)
4282 ecdh_free( &handshake->ecdh_ctx );
4283#endif
4284
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004285#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
4286 polarssl_free( handshake->curves );
4287#endif
4288
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004289#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4290 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4291 /*
4292 * Free only the linked list wrapper, not the keys themselves
4293 * since the belong to the SNI callback
4294 */
4295 if( handshake->sni_key_cert != NULL )
4296 {
4297 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4298
4299 while( cur != NULL )
4300 {
4301 next = cur->next;
4302 polarssl_free( cur );
4303 cur = next;
4304 }
4305 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004306#endif
4307
Paul Bakker48916f92012-09-16 19:57:18 +00004308 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4309}
4310
4311void ssl_session_free( ssl_session *session )
4312{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004313#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004314 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004315 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004316 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004317 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004318 }
Paul Bakkered27a042013-04-18 22:46:23 +02004319#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004320
Paul Bakkera503a632013-08-14 13:48:06 +02004321#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004322 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004323#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004324
Paul Bakker0a597072012-09-25 21:55:46 +00004325 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004326}
4327
Paul Bakker5121ce52009-01-03 21:22:43 +00004328/*
4329 * Free an SSL context
4330 */
4331void ssl_free( ssl_context *ssl )
4332{
4333 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4334
Paul Bakker5121ce52009-01-03 21:22:43 +00004335 if( ssl->out_ctr != NULL )
4336 {
4337 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004338 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004339 }
4340
4341 if( ssl->in_ctr != NULL )
4342 {
4343 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004344 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004345 }
4346
Paul Bakker40e46942009-01-03 21:51:57 +00004347#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004348 mpi_free( &ssl->dhm_P );
4349 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004350#endif
4351
Paul Bakker48916f92012-09-16 19:57:18 +00004352 if( ssl->transform )
4353 {
4354 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004355 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004356 }
4357
4358 if( ssl->handshake )
4359 {
4360 ssl_handshake_free( ssl->handshake );
4361 ssl_transform_free( ssl->transform_negotiate );
4362 ssl_session_free( ssl->session_negotiate );
4363
Paul Bakker6e339b52013-07-03 13:37:05 +02004364 polarssl_free( ssl->handshake );
4365 polarssl_free( ssl->transform_negotiate );
4366 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004367 }
4368
Paul Bakkerc0463502013-02-14 11:19:38 +01004369 if( ssl->session )
4370 {
4371 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004372 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004373 }
4374
Paul Bakkera503a632013-08-14 13:48:06 +02004375#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004376 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004377#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004378
Paul Bakker0be444a2013-08-27 21:55:01 +02004379#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004380 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004381 {
4382 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004383 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004384 ssl->hostname_len = 0;
4385 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004386#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004387
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02004388#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
4389 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
4390 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004391 if( ssl->psk != NULL )
4392 {
4393 memset( ssl->psk, 0, ssl->psk_len );
4394 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4395 polarssl_free( ssl->psk );
4396 polarssl_free( ssl->psk_identity );
4397 ssl->psk_len = 0;
4398 ssl->psk_identity_len = 0;
4399 }
4400#endif
4401
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004402#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004403 ssl_key_cert_free( ssl->key_cert );
4404#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004405
Paul Bakker05ef8352012-05-08 09:17:57 +00004406#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4407 if( ssl_hw_record_finish != NULL )
4408 {
4409 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4410 ssl_hw_record_finish( ssl );
4411 }
4412#endif
4413
Paul Bakker5121ce52009-01-03 21:22:43 +00004414 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004415
Paul Bakker86f04f42013-02-14 11:20:09 +01004416 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004417 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004418}
4419
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004420#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004421/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004422 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004423 */
4424unsigned char ssl_sig_from_pk( pk_context *pk )
4425{
4426#if defined(POLARSSL_RSA_C)
4427 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4428 return( SSL_SIG_RSA );
4429#endif
4430#if defined(POLARSSL_ECDSA_C)
4431 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4432 return( SSL_SIG_ECDSA );
4433#endif
4434 return( SSL_SIG_ANON );
4435}
4436
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004437pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4438{
4439 switch( sig )
4440 {
4441#if defined(POLARSSL_RSA_C)
4442 case SSL_SIG_RSA:
4443 return( POLARSSL_PK_RSA );
4444#endif
4445#if defined(POLARSSL_ECDSA_C)
4446 case SSL_SIG_ECDSA:
4447 return( POLARSSL_PK_ECDSA );
4448#endif
4449 default:
4450 return( POLARSSL_PK_NONE );
4451 }
4452}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004453#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004454
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004455/*
4456 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4457 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004458md_type_t ssl_md_alg_from_hash( unsigned char hash )
4459{
4460 switch( hash )
4461 {
4462#if defined(POLARSSL_MD5_C)
4463 case SSL_HASH_MD5:
4464 return( POLARSSL_MD_MD5 );
4465#endif
4466#if defined(POLARSSL_SHA1_C)
4467 case SSL_HASH_SHA1:
4468 return( POLARSSL_MD_SHA1 );
4469#endif
4470#if defined(POLARSSL_SHA256_C)
4471 case SSL_HASH_SHA224:
4472 return( POLARSSL_MD_SHA224 );
4473 case SSL_HASH_SHA256:
4474 return( POLARSSL_MD_SHA256 );
4475#endif
4476#if defined(POLARSSL_SHA512_C)
4477 case SSL_HASH_SHA384:
4478 return( POLARSSL_MD_SHA384 );
4479 case SSL_HASH_SHA512:
4480 return( POLARSSL_MD_SHA512 );
4481#endif
4482 default:
4483 return( POLARSSL_MD_NONE );
4484 }
4485}
4486
Paul Bakker5121ce52009-01-03 21:22:43 +00004487#endif