blob: d3b4ce3a0924eb6289325369c30d8da5858d2a1a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker68884e32013-01-07 18:20:04 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker0be444a2013-08-27 21:55:01 +020038#include "polarssl/debug.h"
39#include "polarssl/ssl.h"
40
Paul Bakker6e339b52013-07-03 13:37:05 +020041#if defined(POLARSSL_MEMORY_C)
42#include "polarssl/memory.h"
43#else
44#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Paul Bakker5121ce52009-01-03 21:22:43 +000048#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000050#if defined _MSC_VER && !defined strcasecmp
51#define strcasecmp _stricmp
52#endif
53
Paul Bakker05decb22013-08-15 13:33:48 +020054#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020055/*
56 * Convert max_fragment_length codes to length.
57 * RFC 6066 says:
58 * enum{
59 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
60 * } MaxFragmentLength;
61 * and we add 0 -> extension unused
62 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020063static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020064{
65 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
66 512, /* SSL_MAX_FRAG_LEN_512 */
67 1024, /* SSL_MAX_FRAG_LEN_1024 */
68 2048, /* SSL_MAX_FRAG_LEN_2048 */
69 4096, /* SSL_MAX_FRAG_LEN_4096 */
70};
Paul Bakker05decb22013-08-15 13:33:48 +020071#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020072
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020073static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
74{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020075 ssl_session_free( dst );
76 memcpy( dst, src, sizeof( ssl_session ) );
77
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020079 if( src->peer_cert != NULL )
80 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020081 int ret;
82
Paul Bakkerc559c7a2013-09-18 14:13:26 +020083 if( ( dst->peer_cert = polarssl_malloc( sizeof(x509_crt) ) ) == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020084 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
85
Paul Bakkerb6b09562013-09-18 14:17:41 +020086 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020087
Paul Bakkerddf26b42013-09-18 13:46:23 +020088 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
89 src->peer_cert->raw.len ) != 0 ) )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 {
91 polarssl_free( dst->peer_cert );
92 dst->peer_cert = NULL;
93 return( ret );
94 }
95 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020097
Paul Bakkera503a632013-08-14 13:48:06 +020098#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020099 if( src->ticket != NULL )
100 {
101 if( ( dst->ticket = polarssl_malloc( src->ticket_len ) ) == NULL )
102 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
103
104 memcpy( dst->ticket, src->ticket, src->ticket_len );
105 }
Paul Bakkera503a632013-08-14 13:48:06 +0200106#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200107
108 return( 0 );
109}
110
Paul Bakker05ef8352012-05-08 09:17:57 +0000111#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
112int (*ssl_hw_record_init)(ssl_context *ssl,
113 const unsigned char *key_enc, const unsigned char *key_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100114 size_t keylen,
Paul Bakker05ef8352012-05-08 09:17:57 +0000115 const unsigned char *iv_enc, const unsigned char *iv_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100116 size_t ivlen,
117 const unsigned char *mac_enc, const unsigned char *mac_dec,
118 size_t maclen) = NULL;
119int (*ssl_hw_record_activate)(ssl_context *ssl, int direction) = NULL;
Paul Bakker05ef8352012-05-08 09:17:57 +0000120int (*ssl_hw_record_reset)(ssl_context *ssl) = NULL;
121int (*ssl_hw_record_write)(ssl_context *ssl) = NULL;
122int (*ssl_hw_record_read)(ssl_context *ssl) = NULL;
123int (*ssl_hw_record_finish)(ssl_context *ssl) = NULL;
124#endif
125
Paul Bakker5121ce52009-01-03 21:22:43 +0000126/*
127 * Key material generation
128 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200129#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200130static int ssl3_prf( const unsigned char *secret, size_t slen,
131 const char *label,
132 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000133 unsigned char *dstbuf, size_t dlen )
134{
135 size_t i;
136 md5_context md5;
137 sha1_context sha1;
138 unsigned char padding[16];
139 unsigned char sha1sum[20];
140 ((void)label);
141
142 /*
143 * SSLv3:
144 * block =
145 * MD5( secret + SHA1( 'A' + secret + random ) ) +
146 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
147 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
148 * ...
149 */
150 for( i = 0; i < dlen / 16; i++ )
151 {
152 memset( padding, 'A' + i, 1 + i );
153
154 sha1_starts( &sha1 );
155 sha1_update( &sha1, padding, 1 + i );
156 sha1_update( &sha1, secret, slen );
157 sha1_update( &sha1, random, rlen );
158 sha1_finish( &sha1, sha1sum );
159
160 md5_starts( &md5 );
161 md5_update( &md5, secret, slen );
162 md5_update( &md5, sha1sum, 20 );
163 md5_finish( &md5, dstbuf + i * 16 );
164 }
165
166 memset( &md5, 0, sizeof( md5 ) );
167 memset( &sha1, 0, sizeof( sha1 ) );
168
169 memset( padding, 0, sizeof( padding ) );
170 memset( sha1sum, 0, sizeof( sha1sum ) );
171
172 return( 0 );
173}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200174#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000175
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200176#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200177static int tls1_prf( const unsigned char *secret, size_t slen,
178 const char *label,
179 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000180 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000181{
Paul Bakker23986e52011-04-24 08:57:21 +0000182 size_t nb, hs;
183 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200184 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 unsigned char tmp[128];
186 unsigned char h_i[20];
187
188 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000189 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191 hs = ( slen + 1 ) / 2;
192 S1 = secret;
193 S2 = secret + slen - hs;
194
195 nb = strlen( label );
196 memcpy( tmp + 20, label, nb );
197 memcpy( tmp + 20 + nb, random, rlen );
198 nb += rlen;
199
200 /*
201 * First compute P_md5(secret,label+random)[0..dlen]
202 */
203 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
204
205 for( i = 0; i < dlen; i += 16 )
206 {
207 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
208 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
209
210 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
211
212 for( j = 0; j < k; j++ )
213 dstbuf[i + j] = h_i[j];
214 }
215
216 /*
217 * XOR out with P_sha1(secret,label+random)[0..dlen]
218 */
219 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
220
221 for( i = 0; i < dlen; i += 20 )
222 {
223 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
224 sha1_hmac( S2, hs, tmp, 20, tmp );
225
226 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
227
228 for( j = 0; j < k; j++ )
229 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
230 }
231
232 memset( tmp, 0, sizeof( tmp ) );
233 memset( h_i, 0, sizeof( h_i ) );
234
235 return( 0 );
236}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200237#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200239#if defined(POLARSSL_SSL_PROTO_TLS1_2)
240#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200241static int tls_prf_sha256( const unsigned char *secret, size_t slen,
242 const char *label,
243 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000244 unsigned char *dstbuf, size_t dlen )
245{
246 size_t nb;
247 size_t i, j, k;
248 unsigned char tmp[128];
249 unsigned char h_i[32];
250
251 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
252 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
253
254 nb = strlen( label );
255 memcpy( tmp + 32, label, nb );
256 memcpy( tmp + 32 + nb, random, rlen );
257 nb += rlen;
258
259 /*
260 * Compute P_<hash>(secret, label + random)[0..dlen]
261 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200262 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000263
264 for( i = 0; i < dlen; i += 32 )
265 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200266 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
267 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000268
269 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
270
271 for( j = 0; j < k; j++ )
272 dstbuf[i + j] = h_i[j];
273 }
274
275 memset( tmp, 0, sizeof( tmp ) );
276 memset( h_i, 0, sizeof( h_i ) );
277
278 return( 0 );
279}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200280#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000281
Paul Bakker9e36f042013-06-30 14:34:05 +0200282#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200283static int tls_prf_sha384( const unsigned char *secret, size_t slen,
284 const char *label,
285 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000286 unsigned char *dstbuf, size_t dlen )
287{
288 size_t nb;
289 size_t i, j, k;
290 unsigned char tmp[128];
291 unsigned char h_i[48];
292
293 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
294 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
295
296 nb = strlen( label );
297 memcpy( tmp + 48, label, nb );
298 memcpy( tmp + 48 + nb, random, rlen );
299 nb += rlen;
300
301 /*
302 * Compute P_<hash>(secret, label + random)[0..dlen]
303 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200304 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000305
306 for( i = 0; i < dlen; i += 48 )
307 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200308 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
309 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000310
311 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
312
313 for( j = 0; j < k; j++ )
314 dstbuf[i + j] = h_i[j];
315 }
316
317 memset( tmp, 0, sizeof( tmp ) );
318 memset( h_i, 0, sizeof( h_i ) );
319
320 return( 0 );
321}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200322#endif /* POLARSSL_SHA512_C */
323#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000324
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200325static void ssl_update_checksum_start(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200326
327#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
328 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200329static void ssl_update_checksum_md5sha1(ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200330#endif
Paul Bakker380da532012-04-18 16:10:25 +0000331
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200332#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000333static void ssl_calc_verify_ssl(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000334static void ssl_calc_finished_ssl(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200335#endif
336
337#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
338static void ssl_calc_verify_tls(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339static void ssl_calc_finished_tls(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200340#endif
341
342#if defined(POLARSSL_SSL_PROTO_TLS1_2)
343#if defined(POLARSSL_SHA256_C)
344static void ssl_update_checksum_sha256(ssl_context *, const unsigned char *, size_t);
345static void ssl_calc_verify_tls_sha256(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000346static void ssl_calc_finished_tls_sha256(ssl_context *,unsigned char *,int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100348
Paul Bakker9e36f042013-06-30 14:34:05 +0200349#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200350static void ssl_update_checksum_sha384(ssl_context *, const unsigned char *, size_t);
Paul Bakker769075d2012-11-24 11:26:46 +0100351static void ssl_calc_verify_tls_sha384(ssl_context *,unsigned char *);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000352static void ssl_calc_finished_tls_sha384(ssl_context *,unsigned char *,int);
Paul Bakker769075d2012-11-24 11:26:46 +0100353#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200354#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +0000355
Paul Bakker5121ce52009-01-03 21:22:43 +0000356int ssl_derive_keys( ssl_context *ssl )
357{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200358 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 unsigned char keyblk[256];
361 unsigned char *key1;
362 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100363 unsigned char *mac_enc;
364 unsigned char *mac_dec;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000365 unsigned int iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100366 const cipher_info_t *cipher_info;
367 const md_info_t *md_info;
368
Paul Bakker48916f92012-09-16 19:57:18 +0000369 ssl_session *session = ssl->session_negotiate;
370 ssl_transform *transform = ssl->transform_negotiate;
371 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000372
373 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
374
Paul Bakker68884e32013-01-07 18:20:04 +0100375 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
376 if( cipher_info == NULL )
377 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200378 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100379 transform->ciphersuite_info->cipher ) );
380 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
381 }
382
383 md_info = md_info_from_type( transform->ciphersuite_info->mac );
384 if( md_info == NULL )
385 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200386 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100387 transform->ciphersuite_info->mac ) );
388 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
389 }
390
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000392 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000393 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200394#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000395 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000396 {
Paul Bakker48916f92012-09-16 19:57:18 +0000397 handshake->tls_prf = ssl3_prf;
398 handshake->calc_verify = ssl_calc_verify_ssl;
399 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000400 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200401 else
402#endif
403#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
404 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000405 {
Paul Bakker48916f92012-09-16 19:57:18 +0000406 handshake->tls_prf = tls1_prf;
407 handshake->calc_verify = ssl_calc_verify_tls;
408 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000409 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200410 else
411#endif
412#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200413#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
415 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = tls_prf_sha384;
418 handshake->calc_verify = ssl_calc_verify_tls_sha384;
419 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000421 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200422#endif
423#if defined(POLARSSL_SHA256_C)
424 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls_prf_sha256;
427 handshake->calc_verify = ssl_calc_verify_tls_sha256;
428 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200433 {
434 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200435 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200436 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000437
438 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 * SSLv3:
440 * master =
441 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
442 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
443 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200444 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200445 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 * master = PRF( premaster, "master secret", randbytes )[0..47]
447 */
Paul Bakker0a597072012-09-25 21:55:46 +0000448 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 {
Paul Bakker48916f92012-09-16 19:57:18 +0000450 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
451 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
Paul Bakker48916f92012-09-16 19:57:18 +0000453 handshake->tls_prf( handshake->premaster, handshake->pmslen,
454 "master secret",
455 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
Paul Bakker48916f92012-09-16 19:57:18 +0000457 memset( handshake->premaster, 0, sizeof( handshake->premaster ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 }
459 else
460 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
461
462 /*
463 * Swap the client and server random values.
464 */
Paul Bakker48916f92012-09-16 19:57:18 +0000465 memcpy( tmp, handshake->randbytes, 64 );
466 memcpy( handshake->randbytes, tmp + 32, 32 );
467 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 memset( tmp, 0, sizeof( tmp ) );
469
470 /*
471 * SSLv3:
472 * key block =
473 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
474 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
475 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
476 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
477 * ...
478 *
479 * TLSv1:
480 * key block = PRF( master, "key expansion", randbytes )
481 */
Paul Bakker48916f92012-09-16 19:57:18 +0000482 handshake->tls_prf( session->master, 48, "key expansion",
483 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Paul Bakker48916f92012-09-16 19:57:18 +0000485 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
486 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
487 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
488 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
490
Paul Bakker48916f92012-09-16 19:57:18 +0000491 memset( handshake->randbytes, 0, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493 /*
494 * Determine the appropriate key, IV and MAC length.
495 */
Paul Bakker68884e32013-01-07 18:20:04 +0100496
497 if( cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 {
Paul Bakker68884e32013-01-07 18:20:04 +0100499 transform->keylen = cipher_info->key_length;
500 transform->keylen /= 8;
501 transform->minlen = 1;
502 transform->ivlen = 12;
503 transform->fixed_ivlen = 4;
504 transform->maclen = 0;
505 }
506 else
507 {
508 if( md_info->type != POLARSSL_MD_NONE )
509 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200510 int ret;
511
Paul Bakker61d113b2013-07-04 11:51:43 +0200512 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
513 {
514 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
515 return( ret );
516 }
517
518 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
519 {
520 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
521 return( ret );
522 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
Paul Bakker68884e32013-01-07 18:20:04 +0100524 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200525
Paul Bakker1f2bc622013-08-15 13:45:55 +0200526#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200527 /*
528 * If HMAC is to be truncated, we shall keep the leftmost bytes,
529 * (rfc 6066 page 13 or rfc 2104 section 4),
530 * so we only need to adjust the length here.
531 */
532 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
533 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200534#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100535 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
Paul Bakker68884e32013-01-07 18:20:04 +0100537 transform->keylen = cipher_info->key_length;
538 transform->keylen /= 8;
539 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Paul Bakker68884e32013-01-07 18:20:04 +0100541 transform->minlen = transform->keylen;
542 if( transform->minlen < transform->maclen )
543 {
544 if( cipher_info->mode == POLARSSL_MODE_STREAM )
545 transform->minlen = transform->maclen;
546 else
547 transform->minlen += transform->keylen;
548 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 }
550
551 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000552 transform->keylen, transform->minlen, transform->ivlen,
553 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555 /*
556 * Finally setup the cipher contexts, IVs and MAC secrets.
557 */
558 if( ssl->endpoint == SSL_IS_CLIENT )
559 {
Paul Bakker48916f92012-09-16 19:57:18 +0000560 key1 = keyblk + transform->maclen * 2;
561 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
Paul Bakker68884e32013-01-07 18:20:04 +0100563 mac_enc = keyblk;
564 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000566 /*
567 * This is not used in TLS v1.1.
568 */
Paul Bakker48916f92012-09-16 19:57:18 +0000569 iv_copy_len = ( transform->fixed_ivlen ) ?
570 transform->fixed_ivlen : transform->ivlen;
571 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
572 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000573 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 }
575 else
576 {
Paul Bakker48916f92012-09-16 19:57:18 +0000577 key1 = keyblk + transform->maclen * 2 + transform->keylen;
578 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
Paul Bakker68884e32013-01-07 18:20:04 +0100580 mac_enc = keyblk + transform->maclen;
581 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000583 /*
584 * This is not used in TLS v1.1.
585 */
Paul Bakker48916f92012-09-16 19:57:18 +0000586 iv_copy_len = ( transform->fixed_ivlen ) ?
587 transform->fixed_ivlen : transform->ivlen;
588 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
589 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000590 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 }
592
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200593#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100594 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
595 {
596 memcpy( transform->mac_enc, mac_enc, transform->maclen );
597 memcpy( transform->mac_dec, mac_dec, transform->maclen );
598 }
599 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200600#endif
601#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
602 defined(POLARSSL_SSL_PROTO_TLS1_2)
603 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100604 {
605 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
606 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
607 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200608 else
609#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200610 {
611 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200612 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200613 }
Paul Bakker68884e32013-01-07 18:20:04 +0100614
Paul Bakker05ef8352012-05-08 09:17:57 +0000615#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
616 if( ssl_hw_record_init != NULL)
617 {
618 int ret = 0;
619
620 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
621
Paul Bakker07eb38b2012-12-19 14:42:06 +0100622 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
623 transform->iv_enc, transform->iv_dec,
624 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100625 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100626 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000627 {
628 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
629 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
630 }
631 }
632#endif
633
Paul Bakker68884e32013-01-07 18:20:04 +0100634 switch( cipher_info->type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 {
Paul Bakker68884e32013-01-07 18:20:04 +0100636 case POLARSSL_CIPHER_ARC4_128:
Paul Bakker68884e32013-01-07 18:20:04 +0100637 case POLARSSL_CIPHER_DES_EDE3_CBC:
Paul Bakkercca5b812013-08-31 17:40:26 +0200638 case POLARSSL_CIPHER_CAMELLIA_128_CBC:
639 case POLARSSL_CIPHER_CAMELLIA_256_CBC:
Paul Bakker68884e32013-01-07 18:20:04 +0100640 case POLARSSL_CIPHER_AES_128_CBC:
641 case POLARSSL_CIPHER_AES_256_CBC:
Paul Bakkercca5b812013-08-31 17:40:26 +0200642 case POLARSSL_CIPHER_DES_CBC:
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +0200643 case POLARSSL_CIPHER_AES_128_GCM:
644 case POLARSSL_CIPHER_AES_256_GCM:
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200645 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
646 cipher_info ) ) != 0 )
647 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200648 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200649 return( ret );
650 }
651
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200652 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
653 cipher_info ) ) != 0 )
654 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200655 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200656 return( ret );
657 }
658
Paul Bakker45125bc2013-09-04 16:47:11 +0200659 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
660 cipher_info->key_length,
661 POLARSSL_ENCRYPT ) ) != 0 )
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200662 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200663 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
664 return( ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200665 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200666
Paul Bakker45125bc2013-09-04 16:47:11 +0200667 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
668 cipher_info->key_length,
669 POLARSSL_DECRYPT ) ) != 0 )
670 {
671 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
672 return( ret );
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200673 }
674
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200675#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200676 if( cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200677 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200678 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
679 POLARSSL_PADDING_NONE ) ) != 0 )
680 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200681 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200682 return( ret );
683 }
684
685 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
686 POLARSSL_PADDING_NONE ) ) != 0 )
687 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200688 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200689 return( ret );
690 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200691 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200692#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
Paul Bakker68884e32013-01-07 18:20:04 +0100695 case POLARSSL_CIPHER_NULL:
696 break;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000697
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000699 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 }
701
702 memset( keyblk, 0, sizeof( keyblk ) );
703
Paul Bakker2770fbd2012-07-03 13:30:23 +0000704#if defined(POLARSSL_ZLIB_SUPPORT)
705 // Initialize compression
706 //
Paul Bakker48916f92012-09-16 19:57:18 +0000707 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000708 {
709 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
710
Paul Bakker48916f92012-09-16 19:57:18 +0000711 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
712 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000713
Paul Bakker48916f92012-09-16 19:57:18 +0000714 if( deflateInit( &transform->ctx_deflate, Z_DEFAULT_COMPRESSION ) != Z_OK ||
715 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000716 {
717 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
718 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
719 }
720 }
721#endif /* POLARSSL_ZLIB_SUPPORT */
722
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
724
725 return( 0 );
726}
727
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200728#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000729void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000730{
731 md5_context md5;
732 sha1_context sha1;
733 unsigned char pad_1[48];
734 unsigned char pad_2[48];
735
Paul Bakker380da532012-04-18 16:10:25 +0000736 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
Paul Bakker48916f92012-09-16 19:57:18 +0000738 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
739 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
Paul Bakker380da532012-04-18 16:10:25 +0000741 memset( pad_1, 0x36, 48 );
742 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
Paul Bakker48916f92012-09-16 19:57:18 +0000744 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000745 md5_update( &md5, pad_1, 48 );
746 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
Paul Bakker380da532012-04-18 16:10:25 +0000748 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000749 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000750 md5_update( &md5, pad_2, 48 );
751 md5_update( &md5, hash, 16 );
752 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000753
Paul Bakker48916f92012-09-16 19:57:18 +0000754 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000755 sha1_update( &sha1, pad_1, 40 );
756 sha1_finish( &sha1, hash + 16 );
757
758 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000759 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000760 sha1_update( &sha1, pad_2, 40 );
761 sha1_update( &sha1, hash + 16, 20 );
762 sha1_finish( &sha1, hash + 16 );
763
764 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
765 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
766
767 return;
768}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200769#endif
Paul Bakker380da532012-04-18 16:10:25 +0000770
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200771#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000772void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
773{
774 md5_context md5;
775 sha1_context sha1;
776
777 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
778
Paul Bakker48916f92012-09-16 19:57:18 +0000779 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
780 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000781
Paul Bakker48916f92012-09-16 19:57:18 +0000782 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000783 sha1_finish( &sha1, hash + 16 );
784
785 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
786 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
787
788 return;
789}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200790#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000791
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200792#if defined(POLARSSL_SSL_PROTO_TLS1_2)
793#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000794void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
795{
Paul Bakker9e36f042013-06-30 14:34:05 +0200796 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000797
798 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
799
Paul Bakker9e36f042013-06-30 14:34:05 +0200800 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
801 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000802
803 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
804 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
805
806 return;
807}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200808#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000809
Paul Bakker9e36f042013-06-30 14:34:05 +0200810#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000811void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
812{
Paul Bakker9e36f042013-06-30 14:34:05 +0200813 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000814
815 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
816
Paul Bakker9e36f042013-06-30 14:34:05 +0200817 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
818 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000819
Paul Bakkerca4ab492012-04-18 14:23:57 +0000820 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
822
823 return;
824}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200825#endif /* POLARSSL_SHA512_C */
826#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000827
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000829/*
830 * SSLv3.0 MAC functions
831 */
Paul Bakker68884e32013-01-07 18:20:04 +0100832static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
833 unsigned char *buf, size_t len,
834 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000835{
836 unsigned char header[11];
837 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100838 int padlen = 0;
839 int md_size = md_get_size( md_ctx->md_info );
840 int md_type = md_get_type( md_ctx->md_info );
841
842 if( md_type == POLARSSL_MD_MD5 )
843 padlen = 48;
844 else if( md_type == POLARSSL_MD_SHA1 )
845 padlen = 40;
846 else if( md_type == POLARSSL_MD_SHA256 )
847 padlen = 32;
Paul Bakker5121ce52009-01-03 21:22:43 +0000848
849 memcpy( header, ctr, 8 );
850 header[ 8] = (unsigned char) type;
851 header[ 9] = (unsigned char)( len >> 8 );
852 header[10] = (unsigned char)( len );
853
Paul Bakker68884e32013-01-07 18:20:04 +0100854 memset( padding, 0x36, padlen );
855 md_starts( md_ctx );
856 md_update( md_ctx, secret, md_size );
857 md_update( md_ctx, padding, padlen );
858 md_update( md_ctx, header, 11 );
859 md_update( md_ctx, buf, len );
860 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000861
Paul Bakker68884e32013-01-07 18:20:04 +0100862 memset( padding, 0x5C, padlen );
863 md_starts( md_ctx );
864 md_update( md_ctx, secret, md_size );
865 md_update( md_ctx, padding, padlen );
866 md_update( md_ctx, buf + len, md_size );
867 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000868}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200869#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000870
Paul Bakker5121ce52009-01-03 21:22:43 +0000871/*
872 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200873 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000874static int ssl_encrypt_buf( ssl_context *ssl )
875{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200876 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000877
878 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
879
880 /*
881 * Add MAC then encrypt
882 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200883#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000884 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
885 {
Paul Bakker68884e32013-01-07 18:20:04 +0100886 ssl_mac( &ssl->transform_out->md_ctx_enc,
887 ssl->transform_out->mac_enc,
888 ssl->out_msg, ssl->out_msglen,
889 ssl->out_ctr, ssl->out_msgtype );
Paul Bakker5121ce52009-01-03 21:22:43 +0000890 }
891 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200892#endif
893#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
894 defined(POLARSSL_SSL_PROTO_TLS1_2)
895 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000896 {
Paul Bakker68884e32013-01-07 18:20:04 +0100897 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
898 md_hmac_update( &ssl->transform_out->md_ctx_enc,
899 ssl->out_msg, ssl->out_msglen );
900 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
901 ssl->out_msg + ssl->out_msglen );
902 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +0000903 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200904 else
905#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200906 {
907 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200908 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +0200909 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
911 SSL_DEBUG_BUF( 4, "computed mac",
Paul Bakker48916f92012-09-16 19:57:18 +0000912 ssl->out_msg + ssl->out_msglen, ssl->transform_out->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Paul Bakker48916f92012-09-16 19:57:18 +0000914 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
Paul Bakker68884e32013-01-07 18:20:04 +0100916#if defined(POLARSSL_CIPHER_NULL_CIPHER)
917 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
918 {
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200919 ; /* Nothing to do */
Paul Bakker68884e32013-01-07 18:20:04 +0100920 }
921 else
922#endif /* POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200923#if defined(POLARSSL_ARC4_C)
Paul Bakker68884e32013-01-07 18:20:04 +0100924 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200926 int ret;
927 size_t olen = 0;
928
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
930 "including %d bytes of padding",
931 ssl->out_msglen, 0 ) );
932
933 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
934 ssl->out_msg, ssl->out_msglen );
935
Paul Bakker45125bc2013-09-04 16:47:11 +0200936 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200937 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200938 SSL_DEBUG_RET( 1, "cipher_reset", ret );
939 return( ret );
940 }
941
942 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
943 ssl->transform_out->iv_enc,
944 ssl->transform_out->ivlen ) ) != 0 )
945 {
946 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200947 return( ret );
948 }
949
950 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
951 ssl->out_msg, ssl->out_msglen, ssl->out_msg,
952 &olen ) ) != 0 )
953 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200954 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200955 return( ret );
956 }
957
958 if( ssl->out_msglen != olen )
959 {
960 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
961 ssl->out_msglen, olen ) );
962 // TODO Real error number
963 return( -1 );
964 }
965
966 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +0200967 ssl->out_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200968 {
Paul Bakker45125bc2013-09-04 16:47:11 +0200969 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200970 return( ret );
971 }
972
973 if( 0 != olen )
974 {
975 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
976 0, olen ) );
977 // TODO Real error number
978 return( -1 );
979 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 }
Paul Bakker68884e32013-01-07 18:20:04 +0100981 else
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200982#endif /* POLARSSL_ARC4_C */
Paul Bakker68884e32013-01-07 18:20:04 +0100983#if defined(POLARSSL_GCM_C)
984 if( ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
985 ssl->transform_out->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000986 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200987 size_t enc_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000988 unsigned char *enc_msg;
989 unsigned char add_data[13];
990 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
991
Paul Bakkerca4ab492012-04-18 14:23:57 +0000992 enc_msglen = ssl->out_msglen;
993
994 memcpy( add_data, ssl->out_ctr, 8 );
995 add_data[8] = ssl->out_msgtype;
996 add_data[9] = ssl->major_ver;
997 add_data[10] = ssl->minor_ver;
998 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
999 add_data[12] = ssl->out_msglen & 0xFF;
1000
1001 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1002 add_data, 13 );
1003
Paul Bakker68884e32013-01-07 18:20:04 +01001004 /*
1005 * Generate IV
1006 */
1007 ret = ssl->f_rng( ssl->p_rng,
Paul Bakker48916f92012-09-16 19:57:18 +00001008 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
Paul Bakker68884e32013-01-07 18:20:04 +01001009 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1010 if( ret != 0 )
1011 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012
Paul Bakker68884e32013-01-07 18:20:04 +01001013 memcpy( ssl->out_iv,
1014 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1015 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001016
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001017 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
1018 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
1019
Paul Bakker68884e32013-01-07 18:20:04 +01001020 /*
1021 * Fix pointer positions and message length with added IV
1022 */
1023 enc_msg = ssl->out_msg;
1024 enc_msglen = ssl->out_msglen;
1025 ssl->out_msglen += ssl->transform_out->ivlen -
1026 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001027
Paul Bakker68884e32013-01-07 18:20:04 +01001028 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1029 "including %d bytes of padding",
1030 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001031
Paul Bakker68884e32013-01-07 18:20:04 +01001032 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1033 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001034
Paul Bakker68884e32013-01-07 18:20:04 +01001035 /*
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001036 * Encrypt
1037 */
1038 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1039 ssl->transform_out->iv_enc,
1040 ssl->transform_out->ivlen ) ) != 0 ||
1041 ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
1042 {
1043 return( ret );
1044 }
1045
1046 if( ( ret = cipher_update_ad( &ssl->transform_out->cipher_ctx_enc,
1047 add_data, 13 ) ) != 0 )
1048 {
1049 return( ret );
1050 }
1051
1052 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1053 enc_msg, enc_msglen,
1054 enc_msg, &olen ) ) != 0 )
1055 {
1056 return( ret );
1057 }
1058 totlen = olen;
1059
1060 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
1061 enc_msg + olen, &olen ) ) != 0 )
1062 {
1063 return( ret );
1064 }
1065 totlen += olen;
1066
1067 if( totlen != enc_msglen )
1068 {
1069 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1070 return( -1 );
1071 }
1072
1073 /*
1074 * Authenticate
Paul Bakker68884e32013-01-07 18:20:04 +01001075 */
1076 ssl->out_msglen += 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001077
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001078 if( ( ret = cipher_write_tag( &ssl->transform_out->cipher_ctx_enc,
1079 enc_msg + enc_msglen, 16 ) ) != 0 )
1080 {
1081 return( ret );
1082 }
Paul Bakker68884e32013-01-07 18:20:04 +01001083
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001084 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, 16 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001085 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 else
Paul Bakker68884e32013-01-07 18:20:04 +01001087#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001088#if defined(POLARSSL_CIPHER_MODE_CBC)
1089 if( ssl->transform_out->cipher_ctx_enc.cipher_info->mode ==
1090 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001092 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001093 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001094 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001095
Paul Bakker48916f92012-09-16 19:57:18 +00001096 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1097 ssl->transform_out->ivlen;
1098 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 padlen = 0;
1100
1101 for( i = 0; i <= padlen; i++ )
1102 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1103
1104 ssl->out_msglen += padlen + 1;
1105
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001106 enc_msglen = ssl->out_msglen;
1107 enc_msg = ssl->out_msg;
1108
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001109#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001110 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001111 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1112 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001113 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001114 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001115 {
1116 /*
1117 * Generate IV
1118 */
Paul Bakker48916f92012-09-16 19:57:18 +00001119 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1120 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001121 if( ret != 0 )
1122 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001123
Paul Bakker92be97b2013-01-02 17:30:03 +01001124 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001125 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001126
1127 /*
1128 * Fix pointer positions and message length with added IV
1129 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001130 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001131 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001132 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001133 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001134#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001135
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001137 "including %d bytes of IV and %d bytes of padding",
Paul Bakker48916f92012-09-16 19:57:18 +00001138 ssl->out_msglen, ssl->transform_out->ivlen, padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
1140 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001141 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
Paul Bakker45125bc2013-09-04 16:47:11 +02001143 if( ( ret = cipher_reset( &ssl->transform_out->cipher_ctx_enc ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001145 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1146 return( ret );
1147 }
1148
1149 if( ( ret = cipher_set_iv( &ssl->transform_out->cipher_ctx_enc,
1150 ssl->transform_out->iv_enc,
1151 ssl->transform_out->ivlen ) ) != 0 )
1152 {
1153 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001154 return( ret );
1155 }
Paul Bakker68884e32013-01-07 18:20:04 +01001156
Paul Bakkercca5b812013-08-31 17:40:26 +02001157 if( ( ret = cipher_update( &ssl->transform_out->cipher_ctx_enc,
1158 enc_msg, enc_msglen, enc_msg,
1159 &olen ) ) != 0 )
1160 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001161 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001162 return( ret );
1163 }
Paul Bakker68884e32013-01-07 18:20:04 +01001164
Paul Bakkercca5b812013-08-31 17:40:26 +02001165 enc_msglen -= olen;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001166
Paul Bakkercca5b812013-08-31 17:40:26 +02001167 if( ( ret = cipher_finish( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001168 enc_msg + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001169 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001170 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001171 return( ret );
1172 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001173
Paul Bakkercca5b812013-08-31 17:40:26 +02001174 if( enc_msglen != olen )
1175 {
1176 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect %d %d",
1177 enc_msglen, olen ) );
1178 // TODO Real error number
1179 return( -1 );
1180 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001181
1182#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001183 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1184 {
1185 /*
1186 * Save IV in SSL3 and TLS1
1187 */
1188 memcpy( ssl->transform_out->iv_enc,
1189 ssl->transform_out->cipher_ctx_enc.iv,
1190 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001192#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001194 else
1195#endif /* POLARSSL_CIPHER_MODE_CBC */
1196 {
1197 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1198 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Paul Bakkerca4ab492012-04-18 14:23:57 +00001201 for( i = 8; i > 0; i-- )
1202 if( ++ssl->out_ctr[i - 1] != 0 )
1203 break;
1204
Paul Bakker5121ce52009-01-03 21:22:43 +00001205 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1206
1207 return( 0 );
1208}
1209
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001210#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001211
Paul Bakker5121ce52009-01-03 21:22:43 +00001212static int ssl_decrypt_buf( ssl_context *ssl )
1213{
Paul Bakker45829992013-01-03 14:52:21 +01001214 size_t i, padlen = 0, correct = 1;
Paul Bakkerfab5c822012-02-06 16:45:10 +00001215 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +00001216
1217 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1218
Paul Bakker48916f92012-09-16 19:57:18 +00001219 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 {
1221 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001222 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001223 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 }
1225
Paul Bakker68884e32013-01-07 18:20:04 +01001226#if defined(POLARSSL_CIPHER_NULL_CIPHER)
1227 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 {
Paul Bakker68884e32013-01-07 18:20:04 +01001229 padlen = 0;
1230 }
1231 else
1232#endif /* POLARSSL_CIPHER_NULL_CIPHER */
Paul Bakker40e46942009-01-03 21:51:57 +00001233#if defined(POLARSSL_ARC4_C)
Paul Bakker68884e32013-01-07 18:20:04 +01001234 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
1235 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001236 int ret;
1237 size_t olen = 0;
1238
Paul Bakker68884e32013-01-07 18:20:04 +01001239 padlen = 0;
1240
Paul Bakker45125bc2013-09-04 16:47:11 +02001241 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001242 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001243 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1244 return( ret );
1245 }
1246
1247 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1248 ssl->transform_in->iv_dec,
1249 ssl->transform_in->ivlen ) ) != 0 )
1250 {
1251 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001252 return( ret );
1253 }
1254
1255 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1256 ssl->in_msg, ssl->in_msglen, ssl->in_msg,
1257 &olen ) ) != 0 )
1258 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001259 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001260 return( ret );
1261 }
1262
1263 if( ssl->in_msglen != olen )
1264 {
1265 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1266 // TODO Real error number
1267 return( -1 );
1268 }
1269
1270 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001271 ssl->in_msg + olen, &olen ) ) != 0 )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001272 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001273 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001274 return( ret );
1275 }
1276
1277 if( 0 != olen )
1278 {
1279 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1280 // TODO Real error number
1281 return( -1 );
1282 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 }
Paul Bakker68884e32013-01-07 18:20:04 +01001284 else
1285#endif /* POLARSSL_ARC4_C */
1286#if defined(POLARSSL_GCM_C)
1287 if( ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_128_GCM ||
1288 ssl->transform_in->ciphersuite_info->cipher == POLARSSL_CIPHER_AES_256_GCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001289 {
1290 unsigned char *dec_msg;
1291 unsigned char *dec_msg_result;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001292 size_t dec_msglen, olen, totlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001293 unsigned char add_data[13];
1294 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1295
Paul Bakker68884e32013-01-07 18:20:04 +01001296 padlen = 0;
1297
1298 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1299 ssl->transform_in->fixed_ivlen );
1300 dec_msglen -= 16;
1301 dec_msg = ssl->in_msg;
1302 dec_msg_result = ssl->in_msg;
1303 ssl->in_msglen = dec_msglen;
1304
1305 memcpy( add_data, ssl->in_ctr, 8 );
1306 add_data[8] = ssl->in_msgtype;
1307 add_data[9] = ssl->major_ver;
1308 add_data[10] = ssl->minor_ver;
1309 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1310 add_data[12] = ssl->in_msglen & 0xFF;
1311
1312 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1313 add_data, 13 );
1314
1315 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1316 ssl->in_iv,
1317 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1318
1319 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1320 ssl->transform_in->ivlen );
1321 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, 16 );
1322
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001323 /*
1324 * Decrypt
1325 */
1326 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1327 ssl->transform_in->iv_dec,
1328 ssl->transform_in->ivlen ) ) != 0 ||
1329 ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001330 {
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001331 return( ret );
1332 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001333
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001334 if( ( ret = cipher_update_ad( &ssl->transform_in->cipher_ctx_dec,
1335 add_data, 13 ) ) != 0 )
1336 {
1337 return( ret );
1338 }
1339
1340 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1341 dec_msg, dec_msglen,
1342 dec_msg_result, &olen ) ) != 0 )
1343 {
1344 return( ret );
1345 }
1346 totlen = olen;
1347
1348 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
1349 dec_msg_result + olen, &olen ) ) != 0 )
1350 {
1351 return( ret );
1352 }
1353 totlen += olen;
1354
1355 if( totlen != dec_msglen )
1356 {
1357 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1358 return( -1 );
1359 }
1360
1361 /*
1362 * Authenticate
1363 */
1364 if( ( ret = cipher_check_tag( &ssl->transform_in->cipher_ctx_dec,
1365 dec_msg + dec_msglen, 16 ) ) != 0 )
1366 {
1367 SSL_DEBUG_RET( 1, "cipher_check_tag", ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001368 return( POLARSSL_ERR_SSL_INVALID_MAC );
1369 }
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001370
Paul Bakkerca4ab492012-04-18 14:23:57 +00001371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 else
Paul Bakker68884e32013-01-07 18:20:04 +01001373#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001374#if defined(POLARSSL_CIPHER_MODE_CBC)
1375 if( ssl->transform_in->cipher_ctx_dec.cipher_info->mode ==
1376 POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 {
Paul Bakker45829992013-01-03 14:52:21 +01001378 /*
1379 * Decrypt and check the padding
1380 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001381 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001382 unsigned char *dec_msg;
1383 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001384 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001385 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001386 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001387
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 /*
Paul Bakker45829992013-01-03 14:52:21 +01001389 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 */
Paul Bakker48916f92012-09-16 19:57:18 +00001391 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 {
1393 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001394 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001395 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 }
1397
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001398#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001399 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1400 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001401#endif
Paul Bakker45829992013-01-03 14:52:21 +01001402
1403 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1404 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1405 {
1406 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) + 1 ) ( + expl IV )",
1407 ssl->in_msglen, ssl->transform_in->ivlen, ssl->transform_in->maclen ) );
1408 return( POLARSSL_ERR_SSL_INVALID_MAC );
1409 }
1410
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001411 dec_msglen = ssl->in_msglen;
1412 dec_msg = ssl->in_msg;
1413 dec_msg_result = ssl->in_msg;
1414
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001415#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001416 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001417 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001418 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001419 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001420 {
Paul Bakker48916f92012-09-16 19:57:18 +00001421 dec_msglen -= ssl->transform_in->ivlen;
1422 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001423
Paul Bakker48916f92012-09-16 19:57:18 +00001424 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001425 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001426 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001427#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001428
Paul Bakker45125bc2013-09-04 16:47:11 +02001429 if( ( ret = cipher_reset( &ssl->transform_in->cipher_ctx_dec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001431 SSL_DEBUG_RET( 1, "cipher_reset", ret );
1432 return( ret );
1433 }
1434
1435 if( ( ret = cipher_set_iv( &ssl->transform_in->cipher_ctx_dec,
1436 ssl->transform_in->iv_dec,
1437 ssl->transform_in->ivlen ) ) != 0 )
1438 {
1439 SSL_DEBUG_RET( 1, "cipher_set_iv", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001440 return( ret );
1441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Paul Bakkercca5b812013-08-31 17:40:26 +02001443 if( ( ret = cipher_update( &ssl->transform_in->cipher_ctx_dec,
1444 dec_msg, dec_msglen, dec_msg_result,
1445 &olen ) ) != 0 )
1446 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001447 SSL_DEBUG_RET( 1, "cipher_update", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001448 return( ret );
1449 }
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +00001450
Paul Bakkercca5b812013-08-31 17:40:26 +02001451 dec_msglen -= olen;
1452 if( ( ret = cipher_finish( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001453 dec_msg_result + olen, &olen ) ) != 0 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001454 {
Paul Bakker45125bc2013-09-04 16:47:11 +02001455 SSL_DEBUG_RET( 1, "cipher_finish", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001456 return( ret );
1457 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001458
Paul Bakkercca5b812013-08-31 17:40:26 +02001459 if( dec_msglen != olen )
1460 {
1461 SSL_DEBUG_MSG( 1, ( "total encrypted length incorrect" ) );
1462 // TODO Real error number
1463 return( -1 );
1464 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001465
1466#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001467 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1468 {
1469 /*
1470 * Save IV in SSL3 and TLS1
1471 */
1472 memcpy( ssl->transform_in->iv_dec,
1473 ssl->transform_in->cipher_ctx_dec.iv,
1474 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001476#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001477
1478 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001479
1480 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1481 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001482#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001483 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1484 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001485#endif
Paul Bakker45829992013-01-03 14:52:21 +01001486 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001487 correct = 0;
1488 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001489
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001490#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1492 {
Paul Bakker48916f92012-09-16 19:57:18 +00001493 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001495#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1497 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001498 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001499#endif
Paul Bakker45829992013-01-03 14:52:21 +01001500 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 }
1502 }
1503 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001504#endif
1505#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1506 defined(POLARSSL_SSL_PROTO_TLS1_2)
1507 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 {
1509 /*
Paul Bakker45829992013-01-03 14:52:21 +01001510 * TLSv1+: always check the padding up to the first failure
1511 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001513 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001514 size_t padding_idx = ssl->in_msglen - padlen - 1;
1515
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001516 for( i = 1; i <= 256; i++ )
1517 {
1518 real_count &= ( i <= padlen );
1519 pad_count += real_count *
1520 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1521 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001522
1523 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001524
Paul Bakkerd66f0702013-01-31 16:57:45 +01001525#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001526 if( padlen > 0 && correct == 0)
1527 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001528#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001529 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001531 else
1532#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1533 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001534 {
1535 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001536 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001538 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001539 else
1540#endif /* POLARSSL_CIPHER_MODE_CBC */
1541 {
1542 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1543 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001545
1546 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1547 ssl->in_msg, ssl->in_msglen );
1548
1549 /*
1550 * Always compute the MAC (RFC4346, CBCTIME).
1551 */
Paul Bakker48916f92012-09-16 19:57:18 +00001552 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001553
1554 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1555 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
1556
Paul Bakker45829992013-01-03 14:52:21 +01001557 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001558
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001559#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001560 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1561 {
Paul Bakker68884e32013-01-07 18:20:04 +01001562 ssl_mac( &ssl->transform_in->md_ctx_dec,
1563 ssl->transform_in->mac_dec,
1564 ssl->in_msg, ssl->in_msglen,
1565 ssl->in_ctr, ssl->in_msgtype );
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 }
1567 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001568#endif /* POLARSSL_SSL_PROTO_SSL3 */
1569#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1570 defined(POLARSSL_SSL_PROTO_TLS1_2)
1571 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 {
Paul Bakker45829992013-01-03 14:52:21 +01001573 /*
1574 * Process MAC and always update for padlen afterwards to make
1575 * total time independent of padlen
Paul Bakkere47b34b2013-02-27 14:48:00 +01001576 *
1577 * extra_run compensates MAC check for padlen
1578 *
1579 * Known timing attacks:
1580 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1581 *
1582 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1583 * correctly. (We round down instead of up, so -56 is the correct
1584 * value for our calculations instead of -55)
Paul Bakker45829992013-01-03 14:52:21 +01001585 */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001586 int j, extra_run = 0;
1587 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1588 ( 13 + ssl->in_msglen + 8 ) / 64;
1589
1590 extra_run &= correct * 0xFF;
1591
Paul Bakker68884e32013-01-07 18:20:04 +01001592 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1593 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1594 ssl->in_msglen );
1595 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1596 ssl->in_msg + ssl->in_msglen );
1597 for( j = 0; j < extra_run; j++ )
1598 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001599
Paul Bakker68884e32013-01-07 18:20:04 +01001600 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001601 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001602 else
1603#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1604 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001605 {
1606 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001607 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02001608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Paul Bakker48916f92012-09-16 19:57:18 +00001610 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
Paul Bakker48916f92012-09-16 19:57:18 +00001612 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
1614 if( memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Paul Bakker48916f92012-09-16 19:57:18 +00001615 ssl->transform_in->maclen ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001617#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001618 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001619#endif
Paul Bakker45829992013-01-03 14:52:21 +01001620 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 }
1622
1623 /*
Paul Bakker45829992013-01-03 14:52:21 +01001624 * Finally check the correct flag
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 */
Paul Bakker45829992013-01-03 14:52:21 +01001626 if( correct == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +00001627 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
1629 if( ssl->in_msglen == 0 )
1630 {
1631 ssl->nb_zero++;
1632
1633 /*
1634 * Three or more empty messages may be a DoS attack
1635 * (excessive CPU consumption).
1636 */
1637 if( ssl->nb_zero > 3 )
1638 {
1639 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1640 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001641 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 }
1643 }
1644 else
1645 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001646
Paul Bakker23986e52011-04-24 08:57:21 +00001647 for( i = 8; i > 0; i-- )
1648 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 break;
1650
1651 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1652
1653 return( 0 );
1654}
1655
Paul Bakker2770fbd2012-07-03 13:30:23 +00001656#if defined(POLARSSL_ZLIB_SUPPORT)
1657/*
1658 * Compression/decompression functions
1659 */
1660static int ssl_compress_buf( ssl_context *ssl )
1661{
1662 int ret;
1663 unsigned char *msg_post = ssl->out_msg;
1664 size_t len_pre = ssl->out_msglen;
1665 unsigned char *msg_pre;
1666
1667 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1668
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001669 if( len_pre == 0 )
1670 return( 0 );
1671
Paul Bakker6e339b52013-07-03 13:37:05 +02001672 msg_pre = (unsigned char*) polarssl_malloc( len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001673 if( msg_pre == NULL )
1674 {
1675 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len_pre ) );
1676 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1677 }
1678
1679 memcpy( msg_pre, ssl->out_msg, len_pre );
1680
1681 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1682 ssl->out_msglen ) );
1683
1684 SSL_DEBUG_BUF( 4, "before compression: output payload",
1685 ssl->out_msg, ssl->out_msglen );
1686
Paul Bakker48916f92012-09-16 19:57:18 +00001687 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1688 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1689 ssl->transform_out->ctx_deflate.next_out = msg_post;
1690 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001691
Paul Bakker48916f92012-09-16 19:57:18 +00001692 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001693 if( ret != Z_OK )
1694 {
1695 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1696 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1697 }
1698
Paul Bakker48916f92012-09-16 19:57:18 +00001699 ssl->out_msglen = SSL_BUFFER_LEN - ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001700
Paul Bakker6e339b52013-07-03 13:37:05 +02001701 polarssl_free( msg_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001702
1703 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1704 ssl->out_msglen ) );
1705
1706 SSL_DEBUG_BUF( 4, "after compression: output payload",
1707 ssl->out_msg, ssl->out_msglen );
1708
1709 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1710
1711 return( 0 );
1712}
1713
1714static int ssl_decompress_buf( ssl_context *ssl )
1715{
1716 int ret;
1717 unsigned char *msg_post = ssl->in_msg;
1718 size_t len_pre = ssl->in_msglen;
1719 unsigned char *msg_pre;
1720
1721 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1722
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001723 if( len_pre == 0 )
1724 return( 0 );
1725
Paul Bakker6e339b52013-07-03 13:37:05 +02001726 msg_pre = (unsigned char*) polarssl_malloc( len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001727 if( msg_pre == NULL )
1728 {
1729 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len_pre ) );
1730 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1731 }
1732
1733 memcpy( msg_pre, ssl->in_msg, len_pre );
1734
1735 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1736 ssl->in_msglen ) );
1737
1738 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1739 ssl->in_msg, ssl->in_msglen );
1740
Paul Bakker48916f92012-09-16 19:57:18 +00001741 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1742 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1743 ssl->transform_in->ctx_inflate.next_out = msg_post;
1744 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001745
Paul Bakker48916f92012-09-16 19:57:18 +00001746 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001747 if( ret != Z_OK )
1748 {
1749 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1750 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1751 }
1752
Paul Bakker48916f92012-09-16 19:57:18 +00001753 ssl->in_msglen = SSL_MAX_CONTENT_LEN - ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001754
Paul Bakker6e339b52013-07-03 13:37:05 +02001755 polarssl_free( msg_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001756
1757 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1758 ssl->in_msglen ) );
1759
1760 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1761 ssl->in_msg, ssl->in_msglen );
1762
1763 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1764
1765 return( 0 );
1766}
1767#endif /* POLARSSL_ZLIB_SUPPORT */
1768
Paul Bakker5121ce52009-01-03 21:22:43 +00001769/*
1770 * Fill the input message buffer
1771 */
Paul Bakker23986e52011-04-24 08:57:21 +00001772int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001773{
Paul Bakker23986e52011-04-24 08:57:21 +00001774 int ret;
1775 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001776
1777 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1778
1779 while( ssl->in_left < nb_want )
1780 {
1781 len = nb_want - ssl->in_left;
1782 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1783
1784 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1785 ssl->in_left, nb_want ) );
1786 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1787
Paul Bakker831a7552011-05-18 13:32:51 +00001788 if( ret == 0 )
1789 return( POLARSSL_ERR_SSL_CONN_EOF );
1790
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 if( ret < 0 )
1792 return( ret );
1793
1794 ssl->in_left += ret;
1795 }
1796
1797 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1798
1799 return( 0 );
1800}
1801
1802/*
1803 * Flush any data not yet written
1804 */
1805int ssl_flush_output( ssl_context *ssl )
1806{
1807 int ret;
1808 unsigned char *buf;
1809
1810 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1811
1812 while( ssl->out_left > 0 )
1813 {
1814 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1815 5 + ssl->out_msglen, ssl->out_left ) );
1816
Paul Bakker5bd42292012-12-19 14:40:42 +01001817 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001819
Paul Bakker5121ce52009-01-03 21:22:43 +00001820 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1821
1822 if( ret <= 0 )
1823 return( ret );
1824
1825 ssl->out_left -= ret;
1826 }
1827
1828 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1829
1830 return( 0 );
1831}
1832
1833/*
1834 * Record layer functions
1835 */
1836int ssl_write_record( ssl_context *ssl )
1837{
Paul Bakker05ef8352012-05-08 09:17:57 +00001838 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001839 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001840
1841 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1842
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1844 {
1845 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1846 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1847 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1848
Paul Bakker48916f92012-09-16 19:57:18 +00001849 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 }
1851
Paul Bakker2770fbd2012-07-03 13:30:23 +00001852#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001853 if( ssl->transform_out != NULL &&
1854 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001855 {
1856 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1857 {
1858 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1859 return( ret );
1860 }
1861
1862 len = ssl->out_msglen;
1863 }
1864#endif /*POLARSSL_ZLIB_SUPPORT */
1865
Paul Bakker05ef8352012-05-08 09:17:57 +00001866#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
1867 if( ssl_hw_record_write != NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001869 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
Paul Bakker05ef8352012-05-08 09:17:57 +00001871 ret = ssl_hw_record_write( ssl );
1872 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1873 {
1874 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
1875 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
1876 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001877
1878 if( ret == 0 )
1879 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001880 }
1881#endif
1882 if( !done )
1883 {
1884 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1885 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1886 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001887 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1888 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001889
Paul Bakker48916f92012-09-16 19:57:18 +00001890 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001891 {
1892 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1893 {
1894 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1895 return( ret );
1896 }
1897
1898 len = ssl->out_msglen;
1899 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1900 ssl->out_hdr[4] = (unsigned char)( len );
1901 }
1902
1903 ssl->out_left = 5 + ssl->out_msglen;
1904
1905 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1906 "version = [%d:%d], msglen = %d",
1907 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1908 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1909
1910 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001911 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001912 }
1913
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1915 {
1916 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1917 return( ret );
1918 }
1919
1920 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1921
1922 return( 0 );
1923}
1924
1925int ssl_read_record( ssl_context *ssl )
1926{
Paul Bakker05ef8352012-05-08 09:17:57 +00001927 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
1929 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1930
Paul Bakker68884e32013-01-07 18:20:04 +01001931 SSL_DEBUG_BUF( 4, "input record from network",
1932 ssl->in_hdr, 5 + ssl->in_msglen );
1933
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 if( ssl->in_hslen != 0 &&
1935 ssl->in_hslen < ssl->in_msglen )
1936 {
1937 /*
1938 * Get next Handshake message in the current record
1939 */
1940 ssl->in_msglen -= ssl->in_hslen;
1941
Paul Bakker8934a982011-08-05 11:11:53 +00001942 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001943 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001944
1945 ssl->in_hslen = 4;
1946 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1947
1948 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1949 " %d, type = %d, hslen = %d",
1950 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1951
1952 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1953 {
1954 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001955 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 }
1957
1958 if( ssl->in_msglen < ssl->in_hslen )
1959 {
1960 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001961 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 }
1963
Paul Bakker48916f92012-09-16 19:57:18 +00001964 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
1966 return( 0 );
1967 }
1968
1969 ssl->in_hslen = 0;
1970
1971 /*
1972 * Read the record header and validate it
1973 */
1974 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1975 {
1976 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1977 return( ret );
1978 }
1979
1980 ssl->in_msgtype = ssl->in_hdr[0];
1981 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
1982
1983 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
1984 "version = [%d:%d], msglen = %d",
1985 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
1986 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
1987
1988 if( ssl->in_hdr[1] != ssl->major_ver )
1989 {
1990 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001991 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 }
1993
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001994 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001995 {
1996 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001997 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998 }
1999
2000 /*
2001 * Make sure the message length is acceptable
2002 */
Paul Bakker48916f92012-09-16 19:57:18 +00002003 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 {
2005 if( ssl->in_msglen < 1 ||
2006 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2007 {
2008 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002009 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 }
2011 }
2012 else
2013 {
Paul Bakker48916f92012-09-16 19:57:18 +00002014 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002015 {
2016 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002017 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 }
2019
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002020#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002022 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 {
2024 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002025 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002028
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002029#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2030 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 /*
2032 * TLS encrypted messages can have up to 256 bytes of padding
2033 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002034 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002035 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 {
2037 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002038 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 }
2042
2043 /*
2044 * Read and optionally decrypt the message contents
2045 */
2046 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2047 {
2048 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2049 return( ret );
2050 }
2051
2052 SSL_DEBUG_BUF( 4, "input record from network",
2053 ssl->in_hdr, 5 + ssl->in_msglen );
2054
Paul Bakker05ef8352012-05-08 09:17:57 +00002055#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2056 if( ssl_hw_record_read != NULL)
2057 {
2058 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2059
2060 ret = ssl_hw_record_read( ssl );
2061 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2062 {
2063 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
2064 return POLARSSL_ERR_SSL_HW_ACCEL_FAILED;
2065 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002066
2067 if( ret == 0 )
2068 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002069 }
2070#endif
Paul Bakker48916f92012-09-16 19:57:18 +00002071 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 {
2073 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2074 {
Paul Bakker40865c82013-01-31 17:13:13 +01002075#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2076 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2077 {
2078 ssl_send_alert_message( ssl,
2079 SSL_ALERT_LEVEL_FATAL,
2080 SSL_ALERT_MSG_BAD_RECORD_MAC );
2081 }
2082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2084 return( ret );
2085 }
2086
2087 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2088 ssl->in_msg, ssl->in_msglen );
2089
2090 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2091 {
2092 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002093 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
2095 }
2096
Paul Bakker2770fbd2012-07-03 13:30:23 +00002097#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002098 if( ssl->transform_in != NULL &&
2099 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002100 {
2101 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2102 {
2103 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2104 return( ret );
2105 }
2106
2107 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2108 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2109 }
2110#endif /* POLARSSL_ZLIB_SUPPORT */
2111
Paul Bakker0a925182012-04-16 06:46:41 +00002112 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2113 ssl->in_msgtype != SSL_MSG_ALERT &&
2114 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2115 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2116 {
2117 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2118
Paul Bakker48916f92012-09-16 19:57:18 +00002119 if( ( ret = ssl_send_alert_message( ssl,
2120 SSL_ALERT_LEVEL_FATAL,
2121 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002122 {
2123 return( ret );
2124 }
2125
2126 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2127 }
2128
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2130 {
2131 ssl->in_hslen = 4;
2132 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2133
2134 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2135 " %d, type = %d, hslen = %d",
2136 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2137
2138 /*
2139 * Additional checks to validate the handshake header
2140 */
2141 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2142 {
2143 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002144 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 }
2146
2147 if( ssl->in_msglen < ssl->in_hslen )
2148 {
2149 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002150 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 }
2152
Paul Bakker48916f92012-09-16 19:57:18 +00002153 if( ssl->state != SSL_HANDSHAKE_OVER )
2154 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 }
2156
2157 if( ssl->in_msgtype == SSL_MSG_ALERT )
2158 {
2159 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2160 ssl->in_msg[0], ssl->in_msg[1] ) );
2161
2162 /*
2163 * Ignore non-fatal alerts, except close_notify
2164 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002165 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002167 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2168 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002169 /**
2170 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2171 * error identifier.
2172 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002173 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 }
2175
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002176 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2177 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 {
2179 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002180 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 }
2182 }
2183
2184 ssl->in_left = 0;
2185
2186 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2187
2188 return( 0 );
2189}
2190
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002191int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2192{
2193 int ret;
2194
2195 if( ( ret = ssl_send_alert_message( ssl,
2196 SSL_ALERT_LEVEL_FATAL,
2197 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2198 {
2199 return( ret );
2200 }
2201
2202 return( 0 );
2203}
2204
Paul Bakker0a925182012-04-16 06:46:41 +00002205int ssl_send_alert_message( ssl_context *ssl,
2206 unsigned char level,
2207 unsigned char message )
2208{
2209 int ret;
2210
2211 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2212
2213 ssl->out_msgtype = SSL_MSG_ALERT;
2214 ssl->out_msglen = 2;
2215 ssl->out_msg[0] = level;
2216 ssl->out_msg[1] = message;
2217
2218 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2219 {
2220 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2221 return( ret );
2222 }
2223
2224 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2225
2226 return( 0 );
2227}
2228
Paul Bakker5121ce52009-01-03 21:22:43 +00002229/*
2230 * Handshake functions
2231 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002232#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2233 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002234 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2235 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002236int ssl_write_certificate( ssl_context *ssl )
2237{
Paul Bakkered27a042013-04-18 22:46:23 +02002238 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002239 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
2241 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2242
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002243 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002244 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2245 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002246 {
2247 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2248 ssl->state++;
2249 return( 0 );
2250 }
2251
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002252 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002253 return( ret );
2254}
2255
2256int ssl_parse_certificate( ssl_context *ssl )
2257{
2258 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2259 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2260
2261 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2262
2263 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002264 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2265 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002266 {
2267 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2268 ssl->state++;
2269 return( 0 );
2270 }
2271
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002272 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002273 return( ret );
2274}
2275#else
2276int ssl_write_certificate( ssl_context *ssl )
2277{
2278 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2279 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002280 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002281 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2282
2283 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2284
2285 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002286 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2287 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002288 {
2289 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2290 ssl->state++;
2291 return( 0 );
2292 }
2293
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 if( ssl->endpoint == SSL_IS_CLIENT )
2295 {
2296 if( ssl->client_auth == 0 )
2297 {
2298 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2299 ssl->state++;
2300 return( 0 );
2301 }
2302
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002303#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 /*
2305 * If using SSLv3 and got no cert, send an Alert message
2306 * (otherwise an empty Certificate message will be sent).
2307 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002308 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2310 {
2311 ssl->out_msglen = 2;
2312 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002313 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2314 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002315
2316 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2317 goto write_msg;
2318 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002319#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002320 }
2321 else /* SSL_IS_SERVER */
2322 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002323 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 {
2325 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002326 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 }
2328 }
2329
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002330 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
2332 /*
2333 * 0 . 0 handshake type
2334 * 1 . 3 handshake length
2335 * 4 . 6 length of all certs
2336 * 7 . 9 length of cert. 1
2337 * 10 . n-1 peer certificate
2338 * n . n+2 length of cert. 2
2339 * n+3 . ... upper level cert, etc.
2340 */
2341 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002342 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002343
Paul Bakker29087132010-03-21 21:03:34 +00002344 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 {
2346 n = crt->raw.len;
2347 if( i + 3 + n > SSL_MAX_CONTENT_LEN )
2348 {
2349 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2350 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002351 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 }
2353
2354 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2355 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2356 ssl->out_msg[i + 2] = (unsigned char)( n );
2357
2358 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2359 i += n; crt = crt->next;
2360 }
2361
2362 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2363 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2364 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2365
2366 ssl->out_msglen = i;
2367 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2368 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2369
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002370#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002371write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002372#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
2374 ssl->state++;
2375
2376 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2377 {
2378 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2379 return( ret );
2380 }
2381
2382 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2383
Paul Bakkered27a042013-04-18 22:46:23 +02002384 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385}
2386
2387int ssl_parse_certificate( ssl_context *ssl )
2388{
Paul Bakkered27a042013-04-18 22:46:23 +02002389 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002390 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002391 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
2393 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2394
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002395 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002396 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2397 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002398 {
2399 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2400 ssl->state++;
2401 return( 0 );
2402 }
2403
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 if( ssl->endpoint == SSL_IS_SERVER &&
2405 ssl->authmode == SSL_VERIFY_NONE )
2406 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002407 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2409 ssl->state++;
2410 return( 0 );
2411 }
2412
2413 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2414 {
2415 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2416 return( ret );
2417 }
2418
2419 ssl->state++;
2420
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002421#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 /*
2423 * Check if the client sent an empty certificate
2424 */
2425 if( ssl->endpoint == SSL_IS_SERVER &&
2426 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2427 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002428 if( ssl->in_msglen == 2 &&
2429 ssl->in_msgtype == SSL_MSG_ALERT &&
2430 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2431 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 {
2433 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2434
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002435 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2437 return( 0 );
2438 else
Paul Bakker40e46942009-01-03 21:51:57 +00002439 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 }
2441 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002442#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002444#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2445 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 if( ssl->endpoint == SSL_IS_SERVER &&
2447 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2448 {
2449 if( ssl->in_hslen == 7 &&
2450 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2451 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2452 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2453 {
2454 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2455
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002456 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002458 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 else
2460 return( 0 );
2461 }
2462 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002463#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2464 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
2466 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2467 {
2468 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002469 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 }
2471
2472 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2473 {
2474 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002475 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 }
2477
2478 /*
2479 * Same message structure as in ssl_write_certificate()
2480 */
2481 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2482
2483 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2484 {
2485 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002486 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 }
2488
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002489 /* In case we tried to reuse a session but it failed */
2490 if( ssl->session_negotiate->peer_cert != NULL )
2491 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002492 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002493 polarssl_free( ssl->session_negotiate->peer_cert );
2494 }
2495
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002496 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2497 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 {
2499 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002500 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002501 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 }
2503
Paul Bakkerb6b09562013-09-18 14:17:41 +02002504 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
2506 i = 7;
2507
2508 while( i < ssl->in_hslen )
2509 {
2510 if( ssl->in_msg[i] != 0 )
2511 {
2512 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002513 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 }
2515
2516 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2517 | (unsigned int) ssl->in_msg[i + 2];
2518 i += 3;
2519
2520 if( n < 128 || i + n > ssl->in_hslen )
2521 {
2522 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002523 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 }
2525
Paul Bakkerddf26b42013-09-18 13:46:23 +02002526 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2527 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 if( ret != 0 )
2529 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002530 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 return( ret );
2532 }
2533
2534 i += n;
2535 }
2536
Paul Bakker48916f92012-09-16 19:57:18 +00002537 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
2539 if( ssl->authmode != SSL_VERIFY_NONE )
2540 {
2541 if( ssl->ca_chain == NULL )
2542 {
2543 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002544 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 }
2546
Paul Bakkerddf26b42013-09-18 13:46:23 +02002547 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2548 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2549 &ssl->session_negotiate->verify_result,
2550 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
2552 if( ret != 0 )
2553 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
2554
2555 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2556 ret = 0;
2557 }
2558
2559 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2560
2561 return( ret );
2562}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002563#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2564 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2565 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002566
2567int ssl_write_change_cipher_spec( ssl_context *ssl )
2568{
2569 int ret;
2570
2571 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2572
2573 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2574 ssl->out_msglen = 1;
2575 ssl->out_msg[0] = 1;
2576
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 ssl->state++;
2578
2579 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2580 {
2581 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2582 return( ret );
2583 }
2584
2585 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2586
2587 return( 0 );
2588}
2589
2590int ssl_parse_change_cipher_spec( ssl_context *ssl )
2591{
2592 int ret;
2593
2594 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2595
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2597 {
2598 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2599 return( ret );
2600 }
2601
2602 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2603 {
2604 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002605 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 }
2607
2608 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2609 {
2610 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002611 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 }
2613
2614 ssl->state++;
2615
2616 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2617
2618 return( 0 );
2619}
2620
Paul Bakker41c83d32013-03-20 14:39:14 +01002621void ssl_optimize_checksum( ssl_context *ssl,
2622 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002623{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002624 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002625
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002626#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2627 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002628 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002629 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002630 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002631#endif
2632#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2633#if defined(POLARSSL_SHA512_C)
2634 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2635 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2636 else
2637#endif
2638#if defined(POLARSSL_SHA256_C)
2639 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002640 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002641 else
2642#endif
2643#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2644 /* Should never happen */
2645 return;
Paul Bakker380da532012-04-18 16:10:25 +00002646}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002647
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002648static void ssl_update_checksum_start( ssl_context *ssl,
2649 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002650{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002651#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2652 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002653 md5_update( &ssl->handshake->fin_md5 , buf, len );
2654 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002655#endif
2656#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2657#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002658 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002659#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002660#if defined(POLARSSL_SHA512_C)
2661 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002662#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002663#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002664}
2665
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002666#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2667 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002668static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2669 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002670{
Paul Bakker48916f92012-09-16 19:57:18 +00002671 md5_update( &ssl->handshake->fin_md5 , buf, len );
2672 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002673}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002674#endif
Paul Bakker380da532012-04-18 16:10:25 +00002675
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002676#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2677#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002678static void ssl_update_checksum_sha256( 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 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002682}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002683#endif
Paul Bakker380da532012-04-18 16:10:25 +00002684
Paul Bakker9e36f042013-06-30 14:34:05 +02002685#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002686static void ssl_update_checksum_sha384( ssl_context *ssl,
2687 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002688{
Paul Bakker9e36f042013-06-30 14:34:05 +02002689 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002690}
Paul Bakker769075d2012-11-24 11:26:46 +01002691#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002692#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002693
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002694#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002695static void ssl_calc_finished_ssl(
2696 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002697{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002698 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002699 md5_context md5;
2700 sha1_context sha1;
2701
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 unsigned char padbuf[48];
2703 unsigned char md5sum[16];
2704 unsigned char sha1sum[20];
2705
Paul Bakker48916f92012-09-16 19:57:18 +00002706 ssl_session *session = ssl->session_negotiate;
2707 if( !session )
2708 session = ssl->session;
2709
Paul Bakker1ef83d62012-04-11 12:09:53 +00002710 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2711
Paul Bakker48916f92012-09-16 19:57:18 +00002712 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2713 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
2715 /*
2716 * SSLv3:
2717 * hash =
2718 * MD5( master + pad2 +
2719 * MD5( handshake + sender + master + pad1 ) )
2720 * + SHA1( master + pad2 +
2721 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 */
2723
Paul Bakker90995b52013-06-24 19:20:35 +02002724#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002726 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002727#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Paul Bakker90995b52013-06-24 19:20:35 +02002729#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002731 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002732#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Paul Bakker3c2122f2013-06-24 19:03:14 +02002734 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2735 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Paul Bakker1ef83d62012-04-11 12:09:53 +00002737 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
Paul Bakker3c2122f2013-06-24 19:03:14 +02002739 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002740 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002741 md5_update( &md5, padbuf, 48 );
2742 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
Paul Bakker3c2122f2013-06-24 19:03:14 +02002744 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002745 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002746 sha1_update( &sha1, padbuf, 40 );
2747 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Paul Bakker1ef83d62012-04-11 12:09:53 +00002749 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Paul Bakker1ef83d62012-04-11 12:09:53 +00002751 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002752 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002753 md5_update( &md5, padbuf, 48 );
2754 md5_update( &md5, md5sum, 16 );
2755 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Paul Bakker1ef83d62012-04-11 12:09:53 +00002757 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002758 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002759 sha1_update( &sha1, padbuf , 40 );
2760 sha1_update( &sha1, sha1sum, 20 );
2761 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
Paul Bakker1ef83d62012-04-11 12:09:53 +00002763 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002764
Paul Bakker1ef83d62012-04-11 12:09:53 +00002765 memset( &md5, 0, sizeof( md5_context ) );
2766 memset( &sha1, 0, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002767
2768 memset( padbuf, 0, sizeof( padbuf ) );
2769 memset( md5sum, 0, sizeof( md5sum ) );
2770 memset( sha1sum, 0, sizeof( sha1sum ) );
2771
2772 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2773}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002774#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002776#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002777static void ssl_calc_finished_tls(
2778 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002779{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002780 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002781 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002782 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002783 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002784 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
Paul Bakker48916f92012-09-16 19:57:18 +00002786 ssl_session *session = ssl->session_negotiate;
2787 if( !session )
2788 session = ssl->session;
2789
Paul Bakker1ef83d62012-04-11 12:09:53 +00002790 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Paul Bakker48916f92012-09-16 19:57:18 +00002792 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2793 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Paul Bakker1ef83d62012-04-11 12:09:53 +00002795 /*
2796 * TLSv1:
2797 * hash = PRF( master, finished_label,
2798 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2799 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
Paul Bakker90995b52013-06-24 19:20:35 +02002801#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002802 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2803 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002804#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002805
Paul Bakker90995b52013-06-24 19:20:35 +02002806#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002807 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2808 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002809#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002810
2811 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002812 ? "client finished"
2813 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002814
2815 md5_finish( &md5, padbuf );
2816 sha1_finish( &sha1, padbuf + 16 );
2817
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002818 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002819 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002820
2821 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2822
2823 memset( &md5, 0, sizeof( md5_context ) );
2824 memset( &sha1, 0, sizeof( sha1_context ) );
2825
2826 memset( padbuf, 0, sizeof( padbuf ) );
2827
2828 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2829}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002830#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002831
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002832#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2833#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002834static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002835 ssl_context *ssl, unsigned char *buf, int from )
2836{
2837 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002838 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002839 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002840 unsigned char padbuf[32];
2841
Paul Bakker48916f92012-09-16 19:57:18 +00002842 ssl_session *session = ssl->session_negotiate;
2843 if( !session )
2844 session = ssl->session;
2845
Paul Bakker380da532012-04-18 16:10:25 +00002846 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002847
Paul Bakker9e36f042013-06-30 14:34:05 +02002848 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002849
2850 /*
2851 * TLSv1.2:
2852 * hash = PRF( master, finished_label,
2853 * Hash( handshake ) )[0.11]
2854 */
2855
Paul Bakker9e36f042013-06-30 14:34:05 +02002856#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002857 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002858 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002859#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002860
2861 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002862 ? "client finished"
2863 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002864
Paul Bakker9e36f042013-06-30 14:34:05 +02002865 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002866
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002867 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002868 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869
2870 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2871
Paul Bakker9e36f042013-06-30 14:34:05 +02002872 memset( &sha256, 0, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873
2874 memset( padbuf, 0, sizeof( padbuf ) );
2875
2876 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2877}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002878#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879
Paul Bakker9e36f042013-06-30 14:34:05 +02002880#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002881static void ssl_calc_finished_tls_sha384(
2882 ssl_context *ssl, unsigned char *buf, int from )
2883{
2884 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002885 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002886 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002887 unsigned char padbuf[48];
2888
Paul Bakker48916f92012-09-16 19:57:18 +00002889 ssl_session *session = ssl->session_negotiate;
2890 if( !session )
2891 session = ssl->session;
2892
Paul Bakker380da532012-04-18 16:10:25 +00002893 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002894
Paul Bakker9e36f042013-06-30 14:34:05 +02002895 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002896
2897 /*
2898 * TLSv1.2:
2899 * hash = PRF( master, finished_label,
2900 * Hash( handshake ) )[0.11]
2901 */
2902
Paul Bakker9e36f042013-06-30 14:34:05 +02002903#if !defined(POLARSSL_SHA512_ALT)
2904 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
2905 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002906#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00002907
2908 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002909 ? "client finished"
2910 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00002911
Paul Bakker9e36f042013-06-30 14:34:05 +02002912 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002913
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002914 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002915 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002916
2917 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2918
Paul Bakker9e36f042013-06-30 14:34:05 +02002919 memset( &sha512, 0, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002920
2921 memset( padbuf, 0, sizeof( padbuf ) );
2922
2923 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2924}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002925#endif /* POLARSSL_SHA512_C */
2926#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00002927
Paul Bakker48916f92012-09-16 19:57:18 +00002928void ssl_handshake_wrapup( ssl_context *ssl )
2929{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002930 int resume = ssl->handshake->resume;
2931
Paul Bakker48916f92012-09-16 19:57:18 +00002932 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
2933
2934 /*
2935 * Free our handshake params
2936 */
2937 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02002938 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00002939 ssl->handshake = NULL;
2940
2941 /*
2942 * Switch in our now active transform context
2943 */
2944 if( ssl->transform )
2945 {
2946 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02002947 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00002948 }
2949 ssl->transform = ssl->transform_negotiate;
2950 ssl->transform_negotiate = NULL;
2951
Paul Bakker0a597072012-09-25 21:55:46 +00002952 if( ssl->session )
2953 {
2954 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02002955 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00002956 }
2957 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ssl->session_negotiate = NULL;
2959
Paul Bakker0a597072012-09-25 21:55:46 +00002960 /*
2961 * Add cache entry
2962 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002963 if( ssl->f_set_cache != NULL &&
2964 ssl->session->length != 0 &&
2965 resume == 0 )
2966 {
Paul Bakker0a597072012-09-25 21:55:46 +00002967 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
2968 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002969 }
Paul Bakker0a597072012-09-25 21:55:46 +00002970
Paul Bakker48916f92012-09-16 19:57:18 +00002971 ssl->state++;
2972
2973 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
2974}
2975
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976int ssl_write_finished( ssl_context *ssl )
2977{
2978 int ret, hash_len;
2979
2980 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
2981
Paul Bakker92be97b2013-01-02 17:30:03 +01002982 /*
2983 * Set the out_msg pointer to the correct location based on IV length
2984 */
2985 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2986 {
2987 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
2988 ssl->transform_negotiate->fixed_ivlen;
2989 }
2990 else
2991 ssl->out_msg = ssl->out_iv;
2992
Paul Bakker48916f92012-09-16 19:57:18 +00002993 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994
2995 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
2997
Paul Bakker48916f92012-09-16 19:57:18 +00002998 ssl->verify_data_len = hash_len;
2999 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3000
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 ssl->out_msglen = 4 + hash_len;
3002 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3003 ssl->out_msg[0] = SSL_HS_FINISHED;
3004
3005 /*
3006 * In case of session resuming, invert the client and server
3007 * ChangeCipherSpec messages order.
3008 */
Paul Bakker0a597072012-09-25 21:55:46 +00003009 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 {
3011 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003012 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 else
3014 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3015 }
3016 else
3017 ssl->state++;
3018
Paul Bakker48916f92012-09-16 19:57:18 +00003019 /*
3020 * Switch to our negotiated transform and session parameters for outbound data.
3021 */
3022 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3023 ssl->transform_out = ssl->transform_negotiate;
3024 ssl->session_out = ssl->session_negotiate;
3025 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003026
Paul Bakker07eb38b2012-12-19 14:42:06 +01003027#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3028 if( ssl_hw_record_activate != NULL)
3029 {
3030 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3031 {
3032 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3033 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3034 }
3035 }
3036#endif
3037
Paul Bakker5121ce52009-01-03 21:22:43 +00003038 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3039 {
3040 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3041 return( ret );
3042 }
3043
3044 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3045
3046 return( 0 );
3047}
3048
3049int ssl_parse_finished( ssl_context *ssl )
3050{
Paul Bakker23986e52011-04-24 08:57:21 +00003051 int ret;
3052 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 unsigned char buf[36];
3054
3055 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3056
Paul Bakker48916f92012-09-16 19:57:18 +00003057 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Paul Bakker48916f92012-09-16 19:57:18 +00003059 /*
3060 * Switch to our negotiated transform and session parameters for inbound data.
3061 */
3062 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3063 ssl->transform_in = ssl->transform_negotiate;
3064 ssl->session_in = ssl->session_negotiate;
3065 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
Paul Bakker92be97b2013-01-02 17:30:03 +01003067 /*
3068 * Set the in_msg pointer to the correct location based on IV length
3069 */
3070 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3071 {
3072 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3073 ssl->transform_negotiate->fixed_ivlen;
3074 }
3075 else
3076 ssl->in_msg = ssl->in_iv;
3077
Paul Bakker07eb38b2012-12-19 14:42:06 +01003078#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3079 if( ssl_hw_record_activate != NULL)
3080 {
3081 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3082 {
3083 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3084 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3085 }
3086 }
3087#endif
3088
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3090 {
3091 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3092 return( ret );
3093 }
3094
3095 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3096 {
3097 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003098 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 }
3100
Paul Bakker1ef83d62012-04-11 12:09:53 +00003101 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3103
3104 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3105 ssl->in_hslen != 4 + hash_len )
3106 {
3107 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003108 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 }
3110
Paul Bakker5121ce52009-01-03 21:22:43 +00003111 if( memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
3112 {
3113 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003114 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 }
3116
Paul Bakker48916f92012-09-16 19:57:18 +00003117 ssl->verify_data_len = hash_len;
3118 memcpy( ssl->peer_verify_data, buf, hash_len );
3119
Paul Bakker0a597072012-09-25 21:55:46 +00003120 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003121 {
3122 if( ssl->endpoint == SSL_IS_CLIENT )
3123 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3124
3125 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003126 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003127 }
3128 else
3129 ssl->state++;
3130
3131 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3132
3133 return( 0 );
3134}
3135
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003136static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003137{
3138 if( ssl->transform_negotiate )
3139 ssl_transform_free( ssl->transform_negotiate );
3140 else
Paul Bakker6e339b52013-07-03 13:37:05 +02003141 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker48916f92012-09-16 19:57:18 +00003142
3143 if( ssl->session_negotiate )
3144 ssl_session_free( ssl->session_negotiate );
3145 else
Paul Bakker6e339b52013-07-03 13:37:05 +02003146 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakker48916f92012-09-16 19:57:18 +00003147
3148 if( ssl->handshake )
3149 ssl_handshake_free( ssl->handshake );
3150 else
Paul Bakker6e339b52013-07-03 13:37:05 +02003151 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker48916f92012-09-16 19:57:18 +00003152
3153 if( ssl->handshake == NULL ||
3154 ssl->transform_negotiate == NULL ||
3155 ssl->session_negotiate == NULL )
3156 {
3157 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3158 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3159 }
3160
3161 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
3162 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
3163 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
3164
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003165#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3166 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003167 md5_starts( &ssl->handshake->fin_md5 );
3168 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003169#endif
3170#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3171#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003172 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003173#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003174#if defined(POLARSSL_SHA512_C)
3175 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003176#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003177#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003178
3179 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003180 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003181
Paul Bakker61d113b2013-07-04 11:51:43 +02003182#if defined(POLARSSL_ECDH_C)
3183 ecdh_init( &ssl->handshake->ecdh_ctx );
3184#endif
3185
Paul Bakker48916f92012-09-16 19:57:18 +00003186 return( 0 );
3187}
3188
Paul Bakker5121ce52009-01-03 21:22:43 +00003189/*
3190 * Initialize an SSL context
3191 */
3192int ssl_init( ssl_context *ssl )
3193{
Paul Bakker48916f92012-09-16 19:57:18 +00003194 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 int len = SSL_BUFFER_LEN;
3196
3197 memset( ssl, 0, sizeof( ssl_context ) );
3198
Paul Bakker62f2dee2012-09-28 07:31:51 +00003199 /*
3200 * Sane defaults
3201 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003202 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3203 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3204 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3205 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003206
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003207 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003208
Paul Bakker62f2dee2012-09-28 07:31:51 +00003209#if defined(POLARSSL_DHM_C)
3210 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3211 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3212 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3213 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3214 {
3215 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3216 return( ret );
3217 }
3218#endif
3219
3220 /*
3221 * Prepare base structures
3222 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003223 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003225 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 ssl->in_msg = ssl->in_ctr + 13;
3227
3228 if( ssl->in_ctr == NULL )
3229 {
3230 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003231 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 }
3233
Paul Bakker6e339b52013-07-03 13:37:05 +02003234 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003236 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003237 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
3239 if( ssl->out_ctr == NULL )
3240 {
3241 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003242 polarssl_free( ssl-> in_ctr );
Paul Bakker69e095c2011-12-10 21:55:01 +00003243 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 }
3245
3246 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3247 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3248
Paul Bakker606b4ba2013-08-14 16:52:14 +02003249#if defined(POLARSSL_SSL_SESSION_TICKETS)
3250 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3251#endif
3252
Paul Bakker48916f92012-09-16 19:57:18 +00003253 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3254 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003255
3256 return( 0 );
3257}
3258
3259/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003260 * Reset an initialized and used SSL context for re-use while retaining
3261 * all application-set variables, function pointers and data.
3262 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003263int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003264{
Paul Bakker48916f92012-09-16 19:57:18 +00003265 int ret;
3266
Paul Bakker7eb013f2011-10-06 12:37:39 +00003267 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003268 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3269 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3270
3271 ssl->verify_data_len = 0;
3272 memset( ssl->own_verify_data, 0, 36 );
3273 memset( ssl->peer_verify_data, 0, 36 );
3274
Paul Bakker7eb013f2011-10-06 12:37:39 +00003275 ssl->in_offt = NULL;
3276
Paul Bakker92be97b2013-01-02 17:30:03 +01003277 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003278 ssl->in_msgtype = 0;
3279 ssl->in_msglen = 0;
3280 ssl->in_left = 0;
3281
3282 ssl->in_hslen = 0;
3283 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003284 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003285
Paul Bakker92be97b2013-01-02 17:30:03 +01003286 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003287 ssl->out_msgtype = 0;
3288 ssl->out_msglen = 0;
3289 ssl->out_left = 0;
3290
Paul Bakker48916f92012-09-16 19:57:18 +00003291 ssl->transform_in = NULL;
3292 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003293
3294 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3295 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003296
3297#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3298 if( ssl_hw_record_reset != NULL)
3299 {
3300 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003301 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003302 {
3303 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3304 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3305 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003306 }
3307#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003308
Paul Bakker48916f92012-09-16 19:57:18 +00003309 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003310 {
Paul Bakker48916f92012-09-16 19:57:18 +00003311 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003312 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003313 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003314 }
Paul Bakker48916f92012-09-16 19:57:18 +00003315
Paul Bakkerc0463502013-02-14 11:19:38 +01003316 if( ssl->session )
3317 {
3318 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003319 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003320 ssl->session = NULL;
3321 }
3322
Paul Bakker48916f92012-09-16 19:57:18 +00003323 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3324 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003325
3326 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003327}
3328
Paul Bakkera503a632013-08-14 13:48:06 +02003329#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003330/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003331 * Allocate and initialize ticket keys
3332 */
3333static int ssl_ticket_keys_init( ssl_context *ssl )
3334{
3335 int ret;
3336 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003337 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003338
3339 if( ssl->ticket_keys != NULL )
3340 return( 0 );
3341
3342 if( ( tkeys = polarssl_malloc( sizeof( ssl_ticket_keys ) ) ) == NULL )
3343 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3344
3345 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
3346 return( ret );
3347
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003348 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3349 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3350 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3351 {
3352 return( ret );
3353 }
3354
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003355 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
3356 return( ret );
3357
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003358 ssl->ticket_keys = tkeys;
3359
3360 return( 0 );
3361}
Paul Bakkera503a632013-08-14 13:48:06 +02003362#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003363
3364/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003365 * SSL set accessors
3366 */
3367void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3368{
3369 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003370
Paul Bakker606b4ba2013-08-14 16:52:14 +02003371#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003372 if( endpoint == SSL_IS_CLIENT )
3373 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003374#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003375}
3376
3377void ssl_set_authmode( ssl_context *ssl, int authmode )
3378{
3379 ssl->authmode = authmode;
3380}
3381
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003382#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003383void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003384 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003385 void *p_vrfy )
3386{
3387 ssl->f_vrfy = f_vrfy;
3388 ssl->p_vrfy = p_vrfy;
3389}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003390#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003391
Paul Bakker5121ce52009-01-03 21:22:43 +00003392void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003393 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 void *p_rng )
3395{
3396 ssl->f_rng = f_rng;
3397 ssl->p_rng = p_rng;
3398}
3399
3400void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003401 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003402 void *p_dbg )
3403{
3404 ssl->f_dbg = f_dbg;
3405 ssl->p_dbg = p_dbg;
3406}
3407
3408void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003409 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003410 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003411{
3412 ssl->f_recv = f_recv;
3413 ssl->f_send = f_send;
3414 ssl->p_recv = p_recv;
3415 ssl->p_send = p_send;
3416}
3417
Paul Bakker0a597072012-09-25 21:55:46 +00003418void ssl_set_session_cache( ssl_context *ssl,
3419 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3420 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003421{
Paul Bakker0a597072012-09-25 21:55:46 +00003422 ssl->f_get_cache = f_get_cache;
3423 ssl->p_get_cache = p_get_cache;
3424 ssl->f_set_cache = f_set_cache;
3425 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003426}
3427
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003428int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003429{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003430 int ret;
3431
3432 if( ssl == NULL ||
3433 session == NULL ||
3434 ssl->session_negotiate == NULL ||
3435 ssl->endpoint != SSL_IS_CLIENT )
3436 {
3437 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3438 }
3439
3440 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3441 return( ret );
3442
Paul Bakker0a597072012-09-25 21:55:46 +00003443 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003444
3445 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003446}
3447
Paul Bakkerb68cad62012-08-23 08:34:18 +00003448void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003449{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003450 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3451 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3452 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3453 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3454}
3455
3456void ssl_set_ciphersuites_for_version( ssl_context *ssl, const int *ciphersuites,
3457 int major, int minor )
3458{
3459 if( major != SSL_MAJOR_VERSION_3 )
3460 return;
3461
3462 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3463 return;
3464
3465 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003466}
3467
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003468#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003469/* Add a new (empty) key_cert entry an return a pointer to it */
3470static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3471{
3472 ssl_key_cert *key_cert, *last;
3473
3474 if( ( key_cert = polarssl_malloc( sizeof( ssl_key_cert ) ) ) == NULL )
3475 return( NULL );
3476
3477 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3478
3479 /* Append the new key_cert to the (possibly empty) current list */
3480 if( ssl->key_cert == NULL )
3481 ssl->key_cert = key_cert;
3482 else
3483 {
3484 last = ssl->key_cert;
3485 while( last->next != NULL )
3486 last = last->next;
3487 last->next = key_cert;
3488 }
3489
3490 return key_cert;
3491}
3492
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003493void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003494 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003495{
3496 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003497 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003498 ssl->peer_cn = peer_cn;
3499}
3500
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003501int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003502 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003503{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003504 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3505
3506 if( key_cert == NULL )
3507 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3508
3509 key_cert->cert = own_cert;
3510 key_cert->key = pk_key;
3511
3512 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003513}
3514
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003515#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003516int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003517 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003518{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003519 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003520 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003521
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003522 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003523 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3524
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003525 if( ( key_cert->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
3526 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003527
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003528 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003529
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003530 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003531 if( ret != 0 )
3532 return( ret );
3533
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003534 if( ( ret = rsa_copy( key_cert->key->pk_ctx, rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003535 return( ret );
3536
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003537 key_cert->cert = own_cert;
3538 key_cert->key_own_alloc = 1;
3539
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003540 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003541}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003542#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003543
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003544int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003545 void *rsa_key,
3546 rsa_decrypt_func rsa_decrypt,
3547 rsa_sign_func rsa_sign,
3548 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003549{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003550 int ret;
3551 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003552
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003553 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003554 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3555
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003556 if( ( key_cert->key = polarssl_malloc( sizeof( pk_context ) ) ) == NULL )
3557 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003558
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003559 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003560
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003561 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3562 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3563 return( ret );
3564
3565 key_cert->cert = own_cert;
3566 key_cert->key_own_alloc = 1;
3567
3568 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003569}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003570#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003571
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003572#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003573int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3574 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003575{
Paul Bakker6db455e2013-09-18 17:29:31 +02003576 if( psk == NULL || psk_identity == NULL )
3577 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3578
3579 if( ssl->psk != NULL )
3580 {
3581 polarssl_free( ssl->psk );
3582 polarssl_free( ssl->psk_identity );
3583 }
3584
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003585 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003586 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003587
3588 ssl->psk = polarssl_malloc( ssl->psk_len );
3589 ssl->psk_identity = polarssl_malloc( ssl->psk_identity_len );
3590
3591 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3592 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3593
3594 memcpy( ssl->psk, psk, ssl->psk_len );
3595 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003596
3597 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003598}
3599
3600void ssl_set_psk_cb( ssl_context *ssl,
3601 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3602 size_t),
3603 void *p_psk )
3604{
3605 ssl->f_psk = f_psk;
3606 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003607}
3608#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003609
Paul Bakker48916f92012-09-16 19:57:18 +00003610#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003611int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003612{
3613 int ret;
3614
Paul Bakker48916f92012-09-16 19:57:18 +00003615 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003616 {
3617 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3618 return( ret );
3619 }
3620
Paul Bakker48916f92012-09-16 19:57:18 +00003621 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003622 {
3623 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3624 return( ret );
3625 }
3626
3627 return( 0 );
3628}
3629
Paul Bakker1b57b062011-01-06 15:48:19 +00003630int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3631{
3632 int ret;
3633
Paul Bakker48916f92012-09-16 19:57:18 +00003634 if( ( ret = mpi_copy(&ssl->dhm_P, &dhm_ctx->P) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003635 {
3636 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3637 return( ret );
3638 }
3639
Paul Bakker48916f92012-09-16 19:57:18 +00003640 if( ( ret = mpi_copy(&ssl->dhm_G, &dhm_ctx->G) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003641 {
3642 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3643 return( ret );
3644 }
3645
3646 return( 0 );
3647}
Paul Bakker48916f92012-09-16 19:57:18 +00003648#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003649
Paul Bakker0be444a2013-08-27 21:55:01 +02003650#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003651int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003652{
3653 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003654 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003655
3656 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003657
3658 if( ssl->hostname_len + 1 == 0 )
3659 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3660
Paul Bakker6e339b52013-07-03 13:37:05 +02003661 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003662
Paul Bakkerb15b8512012-01-13 13:44:06 +00003663 if( ssl->hostname == NULL )
3664 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3665
Paul Bakker3c2122f2013-06-24 19:03:14 +02003666 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003667 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003668
Paul Bakker40ea7de2009-05-03 10:18:48 +00003669 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003670
3671 return( 0 );
3672}
3673
Paul Bakker5701cdc2012-09-27 21:49:42 +00003674void ssl_set_sni( ssl_context *ssl,
3675 int (*f_sni)(void *, ssl_context *,
3676 const unsigned char *, size_t),
3677 void *p_sni )
3678{
3679 ssl->f_sni = f_sni;
3680 ssl->p_sni = p_sni;
3681}
Paul Bakker0be444a2013-08-27 21:55:01 +02003682#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003683
Paul Bakker490ecc82011-10-06 13:04:09 +00003684void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3685{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003686 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3687 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3688 {
3689 ssl->max_major_ver = major;
3690 ssl->max_minor_ver = minor;
3691 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003692}
3693
Paul Bakker1d29fb52012-09-28 13:28:45 +00003694void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3695{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003696 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3697 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3698 {
3699 ssl->min_major_ver = major;
3700 ssl->min_minor_ver = minor;
3701 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003702}
3703
Paul Bakker05decb22013-08-15 13:33:48 +02003704#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003705int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3706{
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003707 if( mfl_code >= sizeof( mfl_code_to_length ) ||
3708 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003709 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003710 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003711 }
3712
3713 ssl->mfl_code = mfl_code;
3714
3715 return( 0 );
3716}
Paul Bakker05decb22013-08-15 13:33:48 +02003717#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003718
Paul Bakker1f2bc622013-08-15 13:45:55 +02003719#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003720int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003721{
3722 if( ssl->endpoint != SSL_IS_CLIENT )
3723 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3724
Paul Bakker8c1ede62013-07-19 14:14:37 +02003725 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003726
3727 return( 0 );
3728}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003729#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003730
Paul Bakker48916f92012-09-16 19:57:18 +00003731void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3732{
3733 ssl->disable_renegotiation = renegotiation;
3734}
3735
3736void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3737{
3738 ssl->allow_legacy_renegotiation = allow_legacy;
3739}
3740
Paul Bakkera503a632013-08-14 13:48:06 +02003741#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003742int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3743{
3744 ssl->session_tickets = use_tickets;
3745
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003746 if( ssl->endpoint == SSL_IS_CLIENT )
3747 return( 0 );
3748
3749 if( ssl->f_rng == NULL )
3750 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3751
3752 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003753}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003754
3755void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3756{
3757 ssl->ticket_lifetime = lifetime;
3758}
Paul Bakkera503a632013-08-14 13:48:06 +02003759#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003760
Paul Bakker5121ce52009-01-03 21:22:43 +00003761/*
3762 * SSL get accessors
3763 */
Paul Bakker23986e52011-04-24 08:57:21 +00003764size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003765{
3766 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3767}
3768
Paul Bakkerff60ee62010-03-16 21:09:09 +00003769int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003770{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003771 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003772}
3773
Paul Bakkere3166ce2011-01-27 17:40:50 +00003774const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003775{
Paul Bakker926c8e42013-03-06 10:23:34 +01003776 if( ssl == NULL || ssl->session == NULL )
3777 return NULL;
3778
Paul Bakkere3166ce2011-01-27 17:40:50 +00003779 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003780}
3781
Paul Bakker43ca69c2011-01-15 17:35:19 +00003782const char *ssl_get_version( const ssl_context *ssl )
3783{
3784 switch( ssl->minor_ver )
3785 {
3786 case SSL_MINOR_VERSION_0:
3787 return( "SSLv3.0" );
3788
3789 case SSL_MINOR_VERSION_1:
3790 return( "TLSv1.0" );
3791
3792 case SSL_MINOR_VERSION_2:
3793 return( "TLSv1.1" );
3794
Paul Bakker1ef83d62012-04-11 12:09:53 +00003795 case SSL_MINOR_VERSION_3:
3796 return( "TLSv1.2" );
3797
Paul Bakker43ca69c2011-01-15 17:35:19 +00003798 default:
3799 break;
3800 }
3801 return( "unknown" );
3802}
3803
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003804#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003805const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003806{
3807 if( ssl == NULL || ssl->session == NULL )
3808 return NULL;
3809
3810 return ssl->session->peer_cert;
3811}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003812#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003813
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003814int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
3815{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003816 if( ssl == NULL ||
3817 dst == NULL ||
3818 ssl->session == NULL ||
3819 ssl->endpoint != SSL_IS_CLIENT )
3820 {
3821 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3822 }
3823
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003824 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003825}
3826
Paul Bakker5121ce52009-01-03 21:22:43 +00003827/*
Paul Bakker1961b702013-01-25 14:49:24 +01003828 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003829 */
Paul Bakker1961b702013-01-25 14:49:24 +01003830int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003831{
Paul Bakker40e46942009-01-03 21:51:57 +00003832 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00003833
Paul Bakker40e46942009-01-03 21:51:57 +00003834#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01003836 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003837#endif
3838
Paul Bakker40e46942009-01-03 21:51:57 +00003839#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01003841 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003842#endif
3843
Paul Bakker1961b702013-01-25 14:49:24 +01003844 return( ret );
3845}
3846
3847/*
3848 * Perform the SSL handshake
3849 */
3850int ssl_handshake( ssl_context *ssl )
3851{
3852 int ret = 0;
3853
3854 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
3855
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02003856#if defined(POLARSSL_X509_CRT_PARSE_C)
3857 ssl->handshake->key_cert = ssl->key_cert;
3858#endif
3859
Paul Bakker1961b702013-01-25 14:49:24 +01003860 while( ssl->state != SSL_HANDSHAKE_OVER )
3861 {
3862 ret = ssl_handshake_step( ssl );
3863
3864 if( ret != 0 )
3865 break;
3866 }
3867
Paul Bakker5121ce52009-01-03 21:22:43 +00003868 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
3869
3870 return( ret );
3871}
3872
3873/*
Paul Bakker48916f92012-09-16 19:57:18 +00003874 * Renegotiate current connection
3875 */
3876int ssl_renegotiate( ssl_context *ssl )
3877{
3878 int ret;
3879
3880 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
3881
3882 if( ssl->state != SSL_HANDSHAKE_OVER )
3883 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3884
3885 ssl->state = SSL_HELLO_REQUEST;
3886 ssl->renegotiation = SSL_RENEGOTIATION;
3887
3888 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3889 return( ret );
3890
3891 if( ( ret = ssl_handshake( ssl ) ) != 0 )
3892 {
3893 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
3894 return( ret );
3895 }
3896
3897 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
3898
3899 return( 0 );
3900}
3901
3902/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003903 * Receive application data decrypted from the SSL layer
3904 */
Paul Bakker23986e52011-04-24 08:57:21 +00003905int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00003906{
Paul Bakker23986e52011-04-24 08:57:21 +00003907 int ret;
3908 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003909
3910 SSL_DEBUG_MSG( 2, ( "=> read" ) );
3911
3912 if( ssl->state != SSL_HANDSHAKE_OVER )
3913 {
3914 if( ( ret = ssl_handshake( ssl ) ) != 0 )
3915 {
3916 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
3917 return( ret );
3918 }
3919 }
3920
3921 if( ssl->in_offt == NULL )
3922 {
3923 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3924 {
Paul Bakker831a7552011-05-18 13:32:51 +00003925 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
3926 return( 0 );
3927
Paul Bakker5121ce52009-01-03 21:22:43 +00003928 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3929 return( ret );
3930 }
3931
3932 if( ssl->in_msglen == 0 &&
3933 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
3934 {
3935 /*
3936 * OpenSSL sends empty messages to randomize the IV
3937 */
3938 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3939 {
Paul Bakker831a7552011-05-18 13:32:51 +00003940 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
3941 return( 0 );
3942
Paul Bakker5121ce52009-01-03 21:22:43 +00003943 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3944 return( ret );
3945 }
3946 }
3947
Paul Bakker48916f92012-09-16 19:57:18 +00003948 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3949 {
3950 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
3951
3952 if( ssl->endpoint == SSL_IS_CLIENT &&
3953 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
3954 ssl->in_hslen != 4 ) )
3955 {
3956 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
3957 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
3958 }
3959
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003960 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
3961 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
3962 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00003963 {
3964 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
3965
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003966#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003967 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00003968 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003969 /*
3970 * SSLv3 does not have a "no_renegotiation" alert
3971 */
3972 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
3973 return( ret );
3974 }
3975 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003976#endif
3977#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3978 defined(POLARSSL_SSL_PROTO_TLS1_2)
3979 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003980 {
3981 if( ( ret = ssl_send_alert_message( ssl,
3982 SSL_ALERT_LEVEL_WARNING,
3983 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
3984 {
3985 return( ret );
3986 }
Paul Bakker48916f92012-09-16 19:57:18 +00003987 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003988 else
3989#endif
Paul Bakker577e0062013-08-28 11:57:20 +02003990 {
3991 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003992 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02003993 }
Paul Bakker48916f92012-09-16 19:57:18 +00003994 }
3995 else
3996 {
3997 if( ( ret = ssl_renegotiate( ssl ) ) != 0 )
3998 {
3999 SSL_DEBUG_RET( 1, "ssl_renegotiate", ret );
4000 return( ret );
4001 }
4002
4003 return( POLARSSL_ERR_NET_WANT_READ );
4004 }
4005 }
4006 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004007 {
4008 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004009 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010 }
4011
4012 ssl->in_offt = ssl->in_msg;
4013 }
4014
4015 n = ( len < ssl->in_msglen )
4016 ? len : ssl->in_msglen;
4017
4018 memcpy( buf, ssl->in_offt, n );
4019 ssl->in_msglen -= n;
4020
4021 if( ssl->in_msglen == 0 )
4022 /* all bytes consumed */
4023 ssl->in_offt = NULL;
4024 else
4025 /* more data available */
4026 ssl->in_offt += n;
4027
4028 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4029
Paul Bakker23986e52011-04-24 08:57:21 +00004030 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004031}
4032
4033/*
4034 * Send application data to be encrypted by the SSL layer
4035 */
Paul Bakker23986e52011-04-24 08:57:21 +00004036int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004037{
Paul Bakker23986e52011-04-24 08:57:21 +00004038 int ret;
4039 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004040 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004041
4042 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4043
4044 if( ssl->state != SSL_HANDSHAKE_OVER )
4045 {
4046 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4047 {
4048 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4049 return( ret );
4050 }
4051 }
4052
Paul Bakker05decb22013-08-15 13:33:48 +02004053#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004054 /*
4055 * Assume mfl_code is correct since it was checked when set
4056 */
4057 max_len = mfl_code_to_length[ssl->mfl_code];
4058
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004059 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004060 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004061 */
4062 if( ssl->session_out != NULL &&
4063 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4064 {
4065 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4066 }
Paul Bakker05decb22013-08-15 13:33:48 +02004067#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004068
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004069 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004070
Paul Bakker5121ce52009-01-03 21:22:43 +00004071 if( ssl->out_left != 0 )
4072 {
4073 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4074 {
4075 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4076 return( ret );
4077 }
4078 }
Paul Bakker887bd502011-06-08 13:10:54 +00004079 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004080 {
Paul Bakker887bd502011-06-08 13:10:54 +00004081 ssl->out_msglen = n;
4082 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4083 memcpy( ssl->out_msg, buf, n );
4084
4085 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4086 {
4087 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4088 return( ret );
4089 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004090 }
4091
4092 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4093
Paul Bakker23986e52011-04-24 08:57:21 +00004094 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004095}
4096
4097/*
4098 * Notify the peer that the connection is being closed
4099 */
4100int ssl_close_notify( ssl_context *ssl )
4101{
4102 int ret;
4103
4104 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4105
4106 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4107 {
4108 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4109 return( ret );
4110 }
4111
4112 if( ssl->state == SSL_HANDSHAKE_OVER )
4113 {
Paul Bakker48916f92012-09-16 19:57:18 +00004114 if( ( ret = ssl_send_alert_message( ssl,
4115 SSL_ALERT_LEVEL_WARNING,
4116 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004117 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004118 return( ret );
4119 }
4120 }
4121
4122 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4123
4124 return( ret );
4125}
4126
Paul Bakker48916f92012-09-16 19:57:18 +00004127void ssl_transform_free( ssl_transform *transform )
4128{
4129#if defined(POLARSSL_ZLIB_SUPPORT)
4130 deflateEnd( &transform->ctx_deflate );
4131 inflateEnd( &transform->ctx_inflate );
4132#endif
4133
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004134 cipher_free_ctx( &transform->cipher_ctx_enc );
4135 cipher_free_ctx( &transform->cipher_ctx_dec );
4136
Paul Bakker61d113b2013-07-04 11:51:43 +02004137 md_free_ctx( &transform->md_ctx_enc );
4138 md_free_ctx( &transform->md_ctx_dec );
4139
Paul Bakker48916f92012-09-16 19:57:18 +00004140 memset( transform, 0, sizeof( ssl_transform ) );
4141}
4142
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004143#if defined(POLARSSL_X509_CRT_PARSE_C)
4144static void ssl_key_cert_free( ssl_key_cert *key_cert )
4145{
4146 ssl_key_cert *cur = key_cert, *next;
4147
4148 while( cur != NULL )
4149 {
4150 next = cur->next;
4151
4152 if( cur->key_own_alloc )
4153 {
4154 pk_free( cur->key );
4155 polarssl_free( cur->key );
4156 }
4157 polarssl_free( cur );
4158
4159 cur = next;
4160 }
4161}
4162#endif /* POLARSSL_X509_CRT_PARSE_C */
4163
Paul Bakker48916f92012-09-16 19:57:18 +00004164void ssl_handshake_free( ssl_handshake_params *handshake )
4165{
4166#if defined(POLARSSL_DHM_C)
4167 dhm_free( &handshake->dhm_ctx );
4168#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004169#if defined(POLARSSL_ECDH_C)
4170 ecdh_free( &handshake->ecdh_ctx );
4171#endif
4172
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004173#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
4174 polarssl_free( handshake->curves );
4175#endif
4176
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004177#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4178 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4179 /*
4180 * Free only the linked list wrapper, not the keys themselves
4181 * since the belong to the SNI callback
4182 */
4183 if( handshake->sni_key_cert != NULL )
4184 {
4185 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4186
4187 while( cur != NULL )
4188 {
4189 next = cur->next;
4190 polarssl_free( cur );
4191 cur = next;
4192 }
4193 }
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004194#endif
4195
Paul Bakker48916f92012-09-16 19:57:18 +00004196 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4197}
4198
4199void ssl_session_free( ssl_session *session )
4200{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004201#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004202 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004203 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004204 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004205 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004206 }
Paul Bakkered27a042013-04-18 22:46:23 +02004207#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004208
Paul Bakkera503a632013-08-14 13:48:06 +02004209#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004210 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004211#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004212
Paul Bakker0a597072012-09-25 21:55:46 +00004213 memset( session, 0, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004214}
4215
Paul Bakker5121ce52009-01-03 21:22:43 +00004216/*
4217 * Free an SSL context
4218 */
4219void ssl_free( ssl_context *ssl )
4220{
4221 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4222
Paul Bakker5121ce52009-01-03 21:22:43 +00004223 if( ssl->out_ctr != NULL )
4224 {
4225 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004226 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004227 }
4228
4229 if( ssl->in_ctr != NULL )
4230 {
4231 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004232 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004233 }
4234
Paul Bakker40e46942009-01-03 21:51:57 +00004235#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004236 mpi_free( &ssl->dhm_P );
4237 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004238#endif
4239
Paul Bakker48916f92012-09-16 19:57:18 +00004240 if( ssl->transform )
4241 {
4242 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004243 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004244 }
4245
4246 if( ssl->handshake )
4247 {
4248 ssl_handshake_free( ssl->handshake );
4249 ssl_transform_free( ssl->transform_negotiate );
4250 ssl_session_free( ssl->session_negotiate );
4251
Paul Bakker6e339b52013-07-03 13:37:05 +02004252 polarssl_free( ssl->handshake );
4253 polarssl_free( ssl->transform_negotiate );
4254 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004255 }
4256
Paul Bakkerc0463502013-02-14 11:19:38 +01004257 if( ssl->session )
4258 {
4259 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004260 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004261 }
4262
Paul Bakkera503a632013-08-14 13:48:06 +02004263#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004264 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004265#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004266
Paul Bakker0be444a2013-08-27 21:55:01 +02004267#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker6db455e2013-09-18 17:29:31 +02004268 if ( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004269 {
4270 memset( ssl->hostname, 0, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004271 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004272 ssl->hostname_len = 0;
4273 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004274#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004275
Paul Bakker6db455e2013-09-18 17:29:31 +02004276#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
4277 if( ssl->psk != NULL )
4278 {
4279 memset( ssl->psk, 0, ssl->psk_len );
4280 memset( ssl->psk_identity, 0, ssl->psk_identity_len );
4281 polarssl_free( ssl->psk );
4282 polarssl_free( ssl->psk_identity );
4283 ssl->psk_len = 0;
4284 ssl->psk_identity_len = 0;
4285 }
4286#endif
4287
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004288#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004289 ssl_key_cert_free( ssl->key_cert );
4290#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004291
Paul Bakker05ef8352012-05-08 09:17:57 +00004292#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4293 if( ssl_hw_record_finish != NULL )
4294 {
4295 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4296 ssl_hw_record_finish( ssl );
4297 }
4298#endif
4299
Paul Bakker5121ce52009-01-03 21:22:43 +00004300 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004301
Paul Bakker86f04f42013-02-14 11:20:09 +01004302 /* Actually clear after last debug message */
Paul Bakker2da561c2009-02-05 18:00:28 +00004303 memset( ssl, 0, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004304}
4305
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004306#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004307/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004308 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004309 */
4310unsigned char ssl_sig_from_pk( pk_context *pk )
4311{
4312#if defined(POLARSSL_RSA_C)
4313 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4314 return( SSL_SIG_RSA );
4315#endif
4316#if defined(POLARSSL_ECDSA_C)
4317 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4318 return( SSL_SIG_ECDSA );
4319#endif
4320 return( SSL_SIG_ANON );
4321}
4322
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004323pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4324{
4325 switch( sig )
4326 {
4327#if defined(POLARSSL_RSA_C)
4328 case SSL_SIG_RSA:
4329 return( POLARSSL_PK_RSA );
4330#endif
4331#if defined(POLARSSL_ECDSA_C)
4332 case SSL_SIG_ECDSA:
4333 return( POLARSSL_PK_ECDSA );
4334#endif
4335 default:
4336 return( POLARSSL_PK_NONE );
4337 }
4338}
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004339#endif
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004340
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004341/*
4342 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4343 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004344md_type_t ssl_md_alg_from_hash( unsigned char hash )
4345{
4346 switch( hash )
4347 {
4348#if defined(POLARSSL_MD5_C)
4349 case SSL_HASH_MD5:
4350 return( POLARSSL_MD_MD5 );
4351#endif
4352#if defined(POLARSSL_SHA1_C)
4353 case SSL_HASH_SHA1:
4354 return( POLARSSL_MD_SHA1 );
4355#endif
4356#if defined(POLARSSL_SHA256_C)
4357 case SSL_HASH_SHA224:
4358 return( POLARSSL_MD_SHA224 );
4359 case SSL_HASH_SHA256:
4360 return( POLARSSL_MD_SHA256 );
4361#endif
4362#if defined(POLARSSL_SHA512_C)
4363 case SSL_HASH_SHA384:
4364 return( POLARSSL_MD_SHA384 );
4365 case SSL_HASH_SHA512:
4366 return( POLARSSL_MD_SHA512 );
4367#endif
4368 default:
4369 return( POLARSSL_MD_NONE );
4370 }
4371}
4372
Paul Bakker5121ce52009-01-03 21:22:43 +00004373#endif