blob: 0c905ad8eacff1856a68b545c237eca577bbc533 [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 Bakkerb9cfaa02013-10-11 18:58:55 +020083 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
84 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020085 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
86
Paul Bakkerb6b09562013-09-18 14:17:41 +020087 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088
Paul Bakkerddf26b42013-09-18 13:46:23 +020089 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
90 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020091 {
92 polarssl_free( dst->peer_cert );
93 dst->peer_cert = NULL;
94 return( ret );
95 }
96 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098
Paul Bakkera503a632013-08-14 13:48:06 +020099#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 if( src->ticket != NULL )
101 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200102 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
103 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200104 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
105
106 memcpy( dst->ticket, src->ticket, src->ticket_len );
107 }
Paul Bakkera503a632013-08-14 13:48:06 +0200108#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200109
110 return( 0 );
111}
112
Paul Bakker05ef8352012-05-08 09:17:57 +0000113#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
114int (*ssl_hw_record_init)(ssl_context *ssl,
115 const unsigned char *key_enc, const unsigned char *key_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100116 size_t keylen,
Paul Bakker05ef8352012-05-08 09:17:57 +0000117 const unsigned char *iv_enc, const unsigned char *iv_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100118 size_t ivlen,
119 const unsigned char *mac_enc, const unsigned char *mac_dec,
120 size_t maclen) = NULL;
121int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000122int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
123int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
124int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
125int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
126#endif
127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128/*
129 * Key material generation
130 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200131#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200132static int ssl3_prf( const unsigned char *secret, size_t slen,
133 const char *label,
134 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000135 unsigned char *dstbuf, size_t dlen )
136{
137 size_t i;
138 md5_context md5;
139 sha1_context sha1;
140 unsigned char padding[16];
141 unsigned char sha1sum[20];
142 ((void)label);
143
144 /*
145 * SSLv3:
146 * block =
147 * MD5( secret + SHA1( 'A' + secret + random ) ) +
148 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
149 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
150 * ...
151 */
152 for( i = 0; i < dlen / 16; i++ )
153 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200154 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000155
156 sha1_starts( &sha1 );
157 sha1_update( &sha1, padding, 1 + i );
158 sha1_update( &sha1, secret, slen );
159 sha1_update( &sha1, random, rlen );
160 sha1_finish( &sha1, sha1sum );
161
162 md5_starts( &md5 );
163 md5_update( &md5, secret, slen );
164 md5_update( &md5, sha1sum, 20 );
165 md5_finish( &md5, dstbuf + i * 16 );
166 }
167
168 memset( &md5, 0, sizeof( md5 ) );
169 memset( &sha1, 0, sizeof( sha1 ) );
170
171 memset( padding, 0, sizeof( padding ) );
172 memset( sha1sum, 0, sizeof( sha1sum ) );
173
174 return( 0 );
175}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200176#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000177
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200178#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200179static int tls1_prf( const unsigned char *secret, size_t slen,
180 const char *label,
181 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000182 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000183{
Paul Bakker23986e52011-04-24 08:57:21 +0000184 size_t nb, hs;
185 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200186 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 unsigned char tmp[128];
188 unsigned char h_i[20];
189
190 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000191 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
193 hs = ( slen + 1 ) / 2;
194 S1 = secret;
195 S2 = secret + slen - hs;
196
197 nb = strlen( label );
198 memcpy( tmp + 20, label, nb );
199 memcpy( tmp + 20 + nb, random, rlen );
200 nb += rlen;
201
202 /*
203 * First compute P_md5(secret,label+random)[0..dlen]
204 */
205 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
206
207 for( i = 0; i < dlen; i += 16 )
208 {
209 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
210 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
211
212 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
213
214 for( j = 0; j < k; j++ )
215 dstbuf[i + j] = h_i[j];
216 }
217
218 /*
219 * XOR out with P_sha1(secret,label+random)[0..dlen]
220 */
221 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
222
223 for( i = 0; i < dlen; i += 20 )
224 {
225 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
226 sha1_hmac( S2, hs, tmp, 20, tmp );
227
228 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
229
230 for( j = 0; j < k; j++ )
231 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
232 }
233
234 memset( tmp, 0, sizeof( tmp ) );
235 memset( h_i, 0, sizeof( h_i ) );
236
237 return( 0 );
238}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200239#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200241#if defined(POLARSSL_SSL_PROTO_TLS1_2)
242#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200243static int tls_prf_sha256( const unsigned char *secret, size_t slen,
244 const char *label,
245 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000246 unsigned char *dstbuf, size_t dlen )
247{
248 size_t nb;
249 size_t i, j, k;
250 unsigned char tmp[128];
251 unsigned char h_i[32];
252
253 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
254 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
255
256 nb = strlen( label );
257 memcpy( tmp + 32, label, nb );
258 memcpy( tmp + 32 + nb, random, rlen );
259 nb += rlen;
260
261 /*
262 * Compute P_<hash>(secret, label + random)[0..dlen]
263 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200264 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000265
266 for( i = 0; i < dlen; i += 32 )
267 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200268 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
269 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000270
271 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
272
273 for( j = 0; j < k; j++ )
274 dstbuf[i + j] = h_i[j];
275 }
276
277 memset( tmp, 0, sizeof( tmp ) );
278 memset( h_i, 0, sizeof( h_i ) );
279
280 return( 0 );
281}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200282#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
Paul Bakker9e36f042013-06-30 14:34:05 +0200284#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200285static int tls_prf_sha384( const unsigned char *secret, size_t slen,
286 const char *label,
287 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000288 unsigned char *dstbuf, size_t dlen )
289{
290 size_t nb;
291 size_t i, j, k;
292 unsigned char tmp[128];
293 unsigned char h_i[48];
294
295 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
296 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
297
298 nb = strlen( label );
299 memcpy( tmp + 48, label, nb );
300 memcpy( tmp + 48 + nb, random, rlen );
301 nb += rlen;
302
303 /*
304 * Compute P_<hash>(secret, label + random)[0..dlen]
305 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200306 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000307
308 for( i = 0; i < dlen; i += 48 )
309 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200310 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
311 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000312
313 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
314
315 for( j = 0; j < k; j++ )
316 dstbuf[i + j] = h_i[j];
317 }
318
319 memset( tmp, 0, sizeof( tmp ) );
320 memset( h_i, 0, sizeof( h_i ) );
321
322 return( 0 );
323}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200324#endif /* POLARSSL_SHA512_C */
325#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000326
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200327static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200328
329#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
330 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200331static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200332#endif
Paul Bakker380da532012-04-18 16:10:25 +0000333
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200334#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000335static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200337#endif
338
339#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
340static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000341static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif
343
344#if defined(POLARSSL_SSL_PROTO_TLS1_2)
345#if defined(POLARSSL_SHA256_C)
346static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
347static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000348static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200349#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100350
Paul Bakker9e36f042013-06-30 14:34:05 +0200351#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200352static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100353static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000354static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100355#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200356#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +0000357
Paul Bakker5121ce52009-01-03 21:22:43 +0000358int ssl_derive_keys( ssl_context *ssl )
359{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200360 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 unsigned char keyblk[256];
363 unsigned char *key1;
364 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100365 unsigned char *mac_enc;
366 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200367 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100368 const cipher_info_t *cipher_info;
369 const md_info_t *md_info;
370
Paul Bakker48916f92012-09-16 19:57:18 +0000371 ssl_session *session = ssl->session_negotiate;
372 ssl_transform *transform = ssl->transform_negotiate;
373 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
375 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
376
Paul Bakker68884e32013-01-07 18:20:04 +0100377 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
378 if( cipher_info == NULL )
379 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200380 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100381 transform->ciphersuite_info->cipher ) );
382 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
383 }
384
385 md_info = md_info_from_type( transform->ciphersuite_info->mac );
386 if( md_info == NULL )
387 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200388 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100389 transform->ciphersuite_info->mac ) );
390 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
391 }
392
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000394 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000395 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200396#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000397 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000398 {
Paul Bakker48916f92012-09-16 19:57:18 +0000399 handshake->tls_prf = ssl3_prf;
400 handshake->calc_verify = ssl_calc_verify_ssl;
401 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000402 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200403 else
404#endif
405#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
406 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000407 {
Paul Bakker48916f92012-09-16 19:57:18 +0000408 handshake->tls_prf = tls1_prf;
409 handshake->calc_verify = ssl_calc_verify_tls;
410 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000411 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200412 else
413#endif
414#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200415#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200416 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
417 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000418 {
Paul Bakker48916f92012-09-16 19:57:18 +0000419 handshake->tls_prf = tls_prf_sha384;
420 handshake->calc_verify = ssl_calc_verify_tls_sha384;
421 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000422 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000423 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200424#endif
425#if defined(POLARSSL_SHA256_C)
426 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000427 {
Paul Bakker48916f92012-09-16 19:57:18 +0000428 handshake->tls_prf = tls_prf_sha256;
429 handshake->calc_verify = ssl_calc_verify_tls_sha256;
430 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000431 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200432 else
433#endif
434#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200435 {
436 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200437 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200438 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000439
440 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 * SSLv3:
442 * master =
443 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
444 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
445 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200446 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 * master = PRF( premaster, "master secret", randbytes )[0..47]
449 */
Paul Bakker0a597072012-09-25 21:55:46 +0000450 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 {
Paul Bakker48916f92012-09-16 19:57:18 +0000452 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
453 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
Paul Bakker48916f92012-09-16 19:57:18 +0000455 handshake->tls_prf( handshake->premaster, handshake->pmslen,
456 "master secret",
457 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
Paul Bakker48916f92012-09-16 19:57:18 +0000459 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 }
461 else
462 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
463
464 /*
465 * Swap the client and server random values.
466 */
Paul Bakker48916f92012-09-16 19:57:18 +0000467 memcpy( tmp, handshake->randbytes, 64 );
468 memcpy( handshake->randbytes, tmp + 32, 32 );
469 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 memset( tmp, 0, sizeof( tmp ) );
471
472 /*
473 * SSLv3:
474 * key block =
475 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
476 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
477 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
478 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
479 * ...
480 *
481 * TLSv1:
482 * key block = PRF( master, "key expansion", randbytes )
483 */
Paul Bakker48916f92012-09-16 19:57:18 +0000484 handshake->tls_prf( session->master, 48, "key expansion",
485 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
Paul Bakker48916f92012-09-16 19:57:18 +0000487 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
488 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
489 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
490 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
492
Paul Bakker48916f92012-09-16 19:57:18 +0000493 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495 /*
496 * Determine the appropriate key, IV and MAC length.
497 */
Paul Bakker68884e32013-01-07 18:20:04 +0100498
499 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 {
Paul Bakker68884e32013-01-07 18:20:04 +0100501 transform->keylen = cipher_info->key_length;
502 transform->keylen /= 8;
503 transform->minlen = 1;
504 transform->ivlen = 12;
505 transform->fixed_ivlen = 4;
506 transform->maclen = 0;
507 }
508 else
509 {
510 if( md_info->type != POLARSSL_MD_NONE )
511 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200512 int ret;
513
Paul Bakker61d113b2013-07-04 11:51:43 +0200514 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
515 {
516 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
517 return( ret );
518 }
519
520 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
521 {
522 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
523 return( ret );
524 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
Paul Bakker68884e32013-01-07 18:20:04 +0100526 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200527
Paul Bakker1f2bc622013-08-15 13:45:55 +0200528#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200529 /*
530 * If HMAC is to be truncated, we shall keep the leftmost bytes,
531 * (rfc 6066 page 13 or rfc 2104 section 4),
532 * so we only need to adjust the length here.
533 */
534 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
535 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200536#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100537 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
Paul Bakker68884e32013-01-07 18:20:04 +0100539 transform->keylen = cipher_info->key_length;
540 transform->keylen /= 8;
541 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
Paul Bakker68884e32013-01-07 18:20:04 +0100543 transform->minlen = transform->keylen;
544 if( transform->minlen < transform->maclen )
545 {
546 if( cipher_info->mode == POLARSSL_MODE_STREAM )
547 transform->minlen = transform->maclen;
548 else
549 transform->minlen += transform->keylen;
550 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 }
552
553 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000554 transform->keylen, transform->minlen, transform->ivlen,
555 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 /*
558 * Finally setup the cipher contexts, IVs and MAC secrets.
559 */
560 if( ssl->endpoint == SSL_IS_CLIENT )
561 {
Paul Bakker48916f92012-09-16 19:57:18 +0000562 key1 = keyblk + transform->maclen * 2;
563 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
Paul Bakker68884e32013-01-07 18:20:04 +0100565 mac_enc = keyblk;
566 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000568 /*
569 * This is not used in TLS v1.1.
570 */
Paul Bakker48916f92012-09-16 19:57:18 +0000571 iv_copy_len = ( transform->fixed_ivlen ) ?
572 transform->fixed_ivlen : transform->ivlen;
573 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
574 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000575 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 }
577 else
578 {
Paul Bakker48916f92012-09-16 19:57:18 +0000579 key1 = keyblk + transform->maclen * 2 + transform->keylen;
580 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
Paul Bakker68884e32013-01-07 18:20:04 +0100582 mac_enc = keyblk + transform->maclen;
583 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000585 /*
586 * This is not used in TLS v1.1.
587 */
Paul Bakker48916f92012-09-16 19:57:18 +0000588 iv_copy_len = ( transform->fixed_ivlen ) ?
589 transform->fixed_ivlen : transform->ivlen;
590 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
591 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000592 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 }
594
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200595#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100596 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
597 {
598 memcpy( transform->mac_enc, mac_enc, transform->maclen );
599 memcpy( transform->mac_dec, mac_dec, transform->maclen );
600 }
601 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200602#endif
603#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
604 defined(POLARSSL_SSL_PROTO_TLS1_2)
605 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100606 {
607 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
608 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
609 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200610 else
611#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200612 {
613 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200614 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200615 }
Paul Bakker68884e32013-01-07 18:20:04 +0100616
Paul Bakker05ef8352012-05-08 09:17:57 +0000617#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
618 if( ssl_hw_record_init != NULL)
619 {
620 int ret = 0;
621
622 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
623
Paul Bakker07eb38b2012-12-19 14:42:06 +0100624 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
625 transform->iv_enc, transform->iv_dec,
626 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100627 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100628 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000629 {
630 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
631 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
632 }
633 }
634#endif
635
Paul Bakker68884e32013-01-07 18:20:04 +0100636 switch( cipher_info->type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 {
Paul Bakker68884e32013-01-07 18:20:04 +0100638 case POLARSSL_CIPHER_ARC4_128:
Paul Bakker68884e32013-01-07 18:20:04 +0100639 case POLARSSL_CIPHER_DES_EDE3_CBC:
Paul Bakkercca5b812013-08-31 17:40:26 +0200640 case POLARSSL_CIPHER_CAMELLIA_128_CBC:
641 case POLARSSL_CIPHER_CAMELLIA_256_CBC:
Paul Bakker68884e32013-01-07 18:20:04 +0100642 case POLARSSL_CIPHER_AES_128_CBC:
643 case POLARSSL_CIPHER_AES_256_CBC:
Paul Bakkercca5b812013-08-31 17:40:26 +0200644 case POLARSSL_CIPHER_DES_CBC:
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +0200645 case POLARSSL_CIPHER_AES_128_GCM:
646 case POLARSSL_CIPHER_AES_256_GCM:
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200647 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
648 cipher_info ) ) != 0 )
649 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200650 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200651 return( ret );
652 }
653
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200654 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
655 cipher_info ) ) != 0 )
656 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200657 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200658 return( ret );
659 }
660
Paul Bakker45125bc2013-09-04 16:47:11 +0200661 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
662 cipher_info->key_length,
663 POLARSSL_ENCRYPT ) ) != 0 )
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200664 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200665 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
666 return( ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200667 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200668
Paul Bakker45125bc2013-09-04 16:47:11 +0200669 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
670 cipher_info->key_length,
671 POLARSSL_DECRYPT ) ) != 0 )
672 {
673 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
674 return( ret );
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200675 }
676
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200677#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200678 if( cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200679 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200680 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
681 POLARSSL_PADDING_NONE ) ) != 0 )
682 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200683 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200684 return( ret );
685 }
686
687 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
688 POLARSSL_PADDING_NONE ) ) != 0 )
689 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200690 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200691 return( ret );
692 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200693 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200694#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
Paul Bakker68884e32013-01-07 18:20:04 +0100697 case POLARSSL_CIPHER_NULL:
698 break;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000699
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000701 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 }
703
704 memset( keyblk, 0, sizeof( keyblk ) );
705
Paul Bakker2770fbd2012-07-03 13:30:23 +0000706#if defined(POLARSSL_ZLIB_SUPPORT)
707 // Initialize compression
708 //
Paul Bakker48916f92012-09-16 19:57:18 +0000709 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000710 {
Paul Bakker16770332013-10-11 09:59:44 +0200711 if( ssl->compress_buf == NULL )
712 {
713 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
714 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
715 if( ssl->compress_buf == NULL )
716 {
717 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
718 SSL_BUFFER_LEN ) );
719 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
720 }
721 }
722
Paul Bakker2770fbd2012-07-03 13:30:23 +0000723 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
724
Paul Bakker48916f92012-09-16 19:57:18 +0000725 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
726 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000727
Paul Bakker48916f92012-09-16 19:57:18 +0000728 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
729 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000730 {
731 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
732 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
733 }
734 }
735#endif /* POLARSSL_ZLIB_SUPPORT */
736
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
738
739 return( 0 );
740}
741
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200742#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000743void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000744{
745 md5_context md5;
746 sha1_context sha1;
747 unsigned char pad_1[48];
748 unsigned char pad_2[48];
749
Paul Bakker380da532012-04-18 16:10:25 +0000750 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Paul Bakker48916f92012-09-16 19:57:18 +0000752 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
753 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
Paul Bakker380da532012-04-18 16:10:25 +0000755 memset( pad_1, 0x36, 48 );
756 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Paul Bakker48916f92012-09-16 19:57:18 +0000758 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000759 md5_update( &md5, pad_1, 48 );
760 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
Paul Bakker380da532012-04-18 16:10:25 +0000762 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000763 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000764 md5_update( &md5, pad_2, 48 );
765 md5_update( &md5, hash, 16 );
766 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
Paul Bakker48916f92012-09-16 19:57:18 +0000768 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000769 sha1_update( &sha1, pad_1, 40 );
770 sha1_finish( &sha1, hash + 16 );
771
772 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000773 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000774 sha1_update( &sha1, pad_2, 40 );
775 sha1_update( &sha1, hash + 16, 20 );
776 sha1_finish( &sha1, hash + 16 );
777
778 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
779 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
780
781 return;
782}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200783#endif
Paul Bakker380da532012-04-18 16:10:25 +0000784
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200785#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000786void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
787{
788 md5_context md5;
789 sha1_context sha1;
790
791 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
792
Paul Bakker48916f92012-09-16 19:57:18 +0000793 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
794 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000795
Paul Bakker48916f92012-09-16 19:57:18 +0000796 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000797 sha1_finish( &sha1, hash + 16 );
798
799 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
800 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
801
802 return;
803}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200804#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000805
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200806#if defined(POLARSSL_SSL_PROTO_TLS1_2)
807#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000808void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
809{
Paul Bakker9e36f042013-06-30 14:34:05 +0200810 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000811
812 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
813
Paul Bakker9e36f042013-06-30 14:34:05 +0200814 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
815 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000816
817 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
818 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
819
820 return;
821}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200822#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000823
Paul Bakker9e36f042013-06-30 14:34:05 +0200824#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000825void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
826{
Paul Bakker9e36f042013-06-30 14:34:05 +0200827 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000828
829 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
830
Paul Bakker9e36f042013-06-30 14:34:05 +0200831 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
832 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000833
Paul Bakkerca4ab492012-04-18 14:23:57 +0000834 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
836
837 return;
838}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200839#endif /* POLARSSL_SHA512_C */
840#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200842#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
843 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
844 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
845int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
846{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200847 unsigned char *p = ssl->handshake->premaster;
848 unsigned char *end = p + sizeof( ssl->handshake->premaster );
849
850 /*
851 * PMS = struct {
852 * opaque other_secret<0..2^16-1>;
853 * opaque psk<0..2^16-1>;
854 * };
855 * with "other_secret" depending on the particular key exchange
856 */
857#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
858 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
859 {
860 if( end - p < 2 + (int) ssl->psk_len )
861 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
862
863 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
864 *(p++) = (unsigned char)( ssl->psk_len );
865 p += ssl->psk_len;
866 }
867 else
868#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
869#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
870 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
871 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200872 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200873 size_t len = ssl->handshake->dhm_ctx.len;
874
875 if( end - p < 2 + (int) len )
876 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
877
878 *(p++) = (unsigned char)( len >> 8 );
879 *(p++) = (unsigned char)( len );
880 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
881 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
882 {
883 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
884 return( ret );
885 }
886 p += len;
887
888 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
889 }
890 else
891#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
892#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
893 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
894 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200895 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200896 size_t zlen;
897
898 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
899 p + 2, end - (p + 2),
900 ssl->f_rng, ssl->p_rng ) ) != 0 )
901 {
902 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
903 return( ret );
904 }
905
906 *(p++) = (unsigned char)( zlen >> 8 );
907 *(p++) = (unsigned char)( zlen );
908 p += zlen;
909
910 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
911 }
912 else
913#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
914 {
915 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
916 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
917 }
918
919 /* opaque psk<0..2^16-1>; */
920 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
921 *(p++) = (unsigned char)( ssl->psk_len );
922 memcpy( p, ssl->psk, ssl->psk_len );
923 p += ssl->psk_len;
924
925 ssl->handshake->pmslen = p - ssl->handshake->premaster;
926
927 return( 0 );
928}
929#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
930 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
931 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
932
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200933#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000934/*
935 * SSLv3.0 MAC functions
936 */
Paul Bakker68884e32013-01-07 18:20:04 +0100937static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
938 unsigned char *buf, size_t len,
939 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000940{
941 unsigned char header[11];
942 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100943 int padlen = 0;
944 int md_size = md_get_size( md_ctx->md_info );
945 int md_type = md_get_type( md_ctx->md_info );
946
947 if( md_type == POLARSSL_MD_MD5 )
948 padlen = 48;
949 else if( md_type == POLARSSL_MD_SHA1 )
950 padlen = 40;
951 else if( md_type == POLARSSL_MD_SHA256 )
952 padlen = 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
954 memcpy( header, ctr, 8 );
955 header[ 8] = (unsigned char) type;
956 header[ 9] = (unsigned char)( len >> 8 );
957 header[10] = (unsigned char)( len );
958
Paul Bakker68884e32013-01-07 18:20:04 +0100959 memset( padding, 0x36, padlen );
960 md_starts( md_ctx );
961 md_update( md_ctx, secret, md_size );
962 md_update( md_ctx, padding, padlen );
963 md_update( md_ctx, header, 11 );
964 md_update( md_ctx, buf, len );
965 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
Paul Bakker68884e32013-01-07 18:20:04 +0100967 memset( padding, 0x5C, padlen );
968 md_starts( md_ctx );
969 md_update( md_ctx, secret, md_size );
970 md_update( md_ctx, padding, padlen );
971 md_update( md_ctx, buf + len, md_size );
972 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000973}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200974#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000975
Paul Bakker5121ce52009-01-03 21:22:43 +0000976/*
977 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200978 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000979static int ssl_encrypt_buf( ssl_context *ssl )
980{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200981 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
983 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
984
985 /*
986 * Add MAC then encrypt
987 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200988#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
990 {
Paul Bakker68884e32013-01-07 18:20:04 +0100991 ssl_mac( &ssl->transform_out->md_ctx_enc,
992 ssl->transform_out->mac_enc,
993 ssl->out_msg, ssl->out_msglen,
994 ssl->out_ctr, ssl->out_msgtype );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 }
996 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200997#endif
998#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
999 defined(POLARSSL_SSL_PROTO_TLS1_2)
1000 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 {
Paul Bakker68884e32013-01-07 18:20:04 +01001002 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1003 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1004 ssl->out_msg, ssl->out_msglen );
1005 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1006 ssl->out_msg + ssl->out_msglen );
1007 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001008 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001009 else
1010#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001011 {
1012 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001013 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001014 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
1016 SSL_DEBUG_BUF( 4, "computed mac",
Paul Bakker48916f92012-09-16 19:57:18 +00001017 ssl->out_msg + ssl->out_msglen, ssl->transform_out->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Paul Bakker48916f92012-09-16 19:57:18 +00001019 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Paul Bakker68884e32013-01-07 18:20:04 +01001021#if defined(POLARSSL_CIPHER_NULL_CIPHER)
1022 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
1023 {
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001024 ; /* Nothing to do */
Paul Bakker68884e32013-01-07 18:20:04 +01001025 }
1026 else
1027#endif /* POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001028#if defined(POLARSSL_ARC4_C)
Paul Bakker68884e32013-01-07 18:20:04 +01001029 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001031 int ret;
1032 size_t olen = 0;
1033
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1035 "including %d bytes of padding",
1036 ssl->out_msglen, 0 ) );
1037
1038 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1039 ssl->out_msg, ssl->out_msglen );
1040
Paul Bakker45125bc2013-09-04 16:47:11 +02001041 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001042 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001043 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1044 return( ret );
1045 }
1046
1047 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1048 ssl->transform_out->iv_enc,
1049 ssl->transform_out->ivlen ) ) != 0 )
1050 {
1051 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001052 return( ret );
1053 }
1054
1055 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1056 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
1057 &olen ) ) != 0 )
1058 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001059 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001060 return( ret );
1061 }
1062
1063 if( ssl->out_msglen != olen )
1064 {
1065 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1066 ssl->out_msglen, olen ) );
1067 // TODO Real error number
1068 return( -1 );
1069 }
1070
1071 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001072 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001073 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001074 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001075 return( ret );
1076 }
1077
1078 if( 0 != olen )
1079 {
1080 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1081 0, olen ) );
1082 // TODO Real error number
1083 return( -1 );
1084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 }
Paul Bakker68884e32013-01-07 18:20:04 +01001086 else
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001087#endif /* POLARSSL_ARC4_C */
Paul Bakker68884e32013-01-07 18:20:04 +01001088#if defined(POLARSSL_GCM_C)
1089 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
1090 ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001091 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001092 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001093 unsigned char *enc_msg;
1094 unsigned char add_data[13];
1095 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1096
Paul Bakkerca4ab492012-04-18 14:23:57 +00001097 enc_msglen = ssl->out_msglen;
1098
1099 memcpy( add_data, ssl->out_ctr, 8 );
1100 add_data[8] = ssl->out_msgtype;
1101 add_data[9] = ssl->major_ver;
1102 add_data[10] = ssl->minor_ver;
1103 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1104 add_data[12] = ssl->out_msglen & 0xFF;
1105
1106 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1107 add_data, 13 );
1108
Paul Bakker68884e32013-01-07 18:20:04 +01001109 /*
1110 * Generate IV
1111 */
1112 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001113 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001114 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1115 if( ret != 0 )
1116 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001117
Paul Bakker68884e32013-01-07 18:20:04 +01001118 memcpy( ssl->out_iv,
1119 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1120 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001121
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001122 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1123 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1124
Paul Bakker68884e32013-01-07 18:20:04 +01001125 /*
1126 * Fix pointer positions and message length with added IV
1127 */
1128 enc_msg = ssl->out_msg;
1129 enc_msglen = ssl->out_msglen;
1130 ssl->out_msglen += ssl->transform_out->ivlen -
1131 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001132
Paul Bakker68884e32013-01-07 18:20:04 +01001133 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1134 "including %d bytes of padding",
1135 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001136
Paul Bakker68884e32013-01-07 18:20:04 +01001137 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1138 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001139
Paul Bakker68884e32013-01-07 18:20:04 +01001140 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001141 * Encrypt
1142 */
1143 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1144 ssl->transform_out->iv_enc,
1145 ssl->transform_out->ivlen ) ) != 0 ||
1146 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1147 {
1148 return( ret );
1149 }
1150
1151 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1152 add_data, 13 ) ) != 0 )
1153 {
1154 return( ret );
1155 }
1156
1157 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1158 enc_msg, enc_msglen,
1159 enc_msg, &olen ) ) != 0 )
1160 {
1161 return( ret );
1162 }
1163 totlen = olen;
1164
1165 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1166 enc_msg + olen, &olen ) ) != 0 )
1167 {
1168 return( ret );
1169 }
1170 totlen += olen;
1171
1172 if( totlen != enc_msglen )
1173 {
1174 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1175 return( -1 );
1176 }
1177
1178 /*
1179 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001180 */
1181 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001182
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001183 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1184 enc_msg + enc_msglen, 16 ) ) != 0 )
1185 {
1186 return( ret );
1187 }
Paul Bakker68884e32013-01-07 18:20:04 +01001188
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001189 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001190 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 else
Paul Bakker68884e32013-01-07 18:20:04 +01001192#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001193#if defined(POLARSSL_CIPHER_MODE_CBC)
1194 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1195 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001197 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001198 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001199 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001200
Paul Bakker48916f92012-09-16 19:57:18 +00001201 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1202 ssl->transform_out->ivlen;
1203 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 padlen = 0;
1205
1206 for( i = 0; i <= padlen; i++ )
1207 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1208
1209 ssl->out_msglen += padlen + 1;
1210
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001211 enc_msglen = ssl->out_msglen;
1212 enc_msg = ssl->out_msg;
1213
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001214#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001215 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001216 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1217 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001218 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001219 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220 {
1221 /*
1222 * Generate IV
1223 */
Paul Bakker48916f92012-09-16 19:57:18 +00001224 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1225 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001226 if( ret != 0 )
1227 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001228
Paul Bakker92be97b2013-01-02 17:30:03 +01001229 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001230 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001231
1232 /*
1233 * Fix pointer positions and message length with added IV
1234 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001235 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001236 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001237 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001239#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001242 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001243 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
1245 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001246 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Paul Bakker45125bc2013-09-04 16:47:11 +02001248 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001250 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1251 return( ret );
1252 }
1253
1254 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1255 ssl->transform_out->iv_enc,
1256 ssl->transform_out->ivlen ) ) != 0 )
1257 {
1258 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001259 return( ret );
1260 }
Paul Bakker68884e32013-01-07 18:20:04 +01001261
Paul Bakkercca5b812013-08-31 17:40:26 +02001262 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1263 enc_msg, enc_msglen, enc_msg,
1264 &olen ) ) != 0 )
1265 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001266 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001267 return( ret );
1268 }
Paul Bakker68884e32013-01-07 18:20:04 +01001269
Paul Bakkercca5b812013-08-31 17:40:26 +02001270 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001271
Paul Bakkercca5b812013-08-31 17:40:26 +02001272 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001273 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001275 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001276 return( ret );
1277 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001278
Paul Bakkercca5b812013-08-31 17:40:26 +02001279 if( enc_msglen != olen )
1280 {
1281 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1282 enc_msglen, olen ) );
1283 // TODO Real error number
1284 return( -1 );
1285 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001286
1287#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001288 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1289 {
1290 /*
1291 * Save IV in SSL3 and TLS1
1292 */
1293 memcpy( ssl->transform_out->iv_enc,
1294 ssl->transform_out->cipher_ctx_enc.iv,
1295 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001297#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001299 else
1300#endif /* POLARSSL_CIPHER_MODE_CBC */
1301 {
1302 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1303 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1304 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001305
Paul Bakkerca4ab492012-04-18 14:23:57 +00001306 for( i = 8; i > 0; i-- )
1307 if( ++ssl->out_ctr[i - 1] != 0 )
1308 break;
1309
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1311
1312 return( 0 );
1313}
1314
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001315#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001316
Paul Bakker5121ce52009-01-03 21:22:43 +00001317static int ssl_decrypt_buf( ssl_context *ssl )
1318{
Paul Bakker45829992013-01-03 14:52:21 +01001319 size_t i, padlen = 0, correct = 1;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001320 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
1322 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1323
Paul Bakker48916f92012-09-16 19:57:18 +00001324 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 {
1326 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001327 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001328 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 }
1330
Paul Bakker68884e32013-01-07 18:20:04 +01001331#if defined(POLARSSL_CIPHER_NULL_CIPHER)
1332 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 {
Paul Bakker68884e32013-01-07 18:20:04 +01001334 padlen = 0;
1335 }
1336 else
1337#endif /* POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker40e46942009-01-03 21:51:57 +00001338#if defined(POLARSSL_ARC4_C)
Paul Bakker68884e32013-01-07 18:20:04 +01001339 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
1340 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001341 int ret;
1342 size_t olen = 0;
1343
Paul Bakker68884e32013-01-07 18:20:04 +01001344 padlen = 0;
1345
Paul Bakker45125bc2013-09-04 16:47:11 +02001346 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001347 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001348 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1349 return( ret );
1350 }
1351
1352 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1353 ssl->transform_in->iv_dec,
1354 ssl->transform_in->ivlen ) ) != 0 )
1355 {
1356 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001357 return( ret );
1358 }
1359
1360 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1361 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1362 &olen ) ) != 0 )
1363 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001364 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001365 return( ret );
1366 }
1367
1368 if( ssl->in_msglen != olen )
1369 {
1370 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1371 // TODO Real error number
1372 return( -1 );
1373 }
1374
1375 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001376 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001377 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001378 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001379 return( ret );
1380 }
1381
1382 if( 0 != olen )
1383 {
1384 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1385 // TODO Real error number
1386 return( -1 );
1387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 }
Paul Bakker68884e32013-01-07 18:20:04 +01001389 else
1390#endif /* POLARSSL_ARC4_C */
1391#if defined(POLARSSL_GCM_C)
1392 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
1393 ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001394 {
1395 unsigned char *dec_msg;
1396 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001397 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001398 unsigned char add_data[13];
1399 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1400
Paul Bakker68884e32013-01-07 18:20:04 +01001401 padlen = 0;
1402
1403 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1404 ssl->transform_in->fixed_ivlen );
1405 dec_msglen -= 16;
1406 dec_msg = ssl->in_msg;
1407 dec_msg_result = ssl->in_msg;
1408 ssl->in_msglen = dec_msglen;
1409
1410 memcpy( add_data, ssl->in_ctr, 8 );
1411 add_data[8] = ssl->in_msgtype;
1412 add_data[9] = ssl->major_ver;
1413 add_data[10] = ssl->minor_ver;
1414 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1415 add_data[12] = ssl->in_msglen & 0xFF;
1416
1417 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1418 add_data, 13 );
1419
1420 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1421 ssl->in_iv,
1422 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1423
1424 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1425 ssl->transform_in->ivlen );
1426 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1427
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001428 /*
1429 * Decrypt
1430 */
1431 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1432 ssl->transform_in->iv_dec,
1433 ssl->transform_in->ivlen ) ) != 0 ||
1434 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001435 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001436 return( ret );
1437 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001438
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001439 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1440 add_data, 13 ) ) != 0 )
1441 {
1442 return( ret );
1443 }
1444
1445 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1446 dec_msg, dec_msglen,
1447 dec_msg_result, &olen ) ) != 0 )
1448 {
1449 return( ret );
1450 }
1451 totlen = olen;
1452
1453 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1454 dec_msg_result + olen, &olen ) ) != 0 )
1455 {
1456 return( ret );
1457 }
1458 totlen += olen;
1459
1460 if( totlen != dec_msglen )
1461 {
1462 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1463 return( -1 );
1464 }
1465
1466 /*
1467 * Authenticate
1468 */
1469 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1470 dec_msg + dec_msglen, 16 ) ) != 0 )
1471 {
1472 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001473 return( POLARSSL_ERR_SSL_INVALID_MAC );
1474 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001475
Paul Bakkerca4ab492012-04-18 14:23:57 +00001476 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 else
Paul Bakker68884e32013-01-07 18:20:04 +01001478#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001479#if defined(POLARSSL_CIPHER_MODE_CBC)
1480 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1481 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 {
Paul Bakker45829992013-01-03 14:52:21 +01001483 /*
1484 * Decrypt and check the padding
1485 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001486 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001487 unsigned char *dec_msg;
1488 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001489 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001490 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001491 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001492
Paul Bakker5121ce52009-01-03 21:22:43 +00001493 /*
Paul Bakker45829992013-01-03 14:52:21 +01001494 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001495 */
Paul Bakker48916f92012-09-16 19:57:18 +00001496 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001497 {
1498 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001499 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001500 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 }
1502
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001503#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001504 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1505 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001506#endif
Paul Bakker45829992013-01-03 14:52:21 +01001507
1508 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1509 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1510 {
1511 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1512 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1513 return( POLARSSL_ERR_SSL_INVALID_MAC );
1514 }
1515
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001516 dec_msglen = ssl->in_msglen;
1517 dec_msg = ssl->in_msg;
1518 dec_msg_result = ssl->in_msg;
1519
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001520#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001521 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001522 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001523 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001524 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001525 {
Paul Bakker48916f92012-09-16 19:57:18 +00001526 dec_msglen -= ssl->transform_in->ivlen;
1527 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001528
Paul Bakker48916f92012-09-16 19:57:18 +00001529 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001530 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001531 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001532#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001533
Paul Bakker45125bc2013-09-04 16:47:11 +02001534 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001536 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1537 return( ret );
1538 }
1539
1540 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1541 ssl->transform_in->iv_dec,
1542 ssl->transform_in->ivlen ) ) != 0 )
1543 {
1544 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001545 return( ret );
1546 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001547
Paul Bakkercca5b812013-08-31 17:40:26 +02001548 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1549 dec_msg, dec_msglen, dec_msg_result,
1550 &olen ) ) != 0 )
1551 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001552 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001553 return( ret );
1554 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001555
Paul Bakkercca5b812013-08-31 17:40:26 +02001556 dec_msglen -= olen;
1557 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001558 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001559 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001560 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001561 return( ret );
1562 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001563
Paul Bakkercca5b812013-08-31 17:40:26 +02001564 if( dec_msglen != olen )
1565 {
1566 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1567 // TODO Real error number
1568 return( -1 );
1569 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001570
1571#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001572 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1573 {
1574 /*
1575 * Save IV in SSL3 and TLS1
1576 */
1577 memcpy( ssl->transform_in->iv_dec,
1578 ssl->transform_in->cipher_ctx_dec.iv,
1579 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001580 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001581#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001582
1583 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001584
1585 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1586 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001587#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001588 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1589 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001590#endif
Paul Bakker45829992013-01-03 14:52:21 +01001591 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001592 correct = 0;
1593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001595#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001596 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1597 {
Paul Bakker48916f92012-09-16 19:57:18 +00001598 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001599 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001600#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001601 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1602 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001603 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001604#endif
Paul Bakker45829992013-01-03 14:52:21 +01001605 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001606 }
1607 }
1608 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001609#endif
1610#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1611 defined(POLARSSL_SSL_PROTO_TLS1_2)
1612 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 {
1614 /*
Paul Bakker45829992013-01-03 14:52:21 +01001615 * TLSv1+: always check the padding up to the first failure
1616 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001618 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001619 size_t padding_idx = ssl->in_msglen - padlen - 1;
1620
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001621 for( i = 1; i <= 256; i++ )
1622 {
1623 real_count &= ( i <= padlen );
1624 pad_count += real_count *
1625 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1626 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001627
1628 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001629
Paul Bakkerd66f0702013-01-31 16:57:45 +01001630#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001631 if( padlen > 0 && correct == 0)
1632 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001633#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001634 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001636 else
1637#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1638 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001639 {
1640 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001641 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001642 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001644 else
1645#endif /* POLARSSL_CIPHER_MODE_CBC */
1646 {
1647 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1648 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1649 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
1651 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1652 ssl->in_msg, ssl->in_msglen );
1653
1654 /*
1655 * Always compute the MAC (RFC4346, CBCTIME).
1656 */
Paul Bakker48916f92012-09-16 19:57:18 +00001657 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
1659 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1660 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
1661
Paul Bakker45829992013-01-03 14:52:21 +01001662 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001663
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001664#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1666 {
Paul Bakker68884e32013-01-07 18:20:04 +01001667 ssl_mac( &ssl->transform_in->md_ctx_dec,
1668 ssl->transform_in->mac_dec,
1669 ssl->in_msg, ssl->in_msglen,
1670 ssl->in_ctr, ssl->in_msgtype );
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 }
1672 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001673#endif /* POLARSSL_SSL_PROTO_SSL3 */
1674#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1675 defined(POLARSSL_SSL_PROTO_TLS1_2)
1676 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 {
Paul Bakker45829992013-01-03 14:52:21 +01001678 /*
1679 * Process MAC and always update for padlen afterwards to make
1680 * total time independent of padlen
Paul Bakkere47b34b2013-02-27 14:48:00 +01001681 *
1682 * extra_run compensates MAC check for padlen
1683 *
1684 * Known timing attacks:
1685 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1686 *
1687 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1688 * correctly. (We round down instead of up, so -56 is the correct
1689 * value for our calculations instead of -55)
Paul Bakker45829992013-01-03 14:52:21 +01001690 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001691 size_t j, extra_run = 0;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001692 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1693 ( 13 + ssl->in_msglen + 8 ) / 64;
1694
1695 extra_run &= correct * 0xFF;
1696
Paul Bakker68884e32013-01-07 18:20:04 +01001697 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1698 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1699 ssl->in_msglen );
1700 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1701 ssl->in_msg + ssl->in_msglen );
1702 for( j = 0; j < extra_run; j++ )
1703 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001704
Paul Bakker68884e32013-01-07 18:20:04 +01001705 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001707 else
1708#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1709 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001710 {
1711 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001712 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001713 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
Paul Bakker48916f92012-09-16 19:57:18 +00001715 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001716 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
Paul Bakker48916f92012-09-16 19:57:18 +00001717 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001718
1719 if( memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Paul Bakker48916f92012-09-16 19:57:18 +00001720 ssl->transform_in->maclen ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001722#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001723 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001724#endif
Paul Bakker45829992013-01-03 14:52:21 +01001725 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001726 }
1727
1728 /*
Paul Bakker45829992013-01-03 14:52:21 +01001729 * Finally check the correct flag
Paul Bakker5121ce52009-01-03 21:22:43 +00001730 */
Paul Bakker45829992013-01-03 14:52:21 +01001731 if( correct == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +00001732 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001733
1734 if( ssl->in_msglen == 0 )
1735 {
1736 ssl->nb_zero++;
1737
1738 /*
1739 * Three or more empty messages may be a DoS attack
1740 * (excessive CPU consumption).
1741 */
1742 if( ssl->nb_zero > 3 )
1743 {
1744 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1745 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001746 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001747 }
1748 }
1749 else
1750 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001751
Paul Bakker23986e52011-04-24 08:57:21 +00001752 for( i = 8; i > 0; i-- )
1753 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001754 break;
1755
1756 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1757
1758 return( 0 );
1759}
1760
Paul Bakker2770fbd2012-07-03 13:30:23 +00001761#if defined(POLARSSL_ZLIB_SUPPORT)
1762/*
1763 * Compression/decompression functions
1764 */
1765static int ssl_compress_buf( ssl_context *ssl )
1766{
1767 int ret;
1768 unsigned char *msg_post = ssl->out_msg;
1769 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001770 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001771
1772 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1773
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001774 if( len_pre == 0 )
1775 return( 0 );
1776
Paul Bakker2770fbd2012-07-03 13:30:23 +00001777 memcpy( msg_pre, ssl->out_msg, len_pre );
1778
1779 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1780 ssl->out_msglen ) );
1781
1782 SSL_DEBUG_BUF( 4, "before compression: output payload",
1783 ssl->out_msg, ssl->out_msglen );
1784
Paul Bakker48916f92012-09-16 19:57:18 +00001785 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1786 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1787 ssl->transform_out->ctx_deflate.next_out = msg_post;
1788 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001789
Paul Bakker48916f92012-09-16 19:57:18 +00001790 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001791 if( ret != Z_OK )
1792 {
1793 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1794 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1795 }
1796
Paul Bakker48916f92012-09-16 19:57:18 +00001797 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001798
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1800 ssl->out_msglen ) );
1801
1802 SSL_DEBUG_BUF( 4, "after compression: output payload",
1803 ssl->out_msg, ssl->out_msglen );
1804
1805 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1806
1807 return( 0 );
1808}
1809
1810static int ssl_decompress_buf( ssl_context *ssl )
1811{
1812 int ret;
1813 unsigned char *msg_post = ssl->in_msg;
1814 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001815 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001816
1817 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1818
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001819 if( len_pre == 0 )
1820 return( 0 );
1821
Paul Bakker2770fbd2012-07-03 13:30:23 +00001822 memcpy( msg_pre, ssl->in_msg, len_pre );
1823
1824 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1825 ssl->in_msglen ) );
1826
1827 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1828 ssl->in_msg, ssl->in_msglen );
1829
Paul Bakker48916f92012-09-16 19:57:18 +00001830 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1831 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1832 ssl->transform_in->ctx_inflate.next_out = msg_post;
1833 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001834
Paul Bakker48916f92012-09-16 19:57:18 +00001835 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001836 if( ret != Z_OK )
1837 {
1838 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1839 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1840 }
1841
Paul Bakker48916f92012-09-16 19:57:18 +00001842 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001843
Paul Bakker2770fbd2012-07-03 13:30:23 +00001844 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1845 ssl->in_msglen ) );
1846
1847 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1848 ssl->in_msg, ssl->in_msglen );
1849
1850 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1851
1852 return( 0 );
1853}
1854#endif /* POLARSSL_ZLIB_SUPPORT */
1855
Paul Bakker5121ce52009-01-03 21:22:43 +00001856/*
1857 * Fill the input message buffer
1858 */
Paul Bakker23986e52011-04-24 08:57:21 +00001859int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001860{
Paul Bakker23986e52011-04-24 08:57:21 +00001861 int ret;
1862 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
1864 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1865
1866 while( ssl->in_left < nb_want )
1867 {
1868 len = nb_want - ssl->in_left;
1869 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1870
1871 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1872 ssl->in_left, nb_want ) );
1873 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1874
Paul Bakker831a7552011-05-18 13:32:51 +00001875 if( ret == 0 )
1876 return( POLARSSL_ERR_SSL_CONN_EOF );
1877
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 if( ret < 0 )
1879 return( ret );
1880
1881 ssl->in_left += ret;
1882 }
1883
1884 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1885
1886 return( 0 );
1887}
1888
1889/*
1890 * Flush any data not yet written
1891 */
1892int ssl_flush_output( ssl_context *ssl )
1893{
1894 int ret;
1895 unsigned char *buf;
1896
1897 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1898
1899 while( ssl->out_left > 0 )
1900 {
1901 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1902 5 + ssl->out_msglen, ssl->out_left ) );
1903
Paul Bakker5bd42292012-12-19 14:40:42 +01001904 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001906
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1908
1909 if( ret <= 0 )
1910 return( ret );
1911
1912 ssl->out_left -= ret;
1913 }
1914
1915 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1916
1917 return( 0 );
1918}
1919
1920/*
1921 * Record layer functions
1922 */
1923int ssl_write_record( ssl_context *ssl )
1924{
Paul Bakker05ef8352012-05-08 09:17:57 +00001925 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001926 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
1928 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1929
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1931 {
1932 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1933 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1934 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1935
Paul Bakker48916f92012-09-16 19:57:18 +00001936 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 }
1938
Paul Bakker2770fbd2012-07-03 13:30:23 +00001939#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001940 if( ssl->transform_out != NULL &&
1941 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001942 {
1943 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1944 {
1945 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1946 return( ret );
1947 }
1948
1949 len = ssl->out_msglen;
1950 }
1951#endif /*POLARSSL_ZLIB_SUPPORT */
1952
Paul Bakker05ef8352012-05-08 09:17:57 +00001953#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1954 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001956 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957
Paul Bakker05ef8352012-05-08 09:17:57 +00001958 ret = ssl_hw_record_write( ssl );
1959 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1960 {
1961 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1962 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1963 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001964
1965 if( ret == 0 )
1966 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001967 }
1968#endif
1969 if( !done )
1970 {
1971 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1972 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1973 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1975 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001976
Paul Bakker48916f92012-09-16 19:57:18 +00001977 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001978 {
1979 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1980 {
1981 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1982 return( ret );
1983 }
1984
1985 len = ssl->out_msglen;
1986 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1987 ssl->out_hdr[4] = (unsigned char)( len );
1988 }
1989
1990 ssl->out_left = 5 + ssl->out_msglen;
1991
1992 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1993 "version = [%d:%d], msglen = %d",
1994 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1995 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1996
1997 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001998 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 }
2000
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2002 {
2003 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2004 return( ret );
2005 }
2006
2007 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2008
2009 return( 0 );
2010}
2011
2012int ssl_read_record( ssl_context *ssl )
2013{
Paul Bakker05ef8352012-05-08 09:17:57 +00002014 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002015
2016 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2017
Paul Bakker68884e32013-01-07 18:20:04 +01002018 SSL_DEBUG_BUF( 4, "input record from network",
2019 ssl->in_hdr, 5 + ssl->in_msglen );
2020
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 if( ssl->in_hslen != 0 &&
2022 ssl->in_hslen < ssl->in_msglen )
2023 {
2024 /*
2025 * Get next Handshake message in the current record
2026 */
2027 ssl->in_msglen -= ssl->in_hslen;
2028
Paul Bakker8934a982011-08-05 11:11:53 +00002029 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002030 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031
2032 ssl->in_hslen = 4;
2033 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2034
2035 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2036 " %d, type = %d, hslen = %d",
2037 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2038
2039 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2040 {
2041 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002042 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 }
2044
2045 if( ssl->in_msglen < ssl->in_hslen )
2046 {
2047 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002048 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 }
2050
Paul Bakker48916f92012-09-16 19:57:18 +00002051 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002052
2053 return( 0 );
2054 }
2055
2056 ssl->in_hslen = 0;
2057
2058 /*
2059 * Read the record header and validate it
2060 */
2061 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2062 {
2063 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2064 return( ret );
2065 }
2066
2067 ssl->in_msgtype = ssl->in_hdr[0];
2068 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2069
2070 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2071 "version = [%d:%d], msglen = %d",
2072 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2073 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2074
2075 if( ssl->in_hdr[1] != ssl->major_ver )
2076 {
2077 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002078 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 }
2080
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002081 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 {
2083 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002084 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 }
2086
2087 /*
2088 * Make sure the message length is acceptable
2089 */
Paul Bakker48916f92012-09-16 19:57:18 +00002090 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 {
2092 if( ssl->in_msglen < 1 ||
2093 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2094 {
2095 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002096 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 }
2098 }
2099 else
2100 {
Paul Bakker48916f92012-09-16 19:57:18 +00002101 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 {
2103 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002104 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
2106
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002107#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002109 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
2111 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002112 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002114#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002116#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2117 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 /*
2119 * TLS encrypted messages can have up to 256 bytes of padding
2120 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002121 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002122 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
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 }
2129
2130 /*
2131 * Read and optionally decrypt the message contents
2132 */
2133 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2134 {
2135 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2136 return( ret );
2137 }
2138
2139 SSL_DEBUG_BUF( 4, "input record from network",
2140 ssl->in_hdr, 5 + ssl->in_msglen );
2141
Paul Bakker05ef8352012-05-08 09:17:57 +00002142#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2143 if( ssl_hw_record_read != NULL)
2144 {
2145 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2146
2147 ret = ssl_hw_record_read( ssl );
2148 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2149 {
2150 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2151 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2152 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002153
2154 if( ret == 0 )
2155 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002156 }
2157#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002158 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 {
2160 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2161 {
Paul Bakker40865c82013-01-31 17:13:13 +01002162#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2163 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2164 {
2165 ssl_send_alert_message( ssl,
2166 SSL_ALERT_LEVEL_FATAL,
2167 SSL_ALERT_MSG_BAD_RECORD_MAC );
2168 }
2169#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2171 return( ret );
2172 }
2173
2174 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2175 ssl->in_msg, ssl->in_msglen );
2176
2177 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2178 {
2179 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002180 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 }
2182 }
2183
Paul Bakker2770fbd2012-07-03 13:30:23 +00002184#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002185 if( ssl->transform_in != NULL &&
2186 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002187 {
2188 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2189 {
2190 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2191 return( ret );
2192 }
2193
2194 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2195 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2196 }
2197#endif /* POLARSSL_ZLIB_SUPPORT */
2198
Paul Bakker0a925182012-04-16 06:46:41 +00002199 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2200 ssl->in_msgtype != SSL_MSG_ALERT &&
2201 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2202 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2203 {
2204 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2205
Paul Bakker48916f92012-09-16 19:57:18 +00002206 if( ( ret = ssl_send_alert_message( ssl,
2207 SSL_ALERT_LEVEL_FATAL,
2208 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002209 {
2210 return( ret );
2211 }
2212
2213 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2214 }
2215
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2217 {
2218 ssl->in_hslen = 4;
2219 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2220
2221 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2222 " %d, type = %d, hslen = %d",
2223 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2224
2225 /*
2226 * Additional checks to validate the handshake header
2227 */
2228 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2229 {
2230 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002231 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 }
2233
2234 if( ssl->in_msglen < ssl->in_hslen )
2235 {
2236 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002237 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 }
2239
Paul Bakker48916f92012-09-16 19:57:18 +00002240 if( ssl->state != SSL_HANDSHAKE_OVER )
2241 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 }
2243
2244 if( ssl->in_msgtype == SSL_MSG_ALERT )
2245 {
2246 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2247 ssl->in_msg[0], ssl->in_msg[1] ) );
2248
2249 /*
2250 * Ignore non-fatal alerts, except close_notify
2251 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002252 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002254 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2255 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002256 /**
2257 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2258 * error identifier.
2259 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002260 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002263 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2264 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 {
2266 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002267 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 }
2269 }
2270
2271 ssl->in_left = 0;
2272
2273 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2274
2275 return( 0 );
2276}
2277
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002278int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2279{
2280 int ret;
2281
2282 if( ( ret = ssl_send_alert_message( ssl,
2283 SSL_ALERT_LEVEL_FATAL,
2284 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2285 {
2286 return( ret );
2287 }
2288
2289 return( 0 );
2290}
2291
Paul Bakker0a925182012-04-16 06:46:41 +00002292int ssl_send_alert_message( ssl_context *ssl,
2293 unsigned char level,
2294 unsigned char message )
2295{
2296 int ret;
2297
2298 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2299
2300 ssl->out_msgtype = SSL_MSG_ALERT;
2301 ssl->out_msglen = 2;
2302 ssl->out_msg[0] = level;
2303 ssl->out_msg[1] = message;
2304
2305 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2306 {
2307 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2308 return( ret );
2309 }
2310
2311 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2312
2313 return( 0 );
2314}
2315
Paul Bakker5121ce52009-01-03 21:22:43 +00002316/*
2317 * Handshake functions
2318 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002319#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2320 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002321 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2322 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002323int ssl_write_certificate( ssl_context *ssl )
2324{
Paul Bakkered27a042013-04-18 22:46:23 +02002325 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002326 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
2328 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2329
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002330 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002331 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2332 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002333 {
2334 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2335 ssl->state++;
2336 return( 0 );
2337 }
2338
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002339 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002340 return( ret );
2341}
2342
2343int ssl_parse_certificate( ssl_context *ssl )
2344{
2345 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2346 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2347
2348 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2349
2350 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002351 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2352 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002353 {
2354 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2355 ssl->state++;
2356 return( 0 );
2357 }
2358
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002359 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002360 return( ret );
2361}
2362#else
2363int ssl_write_certificate( ssl_context *ssl )
2364{
2365 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2366 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002367 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002368 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2369
2370 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2371
2372 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002373 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2374 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002375 {
2376 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2377 ssl->state++;
2378 return( 0 );
2379 }
2380
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 if( ssl->endpoint == SSL_IS_CLIENT )
2382 {
2383 if( ssl->client_auth == 0 )
2384 {
2385 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2386 ssl->state++;
2387 return( 0 );
2388 }
2389
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002390#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 /*
2392 * If using SSLv3 and got no cert, send an Alert message
2393 * (otherwise an empty Certificate message will be sent).
2394 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002395 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2397 {
2398 ssl->out_msglen = 2;
2399 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002400 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2401 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
2403 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2404 goto write_msg;
2405 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002406#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 }
2408 else /* SSL_IS_SERVER */
2409 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002410 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 {
2412 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002413 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 }
2415 }
2416
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002417 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418
2419 /*
2420 * 0 . 0 handshake type
2421 * 1 . 3 handshake length
2422 * 4 . 6 length of all certs
2423 * 7 . 9 length of cert. 1
2424 * 10 . n-1 peer certificate
2425 * n . n+2 length of cert. 2
2426 * n+3 . ... upper level cert, etc.
2427 */
2428 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002429 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002430
Paul Bakker29087132010-03-21 21:03:34 +00002431 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 {
2433 n = crt->raw.len;
2434 if( i + 3 + n > SSL_MAX_CONTENT_LEN )
2435 {
2436 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2437 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002438 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 }
2440
2441 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2442 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2443 ssl->out_msg[i + 2] = (unsigned char)( n );
2444
2445 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2446 i += n; crt = crt->next;
2447 }
2448
2449 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2450 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2451 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2452
2453 ssl->out_msglen = i;
2454 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2455 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2456
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002457#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002458write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002459#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002460
2461 ssl->state++;
2462
2463 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2464 {
2465 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2466 return( ret );
2467 }
2468
2469 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2470
Paul Bakkered27a042013-04-18 22:46:23 +02002471 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002472}
2473
2474int ssl_parse_certificate( ssl_context *ssl )
2475{
Paul Bakkered27a042013-04-18 22:46:23 +02002476 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002477 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002478 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
2480 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2481
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002482 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002483 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2484 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002485 {
2486 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2487 ssl->state++;
2488 return( 0 );
2489 }
2490
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 if( ssl->endpoint == SSL_IS_SERVER &&
2492 ssl->authmode == SSL_VERIFY_NONE )
2493 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002494 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2496 ssl->state++;
2497 return( 0 );
2498 }
2499
2500 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2501 {
2502 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2503 return( ret );
2504 }
2505
2506 ssl->state++;
2507
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002508#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 /*
2510 * Check if the client sent an empty certificate
2511 */
2512 if( ssl->endpoint == SSL_IS_SERVER &&
2513 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2514 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002515 if( ssl->in_msglen == 2 &&
2516 ssl->in_msgtype == SSL_MSG_ALERT &&
2517 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2518 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 {
2520 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2521
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002522 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2524 return( 0 );
2525 else
Paul Bakker40e46942009-01-03 21:51:57 +00002526 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 }
2528 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002529#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002531#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2532 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 if( ssl->endpoint == SSL_IS_SERVER &&
2534 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2535 {
2536 if( ssl->in_hslen == 7 &&
2537 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2538 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2539 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2540 {
2541 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2542
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002543 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002545 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 else
2547 return( 0 );
2548 }
2549 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002550#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2551 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
2553 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2554 {
2555 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002556 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 }
2558
2559 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2560 {
2561 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002562 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 }
2564
2565 /*
2566 * Same message structure as in ssl_write_certificate()
2567 */
2568 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2569
2570 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2571 {
2572 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002573 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 }
2575
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002576 /* In case we tried to reuse a session but it failed */
2577 if( ssl->session_negotiate->peer_cert != NULL )
2578 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002579 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002580 polarssl_free( ssl->session_negotiate->peer_cert );
2581 }
2582
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002583 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2584 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 {
2586 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002587 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002588 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 }
2590
Paul Bakkerb6b09562013-09-18 14:17:41 +02002591 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
2593 i = 7;
2594
2595 while( i < ssl->in_hslen )
2596 {
2597 if( ssl->in_msg[i] != 0 )
2598 {
2599 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002600 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
2602
2603 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2604 | (unsigned int) ssl->in_msg[i + 2];
2605 i += 3;
2606
2607 if( n < 128 || i + n > ssl->in_hslen )
2608 {
2609 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002610 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 }
2612
Paul Bakkerddf26b42013-09-18 13:46:23 +02002613 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2614 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 if( ret != 0 )
2616 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002617 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 return( ret );
2619 }
2620
2621 i += n;
2622 }
2623
Paul Bakker48916f92012-09-16 19:57:18 +00002624 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
2626 if( ssl->authmode != SSL_VERIFY_NONE )
2627 {
2628 if( ssl->ca_chain == NULL )
2629 {
2630 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002631 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 }
2633
Paul Bakkerddf26b42013-09-18 13:46:23 +02002634 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2635 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2636 &ssl->session_negotiate->verify_result,
2637 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
2639 if( ret != 0 )
2640 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2641
2642 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2643 ret = 0;
2644 }
2645
2646 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2647
2648 return( ret );
2649}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002650#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2651 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2652 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
2654int ssl_write_change_cipher_spec( ssl_context *ssl )
2655{
2656 int ret;
2657
2658 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2659
2660 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2661 ssl->out_msglen = 1;
2662 ssl->out_msg[0] = 1;
2663
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 ssl->state++;
2665
2666 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2667 {
2668 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2669 return( ret );
2670 }
2671
2672 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2673
2674 return( 0 );
2675}
2676
2677int ssl_parse_change_cipher_spec( ssl_context *ssl )
2678{
2679 int ret;
2680
2681 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2682
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2684 {
2685 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2686 return( ret );
2687 }
2688
2689 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2690 {
2691 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002692 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 }
2694
2695 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2696 {
2697 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002698 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700
2701 ssl->state++;
2702
2703 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2704
2705 return( 0 );
2706}
2707
Paul Bakker41c83d32013-03-20 14:39:14 +01002708void ssl_optimize_checksum( ssl_context *ssl,
2709 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002710{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002711 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002712
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002713#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2714 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002715 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002716 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002717 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002718#endif
2719#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2720#if defined(POLARSSL_SHA512_C)
2721 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2722 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2723 else
2724#endif
2725#if defined(POLARSSL_SHA256_C)
2726 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002727 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002728 else
2729#endif
2730#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2731 /* Should never happen */
2732 return;
Paul Bakker380da532012-04-18 16:10:25 +00002733}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002734
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002735static void ssl_update_checksum_start( ssl_context *ssl,
2736 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002737{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002738#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2739 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002740 md5_update( &ssl->handshake->fin_md5 , buf, len );
2741 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002742#endif
2743#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2744#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002745 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002746#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002747#if defined(POLARSSL_SHA512_C)
2748 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002749#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002750#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002751}
2752
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002753#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2754 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002755static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2756 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002757{
Paul Bakker48916f92012-09-16 19:57:18 +00002758 md5_update( &ssl->handshake->fin_md5 , buf, len );
2759 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002760}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002761#endif
Paul Bakker380da532012-04-18 16:10:25 +00002762
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002763#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2764#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002765static void ssl_update_checksum_sha256( ssl_context *ssl,
2766 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002767{
Paul Bakker9e36f042013-06-30 14:34:05 +02002768 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002769}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002770#endif
Paul Bakker380da532012-04-18 16:10:25 +00002771
Paul Bakker9e36f042013-06-30 14:34:05 +02002772#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002773static void ssl_update_checksum_sha384( ssl_context *ssl,
2774 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002775{
Paul Bakker9e36f042013-06-30 14:34:05 +02002776 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002777}
Paul Bakker769075d2012-11-24 11:26:46 +01002778#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002779#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002780
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002781#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002782static void ssl_calc_finished_ssl(
2783 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002784{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002785 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002786 md5_context md5;
2787 sha1_context sha1;
2788
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 unsigned char padbuf[48];
2790 unsigned char md5sum[16];
2791 unsigned char sha1sum[20];
2792
Paul Bakker48916f92012-09-16 19:57:18 +00002793 ssl_session *session = ssl->session_negotiate;
2794 if( !session )
2795 session = ssl->session;
2796
Paul Bakker1ef83d62012-04-11 12:09:53 +00002797 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2798
Paul Bakker48916f92012-09-16 19:57:18 +00002799 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2800 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
2802 /*
2803 * SSLv3:
2804 * hash =
2805 * MD5( master + pad2 +
2806 * MD5( handshake + sender + master + pad1 ) )
2807 * + SHA1( master + pad2 +
2808 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 */
2810
Paul Bakker90995b52013-06-24 19:20:35 +02002811#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002812 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002813 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002814#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Paul Bakker90995b52013-06-24 19:20:35 +02002816#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002818 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002819#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
Paul Bakker3c2122f2013-06-24 19:03:14 +02002821 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2822 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002823
Paul Bakker1ef83d62012-04-11 12:09:53 +00002824 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Paul Bakker3c2122f2013-06-24 19:03:14 +02002826 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002827 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002828 md5_update( &md5, padbuf, 48 );
2829 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Paul Bakker3c2122f2013-06-24 19:03:14 +02002831 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002832 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002833 sha1_update( &sha1, padbuf, 40 );
2834 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Paul Bakker1ef83d62012-04-11 12:09:53 +00002836 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
Paul Bakker1ef83d62012-04-11 12:09:53 +00002838 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002839 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002840 md5_update( &md5, padbuf, 48 );
2841 md5_update( &md5, md5sum, 16 );
2842 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Paul Bakker1ef83d62012-04-11 12:09:53 +00002844 sha1_starts( &sha1 );
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_update( &sha1, sha1sum, 20 );
2848 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Paul Bakker1ef83d62012-04-11 12:09:53 +00002850 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
Paul Bakker1ef83d62012-04-11 12:09:53 +00002852 memset( &md5, 0, sizeof( md5_context ) );
2853 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
2855 memset( padbuf, 0, sizeof( padbuf ) );
2856 memset( md5sum, 0, sizeof( md5sum ) );
2857 memset( sha1sum, 0, sizeof( sha1sum ) );
2858
2859 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2860}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002861#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002863#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002864static void ssl_calc_finished_tls(
2865 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002866{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002867 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002868 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Paul Bakker48916f92012-09-16 19:57:18 +00002873 ssl_session *session = ssl->session_negotiate;
2874 if( !session )
2875 session = ssl->session;
2876
Paul Bakker1ef83d62012-04-11 12:09:53 +00002877 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002878
Paul Bakker48916f92012-09-16 19:57:18 +00002879 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2880 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Paul Bakker1ef83d62012-04-11 12:09:53 +00002882 /*
2883 * TLSv1:
2884 * hash = PRF( master, finished_label,
2885 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2886 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Paul Bakker90995b52013-06-24 19:20:35 +02002888#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002889 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2890 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002891#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002892
Paul Bakker90995b52013-06-24 19:20:35 +02002893#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2895 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002896#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002897
2898 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002899 ? "client finished"
2900 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002901
2902 md5_finish( &md5, padbuf );
2903 sha1_finish( &sha1, padbuf + 16 );
2904
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002905 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002906 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002907
2908 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2909
2910 memset( &md5, 0, sizeof( md5_context ) );
2911 memset( &sha1, 0, sizeof( sha1_context ) );
2912
2913 memset( padbuf, 0, sizeof( padbuf ) );
2914
2915 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2916}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002917#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002918
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002919#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2920#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002921static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002922 ssl_context *ssl, unsigned char *buf, int from )
2923{
2924 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002925 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002926 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002927 unsigned char padbuf[32];
2928
Paul Bakker48916f92012-09-16 19:57:18 +00002929 ssl_session *session = ssl->session_negotiate;
2930 if( !session )
2931 session = ssl->session;
2932
Paul Bakker380da532012-04-18 16:10:25 +00002933 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002934
Paul Bakker9e36f042013-06-30 14:34:05 +02002935 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936
2937 /*
2938 * TLSv1.2:
2939 * hash = PRF( master, finished_label,
2940 * Hash( handshake ) )[0.11]
2941 */
2942
Paul Bakker9e36f042013-06-30 14:34:05 +02002943#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002944 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002945 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002946#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002947
2948 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002949 ? "client finished"
2950 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951
Paul Bakker9e36f042013-06-30 14:34:05 +02002952 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002953
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002954 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002955 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956
2957 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2958
Paul Bakker9e36f042013-06-30 14:34:05 +02002959 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960
2961 memset( padbuf, 0, sizeof( padbuf ) );
2962
2963 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2964}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002965#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002966
Paul Bakker9e36f042013-06-30 14:34:05 +02002967#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002968static void ssl_calc_finished_tls_sha384(
2969 ssl_context *ssl, unsigned char *buf, int from )
2970{
2971 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002972 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002973 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002974 unsigned char padbuf[48];
2975
Paul Bakker48916f92012-09-16 19:57:18 +00002976 ssl_session *session = ssl->session_negotiate;
2977 if( !session )
2978 session = ssl->session;
2979
Paul Bakker380da532012-04-18 16:10:25 +00002980 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002981
Paul Bakker9e36f042013-06-30 14:34:05 +02002982 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002983
2984 /*
2985 * TLSv1.2:
2986 * hash = PRF( master, finished_label,
2987 * Hash( handshake ) )[0.11]
2988 */
2989
Paul Bakker9e36f042013-06-30 14:34:05 +02002990#if !defined(POLARSSL_SHA512_ALT)
2991 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
2992 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002993#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00002994
2995 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002996 ? "client finished"
2997 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00002998
Paul Bakker9e36f042013-06-30 14:34:05 +02002999 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003001 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003002 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003003
3004 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3005
Paul Bakker9e36f042013-06-30 14:34:05 +02003006 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003007
3008 memset( padbuf, 0, sizeof( padbuf ) );
3009
3010 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3011}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003012#endif /* POLARSSL_SHA512_C */
3013#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003014
Paul Bakker48916f92012-09-16 19:57:18 +00003015void ssl_handshake_wrapup( ssl_context *ssl )
3016{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003017 int resume = ssl->handshake->resume;
3018
Paul Bakker48916f92012-09-16 19:57:18 +00003019 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3020
3021 /*
3022 * Free our handshake params
3023 */
3024 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003025 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003026 ssl->handshake = NULL;
3027
3028 /*
3029 * Switch in our now active transform context
3030 */
3031 if( ssl->transform )
3032 {
3033 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003034 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003035 }
3036 ssl->transform = ssl->transform_negotiate;
3037 ssl->transform_negotiate = NULL;
3038
Paul Bakker0a597072012-09-25 21:55:46 +00003039 if( ssl->session )
3040 {
3041 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003042 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003043 }
3044 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003045 ssl->session_negotiate = NULL;
3046
Paul Bakker0a597072012-09-25 21:55:46 +00003047 /*
3048 * Add cache entry
3049 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003050 if( ssl->f_set_cache != NULL &&
3051 ssl->session->length != 0 &&
3052 resume == 0 )
3053 {
Paul Bakker0a597072012-09-25 21:55:46 +00003054 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3055 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003056 }
Paul Bakker0a597072012-09-25 21:55:46 +00003057
Paul Bakker48916f92012-09-16 19:57:18 +00003058 ssl->state++;
3059
3060 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3061}
3062
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063int ssl_write_finished( ssl_context *ssl )
3064{
3065 int ret, hash_len;
3066
3067 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3068
Paul Bakker92be97b2013-01-02 17:30:03 +01003069 /*
3070 * Set the out_msg pointer to the correct location based on IV length
3071 */
3072 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3073 {
3074 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3075 ssl->transform_negotiate->fixed_ivlen;
3076 }
3077 else
3078 ssl->out_msg = ssl->out_iv;
3079
Paul Bakker48916f92012-09-16 19:57:18 +00003080 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081
3082 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3084
Paul Bakker48916f92012-09-16 19:57:18 +00003085 ssl->verify_data_len = hash_len;
3086 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3087
Paul Bakker5121ce52009-01-03 21:22:43 +00003088 ssl->out_msglen = 4 + hash_len;
3089 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3090 ssl->out_msg[0] = SSL_HS_FINISHED;
3091
3092 /*
3093 * In case of session resuming, invert the client and server
3094 * ChangeCipherSpec messages order.
3095 */
Paul Bakker0a597072012-09-25 21:55:46 +00003096 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 {
3098 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003099 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003100 else
3101 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3102 }
3103 else
3104 ssl->state++;
3105
Paul Bakker48916f92012-09-16 19:57:18 +00003106 /*
3107 * Switch to our negotiated transform and session parameters for outbound data.
3108 */
3109 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3110 ssl->transform_out = ssl->transform_negotiate;
3111 ssl->session_out = ssl->session_negotiate;
3112 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Paul Bakker07eb38b2012-12-19 14:42:06 +01003114#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3115 if( ssl_hw_record_activate != NULL)
3116 {
3117 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3118 {
3119 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3120 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3121 }
3122 }
3123#endif
3124
Paul Bakker5121ce52009-01-03 21:22:43 +00003125 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3126 {
3127 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3128 return( ret );
3129 }
3130
3131 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3132
3133 return( 0 );
3134}
3135
3136int ssl_parse_finished( ssl_context *ssl )
3137{
Paul Bakker23986e52011-04-24 08:57:21 +00003138 int ret;
3139 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003140 unsigned char buf[36];
3141
3142 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3143
Paul Bakker48916f92012-09-16 19:57:18 +00003144 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
Paul Bakker48916f92012-09-16 19:57:18 +00003146 /*
3147 * Switch to our negotiated transform and session parameters for inbound data.
3148 */
3149 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3150 ssl->transform_in = ssl->transform_negotiate;
3151 ssl->session_in = ssl->session_negotiate;
3152 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003153
Paul Bakker92be97b2013-01-02 17:30:03 +01003154 /*
3155 * Set the in_msg pointer to the correct location based on IV length
3156 */
3157 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3158 {
3159 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3160 ssl->transform_negotiate->fixed_ivlen;
3161 }
3162 else
3163 ssl->in_msg = ssl->in_iv;
3164
Paul Bakker07eb38b2012-12-19 14:42:06 +01003165#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3166 if( ssl_hw_record_activate != NULL)
3167 {
3168 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3169 {
3170 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3171 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3172 }
3173 }
3174#endif
3175
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3177 {
3178 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3179 return( ret );
3180 }
3181
3182 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3183 {
3184 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003185 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003186 }
3187
Paul Bakker1ef83d62012-04-11 12:09:53 +00003188 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003189 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3190
3191 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3192 ssl->in_hslen != 4 + hash_len )
3193 {
3194 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003195 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 }
3197
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 if( memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
3199 {
3200 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003201 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003202 }
3203
Paul Bakker48916f92012-09-16 19:57:18 +00003204 ssl->verify_data_len = hash_len;
3205 memcpy( ssl->peer_verify_data, buf, hash_len );
3206
Paul Bakker0a597072012-09-25 21:55:46 +00003207 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 {
3209 if( ssl->endpoint == SSL_IS_CLIENT )
3210 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3211
3212 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003213 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 }
3215 else
3216 ssl->state++;
3217
3218 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3219
3220 return( 0 );
3221}
3222
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003223static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003224{
3225 if( ssl->transform_negotiate )
3226 ssl_transform_free( ssl->transform_negotiate );
3227 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003228 {
3229 ssl->transform_negotiate =
3230 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3231 }
Paul Bakker48916f92012-09-16 19:57:18 +00003232
3233 if( ssl->session_negotiate )
3234 ssl_session_free( ssl->session_negotiate );
3235 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003236 {
3237 ssl->session_negotiate =
3238 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3239 }
Paul Bakker48916f92012-09-16 19:57:18 +00003240
3241 if( ssl->handshake )
3242 ssl_handshake_free( ssl->handshake );
3243 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003244 {
3245 ssl->handshake = (ssl_handshake_params *)
3246 polarssl_malloc( sizeof(ssl_handshake_params) );
3247 }
Paul Bakker48916f92012-09-16 19:57:18 +00003248
3249 if( ssl->handshake == NULL ||
3250 ssl->transform_negotiate == NULL ||
3251 ssl->session_negotiate == NULL )
3252 {
3253 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3254 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3255 }
3256
3257 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3258 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3259 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3260
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003261#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3262 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003263 md5_starts( &ssl->handshake->fin_md5 );
3264 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003265#endif
3266#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3267#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003268 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003269#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003270#if defined(POLARSSL_SHA512_C)
3271 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003272#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003273#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003274
3275 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003276 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003277
Paul Bakker61d113b2013-07-04 11:51:43 +02003278#if defined(POLARSSL_ECDH_C)
3279 ecdh_init( &ssl->handshake->ecdh_ctx );
3280#endif
3281
Paul Bakker48916f92012-09-16 19:57:18 +00003282 return( 0 );
3283}
3284
Paul Bakker5121ce52009-01-03 21:22:43 +00003285/*
3286 * Initialize an SSL context
3287 */
3288int ssl_init( ssl_context *ssl )
3289{
Paul Bakker48916f92012-09-16 19:57:18 +00003290 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 int len = SSL_BUFFER_LEN;
3292
3293 memset( ssl, 0, sizeof( ssl_context ) );
3294
Paul Bakker62f2dee2012-09-28 07:31:51 +00003295 /*
3296 * Sane defaults
3297 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003298 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3299 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3300 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3301 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003302
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003303 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003304
Paul Bakker62f2dee2012-09-28 07:31:51 +00003305#if defined(POLARSSL_DHM_C)
3306 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3307 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3308 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3309 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3310 {
3311 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3312 return( ret );
3313 }
3314#endif
3315
3316 /*
3317 * Prepare base structures
3318 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003319 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003320 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003321 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003322 ssl->in_msg = ssl->in_ctr + 13;
3323
3324 if( ssl->in_ctr == NULL )
3325 {
3326 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003327 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003328 }
3329
Paul Bakker6e339b52013-07-03 13:37:05 +02003330 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003332 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003333 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
3335 if( ssl->out_ctr == NULL )
3336 {
3337 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003338 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003339 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003340 }
3341
3342 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3343 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3344
Paul Bakker606b4ba2013-08-14 16:52:14 +02003345#if defined(POLARSSL_SSL_SESSION_TICKETS)
3346 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3347#endif
3348
Paul Bakker48916f92012-09-16 19:57:18 +00003349 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3350 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003351
3352 return( 0 );
3353}
3354
3355/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003356 * Reset an initialized and used SSL context for re-use while retaining
3357 * all application-set variables, function pointers and data.
3358 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003359int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003360{
Paul Bakker48916f92012-09-16 19:57:18 +00003361 int ret;
3362
Paul Bakker7eb013f2011-10-06 12:37:39 +00003363 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003364 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3365 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3366
3367 ssl->verify_data_len = 0;
3368 memset( ssl->own_verify_data, 0, 36 );
3369 memset( ssl->peer_verify_data, 0, 36 );
3370
Paul Bakker7eb013f2011-10-06 12:37:39 +00003371 ssl->in_offt = NULL;
3372
Paul Bakker92be97b2013-01-02 17:30:03 +01003373 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003374 ssl->in_msgtype = 0;
3375 ssl->in_msglen = 0;
3376 ssl->in_left = 0;
3377
3378 ssl->in_hslen = 0;
3379 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003380 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003381
Paul Bakker92be97b2013-01-02 17:30:03 +01003382 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003383 ssl->out_msgtype = 0;
3384 ssl->out_msglen = 0;
3385 ssl->out_left = 0;
3386
Paul Bakker48916f92012-09-16 19:57:18 +00003387 ssl->transform_in = NULL;
3388 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003389
3390 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3391 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003392
3393#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3394 if( ssl_hw_record_reset != NULL)
3395 {
3396 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003397 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003398 {
3399 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3400 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3401 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003402 }
3403#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003404
Paul Bakker48916f92012-09-16 19:57:18 +00003405 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003406 {
Paul Bakker48916f92012-09-16 19:57:18 +00003407 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003408 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003409 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003410 }
Paul Bakker48916f92012-09-16 19:57:18 +00003411
Paul Bakkerc0463502013-02-14 11:19:38 +01003412 if( ssl->session )
3413 {
3414 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003415 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003416 ssl->session = NULL;
3417 }
3418
Paul Bakker48916f92012-09-16 19:57:18 +00003419 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3420 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003421
3422 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003423}
3424
Paul Bakkera503a632013-08-14 13:48:06 +02003425#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003426/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003427 * Allocate and initialize ticket keys
3428 */
3429static int ssl_ticket_keys_init( ssl_context *ssl )
3430{
3431 int ret;
3432 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003433 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003434
3435 if( ssl->ticket_keys != NULL )
3436 return( 0 );
3437
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003438 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3439 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003440 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3441
3442 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
3443 return( ret );
3444
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003445 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3446 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3447 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3448 {
3449 return( ret );
3450 }
3451
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003452 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
3453 return( ret );
3454
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003455 ssl->ticket_keys = tkeys;
3456
3457 return( 0 );
3458}
Paul Bakkera503a632013-08-14 13:48:06 +02003459#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003460
3461/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 * SSL set accessors
3463 */
3464void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3465{
3466 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003467
Paul Bakker606b4ba2013-08-14 16:52:14 +02003468#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003469 if( endpoint == SSL_IS_CLIENT )
3470 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003471#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003472}
3473
3474void ssl_set_authmode( ssl_context *ssl, int authmode )
3475{
3476 ssl->authmode = authmode;
3477}
3478
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003479#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003480void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003481 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003482 void *p_vrfy )
3483{
3484 ssl->f_vrfy = f_vrfy;
3485 ssl->p_vrfy = p_vrfy;
3486}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003487#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003488
Paul Bakker5121ce52009-01-03 21:22:43 +00003489void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003490 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003491 void *p_rng )
3492{
3493 ssl->f_rng = f_rng;
3494 ssl->p_rng = p_rng;
3495}
3496
3497void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003498 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003499 void *p_dbg )
3500{
3501 ssl->f_dbg = f_dbg;
3502 ssl->p_dbg = p_dbg;
3503}
3504
3505void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003506 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003507 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003508{
3509 ssl->f_recv = f_recv;
3510 ssl->f_send = f_send;
3511 ssl->p_recv = p_recv;
3512 ssl->p_send = p_send;
3513}
3514
Paul Bakker0a597072012-09-25 21:55:46 +00003515void ssl_set_session_cache( ssl_context *ssl,
3516 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3517 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003518{
Paul Bakker0a597072012-09-25 21:55:46 +00003519 ssl->f_get_cache = f_get_cache;
3520 ssl->p_get_cache = p_get_cache;
3521 ssl->f_set_cache = f_set_cache;
3522 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003523}
3524
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003525int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003526{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003527 int ret;
3528
3529 if( ssl == NULL ||
3530 session == NULL ||
3531 ssl->session_negotiate == NULL ||
3532 ssl->endpoint != SSL_IS_CLIENT )
3533 {
3534 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3535 }
3536
3537 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3538 return( ret );
3539
Paul Bakker0a597072012-09-25 21:55:46 +00003540 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003541
3542 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003543}
3544
Paul Bakkerb68cad62012-08-23 08:34:18 +00003545void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003546{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003547 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3548 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3549 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3550 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3551}
3552
3553void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3554 int major, int minor )
3555{
3556 if( major != SSL_MAJOR_VERSION_3 )
3557 return;
3558
3559 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3560 return;
3561
3562 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003563}
3564
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003565#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003566/* Add a new (empty) key_cert entry an return a pointer to it */
3567static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3568{
3569 ssl_key_cert *key_cert, *last;
3570
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003571 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3572 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003573 return( NULL );
3574
3575 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3576
3577 /* Append the new key_cert to the (possibly empty) current list */
3578 if( ssl->key_cert == NULL )
3579 ssl->key_cert = key_cert;
3580 else
3581 {
3582 last = ssl->key_cert;
3583 while( last->next != NULL )
3584 last = last->next;
3585 last->next = key_cert;
3586 }
3587
3588 return key_cert;
3589}
3590
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003591void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003592 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003593{
3594 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003595 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 ssl->peer_cn = peer_cn;
3597}
3598
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003599int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003600 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003601{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003602 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3603
3604 if( key_cert == NULL )
3605 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3606
3607 key_cert->cert = own_cert;
3608 key_cert->key = pk_key;
3609
3610 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003611}
3612
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003613#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003614int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003615 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003616{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003617 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003618 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003619
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003620 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003621 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3622
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003623 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3624 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003625 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003626
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003627 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003628
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003629 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003630 if( ret != 0 )
3631 return( ret );
3632
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003633 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003634 return( ret );
3635
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003636 key_cert->cert = own_cert;
3637 key_cert->key_own_alloc = 1;
3638
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003639 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003640}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003641#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003642
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003643int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003644 void *rsa_key,
3645 rsa_decrypt_func rsa_decrypt,
3646 rsa_sign_func rsa_sign,
3647 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003648{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003649 int ret;
3650 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003651
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003652 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003653 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3654
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003655 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3656 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003657 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é-Gonnard1b62c7f2013-10-14 14:02:19 +02003672#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
3673 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
3674 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003675int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3676 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003677{
Paul Bakker6db455e2013-09-18 17:29:31 +02003678 if( psk == NULL || psk_identity == NULL )
3679 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3680
3681 if( ssl->psk != NULL )
3682 {
3683 polarssl_free( ssl->psk );
3684 polarssl_free( ssl->psk_identity );
3685 }
3686
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003687 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003688 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003689
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003690 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3691 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003692
3693 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3694 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3695
3696 memcpy( ssl->psk, psk, ssl->psk_len );
3697 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003698
3699 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003700}
3701
3702void ssl_set_psk_cb( ssl_context *ssl,
3703 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3704 size_t),
3705 void *p_psk )
3706{
3707 ssl->f_psk = f_psk;
3708 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003709}
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003710#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
3711 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
3712 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003713
Paul Bakker48916f92012-09-16 19:57:18 +00003714#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003715int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003716{
3717 int ret;
3718
Paul Bakker48916f92012-09-16 19:57:18 +00003719 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003720 {
3721 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3722 return( ret );
3723 }
3724
Paul Bakker48916f92012-09-16 19:57:18 +00003725 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003726 {
3727 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3728 return( ret );
3729 }
3730
3731 return( 0 );
3732}
3733
Paul Bakker1b57b062011-01-06 15:48:19 +00003734int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3735{
3736 int ret;
3737
Paul Bakker48916f92012-09-16 19:57:18 +00003738 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003739 {
3740 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3741 return( ret );
3742 }
3743
Paul Bakker48916f92012-09-16 19:57:18 +00003744 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003745 {
3746 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3747 return( ret );
3748 }
3749
3750 return( 0 );
3751}
Paul Bakker48916f92012-09-16 19:57:18 +00003752#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003753
Paul Bakker0be444a2013-08-27 21:55:01 +02003754#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003755int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003756{
3757 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003758 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003759
3760 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003761
3762 if( ssl->hostname_len + 1 == 0 )
3763 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3764
Paul Bakker6e339b52013-07-03 13:37:05 +02003765 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003766
Paul Bakkerb15b8512012-01-13 13:44:06 +00003767 if( ssl->hostname == NULL )
3768 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3769
Paul Bakker3c2122f2013-06-24 19:03:14 +02003770 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003771 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003772
Paul Bakker40ea7de2009-05-03 10:18:48 +00003773 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003774
3775 return( 0 );
3776}
3777
Paul Bakker5701cdc2012-09-27 21:49:42 +00003778void ssl_set_sni( ssl_context *ssl,
3779 int (*f_sni)(void *, ssl_context *,
3780 const unsigned char *, size_t),
3781 void *p_sni )
3782{
3783 ssl->f_sni = f_sni;
3784 ssl->p_sni = p_sni;
3785}
Paul Bakker0be444a2013-08-27 21:55:01 +02003786#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003787
Paul Bakker490ecc82011-10-06 13:04:09 +00003788void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3789{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003790 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3791 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3792 {
3793 ssl->max_major_ver = major;
3794 ssl->max_minor_ver = minor;
3795 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003796}
3797
Paul Bakker1d29fb52012-09-28 13:28:45 +00003798void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3799{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003800 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3801 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3802 {
3803 ssl->min_major_ver = major;
3804 ssl->min_minor_ver = minor;
3805 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003806}
3807
Paul Bakker05decb22013-08-15 13:33:48 +02003808#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003809int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3810{
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003811 if( mfl_code >= sizeof( mfl_code_to_length ) ||
3812 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003813 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003814 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003815 }
3816
3817 ssl->mfl_code = mfl_code;
3818
3819 return( 0 );
3820}
Paul Bakker05decb22013-08-15 13:33:48 +02003821#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003822
Paul Bakker1f2bc622013-08-15 13:45:55 +02003823#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003824int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003825{
3826 if( ssl->endpoint != SSL_IS_CLIENT )
3827 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3828
Paul Bakker8c1ede62013-07-19 14:14:37 +02003829 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003830
3831 return( 0 );
3832}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003833#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003834
Paul Bakker48916f92012-09-16 19:57:18 +00003835void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3836{
3837 ssl->disable_renegotiation = renegotiation;
3838}
3839
3840void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3841{
3842 ssl->allow_legacy_renegotiation = allow_legacy;
3843}
3844
Paul Bakkera503a632013-08-14 13:48:06 +02003845#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003846int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3847{
3848 ssl->session_tickets = use_tickets;
3849
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003850 if( ssl->endpoint == SSL_IS_CLIENT )
3851 return( 0 );
3852
3853 if( ssl->f_rng == NULL )
3854 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3855
3856 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003857}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003858
3859void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3860{
3861 ssl->ticket_lifetime = lifetime;
3862}
Paul Bakkera503a632013-08-14 13:48:06 +02003863#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003864
Paul Bakker5121ce52009-01-03 21:22:43 +00003865/*
3866 * SSL get accessors
3867 */
Paul Bakker23986e52011-04-24 08:57:21 +00003868size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003869{
3870 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3871}
3872
Paul Bakkerff60ee62010-03-16 21:09:09 +00003873int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003874{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003875 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003876}
3877
Paul Bakkere3166ce2011-01-27 17:40:50 +00003878const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003879{
Paul Bakker926c8e42013-03-06 10:23:34 +01003880 if( ssl == NULL || ssl->session == NULL )
3881 return NULL;
3882
Paul Bakkere3166ce2011-01-27 17:40:50 +00003883 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003884}
3885
Paul Bakker43ca69c2011-01-15 17:35:19 +00003886const char *ssl_get_version( const ssl_context *ssl )
3887{
3888 switch( ssl->minor_ver )
3889 {
3890 case SSL_MINOR_VERSION_0:
3891 return( "SSLv3.0" );
3892
3893 case SSL_MINOR_VERSION_1:
3894 return( "TLSv1.0" );
3895
3896 case SSL_MINOR_VERSION_2:
3897 return( "TLSv1.1" );
3898
Paul Bakker1ef83d62012-04-11 12:09:53 +00003899 case SSL_MINOR_VERSION_3:
3900 return( "TLSv1.2" );
3901
Paul Bakker43ca69c2011-01-15 17:35:19 +00003902 default:
3903 break;
3904 }
3905 return( "unknown" );
3906}
3907
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003908#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003909const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003910{
3911 if( ssl == NULL || ssl->session == NULL )
3912 return NULL;
3913
3914 return ssl->session->peer_cert;
3915}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003916#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003917
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003918int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3919{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003920 if( ssl == NULL ||
3921 dst == NULL ||
3922 ssl->session == NULL ||
3923 ssl->endpoint != SSL_IS_CLIENT )
3924 {
3925 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3926 }
3927
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003928 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003929}
3930
Paul Bakker5121ce52009-01-03 21:22:43 +00003931/*
Paul Bakker1961b702013-01-25 14:49:24 +01003932 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003933 */
Paul Bakker1961b702013-01-25 14:49:24 +01003934int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003935{
Paul Bakker40e46942009-01-03 21:51:57 +00003936 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003937
Paul Bakker40e46942009-01-03 21:51:57 +00003938#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003939 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01003940 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003941#endif
3942
Paul Bakker40e46942009-01-03 21:51:57 +00003943#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003944 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003945 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003946#endif
3947
Paul Bakker1961b702013-01-25 14:49:24 +01003948 return( ret );
3949}
3950
3951/*
3952 * Perform the SSL handshake
3953 */
3954int ssl_handshake( ssl_context *ssl )
3955{
3956 int ret = 0;
3957
3958 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
3959
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02003960#if defined(POLARSSL_X509_CRT_PARSE_C)
3961 ssl->handshake->key_cert = ssl->key_cert;
3962#endif
3963
Paul Bakker1961b702013-01-25 14:49:24 +01003964 while( ssl->state != SSL_HANDSHAKE_OVER )
3965 {
3966 ret = ssl_handshake_step( ssl );
3967
3968 if( ret != 0 )
3969 break;
3970 }
3971
Paul Bakker5121ce52009-01-03 21:22:43 +00003972 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
3973
3974 return( ret );
3975}
3976
3977/*
Paul Bakker48916f92012-09-16 19:57:18 +00003978 * Renegotiate current connection
3979 */
3980int ssl_renegotiate( ssl_context *ssl )
3981{
3982 int ret;
3983
3984 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
3985
3986 if( ssl->state != SSL_HANDSHAKE_OVER )
3987 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3988
3989 ssl->state = SSL_HELLO_REQUEST;
3990 ssl->renegotiation = SSL_RENEGOTIATION;
3991
3992 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3993 return( ret );
3994
3995 if( ( ret = ssl_handshake( ssl ) ) != 0 )
3996 {
3997 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
3998 return( ret );
3999 }
4000
4001 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4002
4003 return( 0 );
4004}
4005
4006/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004007 * Receive application data decrypted from the SSL layer
4008 */
Paul Bakker23986e52011-04-24 08:57:21 +00004009int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004010{
Paul Bakker23986e52011-04-24 08:57:21 +00004011 int ret;
4012 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004013
4014 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4015
4016 if( ssl->state != SSL_HANDSHAKE_OVER )
4017 {
4018 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4019 {
4020 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4021 return( ret );
4022 }
4023 }
4024
4025 if( ssl->in_offt == NULL )
4026 {
4027 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4028 {
Paul Bakker831a7552011-05-18 13:32:51 +00004029 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4030 return( 0 );
4031
Paul Bakker5121ce52009-01-03 21:22:43 +00004032 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4033 return( ret );
4034 }
4035
4036 if( ssl->in_msglen == 0 &&
4037 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4038 {
4039 /*
4040 * OpenSSL sends empty messages to randomize the IV
4041 */
4042 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4043 {
Paul Bakker831a7552011-05-18 13:32:51 +00004044 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4045 return( 0 );
4046
Paul Bakker5121ce52009-01-03 21:22:43 +00004047 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4048 return( ret );
4049 }
4050 }
4051
Paul Bakker48916f92012-09-16 19:57:18 +00004052 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4053 {
4054 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4055
4056 if( ssl->endpoint == SSL_IS_CLIENT &&
4057 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4058 ssl->in_hslen != 4 ) )
4059 {
4060 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4061 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4062 }
4063
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004064 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4065 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
4066 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004067 {
4068 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4069
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004070#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004071 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004072 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004073 /*
4074 * SSLv3 does not have a "no_renegotiation" alert
4075 */
4076 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4077 return( ret );
4078 }
4079 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004080#endif
4081#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4082 defined(POLARSSL_SSL_PROTO_TLS1_2)
4083 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004084 {
4085 if( ( ret = ssl_send_alert_message( ssl,
4086 SSL_ALERT_LEVEL_WARNING,
4087 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4088 {
4089 return( ret );
4090 }
Paul Bakker48916f92012-09-16 19:57:18 +00004091 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004092 else
4093#endif
Paul Bakker577e0062013-08-28 11:57:20 +02004094 {
4095 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004096 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02004097 }
Paul Bakker48916f92012-09-16 19:57:18 +00004098 }
4099 else
4100 {
4101 if( ( ret = ssl_renegotiate( ssl ) ) != 0 )
4102 {
4103 SSL_DEBUG_RET( 1, "ssl_renegotiate", ret );
4104 return( ret );
4105 }
4106
4107 return( POLARSSL_ERR_NET_WANT_READ );
4108 }
4109 }
4110 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004111 {
4112 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004113 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 }
4115
4116 ssl->in_offt = ssl->in_msg;
4117 }
4118
4119 n = ( len < ssl->in_msglen )
4120 ? len : ssl->in_msglen;
4121
4122 memcpy( buf, ssl->in_offt, n );
4123 ssl->in_msglen -= n;
4124
4125 if( ssl->in_msglen == 0 )
4126 /* all bytes consumed */
4127 ssl->in_offt = NULL;
4128 else
4129 /* more data available */
4130 ssl->in_offt += n;
4131
4132 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4133
Paul Bakker23986e52011-04-24 08:57:21 +00004134 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004135}
4136
4137/*
4138 * Send application data to be encrypted by the SSL layer
4139 */
Paul Bakker23986e52011-04-24 08:57:21 +00004140int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004141{
Paul Bakker23986e52011-04-24 08:57:21 +00004142 int ret;
4143 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004144 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004145
4146 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4147
4148 if( ssl->state != SSL_HANDSHAKE_OVER )
4149 {
4150 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4151 {
4152 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4153 return( ret );
4154 }
4155 }
4156
Paul Bakker05decb22013-08-15 13:33:48 +02004157#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004158 /*
4159 * Assume mfl_code is correct since it was checked when set
4160 */
4161 max_len = mfl_code_to_length[ssl->mfl_code];
4162
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004163 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004164 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004165 */
4166 if( ssl->session_out != NULL &&
4167 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4168 {
4169 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4170 }
Paul Bakker05decb22013-08-15 13:33:48 +02004171#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004172
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004173 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004174
Paul Bakker5121ce52009-01-03 21:22:43 +00004175 if( ssl->out_left != 0 )
4176 {
4177 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4178 {
4179 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4180 return( ret );
4181 }
4182 }
Paul Bakker887bd502011-06-08 13:10:54 +00004183 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004184 {
Paul Bakker887bd502011-06-08 13:10:54 +00004185 ssl->out_msglen = n;
4186 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4187 memcpy( ssl->out_msg, buf, n );
4188
4189 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4190 {
4191 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4192 return( ret );
4193 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004194 }
4195
4196 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4197
Paul Bakker23986e52011-04-24 08:57:21 +00004198 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004199}
4200
4201/*
4202 * Notify the peer that the connection is being closed
4203 */
4204int ssl_close_notify( ssl_context *ssl )
4205{
4206 int ret;
4207
4208 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4209
4210 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4211 {
4212 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4213 return( ret );
4214 }
4215
4216 if( ssl->state == SSL_HANDSHAKE_OVER )
4217 {
Paul Bakker48916f92012-09-16 19:57:18 +00004218 if( ( ret = ssl_send_alert_message( ssl,
4219 SSL_ALERT_LEVEL_WARNING,
4220 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004221 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004222 return( ret );
4223 }
4224 }
4225
4226 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4227
4228 return( ret );
4229}
4230
Paul Bakker48916f92012-09-16 19:57:18 +00004231void ssl_transform_free( ssl_transform *transform )
4232{
4233#if defined(POLARSSL_ZLIB_SUPPORT)
4234 deflateEnd( &transform->ctx_deflate );
4235 inflateEnd( &transform->ctx_inflate );
4236#endif
4237
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004238 cipher_free_ctx( &transform->cipher_ctx_enc );
4239 cipher_free_ctx( &transform->cipher_ctx_dec );
4240
Paul Bakker61d113b2013-07-04 11:51:43 +02004241 md_free_ctx( &transform->md_ctx_enc );
4242 md_free_ctx( &transform->md_ctx_dec );
4243
Paul Bakker48916f92012-09-16 19:57:18 +00004244 memset( transform, 0, sizeof( ssl_transform ) );
4245}
4246
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004247#if defined(POLARSSL_X509_CRT_PARSE_C)
4248static void ssl_key_cert_free( ssl_key_cert *key_cert )
4249{
4250 ssl_key_cert *cur = key_cert, *next;
4251
4252 while( cur != NULL )
4253 {
4254 next = cur->next;
4255
4256 if( cur->key_own_alloc )
4257 {
4258 pk_free( cur->key );
4259 polarssl_free( cur->key );
4260 }
4261 polarssl_free( cur );
4262
4263 cur = next;
4264 }
4265}
4266#endif /* POLARSSL_X509_CRT_PARSE_C */
4267
Paul Bakker48916f92012-09-16 19:57:18 +00004268void ssl_handshake_free( ssl_handshake_params *handshake )
4269{
4270#if defined(POLARSSL_DHM_C)
4271 dhm_free( &handshake->dhm_ctx );
4272#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004273#if defined(POLARSSL_ECDH_C)
4274 ecdh_free( &handshake->ecdh_ctx );
4275#endif
4276
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004277#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004278 /* explicit void pointer cast for buggy MS compiler */
4279 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004280#endif
4281
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004282#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4283 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4284 /*
4285 * Free only the linked list wrapper, not the keys themselves
4286 * since the belong to the SNI callback
4287 */
4288 if( handshake->sni_key_cert != NULL )
4289 {
4290 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4291
4292 while( cur != NULL )
4293 {
4294 next = cur->next;
4295 polarssl_free( cur );
4296 cur = next;
4297 }
4298 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004299#endif
4300
Paul Bakker48916f92012-09-16 19:57:18 +00004301 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4302}
4303
4304void ssl_session_free( ssl_session *session )
4305{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004306#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004307 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004308 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004309 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004310 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004311 }
Paul Bakkered27a042013-04-18 22:46:23 +02004312#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004313
Paul Bakkera503a632013-08-14 13:48:06 +02004314#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004315 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004316#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004317
Paul Bakker0a597072012-09-25 21:55:46 +00004318 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004319}
4320
Paul Bakker5121ce52009-01-03 21:22:43 +00004321/*
4322 * Free an SSL context
4323 */
4324void ssl_free( ssl_context *ssl )
4325{
4326 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4327
Paul Bakker5121ce52009-01-03 21:22:43 +00004328 if( ssl->out_ctr != NULL )
4329 {
4330 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004331 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004332 }
4333
4334 if( ssl->in_ctr != NULL )
4335 {
4336 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004337 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004338 }
4339
Paul Bakker16770332013-10-11 09:59:44 +02004340#if defined(POLARSSL_ZLIB_SUPPORT)
4341 if( ssl->compress_buf != NULL )
4342 {
4343 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4344 polarssl_free( ssl->compress_buf );
4345 }
4346#endif
4347
Paul Bakker40e46942009-01-03 21:51:57 +00004348#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004349 mpi_free( &ssl->dhm_P );
4350 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004351#endif
4352
Paul Bakker48916f92012-09-16 19:57:18 +00004353 if( ssl->transform )
4354 {
4355 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004356 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004357 }
4358
4359 if( ssl->handshake )
4360 {
4361 ssl_handshake_free( ssl->handshake );
4362 ssl_transform_free( ssl->transform_negotiate );
4363 ssl_session_free( ssl->session_negotiate );
4364
Paul Bakker6e339b52013-07-03 13:37:05 +02004365 polarssl_free( ssl->handshake );
4366 polarssl_free( ssl->transform_negotiate );
4367 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004368 }
4369
Paul Bakkerc0463502013-02-14 11:19:38 +01004370 if( ssl->session )
4371 {
4372 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004373 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004374 }
4375
Paul Bakkera503a632013-08-14 13:48:06 +02004376#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004377 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004378#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004379
Paul Bakker0be444a2013-08-27 21:55:01 +02004380#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004381 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004382 {
4383 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004384 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004385 ssl->hostname_len = 0;
4386 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004387#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004388
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02004389#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
4390 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
4391 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004392 if( ssl->psk != NULL )
4393 {
4394 memset( ssl->psk, 0, ssl->psk_len );
4395 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4396 polarssl_free( ssl->psk );
4397 polarssl_free( ssl->psk_identity );
4398 ssl->psk_len = 0;
4399 ssl->psk_identity_len = 0;
4400 }
4401#endif
4402
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004403#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004404 ssl_key_cert_free( ssl->key_cert );
4405#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004406
Paul Bakker05ef8352012-05-08 09:17:57 +00004407#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4408 if( ssl_hw_record_finish != NULL )
4409 {
4410 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4411 ssl_hw_record_finish( ssl );
4412 }
4413#endif
4414
Paul Bakker5121ce52009-01-03 21:22:43 +00004415 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004416
Paul Bakker86f04f42013-02-14 11:20:09 +01004417 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004418 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004419}
4420
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004421#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004422/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004423 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004424 */
4425unsigned char ssl_sig_from_pk( pk_context *pk )
4426{
4427#if defined(POLARSSL_RSA_C)
4428 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4429 return( SSL_SIG_RSA );
4430#endif
4431#if defined(POLARSSL_ECDSA_C)
4432 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4433 return( SSL_SIG_ECDSA );
4434#endif
4435 return( SSL_SIG_ANON );
4436}
4437
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004438pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4439{
4440 switch( sig )
4441 {
4442#if defined(POLARSSL_RSA_C)
4443 case SSL_SIG_RSA:
4444 return( POLARSSL_PK_RSA );
4445#endif
4446#if defined(POLARSSL_ECDSA_C)
4447 case SSL_SIG_ECDSA:
4448 return( POLARSSL_PK_ECDSA );
4449#endif
4450 default:
4451 return( POLARSSL_PK_NONE );
4452 }
4453}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004454#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004455
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004456/*
4457 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4458 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004459md_type_t ssl_md_alg_from_hash( unsigned char hash )
4460{
4461 switch( hash )
4462 {
4463#if defined(POLARSSL_MD5_C)
4464 case SSL_HASH_MD5:
4465 return( POLARSSL_MD_MD5 );
4466#endif
4467#if defined(POLARSSL_SHA1_C)
4468 case SSL_HASH_SHA1:
4469 return( POLARSSL_MD_SHA1 );
4470#endif
4471#if defined(POLARSSL_SHA256_C)
4472 case SSL_HASH_SHA224:
4473 return( POLARSSL_MD_SHA224 );
4474 case SSL_HASH_SHA256:
4475 return( POLARSSL_MD_SHA256 );
4476#endif
4477#if defined(POLARSSL_SHA512_C)
4478 case SSL_HASH_SHA384:
4479 return( POLARSSL_MD_SHA384 );
4480 case SSL_HASH_SHA512:
4481 return( POLARSSL_MD_SHA512 );
4482#endif
4483 default:
4484 return( POLARSSL_MD_NONE );
4485 }
4486}
4487
Paul Bakker5121ce52009-01-03 21:22:43 +00004488#endif