blob: 7450d34a1d4f2c10aedeadddddc53afacee824cb [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é-Gonnard8a3c64d2013-10-14 19:54:10 +0200828#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200829int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
830{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200831 unsigned char *p = ssl->handshake->premaster;
832 unsigned char *end = p + sizeof( ssl->handshake->premaster );
833
834 /*
835 * PMS = struct {
836 * opaque other_secret<0..2^16-1>;
837 * opaque psk<0..2^16-1>;
838 * };
839 * with "other_secret" depending on the particular key exchange
840 */
841#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
842 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
843 {
844 if( end - p < 2 + (int) ssl->psk_len )
845 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
846
847 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
848 *(p++) = (unsigned char)( ssl->psk_len );
849 p += ssl->psk_len;
850 }
851 else
852#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200853#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
854 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
855 {
856 /*
857 * other_secret already set by the ClientKeyExchange message,
858 * and is 48 bytes long
859 */
860 *p++ = 0;
861 *p++ = 48;
862 p += 48;
863 }
864 else
865#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200866#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
867 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
868 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200869 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200870 size_t len = ssl->handshake->dhm_ctx.len;
871
872 if( end - p < 2 + (int) len )
873 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
874
875 *(p++) = (unsigned char)( len >> 8 );
876 *(p++) = (unsigned char)( len );
877 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
878 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
879 {
880 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
881 return( ret );
882 }
883 p += len;
884
885 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
886 }
887 else
888#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
889#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
890 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
891 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200892 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200893 size_t zlen;
894
895 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
896 p + 2, end - (p + 2),
897 ssl->f_rng, ssl->p_rng ) ) != 0 )
898 {
899 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
900 return( ret );
901 }
902
903 *(p++) = (unsigned char)( zlen >> 8 );
904 *(p++) = (unsigned char)( zlen );
905 p += zlen;
906
907 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
908 }
909 else
910#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
911 {
912 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
913 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
914 }
915
916 /* opaque psk<0..2^16-1>; */
917 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
918 *(p++) = (unsigned char)( ssl->psk_len );
919 memcpy( p, ssl->psk, ssl->psk_len );
920 p += ssl->psk_len;
921
922 ssl->handshake->pmslen = p - ssl->handshake->premaster;
923
924 return( 0 );
925}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200926#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200927
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200928#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000929/*
930 * SSLv3.0 MAC functions
931 */
Paul Bakker68884e32013-01-07 18:20:04 +0100932static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
933 unsigned char *buf, size_t len,
934 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000935{
936 unsigned char header[11];
937 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100938 int padlen = 0;
939 int md_size = md_get_size( md_ctx->md_info );
940 int md_type = md_get_type( md_ctx->md_info );
941
942 if( md_type == POLARSSL_MD_MD5 )
943 padlen = 48;
944 else if( md_type == POLARSSL_MD_SHA1 )
945 padlen = 40;
946 else if( md_type == POLARSSL_MD_SHA256 )
947 padlen = 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000948
949 memcpy( header, ctr, 8 );
950 header[ 8] = (unsigned char) type;
951 header[ 9] = (unsigned char)( len >> 8 );
952 header[10] = (unsigned char)( len );
953
Paul Bakker68884e32013-01-07 18:20:04 +0100954 memset( padding, 0x36, padlen );
955 md_starts( md_ctx );
956 md_update( md_ctx, secret, md_size );
957 md_update( md_ctx, padding, padlen );
958 md_update( md_ctx, header, 11 );
959 md_update( md_ctx, buf, len );
960 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Paul Bakker68884e32013-01-07 18:20:04 +0100962 memset( padding, 0x5C, padlen );
963 md_starts( md_ctx );
964 md_update( md_ctx, secret, md_size );
965 md_update( md_ctx, padding, padlen );
966 md_update( md_ctx, buf + len, md_size );
967 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000968}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200969#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000970
Paul Bakker5121ce52009-01-03 21:22:43 +0000971/*
972 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200973 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000974static int ssl_encrypt_buf( ssl_context *ssl )
975{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200976 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000977
978 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
979
980 /*
981 * Add MAC then encrypt
982 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200983#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
985 {
Paul Bakker68884e32013-01-07 18:20:04 +0100986 ssl_mac( &ssl->transform_out->md_ctx_enc,
987 ssl->transform_out->mac_enc,
988 ssl->out_msg, ssl->out_msglen,
989 ssl->out_ctr, ssl->out_msgtype );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 }
991 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200992#endif
993#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
994 defined(POLARSSL_SSL_PROTO_TLS1_2)
995 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 {
Paul Bakker68884e32013-01-07 18:20:04 +0100997 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
998 md_hmac_update( &ssl->transform_out->md_ctx_enc,
999 ssl->out_msg, ssl->out_msglen );
1000 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1001 ssl->out_msg + ssl->out_msglen );
1002 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001004 else
1005#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001006 {
1007 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001008 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001009 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
1011 SSL_DEBUG_BUF( 4, "computed mac",
Paul Bakker48916f92012-09-16 19:57:18 +00001012 ssl->out_msg + ssl->out_msglen, ssl->transform_out->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
Paul Bakker48916f92012-09-16 19:57:18 +00001014 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
Paul Bakker68884e32013-01-07 18:20:04 +01001016#if defined(POLARSSL_CIPHER_NULL_CIPHER)
1017 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
1018 {
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001019 ; /* Nothing to do */
Paul Bakker68884e32013-01-07 18:20:04 +01001020 }
1021 else
1022#endif /* POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001023#if defined(POLARSSL_ARC4_C)
Paul Bakker68884e32013-01-07 18:20:04 +01001024 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001025 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001026 int ret;
1027 size_t olen = 0;
1028
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1030 "including %d bytes of padding",
1031 ssl->out_msglen, 0 ) );
1032
1033 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1034 ssl->out_msg, ssl->out_msglen );
1035
Paul Bakker45125bc2013-09-04 16:47:11 +02001036 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001037 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001038 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1039 return( ret );
1040 }
1041
1042 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1043 ssl->transform_out->iv_enc,
1044 ssl->transform_out->ivlen ) ) != 0 )
1045 {
1046 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001047 return( ret );
1048 }
1049
1050 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1051 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1052 &olen ) ) != 0 )
1053 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001054 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001055 return( ret );
1056 }
1057
1058 if( ssl->out_msglen != olen )
1059 {
1060 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1061 ssl->out_msglen, olen ) );
1062 // TODO Real error number
1063 return( -1 );
1064 }
1065
1066 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001067 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001068 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001069 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001070 return( ret );
1071 }
1072
1073 if( 0 != olen )
1074 {
1075 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1076 0, olen ) );
1077 // TODO Real error number
1078 return( -1 );
1079 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 }
Paul Bakker68884e32013-01-07 18:20:04 +01001081 else
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001082#endif /* POLARSSL_ARC4_C */
Paul Bakker68884e32013-01-07 18:20:04 +01001083#if defined(POLARSSL_GCM_C)
1084 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
1085 ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001086 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001087 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001088 unsigned char *enc_msg;
1089 unsigned char add_data[13];
1090 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1091
Paul Bakkerca4ab492012-04-18 14:23:57 +00001092 enc_msglen = ssl->out_msglen;
1093
1094 memcpy( add_data, ssl->out_ctr, 8 );
1095 add_data[8] = ssl->out_msgtype;
1096 add_data[9] = ssl->major_ver;
1097 add_data[10] = ssl->minor_ver;
1098 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1099 add_data[12] = ssl->out_msglen & 0xFF;
1100
1101 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1102 add_data, 13 );
1103
Paul Bakker68884e32013-01-07 18:20:04 +01001104 /*
1105 * Generate IV
1106 */
1107 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001108 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001109 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1110 if( ret != 0 )
1111 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001112
Paul Bakker68884e32013-01-07 18:20:04 +01001113 memcpy( ssl->out_iv,
1114 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1115 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001116
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001117 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1118 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1119
Paul Bakker68884e32013-01-07 18:20:04 +01001120 /*
1121 * Fix pointer positions and message length with added IV
1122 */
1123 enc_msg = ssl->out_msg;
1124 enc_msglen = ssl->out_msglen;
1125 ssl->out_msglen += ssl->transform_out->ivlen -
1126 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001127
Paul Bakker68884e32013-01-07 18:20:04 +01001128 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1129 "including %d bytes of padding",
1130 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001131
Paul Bakker68884e32013-01-07 18:20:04 +01001132 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1133 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001134
Paul Bakker68884e32013-01-07 18:20:04 +01001135 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001136 * Encrypt
1137 */
1138 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1139 ssl->transform_out->iv_enc,
1140 ssl->transform_out->ivlen ) ) != 0 ||
1141 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1142 {
1143 return( ret );
1144 }
1145
1146 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1147 add_data, 13 ) ) != 0 )
1148 {
1149 return( ret );
1150 }
1151
1152 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1153 enc_msg, enc_msglen,
1154 enc_msg, &olen ) ) != 0 )
1155 {
1156 return( ret );
1157 }
1158 totlen = olen;
1159
1160 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1161 enc_msg + olen, &olen ) ) != 0 )
1162 {
1163 return( ret );
1164 }
1165 totlen += olen;
1166
1167 if( totlen != enc_msglen )
1168 {
1169 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1170 return( -1 );
1171 }
1172
1173 /*
1174 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001175 */
1176 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001177
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001178 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1179 enc_msg + enc_msglen, 16 ) ) != 0 )
1180 {
1181 return( ret );
1182 }
Paul Bakker68884e32013-01-07 18:20:04 +01001183
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001184 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 else
Paul Bakker68884e32013-01-07 18:20:04 +01001187#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001188#if defined(POLARSSL_CIPHER_MODE_CBC)
1189 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1190 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001192 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001193 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001194 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001195
Paul Bakker48916f92012-09-16 19:57:18 +00001196 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1197 ssl->transform_out->ivlen;
1198 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 padlen = 0;
1200
1201 for( i = 0; i <= padlen; i++ )
1202 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1203
1204 ssl->out_msglen += padlen + 1;
1205
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001206 enc_msglen = ssl->out_msglen;
1207 enc_msg = ssl->out_msg;
1208
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001209#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001210 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001211 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1212 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001214 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001215 {
1216 /*
1217 * Generate IV
1218 */
Paul Bakker48916f92012-09-16 19:57:18 +00001219 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1220 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001221 if( ret != 0 )
1222 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001223
Paul Bakker92be97b2013-01-02 17:30:03 +01001224 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001225 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001226
1227 /*
1228 * Fix pointer positions and message length with added IV
1229 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001230 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001231 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001232 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001234#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001237 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001238 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
1240 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001241 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
Paul Bakker45125bc2013-09-04 16:47:11 +02001243 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001245 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1246 return( ret );
1247 }
1248
1249 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1250 ssl->transform_out->iv_enc,
1251 ssl->transform_out->ivlen ) ) != 0 )
1252 {
1253 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001254 return( ret );
1255 }
Paul Bakker68884e32013-01-07 18:20:04 +01001256
Paul Bakkercca5b812013-08-31 17:40:26 +02001257 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1258 enc_msg, enc_msglen, enc_msg,
1259 &olen ) ) != 0 )
1260 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001261 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001262 return( ret );
1263 }
Paul Bakker68884e32013-01-07 18:20:04 +01001264
Paul Bakkercca5b812013-08-31 17:40:26 +02001265 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001266
Paul Bakkercca5b812013-08-31 17:40:26 +02001267 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001268 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001269 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001270 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001271 return( ret );
1272 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001273
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 if( enc_msglen != olen )
1275 {
1276 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1277 enc_msglen, olen ) );
1278 // TODO Real error number
1279 return( -1 );
1280 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001281
1282#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001283 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1284 {
1285 /*
1286 * Save IV in SSL3 and TLS1
1287 */
1288 memcpy( ssl->transform_out->iv_enc,
1289 ssl->transform_out->cipher_ctx_enc.iv,
1290 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001292#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001294 else
1295#endif /* POLARSSL_CIPHER_MODE_CBC */
1296 {
1297 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1298 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001300
Paul Bakkerca4ab492012-04-18 14:23:57 +00001301 for( i = 8; i > 0; i-- )
1302 if( ++ssl->out_ctr[i - 1] != 0 )
1303 break;
1304
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1306
1307 return( 0 );
1308}
1309
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001310#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001311
Paul Bakker5121ce52009-01-03 21:22:43 +00001312static int ssl_decrypt_buf( ssl_context *ssl )
1313{
Paul Bakker45829992013-01-03 14:52:21 +01001314 size_t i, padlen = 0, correct = 1;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001315 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
1317 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1318
Paul Bakker48916f92012-09-16 19:57:18 +00001319 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 {
1321 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001322 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001323 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324 }
1325
Paul Bakker68884e32013-01-07 18:20:04 +01001326#if defined(POLARSSL_CIPHER_NULL_CIPHER)
1327 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 {
Paul Bakker68884e32013-01-07 18:20:04 +01001329 padlen = 0;
1330 }
1331 else
1332#endif /* POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker40e46942009-01-03 21:51:57 +00001333#if defined(POLARSSL_ARC4_C)
Paul Bakker68884e32013-01-07 18:20:04 +01001334 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
1335 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001336 int ret;
1337 size_t olen = 0;
1338
Paul Bakker68884e32013-01-07 18:20:04 +01001339 padlen = 0;
1340
Paul Bakker45125bc2013-09-04 16:47:11 +02001341 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001342 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001343 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1344 return( ret );
1345 }
1346
1347 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1348 ssl->transform_in->iv_dec,
1349 ssl->transform_in->ivlen ) ) != 0 )
1350 {
1351 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001352 return( ret );
1353 }
1354
1355 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1356 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1357 &olen ) ) != 0 )
1358 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001359 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001360 return( ret );
1361 }
1362
1363 if( ssl->in_msglen != olen )
1364 {
1365 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1366 // TODO Real error number
1367 return( -1 );
1368 }
1369
1370 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001371 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001372 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001373 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001374 return( ret );
1375 }
1376
1377 if( 0 != olen )
1378 {
1379 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1380 // TODO Real error number
1381 return( -1 );
1382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 }
Paul Bakker68884e32013-01-07 18:20:04 +01001384 else
1385#endif /* POLARSSL_ARC4_C */
1386#if defined(POLARSSL_GCM_C)
1387 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
1388 ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001389 {
1390 unsigned char *dec_msg;
1391 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001392 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001393 unsigned char add_data[13];
1394 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1395
Paul Bakker68884e32013-01-07 18:20:04 +01001396 padlen = 0;
1397
1398 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1399 ssl->transform_in->fixed_ivlen );
1400 dec_msglen -= 16;
1401 dec_msg = ssl->in_msg;
1402 dec_msg_result = ssl->in_msg;
1403 ssl->in_msglen = dec_msglen;
1404
1405 memcpy( add_data, ssl->in_ctr, 8 );
1406 add_data[8] = ssl->in_msgtype;
1407 add_data[9] = ssl->major_ver;
1408 add_data[10] = ssl->minor_ver;
1409 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1410 add_data[12] = ssl->in_msglen & 0xFF;
1411
1412 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1413 add_data, 13 );
1414
1415 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1416 ssl->in_iv,
1417 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1418
1419 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1420 ssl->transform_in->ivlen );
1421 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1422
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001423 /*
1424 * Decrypt
1425 */
1426 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1427 ssl->transform_in->iv_dec,
1428 ssl->transform_in->ivlen ) ) != 0 ||
1429 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001430 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001431 return( ret );
1432 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001433
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001434 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1435 add_data, 13 ) ) != 0 )
1436 {
1437 return( ret );
1438 }
1439
1440 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1441 dec_msg, dec_msglen,
1442 dec_msg_result, &olen ) ) != 0 )
1443 {
1444 return( ret );
1445 }
1446 totlen = olen;
1447
1448 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1449 dec_msg_result + olen, &olen ) ) != 0 )
1450 {
1451 return( ret );
1452 }
1453 totlen += olen;
1454
1455 if( totlen != dec_msglen )
1456 {
1457 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1458 return( -1 );
1459 }
1460
1461 /*
1462 * Authenticate
1463 */
1464 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1465 dec_msg + dec_msglen, 16 ) ) != 0 )
1466 {
1467 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001468 return( POLARSSL_ERR_SSL_INVALID_MAC );
1469 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001470
Paul Bakkerca4ab492012-04-18 14:23:57 +00001471 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 else
Paul Bakker68884e32013-01-07 18:20:04 +01001473#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001474#if defined(POLARSSL_CIPHER_MODE_CBC)
1475 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1476 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 {
Paul Bakker45829992013-01-03 14:52:21 +01001478 /*
1479 * Decrypt and check the padding
1480 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001481 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001482 unsigned char *dec_msg;
1483 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001484 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001485 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001486 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001487
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 /*
Paul Bakker45829992013-01-03 14:52:21 +01001489 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 */
Paul Bakker48916f92012-09-16 19:57:18 +00001491 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 {
1493 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001494 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001495 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 }
1497
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001498#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001499 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1500 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001501#endif
Paul Bakker45829992013-01-03 14:52:21 +01001502
1503 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1504 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1505 {
1506 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1507 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1508 return( POLARSSL_ERR_SSL_INVALID_MAC );
1509 }
1510
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001511 dec_msglen = ssl->in_msglen;
1512 dec_msg = ssl->in_msg;
1513 dec_msg_result = ssl->in_msg;
1514
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001515#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001516 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001517 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001518 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001519 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001520 {
Paul Bakker48916f92012-09-16 19:57:18 +00001521 dec_msglen -= ssl->transform_in->ivlen;
1522 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001523
Paul Bakker48916f92012-09-16 19:57:18 +00001524 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001525 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001526 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001527#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001528
Paul Bakker45125bc2013-09-04 16:47:11 +02001529 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001531 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1532 return( ret );
1533 }
1534
1535 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1536 ssl->transform_in->iv_dec,
1537 ssl->transform_in->ivlen ) ) != 0 )
1538 {
1539 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001540 return( ret );
1541 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001542
Paul Bakkercca5b812013-08-31 17:40:26 +02001543 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1544 dec_msg, dec_msglen, dec_msg_result,
1545 &olen ) ) != 0 )
1546 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001547 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001548 return( ret );
1549 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001550
Paul Bakkercca5b812013-08-31 17:40:26 +02001551 dec_msglen -= olen;
1552 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001553 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001554 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001555 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001556 return( ret );
1557 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001558
Paul Bakkercca5b812013-08-31 17:40:26 +02001559 if( dec_msglen != olen )
1560 {
1561 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1562 // TODO Real error number
1563 return( -1 );
1564 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001565
1566#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001567 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1568 {
1569 /*
1570 * Save IV in SSL3 and TLS1
1571 */
1572 memcpy( ssl->transform_in->iv_dec,
1573 ssl->transform_in->cipher_ctx_dec.iv,
1574 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001576#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001577
1578 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001579
1580 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1581 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001582#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001583 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1584 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001585#endif
Paul Bakker45829992013-01-03 14:52:21 +01001586 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001587 correct = 0;
1588 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001589
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001590#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1592 {
Paul Bakker48916f92012-09-16 19:57:18 +00001593 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001595#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001596 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1597 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001598 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001599#endif
Paul Bakker45829992013-01-03 14:52:21 +01001600 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001601 }
1602 }
1603 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001604#endif
1605#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1606 defined(POLARSSL_SSL_PROTO_TLS1_2)
1607 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 {
1609 /*
Paul Bakker45829992013-01-03 14:52:21 +01001610 * TLSv1+: always check the padding up to the first failure
1611 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001612 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001613 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001614 size_t padding_idx = ssl->in_msglen - padlen - 1;
1615
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001616 for( i = 1; i <= 256; i++ )
1617 {
1618 real_count &= ( i <= padlen );
1619 pad_count += real_count *
1620 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1621 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001622
1623 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001624
Paul Bakkerd66f0702013-01-31 16:57:45 +01001625#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001626 if( padlen > 0 && correct == 0)
1627 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001628#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001629 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001630 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001631 else
1632#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1633 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001634 {
1635 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001636 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001637 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001638 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001639 else
1640#endif /* POLARSSL_CIPHER_MODE_CBC */
1641 {
1642 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1643 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1644 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001645
1646 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1647 ssl->in_msg, ssl->in_msglen );
1648
1649 /*
1650 * Always compute the MAC (RFC4346, CBCTIME).
1651 */
Paul Bakker48916f92012-09-16 19:57:18 +00001652 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
1654 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1655 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
1656
Paul Bakker45829992013-01-03 14:52:21 +01001657 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001659#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001660 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1661 {
Paul Bakker68884e32013-01-07 18:20:04 +01001662 ssl_mac( &ssl->transform_in->md_ctx_dec,
1663 ssl->transform_in->mac_dec,
1664 ssl->in_msg, ssl->in_msglen,
1665 ssl->in_ctr, ssl->in_msgtype );
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 }
1667 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001668#endif /* POLARSSL_SSL_PROTO_SSL3 */
1669#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1670 defined(POLARSSL_SSL_PROTO_TLS1_2)
1671 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001672 {
Paul Bakker45829992013-01-03 14:52:21 +01001673 /*
1674 * Process MAC and always update for padlen afterwards to make
1675 * total time independent of padlen
Paul Bakkere47b34b2013-02-27 14:48:00 +01001676 *
1677 * extra_run compensates MAC check for padlen
1678 *
1679 * Known timing attacks:
1680 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1681 *
1682 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1683 * correctly. (We round down instead of up, so -56 is the correct
1684 * value for our calculations instead of -55)
Paul Bakker45829992013-01-03 14:52:21 +01001685 */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001686 int j, extra_run = 0;
1687 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1688 ( 13 + ssl->in_msglen + 8 ) / 64;
1689
1690 extra_run &= correct * 0xFF;
1691
Paul Bakker68884e32013-01-07 18:20:04 +01001692 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1693 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1694 ssl->in_msglen );
1695 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1696 ssl->in_msg + ssl->in_msglen );
1697 for( j = 0; j < extra_run; j++ )
1698 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001699
Paul Bakker68884e32013-01-07 18:20:04 +01001700 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001702 else
1703#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1704 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001705 {
1706 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001707 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001708 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001709
Paul Bakker48916f92012-09-16 19:57:18 +00001710 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001711 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
Paul Bakker48916f92012-09-16 19:57:18 +00001712 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001713
1714 if( memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Paul Bakker48916f92012-09-16 19:57:18 +00001715 ssl->transform_in->maclen ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001716 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001717#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001718 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001719#endif
Paul Bakker45829992013-01-03 14:52:21 +01001720 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 }
1722
1723 /*
Paul Bakker45829992013-01-03 14:52:21 +01001724 * Finally check the correct flag
Paul Bakker5121ce52009-01-03 21:22:43 +00001725 */
Paul Bakker45829992013-01-03 14:52:21 +01001726 if( correct == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +00001727 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001728
1729 if( ssl->in_msglen == 0 )
1730 {
1731 ssl->nb_zero++;
1732
1733 /*
1734 * Three or more empty messages may be a DoS attack
1735 * (excessive CPU consumption).
1736 */
1737 if( ssl->nb_zero > 3 )
1738 {
1739 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1740 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001741 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001742 }
1743 }
1744 else
1745 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001746
Paul Bakker23986e52011-04-24 08:57:21 +00001747 for( i = 8; i > 0; i-- )
1748 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001749 break;
1750
1751 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1752
1753 return( 0 );
1754}
1755
Paul Bakker2770fbd2012-07-03 13:30:23 +00001756#if defined(POLARSSL_ZLIB_SUPPORT)
1757/*
1758 * Compression/decompression functions
1759 */
1760static int ssl_compress_buf( ssl_context *ssl )
1761{
1762 int ret;
1763 unsigned char *msg_post = ssl->out_msg;
1764 size_t len_pre = ssl->out_msglen;
1765 unsigned char *msg_pre;
1766
1767 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1768
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001769 if( len_pre == 0 )
1770 return( 0 );
1771
Paul Bakker6e339b52013-07-03 13:37:05 +02001772 msg_pre = (unsigned char*) polarssl_malloc( len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001773 if( msg_pre == NULL )
1774 {
1775 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len_pre ) );
1776 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1777 }
1778
1779 memcpy( msg_pre, ssl->out_msg, len_pre );
1780
1781 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1782 ssl->out_msglen ) );
1783
1784 SSL_DEBUG_BUF( 4, "before compression: output payload",
1785 ssl->out_msg, ssl->out_msglen );
1786
Paul Bakker48916f92012-09-16 19:57:18 +00001787 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1788 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1789 ssl->transform_out->ctx_deflate.next_out = msg_post;
1790 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001791
Paul Bakker48916f92012-09-16 19:57:18 +00001792 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001793 if( ret != Z_OK )
1794 {
1795 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1796 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1797 }
1798
Paul Bakker48916f92012-09-16 19:57:18 +00001799 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001800
Paul Bakker6e339b52013-07-03 13:37:05 +02001801 polarssl_free( msg_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001802
1803 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1804 ssl->out_msglen ) );
1805
1806 SSL_DEBUG_BUF( 4, "after compression: output payload",
1807 ssl->out_msg, ssl->out_msglen );
1808
1809 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1810
1811 return( 0 );
1812}
1813
1814static int ssl_decompress_buf( ssl_context *ssl )
1815{
1816 int ret;
1817 unsigned char *msg_post = ssl->in_msg;
1818 size_t len_pre = ssl->in_msglen;
1819 unsigned char *msg_pre;
1820
1821 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1822
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001823 if( len_pre == 0 )
1824 return( 0 );
1825
Paul Bakker6e339b52013-07-03 13:37:05 +02001826 msg_pre = (unsigned char*) polarssl_malloc( len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001827 if( msg_pre == NULL )
1828 {
1829 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len_pre ) );
1830 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1831 }
1832
1833 memcpy( msg_pre, ssl->in_msg, len_pre );
1834
1835 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1836 ssl->in_msglen ) );
1837
1838 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1839 ssl->in_msg, ssl->in_msglen );
1840
Paul Bakker48916f92012-09-16 19:57:18 +00001841 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1842 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1843 ssl->transform_in->ctx_inflate.next_out = msg_post;
1844 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001845
Paul Bakker48916f92012-09-16 19:57:18 +00001846 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001847 if( ret != Z_OK )
1848 {
1849 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1850 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1851 }
1852
Paul Bakker48916f92012-09-16 19:57:18 +00001853 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001854
Paul Bakker6e339b52013-07-03 13:37:05 +02001855 polarssl_free( msg_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001856
1857 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1858 ssl->in_msglen ) );
1859
1860 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1861 ssl->in_msg, ssl->in_msglen );
1862
1863 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1864
1865 return( 0 );
1866}
1867#endif /* POLARSSL_ZLIB_SUPPORT */
1868
Paul Bakker5121ce52009-01-03 21:22:43 +00001869/*
1870 * Fill the input message buffer
1871 */
Paul Bakker23986e52011-04-24 08:57:21 +00001872int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001873{
Paul Bakker23986e52011-04-24 08:57:21 +00001874 int ret;
1875 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
1877 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1878
1879 while( ssl->in_left < nb_want )
1880 {
1881 len = nb_want - ssl->in_left;
1882 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1883
1884 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1885 ssl->in_left, nb_want ) );
1886 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1887
Paul Bakker831a7552011-05-18 13:32:51 +00001888 if( ret == 0 )
1889 return( POLARSSL_ERR_SSL_CONN_EOF );
1890
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 if( ret < 0 )
1892 return( ret );
1893
1894 ssl->in_left += ret;
1895 }
1896
1897 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1898
1899 return( 0 );
1900}
1901
1902/*
1903 * Flush any data not yet written
1904 */
1905int ssl_flush_output( ssl_context *ssl )
1906{
1907 int ret;
1908 unsigned char *buf;
1909
1910 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1911
1912 while( ssl->out_left > 0 )
1913 {
1914 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1915 5 + ssl->out_msglen, ssl->out_left ) );
1916
Paul Bakker5bd42292012-12-19 14:40:42 +01001917 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001919
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1921
1922 if( ret <= 0 )
1923 return( ret );
1924
1925 ssl->out_left -= ret;
1926 }
1927
1928 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1929
1930 return( 0 );
1931}
1932
1933/*
1934 * Record layer functions
1935 */
1936int ssl_write_record( ssl_context *ssl )
1937{
Paul Bakker05ef8352012-05-08 09:17:57 +00001938 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001939 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001940
1941 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1942
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1944 {
1945 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1946 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1947 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1948
Paul Bakker48916f92012-09-16 19:57:18 +00001949 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 }
1951
Paul Bakker2770fbd2012-07-03 13:30:23 +00001952#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001953 if( ssl->transform_out != NULL &&
1954 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001955 {
1956 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1957 {
1958 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1959 return( ret );
1960 }
1961
1962 len = ssl->out_msglen;
1963 }
1964#endif /*POLARSSL_ZLIB_SUPPORT */
1965
Paul Bakker05ef8352012-05-08 09:17:57 +00001966#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1967 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001969 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
Paul Bakker05ef8352012-05-08 09:17:57 +00001971 ret = ssl_hw_record_write( ssl );
1972 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1973 {
1974 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1975 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1976 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001977
1978 if( ret == 0 )
1979 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001980 }
1981#endif
1982 if( !done )
1983 {
1984 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1985 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1986 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1988 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001989
Paul Bakker48916f92012-09-16 19:57:18 +00001990 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001991 {
1992 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1993 {
1994 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1995 return( ret );
1996 }
1997
1998 len = ssl->out_msglen;
1999 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2000 ssl->out_hdr[4] = (unsigned char)( len );
2001 }
2002
2003 ssl->out_left = 5 + ssl->out_msglen;
2004
2005 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2006 "version = [%d:%d], msglen = %d",
2007 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2008 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2009
2010 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002011 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 }
2013
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2015 {
2016 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2017 return( ret );
2018 }
2019
2020 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2021
2022 return( 0 );
2023}
2024
2025int ssl_read_record( ssl_context *ssl )
2026{
Paul Bakker05ef8352012-05-08 09:17:57 +00002027 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002028
2029 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2030
Paul Bakker68884e32013-01-07 18:20:04 +01002031 SSL_DEBUG_BUF( 4, "input record from network",
2032 ssl->in_hdr, 5 + ssl->in_msglen );
2033
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 if( ssl->in_hslen != 0 &&
2035 ssl->in_hslen < ssl->in_msglen )
2036 {
2037 /*
2038 * Get next Handshake message in the current record
2039 */
2040 ssl->in_msglen -= ssl->in_hslen;
2041
Paul Bakker8934a982011-08-05 11:11:53 +00002042 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002043 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002044
2045 ssl->in_hslen = 4;
2046 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2047
2048 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2049 " %d, type = %d, hslen = %d",
2050 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2051
2052 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2053 {
2054 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002055 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 }
2057
2058 if( ssl->in_msglen < ssl->in_hslen )
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
Paul Bakker48916f92012-09-16 19:57:18 +00002064 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002065
2066 return( 0 );
2067 }
2068
2069 ssl->in_hslen = 0;
2070
2071 /*
2072 * Read the record header and validate it
2073 */
2074 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2075 {
2076 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2077 return( ret );
2078 }
2079
2080 ssl->in_msgtype = ssl->in_hdr[0];
2081 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2082
2083 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2084 "version = [%d:%d], msglen = %d",
2085 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2086 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2087
2088 if( ssl->in_hdr[1] != ssl->major_ver )
2089 {
2090 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002091 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002094 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 {
2096 SSL_DEBUG_MSG( 1, ( "minor 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
2100 /*
2101 * Make sure the message length is acceptable
2102 */
Paul Bakker48916f92012-09-16 19:57:18 +00002103 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 {
2105 if( ssl->in_msglen < 1 ||
2106 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2107 {
2108 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002109 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 }
2111 }
2112 else
2113 {
Paul Bakker48916f92012-09-16 19:57:18 +00002114 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 {
2116 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002117 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 }
2119
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002120#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002122 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 {
2124 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002125 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002128
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002129#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2130 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 /*
2132 * TLS encrypted messages can have up to 256 bytes of padding
2133 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002134 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002135 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 {
2137 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002138 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002140#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 }
2142
2143 /*
2144 * Read and optionally decrypt the message contents
2145 */
2146 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2147 {
2148 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2149 return( ret );
2150 }
2151
2152 SSL_DEBUG_BUF( 4, "input record from network",
2153 ssl->in_hdr, 5 + ssl->in_msglen );
2154
Paul Bakker05ef8352012-05-08 09:17:57 +00002155#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2156 if( ssl_hw_record_read != NULL)
2157 {
2158 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2159
2160 ret = ssl_hw_record_read( ssl );
2161 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2162 {
2163 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2164 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2165 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002166
2167 if( ret == 0 )
2168 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002169 }
2170#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002171 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 {
2173 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2174 {
Paul Bakker40865c82013-01-31 17:13:13 +01002175#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2176 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2177 {
2178 ssl_send_alert_message( ssl,
2179 SSL_ALERT_LEVEL_FATAL,
2180 SSL_ALERT_MSG_BAD_RECORD_MAC );
2181 }
2182#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2184 return( ret );
2185 }
2186
2187 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2188 ssl->in_msg, ssl->in_msglen );
2189
2190 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2191 {
2192 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002193 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 }
2195 }
2196
Paul Bakker2770fbd2012-07-03 13:30:23 +00002197#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002198 if( ssl->transform_in != NULL &&
2199 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002200 {
2201 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2202 {
2203 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2204 return( ret );
2205 }
2206
2207 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2208 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2209 }
2210#endif /* POLARSSL_ZLIB_SUPPORT */
2211
Paul Bakker0a925182012-04-16 06:46:41 +00002212 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2213 ssl->in_msgtype != SSL_MSG_ALERT &&
2214 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2215 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2216 {
2217 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2218
Paul Bakker48916f92012-09-16 19:57:18 +00002219 if( ( ret = ssl_send_alert_message( ssl,
2220 SSL_ALERT_LEVEL_FATAL,
2221 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002222 {
2223 return( ret );
2224 }
2225
2226 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2227 }
2228
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2230 {
2231 ssl->in_hslen = 4;
2232 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2233
2234 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2235 " %d, type = %d, hslen = %d",
2236 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2237
2238 /*
2239 * Additional checks to validate the handshake header
2240 */
2241 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2242 {
2243 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002244 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 }
2246
2247 if( ssl->in_msglen < ssl->in_hslen )
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
Paul Bakker48916f92012-09-16 19:57:18 +00002253 if( ssl->state != SSL_HANDSHAKE_OVER )
2254 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
2256
2257 if( ssl->in_msgtype == SSL_MSG_ALERT )
2258 {
2259 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2260 ssl->in_msg[0], ssl->in_msg[1] ) );
2261
2262 /*
2263 * Ignore non-fatal alerts, except close_notify
2264 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002265 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002267 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2268 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002269 /**
2270 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2271 * error identifier.
2272 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002273 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 }
2275
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002276 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2277 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 {
2279 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002280 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 }
2282 }
2283
2284 ssl->in_left = 0;
2285
2286 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2287
2288 return( 0 );
2289}
2290
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002291int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2292{
2293 int ret;
2294
2295 if( ( ret = ssl_send_alert_message( ssl,
2296 SSL_ALERT_LEVEL_FATAL,
2297 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2298 {
2299 return( ret );
2300 }
2301
2302 return( 0 );
2303}
2304
Paul Bakker0a925182012-04-16 06:46:41 +00002305int ssl_send_alert_message( ssl_context *ssl,
2306 unsigned char level,
2307 unsigned char message )
2308{
2309 int ret;
2310
2311 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2312
2313 ssl->out_msgtype = SSL_MSG_ALERT;
2314 ssl->out_msglen = 2;
2315 ssl->out_msg[0] = level;
2316 ssl->out_msg[1] = message;
2317
2318 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2319 {
2320 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2321 return( ret );
2322 }
2323
2324 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2325
2326 return( 0 );
2327}
2328
Paul Bakker5121ce52009-01-03 21:22:43 +00002329/*
2330 * Handshake functions
2331 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002332#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2333 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002334 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2335 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002336int ssl_write_certificate( ssl_context *ssl )
2337{
Paul Bakkered27a042013-04-18 22:46:23 +02002338 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002339 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002340
2341 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2342
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002343 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002344 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2345 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002346 {
2347 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2348 ssl->state++;
2349 return( 0 );
2350 }
2351
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002352 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002353 return( ret );
2354}
2355
2356int ssl_parse_certificate( ssl_context *ssl )
2357{
2358 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2359 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2360
2361 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2362
2363 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002364 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2365 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002366 {
2367 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2368 ssl->state++;
2369 return( 0 );
2370 }
2371
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002372 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373 return( ret );
2374}
2375#else
2376int ssl_write_certificate( ssl_context *ssl )
2377{
2378 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2379 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002380 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002381 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2382
2383 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2384
2385 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002386 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2387 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002388 {
2389 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2390 ssl->state++;
2391 return( 0 );
2392 }
2393
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 if( ssl->endpoint == SSL_IS_CLIENT )
2395 {
2396 if( ssl->client_auth == 0 )
2397 {
2398 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2399 ssl->state++;
2400 return( 0 );
2401 }
2402
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002403#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 /*
2405 * If using SSLv3 and got no cert, send an Alert message
2406 * (otherwise an empty Certificate message will be sent).
2407 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002408 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2410 {
2411 ssl->out_msglen = 2;
2412 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002413 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2414 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
2416 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2417 goto write_msg;
2418 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002419#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 }
2421 else /* SSL_IS_SERVER */
2422 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002423 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 {
2425 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002426 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 }
2428 }
2429
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002430 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
2432 /*
2433 * 0 . 0 handshake type
2434 * 1 . 3 handshake length
2435 * 4 . 6 length of all certs
2436 * 7 . 9 length of cert. 1
2437 * 10 . n-1 peer certificate
2438 * n . n+2 length of cert. 2
2439 * n+3 . ... upper level cert, etc.
2440 */
2441 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002442 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
Paul Bakker29087132010-03-21 21:03:34 +00002444 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 {
2446 n = crt->raw.len;
2447 if( i + 3 + n > SSL_MAX_CONTENT_LEN )
2448 {
2449 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2450 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002451 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 }
2453
2454 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2455 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2456 ssl->out_msg[i + 2] = (unsigned char)( n );
2457
2458 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2459 i += n; crt = crt->next;
2460 }
2461
2462 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2463 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2464 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2465
2466 ssl->out_msglen = i;
2467 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2468 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2469
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002470#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002471write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002472#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
2474 ssl->state++;
2475
2476 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2477 {
2478 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2479 return( ret );
2480 }
2481
2482 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2483
Paul Bakkered27a042013-04-18 22:46:23 +02002484 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485}
2486
2487int ssl_parse_certificate( ssl_context *ssl )
2488{
Paul Bakkered27a042013-04-18 22:46:23 +02002489 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002490 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002491 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
2493 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2494
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002495 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002496 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2497 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002498 {
2499 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2500 ssl->state++;
2501 return( 0 );
2502 }
2503
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 if( ssl->endpoint == SSL_IS_SERVER &&
2505 ssl->authmode == SSL_VERIFY_NONE )
2506 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002507 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2509 ssl->state++;
2510 return( 0 );
2511 }
2512
2513 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2514 {
2515 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2516 return( ret );
2517 }
2518
2519 ssl->state++;
2520
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002521#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 /*
2523 * Check if the client sent an empty certificate
2524 */
2525 if( ssl->endpoint == SSL_IS_SERVER &&
2526 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2527 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002528 if( ssl->in_msglen == 2 &&
2529 ssl->in_msgtype == SSL_MSG_ALERT &&
2530 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2531 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 {
2533 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2534
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002535 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2537 return( 0 );
2538 else
Paul Bakker40e46942009-01-03 21:51:57 +00002539 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 }
2541 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002542#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002544#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2545 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 if( ssl->endpoint == SSL_IS_SERVER &&
2547 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2548 {
2549 if( ssl->in_hslen == 7 &&
2550 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2551 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2552 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2553 {
2554 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2555
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002556 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002558 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 else
2560 return( 0 );
2561 }
2562 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002563#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2564 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002565
2566 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2567 {
2568 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002569 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 }
2571
2572 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2573 {
2574 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002575 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 }
2577
2578 /*
2579 * Same message structure as in ssl_write_certificate()
2580 */
2581 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2582
2583 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2584 {
2585 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002586 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 }
2588
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002589 /* In case we tried to reuse a session but it failed */
2590 if( ssl->session_negotiate->peer_cert != NULL )
2591 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002592 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002593 polarssl_free( ssl->session_negotiate->peer_cert );
2594 }
2595
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002596 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2597 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 {
2599 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002600 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002601 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 }
2603
Paul Bakkerb6b09562013-09-18 14:17:41 +02002604 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
2606 i = 7;
2607
2608 while( i < ssl->in_hslen )
2609 {
2610 if( ssl->in_msg[i] != 0 )
2611 {
2612 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002613 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 }
2615
2616 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2617 | (unsigned int) ssl->in_msg[i + 2];
2618 i += 3;
2619
2620 if( n < 128 || i + n > ssl->in_hslen )
2621 {
2622 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002623 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 }
2625
Paul Bakkerddf26b42013-09-18 13:46:23 +02002626 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2627 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 if( ret != 0 )
2629 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002630 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 return( ret );
2632 }
2633
2634 i += n;
2635 }
2636
Paul Bakker48916f92012-09-16 19:57:18 +00002637 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
2639 if( ssl->authmode != SSL_VERIFY_NONE )
2640 {
2641 if( ssl->ca_chain == NULL )
2642 {
2643 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002644 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 }
2646
Paul Bakkerddf26b42013-09-18 13:46:23 +02002647 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2648 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2649 &ssl->session_negotiate->verify_result,
2650 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
2652 if( ret != 0 )
2653 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2654
2655 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2656 ret = 0;
2657 }
2658
2659 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2660
2661 return( ret );
2662}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002663#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2664 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2665 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
2667int ssl_write_change_cipher_spec( ssl_context *ssl )
2668{
2669 int ret;
2670
2671 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2672
2673 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2674 ssl->out_msglen = 1;
2675 ssl->out_msg[0] = 1;
2676
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 ssl->state++;
2678
2679 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2680 {
2681 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2682 return( ret );
2683 }
2684
2685 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2686
2687 return( 0 );
2688}
2689
2690int ssl_parse_change_cipher_spec( ssl_context *ssl )
2691{
2692 int ret;
2693
2694 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2695
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2697 {
2698 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2699 return( ret );
2700 }
2701
2702 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2703 {
2704 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002705 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 }
2707
2708 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2709 {
2710 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002711 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 }
2713
2714 ssl->state++;
2715
2716 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2717
2718 return( 0 );
2719}
2720
Paul Bakker41c83d32013-03-20 14:39:14 +01002721void ssl_optimize_checksum( ssl_context *ssl,
2722 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002723{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002724 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002725
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002726#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2727 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002728 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002729 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002730 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002731#endif
2732#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2733#if defined(POLARSSL_SHA512_C)
2734 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2735 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2736 else
2737#endif
2738#if defined(POLARSSL_SHA256_C)
2739 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002740 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002741 else
2742#endif
2743#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2744 /* Should never happen */
2745 return;
Paul Bakker380da532012-04-18 16:10:25 +00002746}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002747
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002748static void ssl_update_checksum_start( ssl_context *ssl,
2749 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002750{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002751#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2752 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002753 md5_update( &ssl->handshake->fin_md5 , buf, len );
2754 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002755#endif
2756#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2757#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002758 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002759#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002760#if defined(POLARSSL_SHA512_C)
2761 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002762#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002763#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002764}
2765
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002766#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2767 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002768static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2769 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002770{
Paul Bakker48916f92012-09-16 19:57:18 +00002771 md5_update( &ssl->handshake->fin_md5 , buf, len );
2772 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002773}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002774#endif
Paul Bakker380da532012-04-18 16:10:25 +00002775
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002776#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2777#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002778static void ssl_update_checksum_sha256( ssl_context *ssl,
2779 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002780{
Paul Bakker9e36f042013-06-30 14:34:05 +02002781 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002782}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002783#endif
Paul Bakker380da532012-04-18 16:10:25 +00002784
Paul Bakker9e36f042013-06-30 14:34:05 +02002785#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002786static void ssl_update_checksum_sha384( ssl_context *ssl,
2787 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002788{
Paul Bakker9e36f042013-06-30 14:34:05 +02002789 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002790}
Paul Bakker769075d2012-11-24 11:26:46 +01002791#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002792#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002793
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002795static void ssl_calc_finished_ssl(
2796 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002797{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002798 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002799 md5_context md5;
2800 sha1_context sha1;
2801
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 unsigned char padbuf[48];
2803 unsigned char md5sum[16];
2804 unsigned char sha1sum[20];
2805
Paul Bakker48916f92012-09-16 19:57:18 +00002806 ssl_session *session = ssl->session_negotiate;
2807 if( !session )
2808 session = ssl->session;
2809
Paul Bakker1ef83d62012-04-11 12:09:53 +00002810 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2811
Paul Bakker48916f92012-09-16 19:57:18 +00002812 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2813 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
2815 /*
2816 * SSLv3:
2817 * hash =
2818 * MD5( master + pad2 +
2819 * MD5( handshake + sender + master + pad1 ) )
2820 * + SHA1( master + pad2 +
2821 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 */
2823
Paul Bakker90995b52013-06-24 19:20:35 +02002824#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002826 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002827#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
Paul Bakker90995b52013-06-24 19:20:35 +02002829#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002830 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002831 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002832#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Paul Bakker3c2122f2013-06-24 19:03:14 +02002834 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2835 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Paul Bakker1ef83d62012-04-11 12:09:53 +00002837 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
Paul Bakker3c2122f2013-06-24 19:03:14 +02002839 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002840 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002841 md5_update( &md5, padbuf, 48 );
2842 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Paul Bakker3c2122f2013-06-24 19:03:14 +02002844 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002845 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002846 sha1_update( &sha1, padbuf, 40 );
2847 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
Paul Bakker1ef83d62012-04-11 12:09:53 +00002849 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
Paul Bakker1ef83d62012-04-11 12:09:53 +00002851 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002852 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002853 md5_update( &md5, padbuf, 48 );
2854 md5_update( &md5, md5sum, 16 );
2855 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Paul Bakker1ef83d62012-04-11 12:09:53 +00002857 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002858 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002859 sha1_update( &sha1, padbuf , 40 );
2860 sha1_update( &sha1, sha1sum, 20 );
2861 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Paul Bakker1ef83d62012-04-11 12:09:53 +00002863 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865 memset( &md5, 0, sizeof( md5_context ) );
2866 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
2868 memset( padbuf, 0, sizeof( padbuf ) );
2869 memset( md5sum, 0, sizeof( md5sum ) );
2870 memset( sha1sum, 0, sizeof( sha1sum ) );
2871
2872 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2873}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002874#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002875
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002876#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002877static void ssl_calc_finished_tls(
2878 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002879{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002880 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002881 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002882 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002884 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
Paul Bakker48916f92012-09-16 19:57:18 +00002886 ssl_session *session = ssl->session_negotiate;
2887 if( !session )
2888 session = ssl->session;
2889
Paul Bakker1ef83d62012-04-11 12:09:53 +00002890 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakker48916f92012-09-16 19:57:18 +00002892 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2893 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
Paul Bakker1ef83d62012-04-11 12:09:53 +00002895 /*
2896 * TLSv1:
2897 * hash = PRF( master, finished_label,
2898 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2899 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002900
Paul Bakker90995b52013-06-24 19:20:35 +02002901#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002902 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2903 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002904#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002905
Paul Bakker90995b52013-06-24 19:20:35 +02002906#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002907 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2908 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002909#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002910
2911 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002912 ? "client finished"
2913 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002914
2915 md5_finish( &md5, padbuf );
2916 sha1_finish( &sha1, padbuf + 16 );
2917
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002918 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002919 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002920
2921 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2922
2923 memset( &md5, 0, sizeof( md5_context ) );
2924 memset( &sha1, 0, sizeof( sha1_context ) );
2925
2926 memset( padbuf, 0, sizeof( padbuf ) );
2927
2928 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2929}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002930#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002931
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002932#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2933#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002934static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002935 ssl_context *ssl, unsigned char *buf, int from )
2936{
2937 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002938 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002939 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002940 unsigned char padbuf[32];
2941
Paul Bakker48916f92012-09-16 19:57:18 +00002942 ssl_session *session = ssl->session_negotiate;
2943 if( !session )
2944 session = ssl->session;
2945
Paul Bakker380da532012-04-18 16:10:25 +00002946 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002947
Paul Bakker9e36f042013-06-30 14:34:05 +02002948 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949
2950 /*
2951 * TLSv1.2:
2952 * hash = PRF( master, finished_label,
2953 * Hash( handshake ) )[0.11]
2954 */
2955
Paul Bakker9e36f042013-06-30 14:34:05 +02002956#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002957 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002958 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002959#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960
2961 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002962 ? "client finished"
2963 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964
Paul Bakker9e36f042013-06-30 14:34:05 +02002965 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002966
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002967 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002968 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969
2970 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2971
Paul Bakker9e36f042013-06-30 14:34:05 +02002972 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002973
2974 memset( padbuf, 0, sizeof( padbuf ) );
2975
2976 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2977}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002978#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979
Paul Bakker9e36f042013-06-30 14:34:05 +02002980#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002981static void ssl_calc_finished_tls_sha384(
2982 ssl_context *ssl, unsigned char *buf, int from )
2983{
2984 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002985 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002986 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002987 unsigned char padbuf[48];
2988
Paul Bakker48916f92012-09-16 19:57:18 +00002989 ssl_session *session = ssl->session_negotiate;
2990 if( !session )
2991 session = ssl->session;
2992
Paul Bakker380da532012-04-18 16:10:25 +00002993 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002994
Paul Bakker9e36f042013-06-30 14:34:05 +02002995 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002996
2997 /*
2998 * TLSv1.2:
2999 * hash = PRF( master, finished_label,
3000 * Hash( handshake ) )[0.11]
3001 */
3002
Paul Bakker9e36f042013-06-30 14:34:05 +02003003#if !defined(POLARSSL_SHA512_ALT)
3004 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3005 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003006#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003007
3008 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003009 ? "client finished"
3010 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003011
Paul Bakker9e36f042013-06-30 14:34:05 +02003012 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003013
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003014 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003015 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003016
3017 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3018
Paul Bakker9e36f042013-06-30 14:34:05 +02003019 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003020
3021 memset( padbuf, 0, sizeof( padbuf ) );
3022
3023 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3024}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003025#endif /* POLARSSL_SHA512_C */
3026#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003027
Paul Bakker48916f92012-09-16 19:57:18 +00003028void ssl_handshake_wrapup( ssl_context *ssl )
3029{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003030 int resume = ssl->handshake->resume;
3031
Paul Bakker48916f92012-09-16 19:57:18 +00003032 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3033
3034 /*
3035 * Free our handshake params
3036 */
3037 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003038 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003039 ssl->handshake = NULL;
3040
3041 /*
3042 * Switch in our now active transform context
3043 */
3044 if( ssl->transform )
3045 {
3046 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003047 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003048 }
3049 ssl->transform = ssl->transform_negotiate;
3050 ssl->transform_negotiate = NULL;
3051
Paul Bakker0a597072012-09-25 21:55:46 +00003052 if( ssl->session )
3053 {
3054 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003055 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003056 }
3057 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003058 ssl->session_negotiate = NULL;
3059
Paul Bakker0a597072012-09-25 21:55:46 +00003060 /*
3061 * Add cache entry
3062 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003063 if( ssl->f_set_cache != NULL &&
3064 ssl->session->length != 0 &&
3065 resume == 0 )
3066 {
Paul Bakker0a597072012-09-25 21:55:46 +00003067 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3068 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003069 }
Paul Bakker0a597072012-09-25 21:55:46 +00003070
Paul Bakker48916f92012-09-16 19:57:18 +00003071 ssl->state++;
3072
3073 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3074}
3075
Paul Bakker1ef83d62012-04-11 12:09:53 +00003076int ssl_write_finished( ssl_context *ssl )
3077{
3078 int ret, hash_len;
3079
3080 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3081
Paul Bakker92be97b2013-01-02 17:30:03 +01003082 /*
3083 * Set the out_msg pointer to the correct location based on IV length
3084 */
3085 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3086 {
3087 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3088 ssl->transform_negotiate->fixed_ivlen;
3089 }
3090 else
3091 ssl->out_msg = ssl->out_iv;
3092
Paul Bakker48916f92012-09-16 19:57:18 +00003093 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003094
3095 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3097
Paul Bakker48916f92012-09-16 19:57:18 +00003098 ssl->verify_data_len = hash_len;
3099 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3100
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 ssl->out_msglen = 4 + hash_len;
3102 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3103 ssl->out_msg[0] = SSL_HS_FINISHED;
3104
3105 /*
3106 * In case of session resuming, invert the client and server
3107 * ChangeCipherSpec messages order.
3108 */
Paul Bakker0a597072012-09-25 21:55:46 +00003109 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003110 {
3111 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003112 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 else
3114 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3115 }
3116 else
3117 ssl->state++;
3118
Paul Bakker48916f92012-09-16 19:57:18 +00003119 /*
3120 * Switch to our negotiated transform and session parameters for outbound data.
3121 */
3122 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3123 ssl->transform_out = ssl->transform_negotiate;
3124 ssl->session_out = ssl->session_negotiate;
3125 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003126
Paul Bakker07eb38b2012-12-19 14:42:06 +01003127#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3128 if( ssl_hw_record_activate != NULL)
3129 {
3130 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3131 {
3132 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3133 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3134 }
3135 }
3136#endif
3137
Paul Bakker5121ce52009-01-03 21:22:43 +00003138 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3139 {
3140 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3141 return( ret );
3142 }
3143
3144 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3145
3146 return( 0 );
3147}
3148
3149int ssl_parse_finished( ssl_context *ssl )
3150{
Paul Bakker23986e52011-04-24 08:57:21 +00003151 int ret;
3152 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003153 unsigned char buf[36];
3154
3155 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3156
Paul Bakker48916f92012-09-16 19:57:18 +00003157 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
Paul Bakker48916f92012-09-16 19:57:18 +00003159 /*
3160 * Switch to our negotiated transform and session parameters for inbound data.
3161 */
3162 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3163 ssl->transform_in = ssl->transform_negotiate;
3164 ssl->session_in = ssl->session_negotiate;
3165 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
Paul Bakker92be97b2013-01-02 17:30:03 +01003167 /*
3168 * Set the in_msg pointer to the correct location based on IV length
3169 */
3170 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3171 {
3172 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3173 ssl->transform_negotiate->fixed_ivlen;
3174 }
3175 else
3176 ssl->in_msg = ssl->in_iv;
3177
Paul Bakker07eb38b2012-12-19 14:42:06 +01003178#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3179 if( ssl_hw_record_activate != NULL)
3180 {
3181 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3182 {
3183 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3184 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3185 }
3186 }
3187#endif
3188
Paul Bakker5121ce52009-01-03 21:22:43 +00003189 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3190 {
3191 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3192 return( ret );
3193 }
3194
3195 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3196 {
3197 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003198 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 }
3200
Paul Bakker1ef83d62012-04-11 12:09:53 +00003201 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003202 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3203
3204 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3205 ssl->in_hslen != 4 + hash_len )
3206 {
3207 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003208 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 }
3210
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 if( memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
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 Bakker48916f92012-09-16 19:57:18 +00003217 ssl->verify_data_len = hash_len;
3218 memcpy( ssl->peer_verify_data, buf, hash_len );
3219
Paul Bakker0a597072012-09-25 21:55:46 +00003220 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003221 {
3222 if( ssl->endpoint == SSL_IS_CLIENT )
3223 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3224
3225 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003226 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 }
3228 else
3229 ssl->state++;
3230
3231 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3232
3233 return( 0 );
3234}
3235
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003236static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003237{
3238 if( ssl->transform_negotiate )
3239 ssl_transform_free( ssl->transform_negotiate );
3240 else
Paul Bakker6e339b52013-07-03 13:37:05 +02003241 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker48916f92012-09-16 19:57:18 +00003242
3243 if( ssl->session_negotiate )
3244 ssl_session_free( ssl->session_negotiate );
3245 else
Paul Bakker6e339b52013-07-03 13:37:05 +02003246 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakker48916f92012-09-16 19:57:18 +00003247
3248 if( ssl->handshake )
3249 ssl_handshake_free( ssl->handshake );
3250 else
Paul Bakker6e339b52013-07-03 13:37:05 +02003251 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker48916f92012-09-16 19:57:18 +00003252
3253 if( ssl->handshake == NULL ||
3254 ssl->transform_negotiate == NULL ||
3255 ssl->session_negotiate == NULL )
3256 {
3257 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3258 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3259 }
3260
3261 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3262 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3263 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3264
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003265#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3266 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003267 md5_starts( &ssl->handshake->fin_md5 );
3268 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003269#endif
3270#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3271#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003272 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003273#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003274#if defined(POLARSSL_SHA512_C)
3275 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003276#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003277#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003278
3279 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003280 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003281
Paul Bakker61d113b2013-07-04 11:51:43 +02003282#if defined(POLARSSL_ECDH_C)
3283 ecdh_init( &ssl->handshake->ecdh_ctx );
3284#endif
3285
Paul Bakker48916f92012-09-16 19:57:18 +00003286 return( 0 );
3287}
3288
Paul Bakker5121ce52009-01-03 21:22:43 +00003289/*
3290 * Initialize an SSL context
3291 */
3292int ssl_init( ssl_context *ssl )
3293{
Paul Bakker48916f92012-09-16 19:57:18 +00003294 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 int len = SSL_BUFFER_LEN;
3296
3297 memset( ssl, 0, sizeof( ssl_context ) );
3298
Paul Bakker62f2dee2012-09-28 07:31:51 +00003299 /*
3300 * Sane defaults
3301 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003302 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3303 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3304 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3305 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003306
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003307 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003308
Paul Bakker62f2dee2012-09-28 07:31:51 +00003309#if defined(POLARSSL_DHM_C)
3310 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3311 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3312 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3313 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3314 {
3315 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3316 return( ret );
3317 }
3318#endif
3319
3320 /*
3321 * Prepare base structures
3322 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003323 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003324 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003325 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 ssl->in_msg = ssl->in_ctr + 13;
3327
3328 if( ssl->in_ctr == NULL )
3329 {
3330 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003331 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 }
3333
Paul Bakker6e339b52013-07-03 13:37:05 +02003334 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003336 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003337 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003338
3339 if( ssl->out_ctr == NULL )
3340 {
3341 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003342 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003343 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 }
3345
3346 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3347 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3348
Paul Bakker606b4ba2013-08-14 16:52:14 +02003349#if defined(POLARSSL_SSL_SESSION_TICKETS)
3350 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3351#endif
3352
Paul Bakker48916f92012-09-16 19:57:18 +00003353 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3354 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
3356 return( 0 );
3357}
3358
3359/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003360 * Reset an initialized and used SSL context for re-use while retaining
3361 * all application-set variables, function pointers and data.
3362 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003363int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003364{
Paul Bakker48916f92012-09-16 19:57:18 +00003365 int ret;
3366
Paul Bakker7eb013f2011-10-06 12:37:39 +00003367 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003368 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3369 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3370
3371 ssl->verify_data_len = 0;
3372 memset( ssl->own_verify_data, 0, 36 );
3373 memset( ssl->peer_verify_data, 0, 36 );
3374
Paul Bakker7eb013f2011-10-06 12:37:39 +00003375 ssl->in_offt = NULL;
3376
Paul Bakker92be97b2013-01-02 17:30:03 +01003377 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003378 ssl->in_msgtype = 0;
3379 ssl->in_msglen = 0;
3380 ssl->in_left = 0;
3381
3382 ssl->in_hslen = 0;
3383 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003384 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003385
Paul Bakker92be97b2013-01-02 17:30:03 +01003386 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003387 ssl->out_msgtype = 0;
3388 ssl->out_msglen = 0;
3389 ssl->out_left = 0;
3390
Paul Bakker48916f92012-09-16 19:57:18 +00003391 ssl->transform_in = NULL;
3392 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003393
3394 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3395 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003396
3397#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3398 if( ssl_hw_record_reset != NULL)
3399 {
3400 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003401 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003402 {
3403 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3404 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3405 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003406 }
3407#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003408
Paul Bakker48916f92012-09-16 19:57:18 +00003409 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003410 {
Paul Bakker48916f92012-09-16 19:57:18 +00003411 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003412 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003413 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003414 }
Paul Bakker48916f92012-09-16 19:57:18 +00003415
Paul Bakkerc0463502013-02-14 11:19:38 +01003416 if( ssl->session )
3417 {
3418 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003419 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003420 ssl->session = NULL;
3421 }
3422
Paul Bakker48916f92012-09-16 19:57:18 +00003423 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3424 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003425
3426 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003427}
3428
Paul Bakkera503a632013-08-14 13:48:06 +02003429#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003430/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003431 * Allocate and initialize ticket keys
3432 */
3433static int ssl_ticket_keys_init( ssl_context *ssl )
3434{
3435 int ret;
3436 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003437 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003438
3439 if( ssl->ticket_keys != NULL )
3440 return( 0 );
3441
3442 if( ( tkeys = polarssl_malloc( sizeof( ssl_ticket_keys ) ) ) == NULL )
3443 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3444
3445 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
3446 return( ret );
3447
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003448 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3449 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3450 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3451 {
3452 return( ret );
3453 }
3454
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003455 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
3456 return( ret );
3457
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003458 ssl->ticket_keys = tkeys;
3459
3460 return( 0 );
3461}
Paul Bakkera503a632013-08-14 13:48:06 +02003462#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003463
3464/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 * SSL set accessors
3466 */
3467void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3468{
3469 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003470
Paul Bakker606b4ba2013-08-14 16:52:14 +02003471#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003472 if( endpoint == SSL_IS_CLIENT )
3473 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003474#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003475}
3476
3477void ssl_set_authmode( ssl_context *ssl, int authmode )
3478{
3479 ssl->authmode = authmode;
3480}
3481
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003482#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003483void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003484 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003485 void *p_vrfy )
3486{
3487 ssl->f_vrfy = f_vrfy;
3488 ssl->p_vrfy = p_vrfy;
3489}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003490#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003491
Paul Bakker5121ce52009-01-03 21:22:43 +00003492void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003493 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003494 void *p_rng )
3495{
3496 ssl->f_rng = f_rng;
3497 ssl->p_rng = p_rng;
3498}
3499
3500void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003501 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 void *p_dbg )
3503{
3504 ssl->f_dbg = f_dbg;
3505 ssl->p_dbg = p_dbg;
3506}
3507
3508void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003509 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003510 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003511{
3512 ssl->f_recv = f_recv;
3513 ssl->f_send = f_send;
3514 ssl->p_recv = p_recv;
3515 ssl->p_send = p_send;
3516}
3517
Paul Bakker0a597072012-09-25 21:55:46 +00003518void ssl_set_session_cache( ssl_context *ssl,
3519 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3520 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003521{
Paul Bakker0a597072012-09-25 21:55:46 +00003522 ssl->f_get_cache = f_get_cache;
3523 ssl->p_get_cache = p_get_cache;
3524 ssl->f_set_cache = f_set_cache;
3525 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003526}
3527
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003528int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003529{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003530 int ret;
3531
3532 if( ssl == NULL ||
3533 session == NULL ||
3534 ssl->session_negotiate == NULL ||
3535 ssl->endpoint != SSL_IS_CLIENT )
3536 {
3537 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3538 }
3539
3540 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3541 return( ret );
3542
Paul Bakker0a597072012-09-25 21:55:46 +00003543 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003544
3545 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003546}
3547
Paul Bakkerb68cad62012-08-23 08:34:18 +00003548void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003549{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003550 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3551 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3552 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3553 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3554}
3555
3556void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3557 int major, int minor )
3558{
3559 if( major != SSL_MAJOR_VERSION_3 )
3560 return;
3561
3562 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3563 return;
3564
3565 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003566}
3567
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003568#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003569/* Add a new (empty) key_cert entry an return a pointer to it */
3570static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3571{
3572 ssl_key_cert *key_cert, *last;
3573
3574 if( ( key_cert = polarssl_malloc( sizeof( ssl_key_cert ) ) ) == NULL )
3575 return( NULL );
3576
3577 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3578
3579 /* Append the new key_cert to the (possibly empty) current list */
3580 if( ssl->key_cert == NULL )
3581 ssl->key_cert = key_cert;
3582 else
3583 {
3584 last = ssl->key_cert;
3585 while( last->next != NULL )
3586 last = last->next;
3587 last->next = key_cert;
3588 }
3589
3590 return key_cert;
3591}
3592
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003593void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003594 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003595{
3596 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003597 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003598 ssl->peer_cn = peer_cn;
3599}
3600
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003601int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003602 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003603{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003604 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3605
3606 if( key_cert == NULL )
3607 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3608
3609 key_cert->cert = own_cert;
3610 key_cert->key = pk_key;
3611
3612 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003613}
3614
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003615#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003616int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003617 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003618{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003619 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003620 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003621
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003622 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003623 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3624
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003625 if( ( key_cert->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
3626 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003627
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003628 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003629
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003630 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003631 if( ret != 0 )
3632 return( ret );
3633
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003634 if( ( ret = rsa_copy( key_cert->key->pk_ctx, rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003635 return( ret );
3636
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003637 key_cert->cert = own_cert;
3638 key_cert->key_own_alloc = 1;
3639
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003640 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003641}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003642#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003643
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003644int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003645 void *rsa_key,
3646 rsa_decrypt_func rsa_decrypt,
3647 rsa_sign_func rsa_sign,
3648 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003649{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003650 int ret;
3651 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003652
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003653 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003654 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3655
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003656 if( ( key_cert->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
3657 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003658
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003659 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003660
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003661 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3662 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3663 return( ret );
3664
3665 key_cert->cert = own_cert;
3666 key_cert->key_own_alloc = 1;
3667
3668 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003669}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003670#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003671
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003672#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003673int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3674 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003675{
Paul Bakker6db455e2013-09-18 17:29:31 +02003676 if( psk == NULL || psk_identity == NULL )
3677 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3678
3679 if( ssl->psk != NULL )
3680 {
3681 polarssl_free( ssl->psk );
3682 polarssl_free( ssl->psk_identity );
3683 }
3684
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003685 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003686 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003687
3688 ssl->psk = polarssl_malloc( ssl->psk_len );
3689 ssl->psk_identity = polarssl_malloc( ssl->psk_identity_len );
3690
3691 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3692 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3693
3694 memcpy( ssl->psk, psk, ssl->psk_len );
3695 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003696
3697 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003698}
3699
3700void ssl_set_psk_cb( ssl_context *ssl,
3701 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3702 size_t),
3703 void *p_psk )
3704{
3705 ssl->f_psk = f_psk;
3706 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003707}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003708#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003709
Paul Bakker48916f92012-09-16 19:57:18 +00003710#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003711int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003712{
3713 int ret;
3714
Paul Bakker48916f92012-09-16 19:57:18 +00003715 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 {
3717 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3718 return( ret );
3719 }
3720
Paul Bakker48916f92012-09-16 19:57:18 +00003721 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003722 {
3723 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3724 return( ret );
3725 }
3726
3727 return( 0 );
3728}
3729
Paul Bakker1b57b062011-01-06 15:48:19 +00003730int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3731{
3732 int ret;
3733
Paul Bakker48916f92012-09-16 19:57:18 +00003734 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003735 {
3736 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3737 return( ret );
3738 }
3739
Paul Bakker48916f92012-09-16 19:57:18 +00003740 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003741 {
3742 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3743 return( ret );
3744 }
3745
3746 return( 0 );
3747}
Paul Bakker48916f92012-09-16 19:57:18 +00003748#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003749
Paul Bakker0be444a2013-08-27 21:55:01 +02003750#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003751int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003752{
3753 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003754 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003755
3756 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003757
3758 if( ssl->hostname_len + 1 == 0 )
3759 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3760
Paul Bakker6e339b52013-07-03 13:37:05 +02003761 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003762
Paul Bakkerb15b8512012-01-13 13:44:06 +00003763 if( ssl->hostname == NULL )
3764 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3765
Paul Bakker3c2122f2013-06-24 19:03:14 +02003766 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003768
Paul Bakker40ea7de2009-05-03 10:18:48 +00003769 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003770
3771 return( 0 );
3772}
3773
Paul Bakker5701cdc2012-09-27 21:49:42 +00003774void ssl_set_sni( ssl_context *ssl,
3775 int (*f_sni)(void *, ssl_context *,
3776 const unsigned char *, size_t),
3777 void *p_sni )
3778{
3779 ssl->f_sni = f_sni;
3780 ssl->p_sni = p_sni;
3781}
Paul Bakker0be444a2013-08-27 21:55:01 +02003782#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003783
Paul Bakker490ecc82011-10-06 13:04:09 +00003784void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3785{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003786 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3787 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3788 {
3789 ssl->max_major_ver = major;
3790 ssl->max_minor_ver = minor;
3791 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003792}
3793
Paul Bakker1d29fb52012-09-28 13:28:45 +00003794void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3795{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003796 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3797 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3798 {
3799 ssl->min_major_ver = major;
3800 ssl->min_minor_ver = minor;
3801 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003802}
3803
Paul Bakker05decb22013-08-15 13:33:48 +02003804#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003805int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3806{
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003807 if( mfl_code >= sizeof( mfl_code_to_length ) ||
3808 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003809 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003810 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003811 }
3812
3813 ssl->mfl_code = mfl_code;
3814
3815 return( 0 );
3816}
Paul Bakker05decb22013-08-15 13:33:48 +02003817#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003818
Paul Bakker1f2bc622013-08-15 13:45:55 +02003819#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003820int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003821{
3822 if( ssl->endpoint != SSL_IS_CLIENT )
3823 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3824
Paul Bakker8c1ede62013-07-19 14:14:37 +02003825 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003826
3827 return( 0 );
3828}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003829#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003830
Paul Bakker48916f92012-09-16 19:57:18 +00003831void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3832{
3833 ssl->disable_renegotiation = renegotiation;
3834}
3835
3836void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3837{
3838 ssl->allow_legacy_renegotiation = allow_legacy;
3839}
3840
Paul Bakkera503a632013-08-14 13:48:06 +02003841#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003842int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3843{
3844 ssl->session_tickets = use_tickets;
3845
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003846 if( ssl->endpoint == SSL_IS_CLIENT )
3847 return( 0 );
3848
3849 if( ssl->f_rng == NULL )
3850 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3851
3852 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003853}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003854
3855void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3856{
3857 ssl->ticket_lifetime = lifetime;
3858}
Paul Bakkera503a632013-08-14 13:48:06 +02003859#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003860
Paul Bakker5121ce52009-01-03 21:22:43 +00003861/*
3862 * SSL get accessors
3863 */
Paul Bakker23986e52011-04-24 08:57:21 +00003864size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003865{
3866 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3867}
3868
Paul Bakkerff60ee62010-03-16 21:09:09 +00003869int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003870{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003871 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003872}
3873
Paul Bakkere3166ce2011-01-27 17:40:50 +00003874const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003875{
Paul Bakker926c8e42013-03-06 10:23:34 +01003876 if( ssl == NULL || ssl->session == NULL )
3877 return NULL;
3878
Paul Bakkere3166ce2011-01-27 17:40:50 +00003879 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003880}
3881
Paul Bakker43ca69c2011-01-15 17:35:19 +00003882const char *ssl_get_version( const ssl_context *ssl )
3883{
3884 switch( ssl->minor_ver )
3885 {
3886 case SSL_MINOR_VERSION_0:
3887 return( "SSLv3.0" );
3888
3889 case SSL_MINOR_VERSION_1:
3890 return( "TLSv1.0" );
3891
3892 case SSL_MINOR_VERSION_2:
3893 return( "TLSv1.1" );
3894
Paul Bakker1ef83d62012-04-11 12:09:53 +00003895 case SSL_MINOR_VERSION_3:
3896 return( "TLSv1.2" );
3897
Paul Bakker43ca69c2011-01-15 17:35:19 +00003898 default:
3899 break;
3900 }
3901 return( "unknown" );
3902}
3903
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003904#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003905const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003906{
3907 if( ssl == NULL || ssl->session == NULL )
3908 return NULL;
3909
3910 return ssl->session->peer_cert;
3911}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003912#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003913
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003914int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3915{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003916 if( ssl == NULL ||
3917 dst == NULL ||
3918 ssl->session == NULL ||
3919 ssl->endpoint != SSL_IS_CLIENT )
3920 {
3921 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3922 }
3923
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003924 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003925}
3926
Paul Bakker5121ce52009-01-03 21:22:43 +00003927/*
Paul Bakker1961b702013-01-25 14:49:24 +01003928 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003929 */
Paul Bakker1961b702013-01-25 14:49:24 +01003930int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003931{
Paul Bakker40e46942009-01-03 21:51:57 +00003932 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003933
Paul Bakker40e46942009-01-03 21:51:57 +00003934#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003935 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01003936 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003937#endif
3938
Paul Bakker40e46942009-01-03 21:51:57 +00003939#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003940 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003941 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003942#endif
3943
Paul Bakker1961b702013-01-25 14:49:24 +01003944 return( ret );
3945}
3946
3947/*
3948 * Perform the SSL handshake
3949 */
3950int ssl_handshake( ssl_context *ssl )
3951{
3952 int ret = 0;
3953
3954 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
3955
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02003956#if defined(POLARSSL_X509_CRT_PARSE_C)
3957 ssl->handshake->key_cert = ssl->key_cert;
3958#endif
3959
Paul Bakker1961b702013-01-25 14:49:24 +01003960 while( ssl->state != SSL_HANDSHAKE_OVER )
3961 {
3962 ret = ssl_handshake_step( ssl );
3963
3964 if( ret != 0 )
3965 break;
3966 }
3967
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
3969
3970 return( ret );
3971}
3972
3973/*
Paul Bakker48916f92012-09-16 19:57:18 +00003974 * Renegotiate current connection
3975 */
3976int ssl_renegotiate( ssl_context *ssl )
3977{
3978 int ret;
3979
3980 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
3981
3982 if( ssl->state != SSL_HANDSHAKE_OVER )
3983 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3984
3985 ssl->state = SSL_HELLO_REQUEST;
3986 ssl->renegotiation = SSL_RENEGOTIATION;
3987
3988 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3989 return( ret );
3990
3991 if( ( ret = ssl_handshake( ssl ) ) != 0 )
3992 {
3993 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
3994 return( ret );
3995 }
3996
3997 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
3998
3999 return( 0 );
4000}
4001
4002/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004003 * Receive application data decrypted from the SSL layer
4004 */
Paul Bakker23986e52011-04-24 08:57:21 +00004005int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004006{
Paul Bakker23986e52011-04-24 08:57:21 +00004007 int ret;
4008 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004009
4010 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4011
4012 if( ssl->state != SSL_HANDSHAKE_OVER )
4013 {
4014 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4015 {
4016 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4017 return( ret );
4018 }
4019 }
4020
4021 if( ssl->in_offt == NULL )
4022 {
4023 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4024 {
Paul Bakker831a7552011-05-18 13:32:51 +00004025 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4026 return( 0 );
4027
Paul Bakker5121ce52009-01-03 21:22:43 +00004028 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4029 return( ret );
4030 }
4031
4032 if( ssl->in_msglen == 0 &&
4033 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4034 {
4035 /*
4036 * OpenSSL sends empty messages to randomize the IV
4037 */
4038 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4039 {
Paul Bakker831a7552011-05-18 13:32:51 +00004040 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4041 return( 0 );
4042
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4044 return( ret );
4045 }
4046 }
4047
Paul Bakker48916f92012-09-16 19:57:18 +00004048 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4049 {
4050 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4051
4052 if( ssl->endpoint == SSL_IS_CLIENT &&
4053 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4054 ssl->in_hslen != 4 ) )
4055 {
4056 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4057 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4058 }
4059
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004060 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4061 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4062 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004063 {
4064 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4065
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004066#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004067 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004068 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004069 /*
4070 * SSLv3 does not have a "no_renegotiation" alert
4071 */
4072 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4073 return( ret );
4074 }
4075 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004076#endif
4077#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4078 defined(POLARSSL_SSL_PROTO_TLS1_2)
4079 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004080 {
4081 if( ( ret = ssl_send_alert_message( ssl,
4082 SSL_ALERT_LEVEL_WARNING,
4083 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4084 {
4085 return( ret );
4086 }
Paul Bakker48916f92012-09-16 19:57:18 +00004087 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004088 else
4089#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004090 {
4091 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004092 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004093 }
Paul Bakker48916f92012-09-16 19:57:18 +00004094 }
4095 else
4096 {
4097 if( ( ret = ssl_renegotiate( ssl ) ) != 0 )
4098 {
4099 SSL_DEBUG_RET( 1, "ssl_renegotiate", ret );
4100 return( ret );
4101 }
4102
4103 return( POLARSSL_ERR_NET_WANT_READ );
4104 }
4105 }
4106 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004107 {
4108 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004109 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004110 }
4111
4112 ssl->in_offt = ssl->in_msg;
4113 }
4114
4115 n = ( len < ssl->in_msglen )
4116 ? len : ssl->in_msglen;
4117
4118 memcpy( buf, ssl->in_offt, n );
4119 ssl->in_msglen -= n;
4120
4121 if( ssl->in_msglen == 0 )
4122 /* all bytes consumed */
4123 ssl->in_offt = NULL;
4124 else
4125 /* more data available */
4126 ssl->in_offt += n;
4127
4128 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4129
Paul Bakker23986e52011-04-24 08:57:21 +00004130 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004131}
4132
4133/*
4134 * Send application data to be encrypted by the SSL layer
4135 */
Paul Bakker23986e52011-04-24 08:57:21 +00004136int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004137{
Paul Bakker23986e52011-04-24 08:57:21 +00004138 int ret;
4139 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004140 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004141
4142 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4143
4144 if( ssl->state != SSL_HANDSHAKE_OVER )
4145 {
4146 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4147 {
4148 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4149 return( ret );
4150 }
4151 }
4152
Paul Bakker05decb22013-08-15 13:33:48 +02004153#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004154 /*
4155 * Assume mfl_code is correct since it was checked when set
4156 */
4157 max_len = mfl_code_to_length[ssl->mfl_code];
4158
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004159 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004160 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004161 */
4162 if( ssl->session_out != NULL &&
4163 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4164 {
4165 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4166 }
Paul Bakker05decb22013-08-15 13:33:48 +02004167#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004168
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004169 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004170
Paul Bakker5121ce52009-01-03 21:22:43 +00004171 if( ssl->out_left != 0 )
4172 {
4173 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4174 {
4175 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4176 return( ret );
4177 }
4178 }
Paul Bakker887bd502011-06-08 13:10:54 +00004179 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004180 {
Paul Bakker887bd502011-06-08 13:10:54 +00004181 ssl->out_msglen = n;
4182 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4183 memcpy( ssl->out_msg, buf, n );
4184
4185 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4186 {
4187 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4188 return( ret );
4189 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004190 }
4191
4192 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4193
Paul Bakker23986e52011-04-24 08:57:21 +00004194 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004195}
4196
4197/*
4198 * Notify the peer that the connection is being closed
4199 */
4200int ssl_close_notify( ssl_context *ssl )
4201{
4202 int ret;
4203
4204 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4205
4206 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4207 {
4208 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4209 return( ret );
4210 }
4211
4212 if( ssl->state == SSL_HANDSHAKE_OVER )
4213 {
Paul Bakker48916f92012-09-16 19:57:18 +00004214 if( ( ret = ssl_send_alert_message( ssl,
4215 SSL_ALERT_LEVEL_WARNING,
4216 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004217 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004218 return( ret );
4219 }
4220 }
4221
4222 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4223
4224 return( ret );
4225}
4226
Paul Bakker48916f92012-09-16 19:57:18 +00004227void ssl_transform_free( ssl_transform *transform )
4228{
4229#if defined(POLARSSL_ZLIB_SUPPORT)
4230 deflateEnd( &transform->ctx_deflate );
4231 inflateEnd( &transform->ctx_inflate );
4232#endif
4233
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004234 cipher_free_ctx( &transform->cipher_ctx_enc );
4235 cipher_free_ctx( &transform->cipher_ctx_dec );
4236
Paul Bakker61d113b2013-07-04 11:51:43 +02004237 md_free_ctx( &transform->md_ctx_enc );
4238 md_free_ctx( &transform->md_ctx_dec );
4239
Paul Bakker48916f92012-09-16 19:57:18 +00004240 memset( transform, 0, sizeof( ssl_transform ) );
4241}
4242
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004243#if defined(POLARSSL_X509_CRT_PARSE_C)
4244static void ssl_key_cert_free( ssl_key_cert *key_cert )
4245{
4246 ssl_key_cert *cur = key_cert, *next;
4247
4248 while( cur != NULL )
4249 {
4250 next = cur->next;
4251
4252 if( cur->key_own_alloc )
4253 {
4254 pk_free( cur->key );
4255 polarssl_free( cur->key );
4256 }
4257 polarssl_free( cur );
4258
4259 cur = next;
4260 }
4261}
4262#endif /* POLARSSL_X509_CRT_PARSE_C */
4263
Paul Bakker48916f92012-09-16 19:57:18 +00004264void ssl_handshake_free( ssl_handshake_params *handshake )
4265{
4266#if defined(POLARSSL_DHM_C)
4267 dhm_free( &handshake->dhm_ctx );
4268#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004269#if defined(POLARSSL_ECDH_C)
4270 ecdh_free( &handshake->ecdh_ctx );
4271#endif
4272
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004273#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
4274 polarssl_free( handshake->curves );
4275#endif
4276
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004277#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4278 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4279 /*
4280 * Free only the linked list wrapper, not the keys themselves
4281 * since the belong to the SNI callback
4282 */
4283 if( handshake->sni_key_cert != NULL )
4284 {
4285 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4286
4287 while( cur != NULL )
4288 {
4289 next = cur->next;
4290 polarssl_free( cur );
4291 cur = next;
4292 }
4293 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004294#endif
4295
Paul Bakker48916f92012-09-16 19:57:18 +00004296 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4297}
4298
4299void ssl_session_free( ssl_session *session )
4300{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004301#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004302 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004303 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004304 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004305 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004306 }
Paul Bakkered27a042013-04-18 22:46:23 +02004307#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004308
Paul Bakkera503a632013-08-14 13:48:06 +02004309#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004310 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004311#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004312
Paul Bakker0a597072012-09-25 21:55:46 +00004313 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004314}
4315
Paul Bakker5121ce52009-01-03 21:22:43 +00004316/*
4317 * Free an SSL context
4318 */
4319void ssl_free( ssl_context *ssl )
4320{
4321 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4322
Paul Bakker5121ce52009-01-03 21:22:43 +00004323 if( ssl->out_ctr != NULL )
4324 {
4325 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004326 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004327 }
4328
4329 if( ssl->in_ctr != NULL )
4330 {
4331 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004332 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004333 }
4334
Paul Bakker40e46942009-01-03 21:51:57 +00004335#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004336 mpi_free( &ssl->dhm_P );
4337 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004338#endif
4339
Paul Bakker48916f92012-09-16 19:57:18 +00004340 if( ssl->transform )
4341 {
4342 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004343 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004344 }
4345
4346 if( ssl->handshake )
4347 {
4348 ssl_handshake_free( ssl->handshake );
4349 ssl_transform_free( ssl->transform_negotiate );
4350 ssl_session_free( ssl->session_negotiate );
4351
Paul Bakker6e339b52013-07-03 13:37:05 +02004352 polarssl_free( ssl->handshake );
4353 polarssl_free( ssl->transform_negotiate );
4354 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004355 }
4356
Paul Bakkerc0463502013-02-14 11:19:38 +01004357 if( ssl->session )
4358 {
4359 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004360 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004361 }
4362
Paul Bakkera503a632013-08-14 13:48:06 +02004363#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004364 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004365#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004366
Paul Bakker0be444a2013-08-27 21:55:01 +02004367#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004368 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004369 {
4370 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004371 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004372 ssl->hostname_len = 0;
4373 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004374#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004375
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004376#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004377 if( ssl->psk != NULL )
4378 {
4379 memset( ssl->psk, 0, ssl->psk_len );
4380 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4381 polarssl_free( ssl->psk );
4382 polarssl_free( ssl->psk_identity );
4383 ssl->psk_len = 0;
4384 ssl->psk_identity_len = 0;
4385 }
4386#endif
4387
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004388#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004389 ssl_key_cert_free( ssl->key_cert );
4390#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004391
Paul Bakker05ef8352012-05-08 09:17:57 +00004392#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4393 if( ssl_hw_record_finish != NULL )
4394 {
4395 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4396 ssl_hw_record_finish( ssl );
4397 }
4398#endif
4399
Paul Bakker5121ce52009-01-03 21:22:43 +00004400 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004401
Paul Bakker86f04f42013-02-14 11:20:09 +01004402 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004403 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004404}
4405
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004406#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004407/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004408 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004409 */
4410unsigned char ssl_sig_from_pk( pk_context *pk )
4411{
4412#if defined(POLARSSL_RSA_C)
4413 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4414 return( SSL_SIG_RSA );
4415#endif
4416#if defined(POLARSSL_ECDSA_C)
4417 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4418 return( SSL_SIG_ECDSA );
4419#endif
4420 return( SSL_SIG_ANON );
4421}
4422
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004423pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4424{
4425 switch( sig )
4426 {
4427#if defined(POLARSSL_RSA_C)
4428 case SSL_SIG_RSA:
4429 return( POLARSSL_PK_RSA );
4430#endif
4431#if defined(POLARSSL_ECDSA_C)
4432 case SSL_SIG_ECDSA:
4433 return( POLARSSL_PK_ECDSA );
4434#endif
4435 default:
4436 return( POLARSSL_PK_NONE );
4437 }
4438}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004439#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004440
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004441/*
4442 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4443 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004444md_type_t ssl_md_alg_from_hash( unsigned char hash )
4445{
4446 switch( hash )
4447 {
4448#if defined(POLARSSL_MD5_C)
4449 case SSL_HASH_MD5:
4450 return( POLARSSL_MD_MD5 );
4451#endif
4452#if defined(POLARSSL_SHA1_C)
4453 case SSL_HASH_SHA1:
4454 return( POLARSSL_MD_SHA1 );
4455#endif
4456#if defined(POLARSSL_SHA256_C)
4457 case SSL_HASH_SHA224:
4458 return( POLARSSL_MD_SHA224 );
4459 case SSL_HASH_SHA256:
4460 return( POLARSSL_MD_SHA256 );
4461#endif
4462#if defined(POLARSSL_SHA512_C)
4463 case SSL_HASH_SHA384:
4464 return( POLARSSL_MD_SHA384 );
4465 case SSL_HASH_SHA512:
4466 return( POLARSSL_MD_SHA512 );
4467#endif
4468 default:
4469 return( POLARSSL_MD_NONE );
4470 }
4471}
4472
Paul Bakker5121ce52009-01-03 21:22:43 +00004473#endif