blob: 065693ac63ef6adffd9ac6d5d287238f6731e95b [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
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200842#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000843/*
844 * SSLv3.0 MAC functions
845 */
Paul Bakker68884e32013-01-07 18:20:04 +0100846static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
847 unsigned char *buf, size_t len,
848 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000849{
850 unsigned char header[11];
851 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100852 int padlen = 0;
853 int md_size = md_get_size( md_ctx->md_info );
854 int md_type = md_get_type( md_ctx->md_info );
855
856 if( md_type == POLARSSL_MD_MD5 )
857 padlen = 48;
858 else if( md_type == POLARSSL_MD_SHA1 )
859 padlen = 40;
860 else if( md_type == POLARSSL_MD_SHA256 )
861 padlen = 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000862
863 memcpy( header, ctr, 8 );
864 header[ 8] = (unsigned char) type;
865 header[ 9] = (unsigned char)( len >> 8 );
866 header[10] = (unsigned char)( len );
867
Paul Bakker68884e32013-01-07 18:20:04 +0100868 memset( padding, 0x36, padlen );
869 md_starts( md_ctx );
870 md_update( md_ctx, secret, md_size );
871 md_update( md_ctx, padding, padlen );
872 md_update( md_ctx, header, 11 );
873 md_update( md_ctx, buf, len );
874 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000875
Paul Bakker68884e32013-01-07 18:20:04 +0100876 memset( padding, 0x5C, padlen );
877 md_starts( md_ctx );
878 md_update( md_ctx, secret, md_size );
879 md_update( md_ctx, padding, padlen );
880 md_update( md_ctx, buf + len, md_size );
881 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000882}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200883#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000884
Paul Bakker5121ce52009-01-03 21:22:43 +0000885/*
886 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200887 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000888static int ssl_encrypt_buf( ssl_context *ssl )
889{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200890 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
892 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
893
894 /*
895 * Add MAC then encrypt
896 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200897#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000898 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
899 {
Paul Bakker68884e32013-01-07 18:20:04 +0100900 ssl_mac( &ssl->transform_out->md_ctx_enc,
901 ssl->transform_out->mac_enc,
902 ssl->out_msg, ssl->out_msglen,
903 ssl->out_ctr, ssl->out_msgtype );
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 }
905 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200906#endif
907#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
908 defined(POLARSSL_SSL_PROTO_TLS1_2)
909 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 {
Paul Bakker68884e32013-01-07 18:20:04 +0100911 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
912 md_hmac_update( &ssl->transform_out->md_ctx_enc,
913 ssl->out_msg, ssl->out_msglen );
914 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
915 ssl->out_msg + ssl->out_msglen );
916 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200918 else
919#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200920 {
921 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200922 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200923 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000924
925 SSL_DEBUG_BUF( 4, "computed mac",
Paul Bakker48916f92012-09-16 19:57:18 +0000926 ssl->out_msg + ssl->out_msglen, ssl->transform_out->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Paul Bakker48916f92012-09-16 19:57:18 +0000928 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Paul Bakker68884e32013-01-07 18:20:04 +0100930#if defined(POLARSSL_CIPHER_NULL_CIPHER)
931 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
932 {
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200933 ; /* Nothing to do */
Paul Bakker68884e32013-01-07 18:20:04 +0100934 }
935 else
936#endif /* POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200937#if defined(POLARSSL_ARC4_C)
Paul Bakker68884e32013-01-07 18:20:04 +0100938 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200940 int ret;
941 size_t olen = 0;
942
Paul Bakker5121ce52009-01-03 21:22:43 +0000943 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
944 "including %d bytes of padding",
945 ssl->out_msglen, 0 ) );
946
947 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
948 ssl->out_msg, ssl->out_msglen );
949
Paul Bakker45125bc2013-09-04 16:47:11 +0200950 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200951 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200952 SSL_DEBUG_RET( 1, "cipher_reset", ret );
953 return( ret );
954 }
955
956 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
957 ssl->transform_out->iv_enc,
958 ssl->transform_out->ivlen ) ) != 0 )
959 {
960 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200961 return( ret );
962 }
963
964 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
965 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
966 &olen ) ) != 0 )
967 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200968 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200969 return( ret );
970 }
971
972 if( ssl->out_msglen != olen )
973 {
974 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
975 ssl->out_msglen, olen ) );
976 // TODO Real error number
977 return( -1 );
978 }
979
980 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +0200981 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200982 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200983 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200984 return( ret );
985 }
986
987 if( 0 != olen )
988 {
989 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
990 0, olen ) );
991 // TODO Real error number
992 return( -1 );
993 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 }
Paul Bakker68884e32013-01-07 18:20:04 +0100995 else
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200996#endif /* POLARSSL_ARC4_C */
Paul Bakker68884e32013-01-07 18:20:04 +0100997#if defined(POLARSSL_GCM_C)
998 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
999 ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001000 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001001 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001002 unsigned char *enc_msg;
1003 unsigned char add_data[13];
1004 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1005
Paul Bakkerca4ab492012-04-18 14:23:57 +00001006 enc_msglen = ssl->out_msglen;
1007
1008 memcpy( add_data, ssl->out_ctr, 8 );
1009 add_data[8] = ssl->out_msgtype;
1010 add_data[9] = ssl->major_ver;
1011 add_data[10] = ssl->minor_ver;
1012 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1013 add_data[12] = ssl->out_msglen & 0xFF;
1014
1015 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1016 add_data, 13 );
1017
Paul Bakker68884e32013-01-07 18:20:04 +01001018 /*
1019 * Generate IV
1020 */
1021 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001022 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001023 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1024 if( ret != 0 )
1025 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001026
Paul Bakker68884e32013-01-07 18:20:04 +01001027 memcpy( ssl->out_iv,
1028 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1029 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001030
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001031 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1032 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1033
Paul Bakker68884e32013-01-07 18:20:04 +01001034 /*
1035 * Fix pointer positions and message length with added IV
1036 */
1037 enc_msg = ssl->out_msg;
1038 enc_msglen = ssl->out_msglen;
1039 ssl->out_msglen += ssl->transform_out->ivlen -
1040 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001041
Paul Bakker68884e32013-01-07 18:20:04 +01001042 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1043 "including %d bytes of padding",
1044 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001045
Paul Bakker68884e32013-01-07 18:20:04 +01001046 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1047 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001048
Paul Bakker68884e32013-01-07 18:20:04 +01001049 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001050 * Encrypt
1051 */
1052 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1053 ssl->transform_out->iv_enc,
1054 ssl->transform_out->ivlen ) ) != 0 ||
1055 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1056 {
1057 return( ret );
1058 }
1059
1060 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1061 add_data, 13 ) ) != 0 )
1062 {
1063 return( ret );
1064 }
1065
1066 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1067 enc_msg, enc_msglen,
1068 enc_msg, &olen ) ) != 0 )
1069 {
1070 return( ret );
1071 }
1072 totlen = olen;
1073
1074 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1075 enc_msg + olen, &olen ) ) != 0 )
1076 {
1077 return( ret );
1078 }
1079 totlen += olen;
1080
1081 if( totlen != enc_msglen )
1082 {
1083 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1084 return( -1 );
1085 }
1086
1087 /*
1088 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001089 */
1090 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001091
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001092 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1093 enc_msg + enc_msglen, 16 ) ) != 0 )
1094 {
1095 return( ret );
1096 }
Paul Bakker68884e32013-01-07 18:20:04 +01001097
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001098 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001099 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 else
Paul Bakker68884e32013-01-07 18:20:04 +01001101#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001102#if defined(POLARSSL_CIPHER_MODE_CBC)
1103 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1104 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001105 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001106 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001107 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001108 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001109
Paul Bakker48916f92012-09-16 19:57:18 +00001110 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1111 ssl->transform_out->ivlen;
1112 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001113 padlen = 0;
1114
1115 for( i = 0; i <= padlen; i++ )
1116 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1117
1118 ssl->out_msglen += padlen + 1;
1119
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001120 enc_msglen = ssl->out_msglen;
1121 enc_msg = ssl->out_msg;
1122
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001123#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001124 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001125 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1126 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001127 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001128 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001129 {
1130 /*
1131 * Generate IV
1132 */
Paul Bakker48916f92012-09-16 19:57:18 +00001133 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1134 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001135 if( ret != 0 )
1136 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001137
Paul Bakker92be97b2013-01-02 17:30:03 +01001138 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001139 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001140
1141 /*
1142 * Fix pointer positions and message length with added IV
1143 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001144 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001145 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001146 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001147 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001148#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001149
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001151 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001152 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
1154 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001155 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Paul Bakker45125bc2013-09-04 16:47:11 +02001157 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001159 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1160 return( ret );
1161 }
1162
1163 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1164 ssl->transform_out->iv_enc,
1165 ssl->transform_out->ivlen ) ) != 0 )
1166 {
1167 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001168 return( ret );
1169 }
Paul Bakker68884e32013-01-07 18:20:04 +01001170
Paul Bakkercca5b812013-08-31 17:40:26 +02001171 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1172 enc_msg, enc_msglen, enc_msg,
1173 &olen ) ) != 0 )
1174 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001175 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001176 return( ret );
1177 }
Paul Bakker68884e32013-01-07 18:20:04 +01001178
Paul Bakkercca5b812013-08-31 17:40:26 +02001179 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001180
Paul Bakkercca5b812013-08-31 17:40:26 +02001181 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001182 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001183 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001184 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001185 return( ret );
1186 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001187
Paul Bakkercca5b812013-08-31 17:40:26 +02001188 if( enc_msglen != olen )
1189 {
1190 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1191 enc_msglen, olen ) );
1192 // TODO Real error number
1193 return( -1 );
1194 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001195
1196#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001197 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1198 {
1199 /*
1200 * Save IV in SSL3 and TLS1
1201 */
1202 memcpy( ssl->transform_out->iv_enc,
1203 ssl->transform_out->cipher_ctx_enc.iv,
1204 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001206#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001208 else
1209#endif /* POLARSSL_CIPHER_MODE_CBC */
1210 {
1211 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1212 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
Paul Bakkerca4ab492012-04-18 14:23:57 +00001215 for( i = 8; i > 0; i-- )
1216 if( ++ssl->out_ctr[i - 1] != 0 )
1217 break;
1218
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1220
1221 return( 0 );
1222}
1223
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001224#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001225
Paul Bakker5121ce52009-01-03 21:22:43 +00001226static int ssl_decrypt_buf( ssl_context *ssl )
1227{
Paul Bakker45829992013-01-03 14:52:21 +01001228 size_t i, padlen = 0, correct = 1;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001229 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
1231 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1232
Paul Bakker48916f92012-09-16 19:57:18 +00001233 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 {
1235 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001236 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001237 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 }
1239
Paul Bakker68884e32013-01-07 18:20:04 +01001240#if defined(POLARSSL_CIPHER_NULL_CIPHER)
1241 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 {
Paul Bakker68884e32013-01-07 18:20:04 +01001243 padlen = 0;
1244 }
1245 else
1246#endif /* POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker40e46942009-01-03 21:51:57 +00001247#if defined(POLARSSL_ARC4_C)
Paul Bakker68884e32013-01-07 18:20:04 +01001248 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
1249 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001250 int ret;
1251 size_t olen = 0;
1252
Paul Bakker68884e32013-01-07 18:20:04 +01001253 padlen = 0;
1254
Paul Bakker45125bc2013-09-04 16:47:11 +02001255 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001256 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001257 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1258 return( ret );
1259 }
1260
1261 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1262 ssl->transform_in->iv_dec,
1263 ssl->transform_in->ivlen ) ) != 0 )
1264 {
1265 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001266 return( ret );
1267 }
1268
1269 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1270 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1271 &olen ) ) != 0 )
1272 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001273 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001274 return( ret );
1275 }
1276
1277 if( ssl->in_msglen != olen )
1278 {
1279 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1280 // TODO Real error number
1281 return( -1 );
1282 }
1283
1284 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001285 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001286 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001287 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001288 return( ret );
1289 }
1290
1291 if( 0 != olen )
1292 {
1293 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1294 // TODO Real error number
1295 return( -1 );
1296 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 }
Paul Bakker68884e32013-01-07 18:20:04 +01001298 else
1299#endif /* POLARSSL_ARC4_C */
1300#if defined(POLARSSL_GCM_C)
1301 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
1302 ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001303 {
1304 unsigned char *dec_msg;
1305 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001306 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001307 unsigned char add_data[13];
1308 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1309
Paul Bakker68884e32013-01-07 18:20:04 +01001310 padlen = 0;
1311
1312 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1313 ssl->transform_in->fixed_ivlen );
1314 dec_msglen -= 16;
1315 dec_msg = ssl->in_msg;
1316 dec_msg_result = ssl->in_msg;
1317 ssl->in_msglen = dec_msglen;
1318
1319 memcpy( add_data, ssl->in_ctr, 8 );
1320 add_data[8] = ssl->in_msgtype;
1321 add_data[9] = ssl->major_ver;
1322 add_data[10] = ssl->minor_ver;
1323 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1324 add_data[12] = ssl->in_msglen & 0xFF;
1325
1326 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1327 add_data, 13 );
1328
1329 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1330 ssl->in_iv,
1331 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1332
1333 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1334 ssl->transform_in->ivlen );
1335 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1336
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001337 /*
1338 * Decrypt
1339 */
1340 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1341 ssl->transform_in->iv_dec,
1342 ssl->transform_in->ivlen ) ) != 0 ||
1343 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001344 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001345 return( ret );
1346 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001347
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001348 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1349 add_data, 13 ) ) != 0 )
1350 {
1351 return( ret );
1352 }
1353
1354 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1355 dec_msg, dec_msglen,
1356 dec_msg_result, &olen ) ) != 0 )
1357 {
1358 return( ret );
1359 }
1360 totlen = olen;
1361
1362 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1363 dec_msg_result + olen, &olen ) ) != 0 )
1364 {
1365 return( ret );
1366 }
1367 totlen += olen;
1368
1369 if( totlen != dec_msglen )
1370 {
1371 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1372 return( -1 );
1373 }
1374
1375 /*
1376 * Authenticate
1377 */
1378 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1379 dec_msg + dec_msglen, 16 ) ) != 0 )
1380 {
1381 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001382 return( POLARSSL_ERR_SSL_INVALID_MAC );
1383 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001384
Paul Bakkerca4ab492012-04-18 14:23:57 +00001385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 else
Paul Bakker68884e32013-01-07 18:20:04 +01001387#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001388#if defined(POLARSSL_CIPHER_MODE_CBC)
1389 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1390 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 {
Paul Bakker45829992013-01-03 14:52:21 +01001392 /*
1393 * Decrypt and check the padding
1394 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001395 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001396 unsigned char *dec_msg;
1397 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001398 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001399 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001400 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001401
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 /*
Paul Bakker45829992013-01-03 14:52:21 +01001403 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 */
Paul Bakker48916f92012-09-16 19:57:18 +00001405 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 {
1407 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001408 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001409 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 }
1411
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001412#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001413 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1414 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001415#endif
Paul Bakker45829992013-01-03 14:52:21 +01001416
1417 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1418 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1419 {
1420 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1421 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1422 return( POLARSSL_ERR_SSL_INVALID_MAC );
1423 }
1424
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001425 dec_msglen = ssl->in_msglen;
1426 dec_msg = ssl->in_msg;
1427 dec_msg_result = ssl->in_msg;
1428
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001429#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001430 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001431 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001432 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001433 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001434 {
Paul Bakker48916f92012-09-16 19:57:18 +00001435 dec_msglen -= ssl->transform_in->ivlen;
1436 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001437
Paul Bakker48916f92012-09-16 19:57:18 +00001438 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001439 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001440 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001441#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001442
Paul Bakker45125bc2013-09-04 16:47:11 +02001443 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001445 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1446 return( ret );
1447 }
1448
1449 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1450 ssl->transform_in->iv_dec,
1451 ssl->transform_in->ivlen ) ) != 0 )
1452 {
1453 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001454 return( ret );
1455 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
Paul Bakkercca5b812013-08-31 17:40:26 +02001457 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1458 dec_msg, dec_msglen, dec_msg_result,
1459 &olen ) ) != 0 )
1460 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001461 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001462 return( ret );
1463 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001464
Paul Bakkercca5b812013-08-31 17:40:26 +02001465 dec_msglen -= olen;
1466 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001467 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001468 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001469 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001470 return( ret );
1471 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001472
Paul Bakkercca5b812013-08-31 17:40:26 +02001473 if( dec_msglen != olen )
1474 {
1475 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1476 // TODO Real error number
1477 return( -1 );
1478 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001479
1480#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001481 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1482 {
1483 /*
1484 * Save IV in SSL3 and TLS1
1485 */
1486 memcpy( ssl->transform_in->iv_dec,
1487 ssl->transform_in->cipher_ctx_dec.iv,
1488 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001490#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001491
1492 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001493
1494 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1495 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001496#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001497 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1498 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001499#endif
Paul Bakker45829992013-01-03 14:52:21 +01001500 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001501 correct = 0;
1502 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001504#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1506 {
Paul Bakker48916f92012-09-16 19:57:18 +00001507 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001509#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1511 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001512 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001513#endif
Paul Bakker45829992013-01-03 14:52:21 +01001514 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001515 }
1516 }
1517 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001518#endif
1519#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1520 defined(POLARSSL_SSL_PROTO_TLS1_2)
1521 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 {
1523 /*
Paul Bakker45829992013-01-03 14:52:21 +01001524 * TLSv1+: always check the padding up to the first failure
1525 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001527 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001528 size_t padding_idx = ssl->in_msglen - padlen - 1;
1529
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001530 for( i = 1; i <= 256; i++ )
1531 {
1532 real_count &= ( i <= padlen );
1533 pad_count += real_count *
1534 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1535 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001536
1537 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001538
Paul Bakkerd66f0702013-01-31 16:57:45 +01001539#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001540 if( padlen > 0 && correct == 0)
1541 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001542#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001543 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001545 else
1546#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1547 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001548 {
1549 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001550 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001553 else
1554#endif /* POLARSSL_CIPHER_MODE_CBC */
1555 {
1556 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1557 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001559
1560 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1561 ssl->in_msg, ssl->in_msglen );
1562
1563 /*
1564 * Always compute the MAC (RFC4346, CBCTIME).
1565 */
Paul Bakker48916f92012-09-16 19:57:18 +00001566 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001567
1568 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1569 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
1570
Paul Bakker45829992013-01-03 14:52:21 +01001571 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001572
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001573#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001574 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1575 {
Paul Bakker68884e32013-01-07 18:20:04 +01001576 ssl_mac( &ssl->transform_in->md_ctx_dec,
1577 ssl->transform_in->mac_dec,
1578 ssl->in_msg, ssl->in_msglen,
1579 ssl->in_ctr, ssl->in_msgtype );
Paul Bakker5121ce52009-01-03 21:22:43 +00001580 }
1581 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001582#endif /* POLARSSL_SSL_PROTO_SSL3 */
1583#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1584 defined(POLARSSL_SSL_PROTO_TLS1_2)
1585 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 {
Paul Bakker45829992013-01-03 14:52:21 +01001587 /*
1588 * Process MAC and always update for padlen afterwards to make
1589 * total time independent of padlen
Paul Bakkere47b34b2013-02-27 14:48:00 +01001590 *
1591 * extra_run compensates MAC check for padlen
1592 *
1593 * Known timing attacks:
1594 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1595 *
1596 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1597 * correctly. (We round down instead of up, so -56 is the correct
1598 * value for our calculations instead of -55)
Paul Bakker45829992013-01-03 14:52:21 +01001599 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001600 size_t j, extra_run = 0;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001601 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1602 ( 13 + ssl->in_msglen + 8 ) / 64;
1603
1604 extra_run &= correct * 0xFF;
1605
Paul Bakker68884e32013-01-07 18:20:04 +01001606 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1607 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1608 ssl->in_msglen );
1609 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1610 ssl->in_msg + ssl->in_msglen );
1611 for( j = 0; j < extra_run; j++ )
1612 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001613
Paul Bakker68884e32013-01-07 18:20:04 +01001614 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001616 else
1617#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1618 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001619 {
1620 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001621 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001622 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
Paul Bakker48916f92012-09-16 19:57:18 +00001624 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
Paul Bakker48916f92012-09-16 19:57:18 +00001626 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
1628 if( memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Paul Bakker48916f92012-09-16 19:57:18 +00001629 ssl->transform_in->maclen ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001630 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001631#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001633#endif
Paul Bakker45829992013-01-03 14:52:21 +01001634 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 }
1636
1637 /*
Paul Bakker45829992013-01-03 14:52:21 +01001638 * Finally check the correct flag
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 */
Paul Bakker45829992013-01-03 14:52:21 +01001640 if( correct == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +00001641 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001642
1643 if( ssl->in_msglen == 0 )
1644 {
1645 ssl->nb_zero++;
1646
1647 /*
1648 * Three or more empty messages may be a DoS attack
1649 * (excessive CPU consumption).
1650 */
1651 if( ssl->nb_zero > 3 )
1652 {
1653 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1654 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001655 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 }
1657 }
1658 else
1659 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001660
Paul Bakker23986e52011-04-24 08:57:21 +00001661 for( i = 8; i > 0; i-- )
1662 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001663 break;
1664
1665 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1666
1667 return( 0 );
1668}
1669
Paul Bakker2770fbd2012-07-03 13:30:23 +00001670#if defined(POLARSSL_ZLIB_SUPPORT)
1671/*
1672 * Compression/decompression functions
1673 */
1674static int ssl_compress_buf( ssl_context *ssl )
1675{
1676 int ret;
1677 unsigned char *msg_post = ssl->out_msg;
1678 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001679 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001680
1681 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1682
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001683 if( len_pre == 0 )
1684 return( 0 );
1685
Paul Bakker2770fbd2012-07-03 13:30:23 +00001686 memcpy( msg_pre, ssl->out_msg, len_pre );
1687
1688 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1689 ssl->out_msglen ) );
1690
1691 SSL_DEBUG_BUF( 4, "before compression: output payload",
1692 ssl->out_msg, ssl->out_msglen );
1693
Paul Bakker48916f92012-09-16 19:57:18 +00001694 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1695 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1696 ssl->transform_out->ctx_deflate.next_out = msg_post;
1697 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001698
Paul Bakker48916f92012-09-16 19:57:18 +00001699 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001700 if( ret != Z_OK )
1701 {
1702 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1703 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1704 }
1705
Paul Bakker48916f92012-09-16 19:57:18 +00001706 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001707
Paul Bakker2770fbd2012-07-03 13:30:23 +00001708 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1709 ssl->out_msglen ) );
1710
1711 SSL_DEBUG_BUF( 4, "after compression: output payload",
1712 ssl->out_msg, ssl->out_msglen );
1713
1714 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1715
1716 return( 0 );
1717}
1718
1719static int ssl_decompress_buf( ssl_context *ssl )
1720{
1721 int ret;
1722 unsigned char *msg_post = ssl->in_msg;
1723 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001724 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001725
1726 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1727
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001728 if( len_pre == 0 )
1729 return( 0 );
1730
Paul Bakker2770fbd2012-07-03 13:30:23 +00001731 memcpy( msg_pre, ssl->in_msg, len_pre );
1732
1733 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1734 ssl->in_msglen ) );
1735
1736 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1737 ssl->in_msg, ssl->in_msglen );
1738
Paul Bakker48916f92012-09-16 19:57:18 +00001739 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1740 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1741 ssl->transform_in->ctx_inflate.next_out = msg_post;
1742 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001743
Paul Bakker48916f92012-09-16 19:57:18 +00001744 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001745 if( ret != Z_OK )
1746 {
1747 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1748 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1749 }
1750
Paul Bakker48916f92012-09-16 19:57:18 +00001751 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001752
Paul Bakker2770fbd2012-07-03 13:30:23 +00001753 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1754 ssl->in_msglen ) );
1755
1756 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1757 ssl->in_msg, ssl->in_msglen );
1758
1759 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1760
1761 return( 0 );
1762}
1763#endif /* POLARSSL_ZLIB_SUPPORT */
1764
Paul Bakker5121ce52009-01-03 21:22:43 +00001765/*
1766 * Fill the input message buffer
1767 */
Paul Bakker23986e52011-04-24 08:57:21 +00001768int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001769{
Paul Bakker23986e52011-04-24 08:57:21 +00001770 int ret;
1771 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
1773 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1774
1775 while( ssl->in_left < nb_want )
1776 {
1777 len = nb_want - ssl->in_left;
1778 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1779
1780 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1781 ssl->in_left, nb_want ) );
1782 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1783
Paul Bakker831a7552011-05-18 13:32:51 +00001784 if( ret == 0 )
1785 return( POLARSSL_ERR_SSL_CONN_EOF );
1786
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 if( ret < 0 )
1788 return( ret );
1789
1790 ssl->in_left += ret;
1791 }
1792
1793 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1794
1795 return( 0 );
1796}
1797
1798/*
1799 * Flush any data not yet written
1800 */
1801int ssl_flush_output( ssl_context *ssl )
1802{
1803 int ret;
1804 unsigned char *buf;
1805
1806 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1807
1808 while( ssl->out_left > 0 )
1809 {
1810 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1811 5 + ssl->out_msglen, ssl->out_left ) );
1812
Paul Bakker5bd42292012-12-19 14:40:42 +01001813 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001815
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1817
1818 if( ret <= 0 )
1819 return( ret );
1820
1821 ssl->out_left -= ret;
1822 }
1823
1824 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1825
1826 return( 0 );
1827}
1828
1829/*
1830 * Record layer functions
1831 */
1832int ssl_write_record( ssl_context *ssl )
1833{
Paul Bakker05ef8352012-05-08 09:17:57 +00001834 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001835 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
1837 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1838
Paul Bakker5121ce52009-01-03 21:22:43 +00001839 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1840 {
1841 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1842 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1843 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1844
Paul Bakker48916f92012-09-16 19:57:18 +00001845 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846 }
1847
Paul Bakker2770fbd2012-07-03 13:30:23 +00001848#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001849 if( ssl->transform_out != NULL &&
1850 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001851 {
1852 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1853 {
1854 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1855 return( ret );
1856 }
1857
1858 len = ssl->out_msglen;
1859 }
1860#endif /*POLARSSL_ZLIB_SUPPORT */
1861
Paul Bakker05ef8352012-05-08 09:17:57 +00001862#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1863 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001865 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001866
Paul Bakker05ef8352012-05-08 09:17:57 +00001867 ret = ssl_hw_record_write( ssl );
1868 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1869 {
1870 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1871 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1872 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001873
1874 if( ret == 0 )
1875 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001876 }
1877#endif
1878 if( !done )
1879 {
1880 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1881 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1882 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1884 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001885
Paul Bakker48916f92012-09-16 19:57:18 +00001886 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001887 {
1888 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1889 {
1890 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1891 return( ret );
1892 }
1893
1894 len = ssl->out_msglen;
1895 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1896 ssl->out_hdr[4] = (unsigned char)( len );
1897 }
1898
1899 ssl->out_left = 5 + ssl->out_msglen;
1900
1901 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1902 "version = [%d:%d], msglen = %d",
1903 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1904 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1905
1906 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001907 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 }
1909
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1911 {
1912 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1913 return( ret );
1914 }
1915
1916 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1917
1918 return( 0 );
1919}
1920
1921int ssl_read_record( ssl_context *ssl )
1922{
Paul Bakker05ef8352012-05-08 09:17:57 +00001923 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
1925 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1926
Paul Bakker68884e32013-01-07 18:20:04 +01001927 SSL_DEBUG_BUF( 4, "input record from network",
1928 ssl->in_hdr, 5 + ssl->in_msglen );
1929
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 if( ssl->in_hslen != 0 &&
1931 ssl->in_hslen < ssl->in_msglen )
1932 {
1933 /*
1934 * Get next Handshake message in the current record
1935 */
1936 ssl->in_msglen -= ssl->in_hslen;
1937
Paul Bakker8934a982011-08-05 11:11:53 +00001938 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001939 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001940
1941 ssl->in_hslen = 4;
1942 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1943
1944 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1945 " %d, type = %d, hslen = %d",
1946 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1947
1948 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1949 {
1950 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001951 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 }
1953
1954 if( ssl->in_msglen < ssl->in_hslen )
1955 {
1956 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001957 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 }
1959
Paul Bakker48916f92012-09-16 19:57:18 +00001960 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001961
1962 return( 0 );
1963 }
1964
1965 ssl->in_hslen = 0;
1966
1967 /*
1968 * Read the record header and validate it
1969 */
1970 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1971 {
1972 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1973 return( ret );
1974 }
1975
1976 ssl->in_msgtype = ssl->in_hdr[0];
1977 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
1978
1979 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
1980 "version = [%d:%d], msglen = %d",
1981 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
1982 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
1983
1984 if( ssl->in_hdr[1] != ssl->major_ver )
1985 {
1986 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001987 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 }
1989
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001990 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001991 {
1992 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001993 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 }
1995
1996 /*
1997 * Make sure the message length is acceptable
1998 */
Paul Bakker48916f92012-09-16 19:57:18 +00001999 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 {
2001 if( ssl->in_msglen < 1 ||
2002 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2003 {
2004 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002005 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 }
2007 }
2008 else
2009 {
Paul Bakker48916f92012-09-16 19:57:18 +00002010 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 {
2012 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002013 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 }
2015
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002016#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002018 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 {
2020 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002021 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002023#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002024
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002025#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2026 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 /*
2028 * TLS encrypted messages can have up to 256 bytes of padding
2029 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002030 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002031 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 {
2033 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002034 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002036#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 }
2038
2039 /*
2040 * Read and optionally decrypt the message contents
2041 */
2042 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2043 {
2044 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2045 return( ret );
2046 }
2047
2048 SSL_DEBUG_BUF( 4, "input record from network",
2049 ssl->in_hdr, 5 + ssl->in_msglen );
2050
Paul Bakker05ef8352012-05-08 09:17:57 +00002051#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2052 if( ssl_hw_record_read != NULL)
2053 {
2054 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2055
2056 ret = ssl_hw_record_read( ssl );
2057 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2058 {
2059 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2060 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2061 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002062
2063 if( ret == 0 )
2064 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002065 }
2066#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002067 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 {
2069 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2070 {
Paul Bakker40865c82013-01-31 17:13:13 +01002071#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2072 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2073 {
2074 ssl_send_alert_message( ssl,
2075 SSL_ALERT_LEVEL_FATAL,
2076 SSL_ALERT_MSG_BAD_RECORD_MAC );
2077 }
2078#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2080 return( ret );
2081 }
2082
2083 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2084 ssl->in_msg, ssl->in_msglen );
2085
2086 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2087 {
2088 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002089 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 }
2091 }
2092
Paul Bakker2770fbd2012-07-03 13:30:23 +00002093#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002094 if( ssl->transform_in != NULL &&
2095 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002096 {
2097 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2098 {
2099 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2100 return( ret );
2101 }
2102
2103 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2104 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2105 }
2106#endif /* POLARSSL_ZLIB_SUPPORT */
2107
Paul Bakker0a925182012-04-16 06:46:41 +00002108 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2109 ssl->in_msgtype != SSL_MSG_ALERT &&
2110 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2111 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2112 {
2113 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2114
Paul Bakker48916f92012-09-16 19:57:18 +00002115 if( ( ret = ssl_send_alert_message( ssl,
2116 SSL_ALERT_LEVEL_FATAL,
2117 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002118 {
2119 return( ret );
2120 }
2121
2122 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2123 }
2124
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2126 {
2127 ssl->in_hslen = 4;
2128 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2129
2130 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2131 " %d, type = %d, hslen = %d",
2132 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2133
2134 /*
2135 * Additional checks to validate the handshake header
2136 */
2137 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2138 {
2139 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002140 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 }
2142
2143 if( ssl->in_msglen < ssl->in_hslen )
2144 {
2145 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002146 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 }
2148
Paul Bakker48916f92012-09-16 19:57:18 +00002149 if( ssl->state != SSL_HANDSHAKE_OVER )
2150 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 }
2152
2153 if( ssl->in_msgtype == SSL_MSG_ALERT )
2154 {
2155 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2156 ssl->in_msg[0], ssl->in_msg[1] ) );
2157
2158 /*
2159 * Ignore non-fatal alerts, except close_notify
2160 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002161 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002163 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2164 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002165 /**
2166 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2167 * error identifier.
2168 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002169 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 }
2171
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002172 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2173 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 {
2175 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002176 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 }
2178 }
2179
2180 ssl->in_left = 0;
2181
2182 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2183
2184 return( 0 );
2185}
2186
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002187int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2188{
2189 int ret;
2190
2191 if( ( ret = ssl_send_alert_message( ssl,
2192 SSL_ALERT_LEVEL_FATAL,
2193 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2194 {
2195 return( ret );
2196 }
2197
2198 return( 0 );
2199}
2200
Paul Bakker0a925182012-04-16 06:46:41 +00002201int ssl_send_alert_message( ssl_context *ssl,
2202 unsigned char level,
2203 unsigned char message )
2204{
2205 int ret;
2206
2207 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2208
2209 ssl->out_msgtype = SSL_MSG_ALERT;
2210 ssl->out_msglen = 2;
2211 ssl->out_msg[0] = level;
2212 ssl->out_msg[1] = message;
2213
2214 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2215 {
2216 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2217 return( ret );
2218 }
2219
2220 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2221
2222 return( 0 );
2223}
2224
Paul Bakker5121ce52009-01-03 21:22:43 +00002225/*
2226 * Handshake functions
2227 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002228#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2229 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002230 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2231 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002232int ssl_write_certificate( ssl_context *ssl )
2233{
Paul Bakkered27a042013-04-18 22:46:23 +02002234 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002235 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
2237 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2238
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002239 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2240 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002241 {
2242 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2243 ssl->state++;
2244 return( 0 );
2245 }
2246
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002247 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002248 return( ret );
2249}
2250
2251int ssl_parse_certificate( ssl_context *ssl )
2252{
2253 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2254 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2255
2256 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2257
2258 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2259 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2260 {
2261 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2262 ssl->state++;
2263 return( 0 );
2264 }
2265
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002266 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002267 return( ret );
2268}
2269#else
2270int ssl_write_certificate( ssl_context *ssl )
2271{
2272 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2273 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002274 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002275 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2276
2277 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2278
2279 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2280 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2281 {
2282 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2283 ssl->state++;
2284 return( 0 );
2285 }
2286
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 if( ssl->endpoint == SSL_IS_CLIENT )
2288 {
2289 if( ssl->client_auth == 0 )
2290 {
2291 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2292 ssl->state++;
2293 return( 0 );
2294 }
2295
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002296#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002297 /*
2298 * If using SSLv3 and got no cert, send an Alert message
2299 * (otherwise an empty Certificate message will be sent).
2300 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002301 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2303 {
2304 ssl->out_msglen = 2;
2305 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002306 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2307 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002308
2309 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2310 goto write_msg;
2311 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002312#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 }
2314 else /* SSL_IS_SERVER */
2315 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002316 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 {
2318 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002319 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002320 }
2321 }
2322
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002323 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002324
2325 /*
2326 * 0 . 0 handshake type
2327 * 1 . 3 handshake length
2328 * 4 . 6 length of all certs
2329 * 7 . 9 length of cert. 1
2330 * 10 . n-1 peer certificate
2331 * n . n+2 length of cert. 2
2332 * n+3 . ... upper level cert, etc.
2333 */
2334 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002335 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
Paul Bakker29087132010-03-21 21:03:34 +00002337 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002338 {
2339 n = crt->raw.len;
2340 if( i + 3 + n > SSL_MAX_CONTENT_LEN )
2341 {
2342 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2343 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002344 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 }
2346
2347 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2348 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2349 ssl->out_msg[i + 2] = (unsigned char)( n );
2350
2351 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2352 i += n; crt = crt->next;
2353 }
2354
2355 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2356 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2357 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2358
2359 ssl->out_msglen = i;
2360 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2361 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2362
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002363#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002364write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002365#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
2367 ssl->state++;
2368
2369 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2370 {
2371 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2372 return( ret );
2373 }
2374
2375 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2376
Paul Bakkered27a042013-04-18 22:46:23 +02002377 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002378}
2379
2380int ssl_parse_certificate( ssl_context *ssl )
2381{
Paul Bakkered27a042013-04-18 22:46:23 +02002382 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002383 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002384 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
2386 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2387
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002388 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2389 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002390 {
2391 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2392 ssl->state++;
2393 return( 0 );
2394 }
2395
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 if( ssl->endpoint == SSL_IS_SERVER &&
2397 ssl->authmode == SSL_VERIFY_NONE )
2398 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002399 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2401 ssl->state++;
2402 return( 0 );
2403 }
2404
2405 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2406 {
2407 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2408 return( ret );
2409 }
2410
2411 ssl->state++;
2412
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002413#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 /*
2415 * Check if the client sent an empty certificate
2416 */
2417 if( ssl->endpoint == SSL_IS_SERVER &&
2418 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2419 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002420 if( ssl->in_msglen == 2 &&
2421 ssl->in_msgtype == SSL_MSG_ALERT &&
2422 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2423 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 {
2425 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2426
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002427 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2429 return( 0 );
2430 else
Paul Bakker40e46942009-01-03 21:51:57 +00002431 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 }
2433 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002434#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002435
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002436#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2437 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 if( ssl->endpoint == SSL_IS_SERVER &&
2439 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2440 {
2441 if( ssl->in_hslen == 7 &&
2442 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2443 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2444 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2445 {
2446 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2447
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002448 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002450 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 else
2452 return( 0 );
2453 }
2454 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002455#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2456 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
2458 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2459 {
2460 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002461 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 }
2463
2464 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2465 {
2466 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002467 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 }
2469
2470 /*
2471 * Same message structure as in ssl_write_certificate()
2472 */
2473 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2474
2475 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2476 {
2477 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002478 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 }
2480
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002481 /* In case we tried to reuse a session but it failed */
2482 if( ssl->session_negotiate->peer_cert != NULL )
2483 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002484 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002485 polarssl_free( ssl->session_negotiate->peer_cert );
2486 }
2487
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002488 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2489 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 {
2491 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002492 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002493 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 }
2495
Paul Bakkerb6b09562013-09-18 14:17:41 +02002496 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
2498 i = 7;
2499
2500 while( i < ssl->in_hslen )
2501 {
2502 if( ssl->in_msg[i] != 0 )
2503 {
2504 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002505 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 }
2507
2508 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2509 | (unsigned int) ssl->in_msg[i + 2];
2510 i += 3;
2511
2512 if( n < 128 || i + n > ssl->in_hslen )
2513 {
2514 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002515 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 }
2517
Paul Bakkerddf26b42013-09-18 13:46:23 +02002518 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2519 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 if( ret != 0 )
2521 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002522 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 return( ret );
2524 }
2525
2526 i += n;
2527 }
2528
Paul Bakker48916f92012-09-16 19:57:18 +00002529 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
2531 if( ssl->authmode != SSL_VERIFY_NONE )
2532 {
2533 if( ssl->ca_chain == NULL )
2534 {
2535 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002536 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002537 }
2538
Paul Bakkerddf26b42013-09-18 13:46:23 +02002539 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2540 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2541 &ssl->session_negotiate->verify_result,
2542 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
2544 if( ret != 0 )
2545 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2546
2547 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2548 ret = 0;
2549 }
2550
2551 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2552
2553 return( ret );
2554}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002555#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2556 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2557 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
2559int ssl_write_change_cipher_spec( ssl_context *ssl )
2560{
2561 int ret;
2562
2563 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2564
2565 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2566 ssl->out_msglen = 1;
2567 ssl->out_msg[0] = 1;
2568
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 ssl->state++;
2570
2571 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2572 {
2573 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2574 return( ret );
2575 }
2576
2577 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2578
2579 return( 0 );
2580}
2581
2582int ssl_parse_change_cipher_spec( ssl_context *ssl )
2583{
2584 int ret;
2585
2586 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2587
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2589 {
2590 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2591 return( ret );
2592 }
2593
2594 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2595 {
2596 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002597 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 }
2599
2600 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2601 {
2602 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002603 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 }
2605
2606 ssl->state++;
2607
2608 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2609
2610 return( 0 );
2611}
2612
Paul Bakker41c83d32013-03-20 14:39:14 +01002613void ssl_optimize_checksum( ssl_context *ssl,
2614 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002615{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002616 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002617
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002618#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2619 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002620 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002621 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002622 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002623#endif
2624#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2625#if defined(POLARSSL_SHA512_C)
2626 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2627 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2628 else
2629#endif
2630#if defined(POLARSSL_SHA256_C)
2631 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002632 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002633 else
2634#endif
2635#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2636 /* Should never happen */
2637 return;
Paul Bakker380da532012-04-18 16:10:25 +00002638}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002639
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002640static void ssl_update_checksum_start( ssl_context *ssl,
2641 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002642{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002643#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2644 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002645 md5_update( &ssl->handshake->fin_md5 , buf, len );
2646 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002647#endif
2648#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2649#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002650 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002651#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002652#if defined(POLARSSL_SHA512_C)
2653 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002654#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002655#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002656}
2657
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002658#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2659 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002660static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2661 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002662{
Paul Bakker48916f92012-09-16 19:57:18 +00002663 md5_update( &ssl->handshake->fin_md5 , buf, len );
2664 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002665}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002666#endif
Paul Bakker380da532012-04-18 16:10:25 +00002667
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002668#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2669#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002670static void ssl_update_checksum_sha256( ssl_context *ssl,
2671 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002672{
Paul Bakker9e36f042013-06-30 14:34:05 +02002673 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002674}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002675#endif
Paul Bakker380da532012-04-18 16:10:25 +00002676
Paul Bakker9e36f042013-06-30 14:34:05 +02002677#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002678static void ssl_update_checksum_sha384( ssl_context *ssl,
2679 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002680{
Paul Bakker9e36f042013-06-30 14:34:05 +02002681 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002682}
Paul Bakker769075d2012-11-24 11:26:46 +01002683#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002684#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002685
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002686#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002687static void ssl_calc_finished_ssl(
2688 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002689{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002690 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002691 md5_context md5;
2692 sha1_context sha1;
2693
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 unsigned char padbuf[48];
2695 unsigned char md5sum[16];
2696 unsigned char sha1sum[20];
2697
Paul Bakker48916f92012-09-16 19:57:18 +00002698 ssl_session *session = ssl->session_negotiate;
2699 if( !session )
2700 session = ssl->session;
2701
Paul Bakker1ef83d62012-04-11 12:09:53 +00002702 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2703
Paul Bakker48916f92012-09-16 19:57:18 +00002704 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2705 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706
2707 /*
2708 * SSLv3:
2709 * hash =
2710 * MD5( master + pad2 +
2711 * MD5( handshake + sender + master + pad1 ) )
2712 * + SHA1( master + pad2 +
2713 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 */
2715
Paul Bakker90995b52013-06-24 19:20:35 +02002716#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002718 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Paul Bakker90995b52013-06-24 19:20:35 +02002721#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002723 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002724#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Paul Bakker3c2122f2013-06-24 19:03:14 +02002726 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2727 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Paul Bakker1ef83d62012-04-11 12:09:53 +00002729 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730
Paul Bakker3c2122f2013-06-24 19:03:14 +02002731 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002732 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002733 md5_update( &md5, padbuf, 48 );
2734 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002735
Paul Bakker3c2122f2013-06-24 19:03:14 +02002736 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002737 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002738 sha1_update( &sha1, padbuf, 40 );
2739 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Paul Bakker1ef83d62012-04-11 12:09:53 +00002741 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Paul Bakker1ef83d62012-04-11 12:09:53 +00002743 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002744 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002745 md5_update( &md5, padbuf, 48 );
2746 md5_update( &md5, md5sum, 16 );
2747 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Paul Bakker1ef83d62012-04-11 12:09:53 +00002749 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002750 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002751 sha1_update( &sha1, padbuf , 40 );
2752 sha1_update( &sha1, sha1sum, 20 );
2753 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002754
Paul Bakker1ef83d62012-04-11 12:09:53 +00002755 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Paul Bakker1ef83d62012-04-11 12:09:53 +00002757 memset( &md5, 0, sizeof( md5_context ) );
2758 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
2760 memset( padbuf, 0, sizeof( padbuf ) );
2761 memset( md5sum, 0, sizeof( md5sum ) );
2762 memset( sha1sum, 0, sizeof( sha1sum ) );
2763
2764 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2765}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002766#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002767
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002768#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002769static void ssl_calc_finished_tls(
2770 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002771{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002772 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002773 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002774 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002775 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002776 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
Paul Bakker48916f92012-09-16 19:57:18 +00002778 ssl_session *session = ssl->session_negotiate;
2779 if( !session )
2780 session = ssl->session;
2781
Paul Bakker1ef83d62012-04-11 12:09:53 +00002782 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
Paul Bakker48916f92012-09-16 19:57:18 +00002784 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2785 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
Paul Bakker1ef83d62012-04-11 12:09:53 +00002787 /*
2788 * TLSv1:
2789 * hash = PRF( master, finished_label,
2790 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2791 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Paul Bakker90995b52013-06-24 19:20:35 +02002793#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002794 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2795 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002796#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002797
Paul Bakker90995b52013-06-24 19:20:35 +02002798#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002799 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2800 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002801#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002802
2803 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002804 ? "client finished"
2805 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002806
2807 md5_finish( &md5, padbuf );
2808 sha1_finish( &sha1, padbuf + 16 );
2809
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002810 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002811 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002812
2813 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2814
2815 memset( &md5, 0, sizeof( md5_context ) );
2816 memset( &sha1, 0, sizeof( sha1_context ) );
2817
2818 memset( padbuf, 0, sizeof( padbuf ) );
2819
2820 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2821}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002822#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002823
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002824#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2825#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002826static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002827 ssl_context *ssl, unsigned char *buf, int from )
2828{
2829 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002830 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002831 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002832 unsigned char padbuf[32];
2833
Paul Bakker48916f92012-09-16 19:57:18 +00002834 ssl_session *session = ssl->session_negotiate;
2835 if( !session )
2836 session = ssl->session;
2837
Paul Bakker380da532012-04-18 16:10:25 +00002838 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002839
Paul Bakker9e36f042013-06-30 14:34:05 +02002840 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002841
2842 /*
2843 * TLSv1.2:
2844 * hash = PRF( master, finished_label,
2845 * Hash( handshake ) )[0.11]
2846 */
2847
Paul Bakker9e36f042013-06-30 14:34:05 +02002848#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002849 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002850 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002851#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002852
2853 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002854 ? "client finished"
2855 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002856
Paul Bakker9e36f042013-06-30 14:34:05 +02002857 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002858
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002859 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002860 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002861
2862 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2863
Paul Bakker9e36f042013-06-30 14:34:05 +02002864 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865
2866 memset( padbuf, 0, sizeof( padbuf ) );
2867
2868 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2869}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002870#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871
Paul Bakker9e36f042013-06-30 14:34:05 +02002872#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002873static void ssl_calc_finished_tls_sha384(
2874 ssl_context *ssl, unsigned char *buf, int from )
2875{
2876 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002877 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002878 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002879 unsigned char padbuf[48];
2880
Paul Bakker48916f92012-09-16 19:57:18 +00002881 ssl_session *session = ssl->session_negotiate;
2882 if( !session )
2883 session = ssl->session;
2884
Paul Bakker380da532012-04-18 16:10:25 +00002885 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002886
Paul Bakker9e36f042013-06-30 14:34:05 +02002887 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002888
2889 /*
2890 * TLSv1.2:
2891 * hash = PRF( master, finished_label,
2892 * Hash( handshake ) )[0.11]
2893 */
2894
Paul Bakker9e36f042013-06-30 14:34:05 +02002895#if !defined(POLARSSL_SHA512_ALT)
2896 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
2897 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002898#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00002899
2900 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002901 ? "client finished"
2902 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00002903
Paul Bakker9e36f042013-06-30 14:34:05 +02002904 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002905
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002906 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002907 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002908
2909 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2910
Paul Bakker9e36f042013-06-30 14:34:05 +02002911 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002912
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_SHA512_C */
2918#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00002919
Paul Bakker48916f92012-09-16 19:57:18 +00002920void ssl_handshake_wrapup( ssl_context *ssl )
2921{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002922 int resume = ssl->handshake->resume;
2923
Paul Bakker48916f92012-09-16 19:57:18 +00002924 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
2925
2926 /*
2927 * Free our handshake params
2928 */
2929 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02002930 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00002931 ssl->handshake = NULL;
2932
2933 /*
2934 * Switch in our now active transform context
2935 */
2936 if( ssl->transform )
2937 {
2938 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02002939 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00002940 }
2941 ssl->transform = ssl->transform_negotiate;
2942 ssl->transform_negotiate = NULL;
2943
Paul Bakker0a597072012-09-25 21:55:46 +00002944 if( ssl->session )
2945 {
2946 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02002947 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00002948 }
2949 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00002950 ssl->session_negotiate = NULL;
2951
Paul Bakker0a597072012-09-25 21:55:46 +00002952 /*
2953 * Add cache entry
2954 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002955 if( ssl->f_set_cache != NULL &&
2956 ssl->session->length != 0 &&
2957 resume == 0 )
2958 {
Paul Bakker0a597072012-09-25 21:55:46 +00002959 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
2960 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002961 }
Paul Bakker0a597072012-09-25 21:55:46 +00002962
Paul Bakker48916f92012-09-16 19:57:18 +00002963 ssl->state++;
2964
2965 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
2966}
2967
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968int ssl_write_finished( ssl_context *ssl )
2969{
2970 int ret, hash_len;
2971
2972 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
2973
Paul Bakker92be97b2013-01-02 17:30:03 +01002974 /*
2975 * Set the out_msg pointer to the correct location based on IV length
2976 */
2977 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2978 {
2979 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
2980 ssl->transform_negotiate->fixed_ivlen;
2981 }
2982 else
2983 ssl->out_msg = ssl->out_iv;
2984
Paul Bakker48916f92012-09-16 19:57:18 +00002985 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986
2987 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00002988 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
2989
Paul Bakker48916f92012-09-16 19:57:18 +00002990 ssl->verify_data_len = hash_len;
2991 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
2992
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 ssl->out_msglen = 4 + hash_len;
2994 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2995 ssl->out_msg[0] = SSL_HS_FINISHED;
2996
2997 /*
2998 * In case of session resuming, invert the client and server
2999 * ChangeCipherSpec messages order.
3000 */
Paul Bakker0a597072012-09-25 21:55:46 +00003001 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003002 {
3003 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003004 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003005 else
3006 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3007 }
3008 else
3009 ssl->state++;
3010
Paul Bakker48916f92012-09-16 19:57:18 +00003011 /*
3012 * Switch to our negotiated transform and session parameters for outbound data.
3013 */
3014 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3015 ssl->transform_out = ssl->transform_negotiate;
3016 ssl->session_out = ssl->session_negotiate;
3017 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
Paul Bakker07eb38b2012-12-19 14:42:06 +01003019#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3020 if( ssl_hw_record_activate != NULL)
3021 {
3022 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3023 {
3024 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3025 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3026 }
3027 }
3028#endif
3029
Paul Bakker5121ce52009-01-03 21:22:43 +00003030 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3031 {
3032 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3033 return( ret );
3034 }
3035
3036 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3037
3038 return( 0 );
3039}
3040
3041int ssl_parse_finished( ssl_context *ssl )
3042{
Paul Bakker23986e52011-04-24 08:57:21 +00003043 int ret;
3044 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 unsigned char buf[36];
3046
3047 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3048
Paul Bakker48916f92012-09-16 19:57:18 +00003049 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Paul Bakker48916f92012-09-16 19:57:18 +00003051 /*
3052 * Switch to our negotiated transform and session parameters for inbound data.
3053 */
3054 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3055 ssl->transform_in = ssl->transform_negotiate;
3056 ssl->session_in = ssl->session_negotiate;
3057 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Paul Bakker92be97b2013-01-02 17:30:03 +01003059 /*
3060 * Set the in_msg pointer to the correct location based on IV length
3061 */
3062 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3063 {
3064 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3065 ssl->transform_negotiate->fixed_ivlen;
3066 }
3067 else
3068 ssl->in_msg = ssl->in_iv;
3069
Paul Bakker07eb38b2012-12-19 14:42:06 +01003070#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3071 if( ssl_hw_record_activate != NULL)
3072 {
3073 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3074 {
3075 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3076 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3077 }
3078 }
3079#endif
3080
Paul Bakker5121ce52009-01-03 21:22:43 +00003081 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3082 {
3083 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3084 return( ret );
3085 }
3086
3087 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3088 {
3089 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003090 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 }
3092
Paul Bakker1ef83d62012-04-11 12:09:53 +00003093 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3095
3096 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3097 ssl->in_hslen != 4 + hash_len )
3098 {
3099 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003100 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 }
3102
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 if( memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
3104 {
3105 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003106 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003107 }
3108
Paul Bakker48916f92012-09-16 19:57:18 +00003109 ssl->verify_data_len = hash_len;
3110 memcpy( ssl->peer_verify_data, buf, hash_len );
3111
Paul Bakker0a597072012-09-25 21:55:46 +00003112 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 {
3114 if( ssl->endpoint == SSL_IS_CLIENT )
3115 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3116
3117 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003118 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003119 }
3120 else
3121 ssl->state++;
3122
3123 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3124
3125 return( 0 );
3126}
3127
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003128static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003129{
3130 if( ssl->transform_negotiate )
3131 ssl_transform_free( ssl->transform_negotiate );
3132 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003133 {
3134 ssl->transform_negotiate =
3135 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3136 }
Paul Bakker48916f92012-09-16 19:57:18 +00003137
3138 if( ssl->session_negotiate )
3139 ssl_session_free( ssl->session_negotiate );
3140 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003141 {
3142 ssl->session_negotiate =
3143 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3144 }
Paul Bakker48916f92012-09-16 19:57:18 +00003145
3146 if( ssl->handshake )
3147 ssl_handshake_free( ssl->handshake );
3148 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003149 {
3150 ssl->handshake = (ssl_handshake_params *)
3151 polarssl_malloc( sizeof(ssl_handshake_params) );
3152 }
Paul Bakker48916f92012-09-16 19:57:18 +00003153
3154 if( ssl->handshake == NULL ||
3155 ssl->transform_negotiate == NULL ||
3156 ssl->session_negotiate == NULL )
3157 {
3158 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3159 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3160 }
3161
3162 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3163 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3164 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3165
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003166#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3167 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003168 md5_starts( &ssl->handshake->fin_md5 );
3169 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003170#endif
3171#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3172#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003173 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003174#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003175#if defined(POLARSSL_SHA512_C)
3176 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003177#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003178#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003179
3180 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003181 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003182
Paul Bakker61d113b2013-07-04 11:51:43 +02003183#if defined(POLARSSL_ECDH_C)
3184 ecdh_init( &ssl->handshake->ecdh_ctx );
3185#endif
3186
Paul Bakker48916f92012-09-16 19:57:18 +00003187 return( 0 );
3188}
3189
Paul Bakker5121ce52009-01-03 21:22:43 +00003190/*
3191 * Initialize an SSL context
3192 */
3193int ssl_init( ssl_context *ssl )
3194{
Paul Bakker48916f92012-09-16 19:57:18 +00003195 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 int len = SSL_BUFFER_LEN;
3197
3198 memset( ssl, 0, sizeof( ssl_context ) );
3199
Paul Bakker62f2dee2012-09-28 07:31:51 +00003200 /*
3201 * Sane defaults
3202 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003203 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3204 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3205 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3206 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003207
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003208 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003209
Paul Bakker62f2dee2012-09-28 07:31:51 +00003210#if defined(POLARSSL_DHM_C)
3211 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3212 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3213 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3214 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3215 {
3216 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3217 return( ret );
3218 }
3219#endif
3220
3221 /*
3222 * Prepare base structures
3223 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003224 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003225 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003226 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 ssl->in_msg = ssl->in_ctr + 13;
3228
3229 if( ssl->in_ctr == NULL )
3230 {
3231 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003232 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 }
3234
Paul Bakker6e339b52013-07-03 13:37:05 +02003235 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003237 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003238 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003239
3240 if( ssl->out_ctr == NULL )
3241 {
3242 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003243 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003244 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 }
3246
3247 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3248 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3249
Paul Bakker606b4ba2013-08-14 16:52:14 +02003250#if defined(POLARSSL_SSL_SESSION_TICKETS)
3251 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3252#endif
3253
Paul Bakker48916f92012-09-16 19:57:18 +00003254 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3255 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
3257 return( 0 );
3258}
3259
3260/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003261 * Reset an initialized and used SSL context for re-use while retaining
3262 * all application-set variables, function pointers and data.
3263 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003264int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003265{
Paul Bakker48916f92012-09-16 19:57:18 +00003266 int ret;
3267
Paul Bakker7eb013f2011-10-06 12:37:39 +00003268 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003269 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3270 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3271
3272 ssl->verify_data_len = 0;
3273 memset( ssl->own_verify_data, 0, 36 );
3274 memset( ssl->peer_verify_data, 0, 36 );
3275
Paul Bakker7eb013f2011-10-06 12:37:39 +00003276 ssl->in_offt = NULL;
3277
Paul Bakker92be97b2013-01-02 17:30:03 +01003278 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003279 ssl->in_msgtype = 0;
3280 ssl->in_msglen = 0;
3281 ssl->in_left = 0;
3282
3283 ssl->in_hslen = 0;
3284 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003285 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003286
Paul Bakker92be97b2013-01-02 17:30:03 +01003287 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003288 ssl->out_msgtype = 0;
3289 ssl->out_msglen = 0;
3290 ssl->out_left = 0;
3291
Paul Bakker48916f92012-09-16 19:57:18 +00003292 ssl->transform_in = NULL;
3293 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003294
3295 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3296 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003297
3298#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3299 if( ssl_hw_record_reset != NULL)
3300 {
3301 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003302 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003303 {
3304 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3305 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3306 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003307 }
3308#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003309
Paul Bakker48916f92012-09-16 19:57:18 +00003310 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003311 {
Paul Bakker48916f92012-09-16 19:57:18 +00003312 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003313 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003314 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003315 }
Paul Bakker48916f92012-09-16 19:57:18 +00003316
Paul Bakkerc0463502013-02-14 11:19:38 +01003317 if( ssl->session )
3318 {
3319 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003320 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003321 ssl->session = NULL;
3322 }
3323
Paul Bakker48916f92012-09-16 19:57:18 +00003324 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3325 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003326
3327 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003328}
3329
Paul Bakkera503a632013-08-14 13:48:06 +02003330#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003331/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003332 * Allocate and initialize ticket keys
3333 */
3334static int ssl_ticket_keys_init( ssl_context *ssl )
3335{
3336 int ret;
3337 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003338 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003339
3340 if( ssl->ticket_keys != NULL )
3341 return( 0 );
3342
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003343 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3344 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003345 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3346
3347 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
3348 return( ret );
3349
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003350 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3351 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3352 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3353 {
3354 return( ret );
3355 }
3356
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003357 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
3358 return( ret );
3359
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003360 ssl->ticket_keys = tkeys;
3361
3362 return( 0 );
3363}
Paul Bakkera503a632013-08-14 13:48:06 +02003364#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003365
3366/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003367 * SSL set accessors
3368 */
3369void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3370{
3371 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003372
Paul Bakker606b4ba2013-08-14 16:52:14 +02003373#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003374 if( endpoint == SSL_IS_CLIENT )
3375 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003376#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003377}
3378
3379void ssl_set_authmode( ssl_context *ssl, int authmode )
3380{
3381 ssl->authmode = authmode;
3382}
3383
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003384#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003385void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003386 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003387 void *p_vrfy )
3388{
3389 ssl->f_vrfy = f_vrfy;
3390 ssl->p_vrfy = p_vrfy;
3391}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003392#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003393
Paul Bakker5121ce52009-01-03 21:22:43 +00003394void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003395 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 void *p_rng )
3397{
3398 ssl->f_rng = f_rng;
3399 ssl->p_rng = p_rng;
3400}
3401
3402void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003403 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 void *p_dbg )
3405{
3406 ssl->f_dbg = f_dbg;
3407 ssl->p_dbg = p_dbg;
3408}
3409
3410void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003411 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003412 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003413{
3414 ssl->f_recv = f_recv;
3415 ssl->f_send = f_send;
3416 ssl->p_recv = p_recv;
3417 ssl->p_send = p_send;
3418}
3419
Paul Bakker0a597072012-09-25 21:55:46 +00003420void ssl_set_session_cache( ssl_context *ssl,
3421 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3422 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003423{
Paul Bakker0a597072012-09-25 21:55:46 +00003424 ssl->f_get_cache = f_get_cache;
3425 ssl->p_get_cache = p_get_cache;
3426 ssl->f_set_cache = f_set_cache;
3427 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003428}
3429
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003430int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003431{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003432 int ret;
3433
3434 if( ssl == NULL ||
3435 session == NULL ||
3436 ssl->session_negotiate == NULL ||
3437 ssl->endpoint != SSL_IS_CLIENT )
3438 {
3439 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3440 }
3441
3442 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3443 return( ret );
3444
Paul Bakker0a597072012-09-25 21:55:46 +00003445 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003446
3447 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003448}
3449
Paul Bakkerb68cad62012-08-23 08:34:18 +00003450void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003451{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003452 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3453 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3454 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3455 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3456}
3457
3458void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3459 int major, int minor )
3460{
3461 if( major != SSL_MAJOR_VERSION_3 )
3462 return;
3463
3464 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3465 return;
3466
3467 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003468}
3469
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003470#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003471/* Add a new (empty) key_cert entry an return a pointer to it */
3472static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3473{
3474 ssl_key_cert *key_cert, *last;
3475
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003476 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3477 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003478 return( NULL );
3479
3480 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3481
3482 /* Append the new key_cert to the (possibly empty) current list */
3483 if( ssl->key_cert == NULL )
3484 ssl->key_cert = key_cert;
3485 else
3486 {
3487 last = ssl->key_cert;
3488 while( last->next != NULL )
3489 last = last->next;
3490 last->next = key_cert;
3491 }
3492
3493 return key_cert;
3494}
3495
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003496void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003497 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003498{
3499 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003500 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003501 ssl->peer_cn = peer_cn;
3502}
3503
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003504int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003505 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003506{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003507 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3508
3509 if( key_cert == NULL )
3510 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3511
3512 key_cert->cert = own_cert;
3513 key_cert->key = pk_key;
3514
3515 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003516}
3517
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003518#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003519int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003520 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003521{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003522 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003523 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003524
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003525 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003526 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3527
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003528 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3529 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003530 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003531
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003532 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003533
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003534 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003535 if( ret != 0 )
3536 return( ret );
3537
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003538 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003539 return( ret );
3540
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003541 key_cert->cert = own_cert;
3542 key_cert->key_own_alloc = 1;
3543
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003544 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003545}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003546#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003547
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003548int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003549 void *rsa_key,
3550 rsa_decrypt_func rsa_decrypt,
3551 rsa_sign_func rsa_sign,
3552 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003553{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003554 int ret;
3555 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003556
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003557 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003558 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3559
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003560 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3561 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003562 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003563
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003564 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003565
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003566 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3567 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3568 return( ret );
3569
3570 key_cert->cert = own_cert;
3571 key_cert->key_own_alloc = 1;
3572
3573 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003574}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003575#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003576
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003577#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003578int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3579 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003580{
Paul Bakker6db455e2013-09-18 17:29:31 +02003581 if( psk == NULL || psk_identity == NULL )
3582 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3583
3584 if( ssl->psk != NULL )
3585 {
3586 polarssl_free( ssl->psk );
3587 polarssl_free( ssl->psk_identity );
3588 }
3589
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003590 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003591 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003592
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003593 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
3594 ssl->psk_identity = (unsigned char *) polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003595
3596 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3597 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3598
3599 memcpy( ssl->psk, psk, ssl->psk_len );
3600 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003601
3602 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003603}
3604
3605void ssl_set_psk_cb( ssl_context *ssl,
3606 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3607 size_t),
3608 void *p_psk )
3609{
3610 ssl->f_psk = f_psk;
3611 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003612}
3613#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003614
Paul Bakker48916f92012-09-16 19:57:18 +00003615#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003616int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003617{
3618 int ret;
3619
Paul Bakker48916f92012-09-16 19:57:18 +00003620 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003621 {
3622 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3623 return( ret );
3624 }
3625
Paul Bakker48916f92012-09-16 19:57:18 +00003626 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003627 {
3628 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3629 return( ret );
3630 }
3631
3632 return( 0 );
3633}
3634
Paul Bakker1b57b062011-01-06 15:48:19 +00003635int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3636{
3637 int ret;
3638
Paul Bakker48916f92012-09-16 19:57:18 +00003639 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003640 {
3641 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3642 return( ret );
3643 }
3644
Paul Bakker48916f92012-09-16 19:57:18 +00003645 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003646 {
3647 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3648 return( ret );
3649 }
3650
3651 return( 0 );
3652}
Paul Bakker48916f92012-09-16 19:57:18 +00003653#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003654
Paul Bakker0be444a2013-08-27 21:55:01 +02003655#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003656int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003657{
3658 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003659 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003660
3661 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003662
3663 if( ssl->hostname_len + 1 == 0 )
3664 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3665
Paul Bakker6e339b52013-07-03 13:37:05 +02003666 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003667
Paul Bakkerb15b8512012-01-13 13:44:06 +00003668 if( ssl->hostname == NULL )
3669 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3670
Paul Bakker3c2122f2013-06-24 19:03:14 +02003671 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003672 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003673
Paul Bakker40ea7de2009-05-03 10:18:48 +00003674 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003675
3676 return( 0 );
3677}
3678
Paul Bakker5701cdc2012-09-27 21:49:42 +00003679void ssl_set_sni( ssl_context *ssl,
3680 int (*f_sni)(void *, ssl_context *,
3681 const unsigned char *, size_t),
3682 void *p_sni )
3683{
3684 ssl->f_sni = f_sni;
3685 ssl->p_sni = p_sni;
3686}
Paul Bakker0be444a2013-08-27 21:55:01 +02003687#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003688
Paul Bakker490ecc82011-10-06 13:04:09 +00003689void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3690{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003691 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3692 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3693 {
3694 ssl->max_major_ver = major;
3695 ssl->max_minor_ver = minor;
3696 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003697}
3698
Paul Bakker1d29fb52012-09-28 13:28:45 +00003699void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3700{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003701 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3702 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3703 {
3704 ssl->min_major_ver = major;
3705 ssl->min_minor_ver = minor;
3706 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003707}
3708
Paul Bakker05decb22013-08-15 13:33:48 +02003709#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003710int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3711{
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003712 if( mfl_code >= sizeof( mfl_code_to_length ) ||
3713 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003714 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003715 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003716 }
3717
3718 ssl->mfl_code = mfl_code;
3719
3720 return( 0 );
3721}
Paul Bakker05decb22013-08-15 13:33:48 +02003722#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003723
Paul Bakker1f2bc622013-08-15 13:45:55 +02003724#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003725int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003726{
3727 if( ssl->endpoint != SSL_IS_CLIENT )
3728 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3729
Paul Bakker8c1ede62013-07-19 14:14:37 +02003730 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003731
3732 return( 0 );
3733}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003734#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003735
Paul Bakker48916f92012-09-16 19:57:18 +00003736void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3737{
3738 ssl->disable_renegotiation = renegotiation;
3739}
3740
3741void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3742{
3743 ssl->allow_legacy_renegotiation = allow_legacy;
3744}
3745
Paul Bakkera503a632013-08-14 13:48:06 +02003746#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003747int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3748{
3749 ssl->session_tickets = use_tickets;
3750
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003751 if( ssl->endpoint == SSL_IS_CLIENT )
3752 return( 0 );
3753
3754 if( ssl->f_rng == NULL )
3755 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3756
3757 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003758}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003759
3760void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3761{
3762 ssl->ticket_lifetime = lifetime;
3763}
Paul Bakkera503a632013-08-14 13:48:06 +02003764#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003765
Paul Bakker5121ce52009-01-03 21:22:43 +00003766/*
3767 * SSL get accessors
3768 */
Paul Bakker23986e52011-04-24 08:57:21 +00003769size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003770{
3771 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3772}
3773
Paul Bakkerff60ee62010-03-16 21:09:09 +00003774int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003775{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003776 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777}
3778
Paul Bakkere3166ce2011-01-27 17:40:50 +00003779const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003780{
Paul Bakker926c8e42013-03-06 10:23:34 +01003781 if( ssl == NULL || ssl->session == NULL )
3782 return NULL;
3783
Paul Bakkere3166ce2011-01-27 17:40:50 +00003784 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003785}
3786
Paul Bakker43ca69c2011-01-15 17:35:19 +00003787const char *ssl_get_version( const ssl_context *ssl )
3788{
3789 switch( ssl->minor_ver )
3790 {
3791 case SSL_MINOR_VERSION_0:
3792 return( "SSLv3.0" );
3793
3794 case SSL_MINOR_VERSION_1:
3795 return( "TLSv1.0" );
3796
3797 case SSL_MINOR_VERSION_2:
3798 return( "TLSv1.1" );
3799
Paul Bakker1ef83d62012-04-11 12:09:53 +00003800 case SSL_MINOR_VERSION_3:
3801 return( "TLSv1.2" );
3802
Paul Bakker43ca69c2011-01-15 17:35:19 +00003803 default:
3804 break;
3805 }
3806 return( "unknown" );
3807}
3808
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003809#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003810const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003811{
3812 if( ssl == NULL || ssl->session == NULL )
3813 return NULL;
3814
3815 return ssl->session->peer_cert;
3816}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003817#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003818
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003819int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3820{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003821 if( ssl == NULL ||
3822 dst == NULL ||
3823 ssl->session == NULL ||
3824 ssl->endpoint != SSL_IS_CLIENT )
3825 {
3826 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3827 }
3828
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003829 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003830}
3831
Paul Bakker5121ce52009-01-03 21:22:43 +00003832/*
Paul Bakker1961b702013-01-25 14:49:24 +01003833 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003834 */
Paul Bakker1961b702013-01-25 14:49:24 +01003835int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003836{
Paul Bakker40e46942009-01-03 21:51:57 +00003837 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003838
Paul Bakker40e46942009-01-03 21:51:57 +00003839#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01003841 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003842#endif
3843
Paul Bakker40e46942009-01-03 21:51:57 +00003844#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003845 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003846 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003847#endif
3848
Paul Bakker1961b702013-01-25 14:49:24 +01003849 return( ret );
3850}
3851
3852/*
3853 * Perform the SSL handshake
3854 */
3855int ssl_handshake( ssl_context *ssl )
3856{
3857 int ret = 0;
3858
3859 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
3860
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02003861#if defined(POLARSSL_X509_CRT_PARSE_C)
3862 ssl->handshake->key_cert = ssl->key_cert;
3863#endif
3864
Paul Bakker1961b702013-01-25 14:49:24 +01003865 while( ssl->state != SSL_HANDSHAKE_OVER )
3866 {
3867 ret = ssl_handshake_step( ssl );
3868
3869 if( ret != 0 )
3870 break;
3871 }
3872
Paul Bakker5121ce52009-01-03 21:22:43 +00003873 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
3874
3875 return( ret );
3876}
3877
3878/*
Paul Bakker48916f92012-09-16 19:57:18 +00003879 * Renegotiate current connection
3880 */
3881int ssl_renegotiate( ssl_context *ssl )
3882{
3883 int ret;
3884
3885 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
3886
3887 if( ssl->state != SSL_HANDSHAKE_OVER )
3888 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3889
3890 ssl->state = SSL_HELLO_REQUEST;
3891 ssl->renegotiation = SSL_RENEGOTIATION;
3892
3893 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3894 return( ret );
3895
3896 if( ( ret = ssl_handshake( ssl ) ) != 0 )
3897 {
3898 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
3899 return( ret );
3900 }
3901
3902 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
3903
3904 return( 0 );
3905}
3906
3907/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003908 * Receive application data decrypted from the SSL layer
3909 */
Paul Bakker23986e52011-04-24 08:57:21 +00003910int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00003911{
Paul Bakker23986e52011-04-24 08:57:21 +00003912 int ret;
3913 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003914
3915 SSL_DEBUG_MSG( 2, ( "=> read" ) );
3916
3917 if( ssl->state != SSL_HANDSHAKE_OVER )
3918 {
3919 if( ( ret = ssl_handshake( ssl ) ) != 0 )
3920 {
3921 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
3922 return( ret );
3923 }
3924 }
3925
3926 if( ssl->in_offt == NULL )
3927 {
3928 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3929 {
Paul Bakker831a7552011-05-18 13:32:51 +00003930 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
3931 return( 0 );
3932
Paul Bakker5121ce52009-01-03 21:22:43 +00003933 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3934 return( ret );
3935 }
3936
3937 if( ssl->in_msglen == 0 &&
3938 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
3939 {
3940 /*
3941 * OpenSSL sends empty messages to randomize the IV
3942 */
3943 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3944 {
Paul Bakker831a7552011-05-18 13:32:51 +00003945 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
3946 return( 0 );
3947
Paul Bakker5121ce52009-01-03 21:22:43 +00003948 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3949 return( ret );
3950 }
3951 }
3952
Paul Bakker48916f92012-09-16 19:57:18 +00003953 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3954 {
3955 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
3956
3957 if( ssl->endpoint == SSL_IS_CLIENT &&
3958 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
3959 ssl->in_hslen != 4 ) )
3960 {
3961 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
3962 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
3963 }
3964
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003965 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
3966 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
3967 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003968 {
3969 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
3970
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003971#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003972 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003973 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003974 /*
3975 * SSLv3 does not have a "no_renegotiation" alert
3976 */
3977 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
3978 return( ret );
3979 }
3980 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003981#endif
3982#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3983 defined(POLARSSL_SSL_PROTO_TLS1_2)
3984 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003985 {
3986 if( ( ret = ssl_send_alert_message( ssl,
3987 SSL_ALERT_LEVEL_WARNING,
3988 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
3989 {
3990 return( ret );
3991 }
Paul Bakker48916f92012-09-16 19:57:18 +00003992 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003993 else
3994#endif
Paul Bakker577e0062013-08-28 11:57:20 +02003995 {
3996 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003997 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02003998 }
Paul Bakker48916f92012-09-16 19:57:18 +00003999 }
4000 else
4001 {
4002 if( ( ret = ssl_renegotiate( ssl ) ) != 0 )
4003 {
4004 SSL_DEBUG_RET( 1, "ssl_renegotiate", ret );
4005 return( ret );
4006 }
4007
4008 return( POLARSSL_ERR_NET_WANT_READ );
4009 }
4010 }
4011 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004012 {
4013 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004014 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015 }
4016
4017 ssl->in_offt = ssl->in_msg;
4018 }
4019
4020 n = ( len < ssl->in_msglen )
4021 ? len : ssl->in_msglen;
4022
4023 memcpy( buf, ssl->in_offt, n );
4024 ssl->in_msglen -= n;
4025
4026 if( ssl->in_msglen == 0 )
4027 /* all bytes consumed */
4028 ssl->in_offt = NULL;
4029 else
4030 /* more data available */
4031 ssl->in_offt += n;
4032
4033 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4034
Paul Bakker23986e52011-04-24 08:57:21 +00004035 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004036}
4037
4038/*
4039 * Send application data to be encrypted by the SSL layer
4040 */
Paul Bakker23986e52011-04-24 08:57:21 +00004041int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004042{
Paul Bakker23986e52011-04-24 08:57:21 +00004043 int ret;
4044 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004045 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004046
4047 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4048
4049 if( ssl->state != SSL_HANDSHAKE_OVER )
4050 {
4051 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4052 {
4053 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4054 return( ret );
4055 }
4056 }
4057
Paul Bakker05decb22013-08-15 13:33:48 +02004058#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004059 /*
4060 * Assume mfl_code is correct since it was checked when set
4061 */
4062 max_len = mfl_code_to_length[ssl->mfl_code];
4063
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004064 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004065 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004066 */
4067 if( ssl->session_out != NULL &&
4068 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4069 {
4070 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4071 }
Paul Bakker05decb22013-08-15 13:33:48 +02004072#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004073
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004074 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004075
Paul Bakker5121ce52009-01-03 21:22:43 +00004076 if( ssl->out_left != 0 )
4077 {
4078 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4079 {
4080 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4081 return( ret );
4082 }
4083 }
Paul Bakker887bd502011-06-08 13:10:54 +00004084 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004085 {
Paul Bakker887bd502011-06-08 13:10:54 +00004086 ssl->out_msglen = n;
4087 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4088 memcpy( ssl->out_msg, buf, n );
4089
4090 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4091 {
4092 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4093 return( ret );
4094 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004095 }
4096
4097 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4098
Paul Bakker23986e52011-04-24 08:57:21 +00004099 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004100}
4101
4102/*
4103 * Notify the peer that the connection is being closed
4104 */
4105int ssl_close_notify( ssl_context *ssl )
4106{
4107 int ret;
4108
4109 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4110
4111 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4112 {
4113 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4114 return( ret );
4115 }
4116
4117 if( ssl->state == SSL_HANDSHAKE_OVER )
4118 {
Paul Bakker48916f92012-09-16 19:57:18 +00004119 if( ( ret = ssl_send_alert_message( ssl,
4120 SSL_ALERT_LEVEL_WARNING,
4121 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004122 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004123 return( ret );
4124 }
4125 }
4126
4127 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4128
4129 return( ret );
4130}
4131
Paul Bakker48916f92012-09-16 19:57:18 +00004132void ssl_transform_free( ssl_transform *transform )
4133{
4134#if defined(POLARSSL_ZLIB_SUPPORT)
4135 deflateEnd( &transform->ctx_deflate );
4136 inflateEnd( &transform->ctx_inflate );
4137#endif
4138
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004139 cipher_free_ctx( &transform->cipher_ctx_enc );
4140 cipher_free_ctx( &transform->cipher_ctx_dec );
4141
Paul Bakker61d113b2013-07-04 11:51:43 +02004142 md_free_ctx( &transform->md_ctx_enc );
4143 md_free_ctx( &transform->md_ctx_dec );
4144
Paul Bakker48916f92012-09-16 19:57:18 +00004145 memset( transform, 0, sizeof( ssl_transform ) );
4146}
4147
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004148#if defined(POLARSSL_X509_CRT_PARSE_C)
4149static void ssl_key_cert_free( ssl_key_cert *key_cert )
4150{
4151 ssl_key_cert *cur = key_cert, *next;
4152
4153 while( cur != NULL )
4154 {
4155 next = cur->next;
4156
4157 if( cur->key_own_alloc )
4158 {
4159 pk_free( cur->key );
4160 polarssl_free( cur->key );
4161 }
4162 polarssl_free( cur );
4163
4164 cur = next;
4165 }
4166}
4167#endif /* POLARSSL_X509_CRT_PARSE_C */
4168
Paul Bakker48916f92012-09-16 19:57:18 +00004169void ssl_handshake_free( ssl_handshake_params *handshake )
4170{
4171#if defined(POLARSSL_DHM_C)
4172 dhm_free( &handshake->dhm_ctx );
4173#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004174#if defined(POLARSSL_ECDH_C)
4175 ecdh_free( &handshake->ecdh_ctx );
4176#endif
4177
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004178#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerbeccd9f2013-10-11 15:20:27 +02004179 /* explicit void pointer cast for buggy MS compiler */
4180 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004181#endif
4182
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004183#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4184 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4185 /*
4186 * Free only the linked list wrapper, not the keys themselves
4187 * since the belong to the SNI callback
4188 */
4189 if( handshake->sni_key_cert != NULL )
4190 {
4191 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4192
4193 while( cur != NULL )
4194 {
4195 next = cur->next;
4196 polarssl_free( cur );
4197 cur = next;
4198 }
4199 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004200#endif
4201
Paul Bakker48916f92012-09-16 19:57:18 +00004202 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4203}
4204
4205void ssl_session_free( ssl_session *session )
4206{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004207#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004208 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004209 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004210 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004211 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004212 }
Paul Bakkered27a042013-04-18 22:46:23 +02004213#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004214
Paul Bakkera503a632013-08-14 13:48:06 +02004215#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004216 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004217#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004218
Paul Bakker0a597072012-09-25 21:55:46 +00004219 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004220}
4221
Paul Bakker5121ce52009-01-03 21:22:43 +00004222/*
4223 * Free an SSL context
4224 */
4225void ssl_free( ssl_context *ssl )
4226{
4227 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4228
Paul Bakker5121ce52009-01-03 21:22:43 +00004229 if( ssl->out_ctr != NULL )
4230 {
4231 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004232 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004233 }
4234
4235 if( ssl->in_ctr != NULL )
4236 {
4237 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004238 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004239 }
4240
Paul Bakker16770332013-10-11 09:59:44 +02004241#if defined(POLARSSL_ZLIB_SUPPORT)
4242 if( ssl->compress_buf != NULL )
4243 {
4244 memset( ssl->compress_buf, 0, SSL_BUFFER_LEN );
4245 polarssl_free( ssl->compress_buf );
4246 }
4247#endif
4248
Paul Bakker40e46942009-01-03 21:51:57 +00004249#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004250 mpi_free( &ssl->dhm_P );
4251 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004252#endif
4253
Paul Bakker48916f92012-09-16 19:57:18 +00004254 if( ssl->transform )
4255 {
4256 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004257 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004258 }
4259
4260 if( ssl->handshake )
4261 {
4262 ssl_handshake_free( ssl->handshake );
4263 ssl_transform_free( ssl->transform_negotiate );
4264 ssl_session_free( ssl->session_negotiate );
4265
Paul Bakker6e339b52013-07-03 13:37:05 +02004266 polarssl_free( ssl->handshake );
4267 polarssl_free( ssl->transform_negotiate );
4268 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004269 }
4270
Paul Bakkerc0463502013-02-14 11:19:38 +01004271 if( ssl->session )
4272 {
4273 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004274 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004275 }
4276
Paul Bakkera503a632013-08-14 13:48:06 +02004277#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004278 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004279#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004280
Paul Bakker0be444a2013-08-27 21:55:01 +02004281#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004282 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004283 {
4284 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004285 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004286 ssl->hostname_len = 0;
4287 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004288#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004289
Paul Bakker6db455e2013-09-18 17:29:31 +02004290#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
4291 if( ssl->psk != NULL )
4292 {
4293 memset( ssl->psk, 0, ssl->psk_len );
4294 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4295 polarssl_free( ssl->psk );
4296 polarssl_free( ssl->psk_identity );
4297 ssl->psk_len = 0;
4298 ssl->psk_identity_len = 0;
4299 }
4300#endif
4301
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004302#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004303 ssl_key_cert_free( ssl->key_cert );
4304#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004305
Paul Bakker05ef8352012-05-08 09:17:57 +00004306#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4307 if( ssl_hw_record_finish != NULL )
4308 {
4309 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4310 ssl_hw_record_finish( ssl );
4311 }
4312#endif
4313
Paul Bakker5121ce52009-01-03 21:22:43 +00004314 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004315
Paul Bakker86f04f42013-02-14 11:20:09 +01004316 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004317 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004318}
4319
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004320#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004321/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004322 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004323 */
4324unsigned char ssl_sig_from_pk( pk_context *pk )
4325{
4326#if defined(POLARSSL_RSA_C)
4327 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4328 return( SSL_SIG_RSA );
4329#endif
4330#if defined(POLARSSL_ECDSA_C)
4331 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4332 return( SSL_SIG_ECDSA );
4333#endif
4334 return( SSL_SIG_ANON );
4335}
4336
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004337pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4338{
4339 switch( sig )
4340 {
4341#if defined(POLARSSL_RSA_C)
4342 case SSL_SIG_RSA:
4343 return( POLARSSL_PK_RSA );
4344#endif
4345#if defined(POLARSSL_ECDSA_C)
4346 case SSL_SIG_ECDSA:
4347 return( POLARSSL_PK_ECDSA );
4348#endif
4349 default:
4350 return( POLARSSL_PK_NONE );
4351 }
4352}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004353#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004354
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004355/*
4356 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4357 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004358md_type_t ssl_md_alg_from_hash( unsigned char hash )
4359{
4360 switch( hash )
4361 {
4362#if defined(POLARSSL_MD5_C)
4363 case SSL_HASH_MD5:
4364 return( POLARSSL_MD_MD5 );
4365#endif
4366#if defined(POLARSSL_SHA1_C)
4367 case SSL_HASH_SHA1:
4368 return( POLARSSL_MD_SHA1 );
4369#endif
4370#if defined(POLARSSL_SHA256_C)
4371 case SSL_HASH_SHA224:
4372 return( POLARSSL_MD_SHA224 );
4373 case SSL_HASH_SHA256:
4374 return( POLARSSL_MD_SHA256 );
4375#endif
4376#if defined(POLARSSL_SHA512_C)
4377 case SSL_HASH_SHA384:
4378 return( POLARSSL_MD_SHA384 );
4379 case SSL_HASH_SHA512:
4380 return( POLARSSL_MD_SHA512 );
4381#endif
4382 default:
4383 return( POLARSSL_MD_NONE );
4384 }
4385}
4386
Paul Bakker5121ce52009-01-03 21:22:43 +00004387#endif