blob: b608e5c98451bb66f871974c5e52704b9b65b569 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
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 = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200473#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
474 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200475 {
476 unsigned char session_hash[48];
477 size_t hash_len;
478
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200479 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200480
481 ssl->handshake->calc_verify( ssl, session_hash );
482
483#if defined(POLARSSL_SSL_PROTO_TLS1_2)
484 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
485 {
486#if defined(POLARSSL_SHA512_C)
487 if( ssl->transform_negotiate->ciphersuite_info->mac ==
488 POLARSSL_MD_SHA384 )
489 {
490 hash_len = 48;
491 }
492 else
493#endif
494 hash_len = 32;
495 }
496 else
497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
498 hash_len = 36;
499
500 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
501
502 handshake->tls_prf( handshake->premaster, handshake->pmslen,
503 "extended master secret",
504 session_hash, hash_len, session->master, 48 );
505
506 }
507 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200508#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf( handshake->premaster, handshake->pmslen,
510 "master secret",
511 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200513
Paul Bakker34617722014-06-13 17:20:13 +0200514 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 }
516 else
517 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
518
519 /*
520 * Swap the client and server random values.
521 */
Paul Bakker48916f92012-09-16 19:57:18 +0000522 memcpy( tmp, handshake->randbytes, 64 );
523 memcpy( handshake->randbytes, tmp + 32, 32 );
524 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200525 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 /*
528 * SSLv3:
529 * key block =
530 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
532 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
533 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
534 * ...
535 *
536 * TLSv1:
537 * key block = PRF( master, "key expansion", randbytes )
538 */
Paul Bakker48916f92012-09-16 19:57:18 +0000539 handshake->tls_prf( session->master, 48, "key expansion",
540 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
543 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
544 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
545 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
547
Paul Bakker34617722014-06-13 17:20:13 +0200548 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 /*
551 * Determine the appropriate key, IV and MAC length.
552 */
Paul Bakker68884e32013-01-07 18:20:04 +0100553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 transform->keylen = cipher_info->key_length / 8;
555
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200556 if( cipher_info->mode == POLARSSL_MODE_GCM ||
557 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200559 transform->maclen = 0;
560
Paul Bakker68884e32013-01-07 18:20:04 +0100561 transform->ivlen = 12;
562 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200563
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200564 /* Minimum length is expicit IV + tag */
565 transform->minlen = transform->ivlen - transform->fixed_ivlen
566 + ( transform->ciphersuite_info->flags &
567 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100568 }
569 else
570 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200571 int ret;
572
573 /* Initialize HMAC contexts */
574 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
575 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100576 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
578 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100579 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200581 /* Get MAC length */
582 transform->maclen = md_get_size( md_info );
583
584#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
585 /*
586 * If HMAC is to be truncated, we shall keep the leftmost bytes,
587 * (rfc 6066 page 13 or rfc 2104 section 4),
588 * so we only need to adjust the length here.
589 */
590 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
591 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
592#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
593
594 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100595 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200597 /* Minimum length */
598 if( cipher_info->mode == POLARSSL_MODE_STREAM )
599 transform->minlen = transform->maclen;
600 else
Paul Bakker68884e32013-01-07 18:20:04 +0100601 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200602 /*
603 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100604 * 1. if EtM is in use: one block plus MAC
605 * otherwise: * first multiple of blocklen greater than maclen
606 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200607 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100608#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
609 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
610 {
611 transform->minlen = transform->maclen
612 + cipher_info->block_size;
613 }
614 else
615#endif
616 {
617 transform->minlen = transform->maclen
618 + cipher_info->block_size
619 - transform->maclen % cipher_info->block_size;
620 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200621
622#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
623 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
624 ssl->minor_ver == SSL_MINOR_VERSION_1 )
625 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100626 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200627#endif
628#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
629 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
630 ssl->minor_ver == SSL_MINOR_VERSION_3 )
631 {
632 transform->minlen += transform->ivlen;
633 }
634 else
635#endif
636 {
637 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
638 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
639 }
Paul Bakker68884e32013-01-07 18:20:04 +0100640 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 }
642
643 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000644 transform->keylen, transform->minlen, transform->ivlen,
645 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 /*
648 * Finally setup the cipher contexts, IVs and MAC secrets.
649 */
650 if( ssl->endpoint == SSL_IS_CLIENT )
651 {
Paul Bakker48916f92012-09-16 19:57:18 +0000652 key1 = keyblk + transform->maclen * 2;
653 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Paul Bakker68884e32013-01-07 18:20:04 +0100655 mac_enc = keyblk;
656 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000658 /*
659 * This is not used in TLS v1.1.
660 */
Paul Bakker48916f92012-09-16 19:57:18 +0000661 iv_copy_len = ( transform->fixed_ivlen ) ?
662 transform->fixed_ivlen : transform->ivlen;
663 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
664 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000665 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 }
667 else
668 {
Paul Bakker48916f92012-09-16 19:57:18 +0000669 key1 = keyblk + transform->maclen * 2 + transform->keylen;
670 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Paul Bakker68884e32013-01-07 18:20:04 +0100672 mac_enc = keyblk + transform->maclen;
673 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000675 /*
676 * This is not used in TLS v1.1.
677 */
Paul Bakker48916f92012-09-16 19:57:18 +0000678 iv_copy_len = ( transform->fixed_ivlen ) ?
679 transform->fixed_ivlen : transform->ivlen;
680 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
681 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000682 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 }
684
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200685#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100686 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
687 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100688 if( transform->maclen > sizeof transform->mac_enc )
689 {
690 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200691 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100692 }
693
Paul Bakker68884e32013-01-07 18:20:04 +0100694 memcpy( transform->mac_enc, mac_enc, transform->maclen );
695 memcpy( transform->mac_dec, mac_dec, transform->maclen );
696 }
697 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200698#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200699#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
700 defined(POLARSSL_SSL_PROTO_TLS1_2)
701 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100702 {
703 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
704 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
705 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200706 else
707#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200708 {
709 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200710 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200711 }
Paul Bakker68884e32013-01-07 18:20:04 +0100712
Paul Bakker05ef8352012-05-08 09:17:57 +0000713#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200714 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000715 {
716 int ret = 0;
717
718 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
719
Paul Bakker07eb38b2012-12-19 14:42:06 +0100720 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
721 transform->iv_enc, transform->iv_dec,
722 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100723 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100724 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000725 {
726 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200727 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000728 }
729 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200730#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000731
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200732 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
733 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200735 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
736 return( ret );
737 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200738
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200739 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
740 cipher_info ) ) != 0 )
741 {
742 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
743 return( ret );
744 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200745
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200746 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
747 cipher_info->key_length,
748 POLARSSL_ENCRYPT ) ) != 0 )
749 {
750 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
751 return( ret );
752 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200753
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200754 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
755 cipher_info->key_length,
756 POLARSSL_DECRYPT ) ) != 0 )
757 {
758 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
759 return( ret );
760 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200761
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200762#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200763 if( cipher_info->mode == POLARSSL_MODE_CBC )
764 {
765 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
766 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200767 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200768 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
769 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200770 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200771
772 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
773 POLARSSL_PADDING_NONE ) ) != 0 )
774 {
775 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
776 return( ret );
777 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200779#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000780
Paul Bakker34617722014-06-13 17:20:13 +0200781 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Paul Bakker2770fbd2012-07-03 13:30:23 +0000783#if defined(POLARSSL_ZLIB_SUPPORT)
784 // Initialize compression
785 //
Paul Bakker48916f92012-09-16 19:57:18 +0000786 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000787 {
Paul Bakker16770332013-10-11 09:59:44 +0200788 if( ssl->compress_buf == NULL )
789 {
790 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
791 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
792 if( ssl->compress_buf == NULL )
793 {
794 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
795 SSL_BUFFER_LEN ) );
796 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
797 }
798 }
799
Paul Bakker2770fbd2012-07-03 13:30:23 +0000800 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
801
Paul Bakker48916f92012-09-16 19:57:18 +0000802 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
803 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000804
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200805 if( deflateInit( &transform->ctx_deflate,
806 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000807 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000808 {
809 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
810 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
811 }
812 }
813#endif /* POLARSSL_ZLIB_SUPPORT */
814
Paul Bakker5121ce52009-01-03 21:22:43 +0000815 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
816
817 return( 0 );
818}
819
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200820#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000821void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000822{
823 md5_context md5;
824 sha1_context sha1;
825 unsigned char pad_1[48];
826 unsigned char pad_2[48];
827
Paul Bakker380da532012-04-18 16:10:25 +0000828 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Paul Bakker48916f92012-09-16 19:57:18 +0000830 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
831 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000832
Paul Bakker380da532012-04-18 16:10:25 +0000833 memset( pad_1, 0x36, 48 );
834 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Paul Bakker48916f92012-09-16 19:57:18 +0000836 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000837 md5_update( &md5, pad_1, 48 );
838 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Paul Bakker380da532012-04-18 16:10:25 +0000840 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000841 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000842 md5_update( &md5, pad_2, 48 );
843 md5_update( &md5, hash, 16 );
844 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Paul Bakker48916f92012-09-16 19:57:18 +0000846 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000847 sha1_update( &sha1, pad_1, 40 );
848 sha1_finish( &sha1, hash + 16 );
849
850 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000851 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000852 sha1_update( &sha1, pad_2, 40 );
853 sha1_update( &sha1, hash + 16, 20 );
854 sha1_finish( &sha1, hash + 16 );
855
856 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
857 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
858
Paul Bakker5b4af392014-06-26 12:09:34 +0200859 md5_free( &md5 );
860 sha1_free( &sha1 );
861
Paul Bakker380da532012-04-18 16:10:25 +0000862 return;
863}
Paul Bakker9af723c2014-05-01 13:03:14 +0200864#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000865
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200866#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000867void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
868{
869 md5_context md5;
870 sha1_context sha1;
871
872 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
873
Paul Bakker48916f92012-09-16 19:57:18 +0000874 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
875 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000876
Paul Bakker48916f92012-09-16 19:57:18 +0000877 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000878 sha1_finish( &sha1, hash + 16 );
879
880 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
881 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
882
Paul Bakker5b4af392014-06-26 12:09:34 +0200883 md5_free( &md5 );
884 sha1_free( &sha1 );
885
Paul Bakker380da532012-04-18 16:10:25 +0000886 return;
887}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200888#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000889
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200890#if defined(POLARSSL_SSL_PROTO_TLS1_2)
891#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000892void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
893{
Paul Bakker9e36f042013-06-30 14:34:05 +0200894 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000895
896 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
897
Paul Bakker9e36f042013-06-30 14:34:05 +0200898 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
899 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000900
901 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
902 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
903
Paul Bakker5b4af392014-06-26 12:09:34 +0200904 sha256_free( &sha256 );
905
Paul Bakker380da532012-04-18 16:10:25 +0000906 return;
907}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200908#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000909
Paul Bakker9e36f042013-06-30 14:34:05 +0200910#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000911void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
912{
Paul Bakker9e36f042013-06-30 14:34:05 +0200913 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000914
915 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
916
Paul Bakker9e36f042013-06-30 14:34:05 +0200917 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
918 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
Paul Bakkerca4ab492012-04-18 14:23:57 +0000920 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
922
Paul Bakker5b4af392014-06-26 12:09:34 +0200923 sha512_free( &sha512 );
924
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 return;
926}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200927#endif /* POLARSSL_SHA512_C */
928#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200930#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200931int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
932{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200933 unsigned char *p = ssl->handshake->premaster;
934 unsigned char *end = p + sizeof( ssl->handshake->premaster );
935
936 /*
937 * PMS = struct {
938 * opaque other_secret<0..2^16-1>;
939 * opaque psk<0..2^16-1>;
940 * };
941 * with "other_secret" depending on the particular key exchange
942 */
943#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
944 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
945 {
946 if( end - p < 2 + (int) ssl->psk_len )
947 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
948
949 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
950 *(p++) = (unsigned char)( ssl->psk_len );
951 p += ssl->psk_len;
952 }
953 else
954#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200955#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
956 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
957 {
958 /*
959 * other_secret already set by the ClientKeyExchange message,
960 * and is 48 bytes long
961 */
962 *p++ = 0;
963 *p++ = 48;
964 p += 48;
965 }
966 else
967#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200968#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
969 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
970 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200971 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200972 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200973
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200974 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200975 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200976 p + 2, &len,
977 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200978 {
979 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
980 return( ret );
981 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200982 *(p++) = (unsigned char)( len >> 8 );
983 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200984 p += len;
985
986 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
987 }
988 else
989#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
990#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
991 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
992 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200993 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200994 size_t zlen;
995
996 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200997 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200998 ssl->f_rng, ssl->p_rng ) ) != 0 )
999 {
1000 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1001 return( ret );
1002 }
1003
1004 *(p++) = (unsigned char)( zlen >> 8 );
1005 *(p++) = (unsigned char)( zlen );
1006 p += zlen;
1007
1008 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1009 }
1010 else
1011#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1012 {
1013 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001014 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001015 }
1016
1017 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001018 if( end - p < 2 + (int) ssl->psk_len )
1019 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1020
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001021 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1022 *(p++) = (unsigned char)( ssl->psk_len );
1023 memcpy( p, ssl->psk, ssl->psk_len );
1024 p += ssl->psk_len;
1025
1026 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1027
1028 return( 0 );
1029}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001030#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001031
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001032#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001033/*
1034 * SSLv3.0 MAC functions
1035 */
Paul Bakker68884e32013-01-07 18:20:04 +01001036static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1037 unsigned char *buf, size_t len,
1038 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001039{
1040 unsigned char header[11];
1041 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001042 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001043 int md_size = md_get_size( md_ctx->md_info );
1044 int md_type = md_get_type( md_ctx->md_info );
1045
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001046 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001047 if( md_type == POLARSSL_MD_MD5 )
1048 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001049 else
Paul Bakker68884e32013-01-07 18:20:04 +01001050 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001051
1052 memcpy( header, ctr, 8 );
1053 header[ 8] = (unsigned char) type;
1054 header[ 9] = (unsigned char)( len >> 8 );
1055 header[10] = (unsigned char)( len );
1056
Paul Bakker68884e32013-01-07 18:20:04 +01001057 memset( padding, 0x36, padlen );
1058 md_starts( md_ctx );
1059 md_update( md_ctx, secret, md_size );
1060 md_update( md_ctx, padding, padlen );
1061 md_update( md_ctx, header, 11 );
1062 md_update( md_ctx, buf, len );
1063 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001064
Paul Bakker68884e32013-01-07 18:20:04 +01001065 memset( padding, 0x5C, padlen );
1066 md_starts( md_ctx );
1067 md_update( md_ctx, secret, md_size );
1068 md_update( md_ctx, padding, padlen );
1069 md_update( md_ctx, buf + len, md_size );
1070 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001071}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001072#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001073
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001074#define MAC_NONE 0
1075#define MAC_PLAINTEXT 1
1076#define MAC_CIPHERTEXT 2
1077
1078/*
1079 * Is MAC applied on ciphertext, cleartext or not at all?
1080 */
1081static char ssl_get_mac_order( ssl_context *ssl,
1082 const ssl_session *session,
1083 cipher_mode_t mode )
1084{
1085#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
1086 if( mode == POLARSSL_MODE_STREAM )
1087 return( MAC_PLAINTEXT );
1088#endif
1089
1090#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1091 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
1092 if( mode == POLARSSL_MODE_CBC )
1093 {
1094#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1095 if( session != NULL && session->encrypt_then_mac == SSL_ETM_ENABLED )
1096 {
1097 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1098 return( MAC_CIPHERTEXT );
1099 }
1100#endif
1101
1102 return( MAC_PLAINTEXT );
1103 }
1104#endif
1105
1106 return( MAC_NONE );
1107}
1108
Paul Bakker5121ce52009-01-03 21:22:43 +00001109/*
1110 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001111 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001112static int ssl_encrypt_buf( ssl_context *ssl )
1113{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001114 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001115 const cipher_mode_t mode = cipher_get_cipher_mode(
1116 &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001117 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
1119 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1120
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001121 mac_order = ssl_get_mac_order( ssl, ssl->session_out, mode );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001122
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001124 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001125 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001126#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1127 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1128 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001129 if( mac_order == MAC_PLAINTEXT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001131#if defined(POLARSSL_SSL_PROTO_SSL3)
1132 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1133 {
1134 ssl_mac( &ssl->transform_out->md_ctx_enc,
1135 ssl->transform_out->mac_enc,
1136 ssl->out_msg, ssl->out_msglen,
1137 ssl->out_ctr, ssl->out_msgtype );
1138 }
1139 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001140#endif
1141#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001142 defined(POLARSSL_SSL_PROTO_TLS1_2)
1143 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1144 {
1145 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1146 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1147 ssl->out_msg, ssl->out_msglen );
1148 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1149 ssl->out_msg + ssl->out_msglen );
1150 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1151 }
1152 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001153#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001154 {
1155 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001156 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001157 }
1158
1159 SSL_DEBUG_BUF( 4, "computed mac",
1160 ssl->out_msg + ssl->out_msglen,
1161 ssl->transform_out->maclen );
1162
1163 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001164 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001165#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001167 /*
1168 * Encrypt
1169 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001170#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001171 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001173 int ret;
1174 size_t olen = 0;
1175
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1177 "including %d bytes of padding",
1178 ssl->out_msglen, 0 ) );
1179
1180 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1181 ssl->out_msg, ssl->out_msglen );
1182
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001183 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001184 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001185 ssl->transform_out->ivlen,
1186 ssl->out_msg, ssl->out_msglen,
1187 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001188 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001189 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001190 return( ret );
1191 }
1192
1193 if( ssl->out_msglen != olen )
1194 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001195 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001196 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 }
Paul Bakker68884e32013-01-07 18:20:04 +01001199 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001200#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001201#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1202 if( mode == POLARSSL_MODE_GCM ||
1203 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001204 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001205 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001206 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001207 unsigned char *enc_msg;
1208 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001209 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1210 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001211
Paul Bakkerca4ab492012-04-18 14:23:57 +00001212 memcpy( add_data, ssl->out_ctr, 8 );
1213 add_data[8] = ssl->out_msgtype;
1214 add_data[9] = ssl->major_ver;
1215 add_data[10] = ssl->minor_ver;
1216 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1217 add_data[12] = ssl->out_msglen & 0xFF;
1218
1219 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1220 add_data, 13 );
1221
Paul Bakker68884e32013-01-07 18:20:04 +01001222 /*
1223 * Generate IV
1224 */
1225 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001226 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1227 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001228 if( ret != 0 )
1229 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001230
Paul Bakker68884e32013-01-07 18:20:04 +01001231 memcpy( ssl->out_iv,
1232 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1233 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001234
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001235 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001236 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001237
Paul Bakker68884e32013-01-07 18:20:04 +01001238 /*
1239 * Fix pointer positions and message length with added IV
1240 */
1241 enc_msg = ssl->out_msg;
1242 enc_msglen = ssl->out_msglen;
1243 ssl->out_msglen += ssl->transform_out->ivlen -
1244 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001245
Paul Bakker68884e32013-01-07 18:20:04 +01001246 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1247 "including %d bytes of padding",
1248 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001249
Paul Bakker68884e32013-01-07 18:20:04 +01001250 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1251 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001252
Paul Bakker68884e32013-01-07 18:20:04 +01001253 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001254 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001255 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001256 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1257 ssl->transform_out->iv_enc,
1258 ssl->transform_out->ivlen,
1259 add_data, 13,
1260 enc_msg, enc_msglen,
1261 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001262 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001263 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001264 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001265 return( ret );
1266 }
1267
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001268 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001269 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001270 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001271 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001272 }
1273
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001274 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001275
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001276 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001279#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001280#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1281 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001282 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001284 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001285 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001286 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001287
Paul Bakker48916f92012-09-16 19:57:18 +00001288 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1289 ssl->transform_out->ivlen;
1290 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 padlen = 0;
1292
1293 for( i = 0; i <= padlen; i++ )
1294 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1295
1296 ssl->out_msglen += padlen + 1;
1297
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001298 enc_msglen = ssl->out_msglen;
1299 enc_msg = ssl->out_msg;
1300
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001301#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001302 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001303 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1304 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001306 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001307 {
1308 /*
1309 * Generate IV
1310 */
Paul Bakker48916f92012-09-16 19:57:18 +00001311 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1312 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001313 if( ret != 0 )
1314 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001315
Paul Bakker92be97b2013-01-02 17:30:03 +01001316 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001317 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001318
1319 /*
1320 * Fix pointer positions and message length with added IV
1321 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001322 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001323 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001324 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001325 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001326#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001327
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001329 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001330 ssl->out_msglen, ssl->transform_out->ivlen,
1331 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
1333 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001334 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001336 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001337 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001338 ssl->transform_out->ivlen,
1339 enc_msg, enc_msglen,
1340 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001341 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001342 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001343 return( ret );
1344 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001345
Paul Bakkercca5b812013-08-31 17:40:26 +02001346 if( enc_msglen != olen )
1347 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001348 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001349 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001350 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001351
1352#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001353 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1354 {
1355 /*
1356 * Save IV in SSL3 and TLS1
1357 */
1358 memcpy( ssl->transform_out->iv_enc,
1359 ssl->transform_out->cipher_ctx_enc.iv,
1360 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001362#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001363
1364#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1365 if( mac_order == MAC_CIPHERTEXT )
1366 {
1367 /*
1368 * MAC(MAC_write_key, seq_num +
1369 * TLSCipherText.type +
1370 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001371 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001372 * IV + // except for TLS 1.0
1373 * ENC(content + padding + padding_length));
1374 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001375 unsigned char pseudo_hdr[13];
1376
1377 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1378 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001379 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1380 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1381
1382 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001383
1384 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1385 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1386 ssl->out_iv, ssl->out_msglen );
1387 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1388 ssl->out_iv + ssl->out_msglen );
1389 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1390
1391 ssl->out_msglen += ssl->transform_out->maclen;
1392 }
1393#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001395 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001396#endif /* POLARSSL_CIPHER_MODE_CBC &&
1397 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001398 {
1399 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001400 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001401 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Paul Bakkerca4ab492012-04-18 14:23:57 +00001403 for( i = 8; i > 0; i-- )
1404 if( ++ssl->out_ctr[i - 1] != 0 )
1405 break;
1406
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001407 /* The loops goes to its end iff the counter is wrapping */
1408 if( i == 0 )
1409 {
1410 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1411 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1412 }
1413
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1415
1416 return( 0 );
1417}
1418
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001419#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001420
Paul Bakker5121ce52009-01-03 21:22:43 +00001421static int ssl_decrypt_buf( ssl_context *ssl )
1422{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001423 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001424 const cipher_mode_t mode = cipher_get_cipher_mode(
1425 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001426#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1427 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1428 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1429 size_t padlen = 0, correct = 1;
1430#endif
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001431 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
1433 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1434
Paul Bakker48916f92012-09-16 19:57:18 +00001435 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 {
1437 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001438 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001439 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
1441
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001442 mac_order = ssl_get_mac_order( ssl, ssl->session_in, mode );
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001443
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001444#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001445 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001446 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001447 int ret;
1448 size_t olen = 0;
1449
Paul Bakker68884e32013-01-07 18:20:04 +01001450 padlen = 0;
1451
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001452 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001453 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001454 ssl->transform_in->ivlen,
1455 ssl->in_msg, ssl->in_msglen,
1456 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001457 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001458 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001459 return( ret );
1460 }
1461
1462 if( ssl->in_msglen != olen )
1463 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001464 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001465 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 }
Paul Bakker68884e32013-01-07 18:20:04 +01001468 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001469#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001470#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1471 if( mode == POLARSSL_MODE_GCM ||
1472 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001473 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001474 int ret;
1475 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001476 unsigned char *dec_msg;
1477 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001478 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001479 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1480 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001481 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1482 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001483
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001484 if( ssl->in_msglen < explicit_iv_len + taglen )
1485 {
1486 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1487 "+ taglen (%d)", ssl->in_msglen,
1488 explicit_iv_len, taglen ) );
1489 return( POLARSSL_ERR_SSL_INVALID_MAC );
1490 }
1491 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1492
Paul Bakker68884e32013-01-07 18:20:04 +01001493 dec_msg = ssl->in_msg;
1494 dec_msg_result = ssl->in_msg;
1495 ssl->in_msglen = dec_msglen;
1496
1497 memcpy( add_data, ssl->in_ctr, 8 );
1498 add_data[8] = ssl->in_msgtype;
1499 add_data[9] = ssl->major_ver;
1500 add_data[10] = ssl->minor_ver;
1501 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1502 add_data[12] = ssl->in_msglen & 0xFF;
1503
1504 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1505 add_data, 13 );
1506
1507 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1508 ssl->in_iv,
1509 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1510
1511 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1512 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001513 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001514
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001515 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001516 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001517 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001518 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1519 ssl->transform_in->iv_dec,
1520 ssl->transform_in->ivlen,
1521 add_data, 13,
1522 dec_msg, dec_msglen,
1523 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001524 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001525 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001526 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1527
1528 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1529 return( POLARSSL_ERR_SSL_INVALID_MAC );
1530
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001531 return( ret );
1532 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001533
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001534 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001535 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001536 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001537 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001538 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001541#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001542#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1543 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001544 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 {
Paul Bakker45829992013-01-03 14:52:21 +01001546 /*
1547 * Decrypt and check the padding
1548 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001549 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001550 unsigned char *dec_msg;
1551 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001552 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001553 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001554 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001555
Paul Bakker5121ce52009-01-03 21:22:43 +00001556 /*
Paul Bakker45829992013-01-03 14:52:21 +01001557 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001559#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001560 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1561 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001562#endif
Paul Bakker45829992013-01-03 14:52:21 +01001563
1564 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1565 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1566 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001567 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1568 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1569 ssl->transform_in->ivlen,
1570 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001571 return( POLARSSL_ERR_SSL_INVALID_MAC );
1572 }
1573
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001574 dec_msglen = ssl->in_msglen;
1575 dec_msg = ssl->in_msg;
1576 dec_msg_result = ssl->in_msg;
1577
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001578 /*
1579 * Authenticate before decrypt if enabled
1580 */
1581#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1582 if( mac_order == MAC_CIPHERTEXT )
1583 {
1584 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001585 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001586
1587 dec_msglen -= ssl->transform_in->maclen;
1588 ssl->in_msglen -= ssl->transform_in->maclen;
1589
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001590 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1591 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1592 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1593 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1594
1595 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1596
1597 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001598 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1599 ssl->in_iv, ssl->in_msglen );
1600 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1601 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1602
1603 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1604 ssl->transform_in->maclen );
1605 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1606 ssl->transform_in->maclen );
1607
1608 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1609 ssl->transform_in->maclen ) != 0 )
1610 {
1611 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1612
1613 return( POLARSSL_ERR_SSL_INVALID_MAC );
1614 }
1615 }
1616#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1617
1618 /*
1619 * Check length sanity
1620 */
1621 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1622 {
1623 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1624 ssl->in_msglen, ssl->transform_in->ivlen ) );
1625 return( POLARSSL_ERR_SSL_INVALID_MAC );
1626 }
1627
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001628#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001629 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001630 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001631 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001632 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001633 {
Paul Bakker48916f92012-09-16 19:57:18 +00001634 dec_msglen -= ssl->transform_in->ivlen;
1635 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001636
Paul Bakker48916f92012-09-16 19:57:18 +00001637 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001638 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001639 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001640#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001641
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001642 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001643 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001644 ssl->transform_in->ivlen,
1645 dec_msg, dec_msglen,
1646 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001647 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001648 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001649 return( ret );
1650 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001651
Paul Bakkercca5b812013-08-31 17:40:26 +02001652 if( dec_msglen != olen )
1653 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001654 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001655 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001656 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001657
1658#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001659 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1660 {
1661 /*
1662 * Save IV in SSL3 and TLS1
1663 */
1664 memcpy( ssl->transform_in->iv_dec,
1665 ssl->transform_in->cipher_ctx_dec.iv,
1666 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001668#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
1670 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001671
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001672 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
1673 mac_order == MAC_PLAINTEXT )
Paul Bakker45829992013-01-03 14:52:21 +01001674 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001675#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001676 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1677 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001678#endif
Paul Bakker45829992013-01-03 14:52:21 +01001679 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001680 correct = 0;
1681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001683#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001684 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1685 {
Paul Bakker48916f92012-09-16 19:57:18 +00001686 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001688#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001689 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1690 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001691 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001692#endif
Paul Bakker45829992013-01-03 14:52:21 +01001693 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 }
1695 }
1696 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001697#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001698#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1699 defined(POLARSSL_SSL_PROTO_TLS1_2)
1700 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 {
1702 /*
Paul Bakker45829992013-01-03 14:52:21 +01001703 * TLSv1+: always check the padding up to the first failure
1704 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001706 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001707 size_t padding_idx = ssl->in_msglen - padlen - 1;
1708
Paul Bakker956c9e02013-12-19 14:42:28 +01001709 /*
1710 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001711 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001712 *
Paul Bakker61885c72014-04-25 12:59:03 +02001713 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1714 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001715 *
1716 * In both cases we reset padding_idx to a safe value (0) to
1717 * prevent out-of-buffer reads.
1718 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001719 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001720 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1721 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001722
1723 padding_idx *= correct;
1724
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001725 for( i = 1; i <= 256; i++ )
1726 {
1727 real_count &= ( i <= padlen );
1728 pad_count += real_count *
1729 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1730 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001731
1732 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001733
Paul Bakkerd66f0702013-01-31 16:57:45 +01001734#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001735 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001736 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001737#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001738 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001739 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001740 else
1741#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1742 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001743 {
1744 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001745 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001746 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001747
1748 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001749 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001750 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001751#endif /* POLARSSL_CIPHER_MODE_CBC &&
1752 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001753 {
1754 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001755 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001756 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001757
1758 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1759 ssl->in_msg, ssl->in_msglen );
1760
1761 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001762 * Authenticate if not done yet.
1763 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001765#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1766 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1767 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001768 if( mac_order == MAC_PLAINTEXT )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001769 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001770 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1771
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001772 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001774 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1775 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001776
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001777 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001779#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001780 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1781 {
1782 ssl_mac( &ssl->transform_in->md_ctx_dec,
1783 ssl->transform_in->mac_dec,
1784 ssl->in_msg, ssl->in_msglen,
1785 ssl->in_ctr, ssl->in_msgtype );
1786 }
1787 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001788#endif /* POLARSSL_SSL_PROTO_SSL3 */
1789#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001790 defined(POLARSSL_SSL_PROTO_TLS1_2)
1791 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1792 {
1793 /*
1794 * Process MAC and always update for padlen afterwards to make
1795 * total time independent of padlen
1796 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001797 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001798 *
1799 * Known timing attacks:
1800 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1801 *
1802 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1803 * correctly. (We round down instead of up, so -56 is the correct
1804 * value for our calculations instead of -55)
1805 */
1806 size_t j, extra_run = 0;
1807 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1808 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001809
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001810 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001811
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001812 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1813 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1814 ssl->in_msglen );
1815 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1816 ssl->in_msg + ssl->in_msglen );
1817 for( j = 0; j < extra_run; j++ )
1818 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001819
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1821 }
1822 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001823#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001824 POLARSSL_SSL_PROTO_TLS1_2 */
1825 {
1826 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001827 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001828 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001829
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1831 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1832 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001834 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001835 ssl->transform_in->maclen ) != 0 )
1836 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001837#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001838 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001839#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001840 correct = 0;
1841 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001843 /*
1844 * Finally check the correct flag
1845 */
1846 if( correct == 0 )
1847 return( POLARSSL_ERR_SSL_INVALID_MAC );
1848 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001849#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
1851 if( ssl->in_msglen == 0 )
1852 {
1853 ssl->nb_zero++;
1854
1855 /*
1856 * Three or more empty messages may be a DoS attack
1857 * (excessive CPU consumption).
1858 */
1859 if( ssl->nb_zero > 3 )
1860 {
1861 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1862 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001863 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 }
1865 }
1866 else
1867 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001868
Paul Bakker23986e52011-04-24 08:57:21 +00001869 for( i = 8; i > 0; i-- )
1870 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 break;
1872
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001873 /* The loops goes to its end iff the counter is wrapping */
1874 if( i == 0 )
1875 {
1876 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1877 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1878 }
1879
Paul Bakker5121ce52009-01-03 21:22:43 +00001880 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1881
1882 return( 0 );
1883}
1884
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001885#undef MAC_NONE
1886#undef MAC_PLAINTEXT
1887#undef MAC_CIPHERTEXT
1888
Paul Bakker2770fbd2012-07-03 13:30:23 +00001889#if defined(POLARSSL_ZLIB_SUPPORT)
1890/*
1891 * Compression/decompression functions
1892 */
1893static int ssl_compress_buf( ssl_context *ssl )
1894{
1895 int ret;
1896 unsigned char *msg_post = ssl->out_msg;
1897 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001898 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001899
1900 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1901
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001902 if( len_pre == 0 )
1903 return( 0 );
1904
Paul Bakker2770fbd2012-07-03 13:30:23 +00001905 memcpy( msg_pre, ssl->out_msg, len_pre );
1906
1907 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1908 ssl->out_msglen ) );
1909
1910 SSL_DEBUG_BUF( 4, "before compression: output payload",
1911 ssl->out_msg, ssl->out_msglen );
1912
Paul Bakker48916f92012-09-16 19:57:18 +00001913 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1914 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1915 ssl->transform_out->ctx_deflate.next_out = msg_post;
1916 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001917
Paul Bakker48916f92012-09-16 19:57:18 +00001918 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001919 if( ret != Z_OK )
1920 {
1921 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1922 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1923 }
1924
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001925 ssl->out_msglen = SSL_BUFFER_LEN -
1926 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001927
Paul Bakker2770fbd2012-07-03 13:30:23 +00001928 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1929 ssl->out_msglen ) );
1930
1931 SSL_DEBUG_BUF( 4, "after compression: output payload",
1932 ssl->out_msg, ssl->out_msglen );
1933
1934 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1935
1936 return( 0 );
1937}
1938
1939static int ssl_decompress_buf( ssl_context *ssl )
1940{
1941 int ret;
1942 unsigned char *msg_post = ssl->in_msg;
1943 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001944 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001945
1946 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1947
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001948 if( len_pre == 0 )
1949 return( 0 );
1950
Paul Bakker2770fbd2012-07-03 13:30:23 +00001951 memcpy( msg_pre, ssl->in_msg, len_pre );
1952
1953 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1954 ssl->in_msglen ) );
1955
1956 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1957 ssl->in_msg, ssl->in_msglen );
1958
Paul Bakker48916f92012-09-16 19:57:18 +00001959 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1960 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1961 ssl->transform_in->ctx_inflate.next_out = msg_post;
1962 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001963
Paul Bakker48916f92012-09-16 19:57:18 +00001964 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001965 if( ret != Z_OK )
1966 {
1967 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1968 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1969 }
1970
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001971 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1972 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001973
Paul Bakker2770fbd2012-07-03 13:30:23 +00001974 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1975 ssl->in_msglen ) );
1976
1977 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1978 ssl->in_msg, ssl->in_msglen );
1979
1980 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1981
1982 return( 0 );
1983}
1984#endif /* POLARSSL_ZLIB_SUPPORT */
1985
Paul Bakker5121ce52009-01-03 21:22:43 +00001986/*
1987 * Fill the input message buffer
1988 */
Paul Bakker23986e52011-04-24 08:57:21 +00001989int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001990{
Paul Bakker23986e52011-04-24 08:57:21 +00001991 int ret;
1992 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001993
1994 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1995
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001996 if( nb_want > SSL_BUFFER_LEN - 8 )
1997 {
1998 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1999 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2000 }
2001
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 while( ssl->in_left < nb_want )
2003 {
2004 len = nb_want - ssl->in_left;
2005 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2006
2007 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2008 ssl->in_left, nb_want ) );
2009 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2010
Paul Bakker831a7552011-05-18 13:32:51 +00002011 if( ret == 0 )
2012 return( POLARSSL_ERR_SSL_CONN_EOF );
2013
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 if( ret < 0 )
2015 return( ret );
2016
2017 ssl->in_left += ret;
2018 }
2019
2020 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2021
2022 return( 0 );
2023}
2024
2025/*
2026 * Flush any data not yet written
2027 */
2028int ssl_flush_output( ssl_context *ssl )
2029{
2030 int ret;
2031 unsigned char *buf;
2032
2033 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2034
2035 while( ssl->out_left > 0 )
2036 {
2037 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2038 5 + ssl->out_msglen, ssl->out_left ) );
2039
Paul Bakker5bd42292012-12-19 14:40:42 +01002040 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002042
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2044
2045 if( ret <= 0 )
2046 return( ret );
2047
2048 ssl->out_left -= ret;
2049 }
2050
2051 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2052
2053 return( 0 );
2054}
2055
2056/*
2057 * Record layer functions
2058 */
2059int ssl_write_record( ssl_context *ssl )
2060{
Paul Bakker05ef8352012-05-08 09:17:57 +00002061 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002062 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002063
2064 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2065
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2067 {
2068 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2069 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2070 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2071
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002072 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2073 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 }
2075
Paul Bakker2770fbd2012-07-03 13:30:23 +00002076#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002077 if( ssl->transform_out != NULL &&
2078 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002079 {
2080 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2081 {
2082 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2083 return( ret );
2084 }
2085
2086 len = ssl->out_msglen;
2087 }
2088#endif /*POLARSSL_ZLIB_SUPPORT */
2089
Paul Bakker05ef8352012-05-08 09:17:57 +00002090#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002091 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002093 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
Paul Bakker05ef8352012-05-08 09:17:57 +00002095 ret = ssl_hw_record_write( ssl );
2096 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2097 {
2098 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002099 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002100 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002101
2102 if( ret == 0 )
2103 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002104 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002105#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002106 if( !done )
2107 {
2108 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2109 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2110 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2112 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002113
Paul Bakker48916f92012-09-16 19:57:18 +00002114 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002115 {
2116 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2117 {
2118 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2119 return( ret );
2120 }
2121
2122 len = ssl->out_msglen;
2123 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2124 ssl->out_hdr[4] = (unsigned char)( len );
2125 }
2126
2127 ssl->out_left = 5 + ssl->out_msglen;
2128
2129 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2130 "version = [%d:%d], msglen = %d",
2131 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2132 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2133
2134 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002135 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 }
2137
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2139 {
2140 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2141 return( ret );
2142 }
2143
2144 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2145
2146 return( 0 );
2147}
2148
2149int ssl_read_record( ssl_context *ssl )
2150{
Paul Bakker05ef8352012-05-08 09:17:57 +00002151 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
2153 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2154
2155 if( ssl->in_hslen != 0 &&
2156 ssl->in_hslen < ssl->in_msglen )
2157 {
2158 /*
2159 * Get next Handshake message in the current record
2160 */
2161 ssl->in_msglen -= ssl->in_hslen;
2162
Paul Bakker8934a982011-08-05 11:11:53 +00002163 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002164 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
2166 ssl->in_hslen = 4;
2167 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2168
2169 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2170 " %d, type = %d, hslen = %d",
2171 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2172
2173 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2174 {
2175 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002176 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 }
2178
2179 if( ssl->in_msglen < ssl->in_hslen )
2180 {
2181 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002182 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 }
2184
Paul Bakker4224bc02014-04-08 14:36:50 +02002185 if( ssl->state != SSL_HANDSHAKE_OVER )
2186 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187
2188 return( 0 );
2189 }
2190
2191 ssl->in_hslen = 0;
2192
2193 /*
2194 * Read the record header and validate it
2195 */
2196 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2197 {
2198 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2199 return( ret );
2200 }
2201
2202 ssl->in_msgtype = ssl->in_hdr[0];
2203 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2204
2205 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2206 "version = [%d:%d], msglen = %d",
2207 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2208 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2209
2210 if( ssl->in_hdr[1] != ssl->major_ver )
2211 {
2212 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002213 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 }
2215
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002216 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 {
2218 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002219 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 }
2221
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002222 /* Sanity check (outer boundaries) */
2223 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2224 {
2225 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2226 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2227 }
2228
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002230 * Make sure the message length is acceptable for the current transform
2231 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 */
Paul Bakker48916f92012-09-16 19:57:18 +00002233 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002235 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 {
2237 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002238 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
2240 }
2241 else
2242 {
Paul Bakker48916f92012-09-16 19:57:18 +00002243 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 {
2245 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002246 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 }
2248
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002249#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002251 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 {
2253 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002254 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002256#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002258#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2259 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 /*
2261 * TLS encrypted messages can have up to 256 bytes of padding
2262 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002263 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002264 ssl->in_msglen > ssl->transform_in->minlen +
2265 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 {
2267 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002268 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002270#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 }
2272
2273 /*
2274 * Read and optionally decrypt the message contents
2275 */
2276 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2277 {
2278 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2279 return( ret );
2280 }
2281
2282 SSL_DEBUG_BUF( 4, "input record from network",
2283 ssl->in_hdr, 5 + ssl->in_msglen );
2284
Paul Bakker05ef8352012-05-08 09:17:57 +00002285#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002286 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002287 {
2288 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2289
2290 ret = ssl_hw_record_read( ssl );
2291 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2292 {
2293 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002294 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002295 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002296
2297 if( ret == 0 )
2298 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002299 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002300#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002301 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 {
2303 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2304 {
Paul Bakker40865c82013-01-31 17:13:13 +01002305#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2306 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2307 {
2308 ssl_send_alert_message( ssl,
2309 SSL_ALERT_LEVEL_FATAL,
2310 SSL_ALERT_MSG_BAD_RECORD_MAC );
2311 }
2312#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2314 return( ret );
2315 }
2316
2317 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2318 ssl->in_msg, ssl->in_msglen );
2319
2320 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2321 {
2322 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002323 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 }
2325 }
2326
Paul Bakker2770fbd2012-07-03 13:30:23 +00002327#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002328 if( ssl->transform_in != NULL &&
2329 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002330 {
2331 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2332 {
2333 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2334 return( ret );
2335 }
2336
2337 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2338 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2339 }
2340#endif /* POLARSSL_ZLIB_SUPPORT */
2341
Paul Bakker0a925182012-04-16 06:46:41 +00002342 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2343 ssl->in_msgtype != SSL_MSG_ALERT &&
2344 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2345 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2346 {
2347 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2348
Paul Bakker48916f92012-09-16 19:57:18 +00002349 if( ( ret = ssl_send_alert_message( ssl,
2350 SSL_ALERT_LEVEL_FATAL,
2351 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002352 {
2353 return( ret );
2354 }
2355
2356 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2357 }
2358
Paul Bakker5121ce52009-01-03 21:22:43 +00002359 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2360 {
2361 ssl->in_hslen = 4;
2362 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2363
2364 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2365 " %d, type = %d, hslen = %d",
2366 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2367
2368 /*
2369 * Additional checks to validate the handshake header
2370 */
2371 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2372 {
2373 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002374 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 }
2376
2377 if( ssl->in_msglen < ssl->in_hslen )
2378 {
2379 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002380 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 }
2382
Paul Bakker48916f92012-09-16 19:57:18 +00002383 if( ssl->state != SSL_HANDSHAKE_OVER )
2384 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 }
2386
2387 if( ssl->in_msgtype == SSL_MSG_ALERT )
2388 {
2389 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2390 ssl->in_msg[0], ssl->in_msg[1] ) );
2391
2392 /*
2393 * Ignore non-fatal alerts, except close_notify
2394 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002395 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002397 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2398 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002399 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 }
2401
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002402 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2403 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 {
2405 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002406 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 }
2408 }
2409
2410 ssl->in_left = 0;
2411
2412 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2413
2414 return( 0 );
2415}
2416
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002417int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2418{
2419 int ret;
2420
2421 if( ( ret = ssl_send_alert_message( ssl,
2422 SSL_ALERT_LEVEL_FATAL,
2423 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2424 {
2425 return( ret );
2426 }
2427
2428 return( 0 );
2429}
2430
Paul Bakker0a925182012-04-16 06:46:41 +00002431int ssl_send_alert_message( ssl_context *ssl,
2432 unsigned char level,
2433 unsigned char message )
2434{
2435 int ret;
2436
2437 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2438
2439 ssl->out_msgtype = SSL_MSG_ALERT;
2440 ssl->out_msglen = 2;
2441 ssl->out_msg[0] = level;
2442 ssl->out_msg[1] = message;
2443
2444 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2445 {
2446 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2447 return( ret );
2448 }
2449
2450 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2451
2452 return( 0 );
2453}
2454
Paul Bakker5121ce52009-01-03 21:22:43 +00002455/*
2456 * Handshake functions
2457 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002458#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2459 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2460 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2461 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2462 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2463 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2464 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002465int ssl_write_certificate( ssl_context *ssl )
2466{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002467 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2470
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002471 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002472 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2473 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002474 {
2475 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2476 ssl->state++;
2477 return( 0 );
2478 }
2479
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002480 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2481 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002482}
2483
2484int ssl_parse_certificate( ssl_context *ssl )
2485{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002486 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2487
2488 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2489
2490 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002491 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2492 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002493 {
2494 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2495 ssl->state++;
2496 return( 0 );
2497 }
2498
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002499 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2500 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501}
2502#else
2503int ssl_write_certificate( ssl_context *ssl )
2504{
2505 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2506 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002507 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002508 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2509
2510 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2511
2512 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002513 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2514 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002515 {
2516 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2517 ssl->state++;
2518 return( 0 );
2519 }
2520
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 if( ssl->endpoint == SSL_IS_CLIENT )
2522 {
2523 if( ssl->client_auth == 0 )
2524 {
2525 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2526 ssl->state++;
2527 return( 0 );
2528 }
2529
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002530#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 /*
2532 * If using SSLv3 and got no cert, send an Alert message
2533 * (otherwise an empty Certificate message will be sent).
2534 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002535 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2537 {
2538 ssl->out_msglen = 2;
2539 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002540 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2541 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
2543 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2544 goto write_msg;
2545 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002546#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002547 }
2548 else /* SSL_IS_SERVER */
2549 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002550 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 {
2552 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002553 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555 }
2556
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002557 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
2559 /*
2560 * 0 . 0 handshake type
2561 * 1 . 3 handshake length
2562 * 4 . 6 length of all certs
2563 * 7 . 9 length of cert. 1
2564 * 10 . n-1 peer certificate
2565 * n . n+2 length of cert. 2
2566 * n+3 . ... upper level cert, etc.
2567 */
2568 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002569 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Paul Bakker29087132010-03-21 21:03:34 +00002571 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 {
2573 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002574 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 {
2576 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2577 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002578 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 }
2580
2581 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2582 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2583 ssl->out_msg[i + 2] = (unsigned char)( n );
2584
2585 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2586 i += n; crt = crt->next;
2587 }
2588
2589 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2590 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2591 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2592
2593 ssl->out_msglen = i;
2594 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2595 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2596
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002597#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002598write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002599#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
2601 ssl->state++;
2602
2603 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2604 {
2605 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2606 return( ret );
2607 }
2608
2609 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2610
Paul Bakkered27a042013-04-18 22:46:23 +02002611 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612}
2613
2614int ssl_parse_certificate( ssl_context *ssl )
2615{
Paul Bakkered27a042013-04-18 22:46:23 +02002616 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002617 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002618 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
2620 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2621
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002622 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002623 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2624 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002625 {
2626 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2627 ssl->state++;
2628 return( 0 );
2629 }
2630
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002632 ( ssl->authmode == SSL_VERIFY_NONE ||
2633 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002635 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2637 ssl->state++;
2638 return( 0 );
2639 }
2640
2641 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2642 {
2643 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2644 return( ret );
2645 }
2646
2647 ssl->state++;
2648
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002649#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 /*
2651 * Check if the client sent an empty certificate
2652 */
2653 if( ssl->endpoint == SSL_IS_SERVER &&
2654 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2655 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002656 if( ssl->in_msglen == 2 &&
2657 ssl->in_msgtype == SSL_MSG_ALERT &&
2658 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2659 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 {
2661 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2662
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002663 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2665 return( 0 );
2666 else
Paul Bakker40e46942009-01-03 21:51:57 +00002667 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 }
2669 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002670#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002672#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2673 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 if( ssl->endpoint == SSL_IS_SERVER &&
2675 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2676 {
2677 if( ssl->in_hslen == 7 &&
2678 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2679 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2680 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2681 {
2682 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2683
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002684 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002686 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 else
2688 return( 0 );
2689 }
2690 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002691#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2692 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
2694 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2695 {
2696 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002697 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 }
2699
2700 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2701 {
2702 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002703 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 }
2705
2706 /*
2707 * Same message structure as in ssl_write_certificate()
2708 */
2709 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2710
2711 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2712 {
2713 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002714 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 }
2716
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002717 /* In case we tried to reuse a session but it failed */
2718 if( ssl->session_negotiate->peer_cert != NULL )
2719 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002720 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002721 polarssl_free( ssl->session_negotiate->peer_cert );
2722 }
2723
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002724 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2725 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 {
2727 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002728 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002729 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 }
2731
Paul Bakkerb6b09562013-09-18 14:17:41 +02002732 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
2734 i = 7;
2735
2736 while( i < ssl->in_hslen )
2737 {
2738 if( ssl->in_msg[i] != 0 )
2739 {
2740 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002741 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 }
2743
2744 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2745 | (unsigned int) ssl->in_msg[i + 2];
2746 i += 3;
2747
2748 if( n < 128 || i + n > ssl->in_hslen )
2749 {
2750 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002751 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 }
2753
Paul Bakkerddf26b42013-09-18 13:46:23 +02002754 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2755 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 if( ret != 0 )
2757 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002758 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 return( ret );
2760 }
2761
2762 i += n;
2763 }
2764
Paul Bakker48916f92012-09-16 19:57:18 +00002765 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002767 /*
2768 * On client, make sure the server cert doesn't change during renego to
2769 * avoid "triple handshake" attack: https://secure-resumption.com/
2770 */
2771 if( ssl->endpoint == SSL_IS_CLIENT &&
2772 ssl->renegotiation == SSL_RENEGOTIATION )
2773 {
2774 if( ssl->session->peer_cert == NULL )
2775 {
2776 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2777 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2778 }
2779
2780 if( ssl->session->peer_cert->raw.len !=
2781 ssl->session_negotiate->peer_cert->raw.len ||
2782 memcmp( ssl->session->peer_cert->raw.p,
2783 ssl->session_negotiate->peer_cert->raw.p,
2784 ssl->session->peer_cert->raw.len ) != 0 )
2785 {
2786 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2787 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2788 }
2789 }
2790
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 if( ssl->authmode != SSL_VERIFY_NONE )
2792 {
2793 if( ssl->ca_chain == NULL )
2794 {
2795 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002796 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 }
2798
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002799 /*
2800 * Main check: verify certificate
2801 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002802 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2803 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2804 &ssl->session_negotiate->verify_result,
2805 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
2807 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002808 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002810 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002811
2812 /*
2813 * Secondary checks: always done, but change 'ret' only if it was 0
2814 */
2815
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002816#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002817 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002818 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002819
2820 /* If certificate uses an EC key, make sure the curve is OK */
2821 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2822 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2823 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002824 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002825 if( ret == 0 )
2826 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002827 }
2828 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002829#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002831 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2832 ciphersuite_info,
2833 ! ssl->endpoint ) != 0 )
2834 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002835 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002836 if( ret == 0 )
2837 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2838 }
2839
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2841 ret = 0;
2842 }
2843
2844 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2845
2846 return( ret );
2847}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002848#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2849 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2850 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2851 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2852 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2853 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2854 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
2856int ssl_write_change_cipher_spec( ssl_context *ssl )
2857{
2858 int ret;
2859
2860 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2861
2862 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2863 ssl->out_msglen = 1;
2864 ssl->out_msg[0] = 1;
2865
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 ssl->state++;
2867
2868 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2869 {
2870 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2871 return( ret );
2872 }
2873
2874 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2875
2876 return( 0 );
2877}
2878
2879int ssl_parse_change_cipher_spec( ssl_context *ssl )
2880{
2881 int ret;
2882
2883 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2884
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2886 {
2887 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2888 return( ret );
2889 }
2890
2891 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2892 {
2893 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002894 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002895 }
2896
2897 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2898 {
2899 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002900 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 }
2902
2903 ssl->state++;
2904
2905 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2906
2907 return( 0 );
2908}
2909
Paul Bakker41c83d32013-03-20 14:39:14 +01002910void ssl_optimize_checksum( ssl_context *ssl,
2911 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002912{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002913 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002914
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002915#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2916 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002917 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002918 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002919 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002920#endif
2921#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2922#if defined(POLARSSL_SHA512_C)
2923 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2924 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2925 else
2926#endif
2927#if defined(POLARSSL_SHA256_C)
2928 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002929 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002930 else
2931#endif
2932#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002933 {
2934 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002935 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002936 }
Paul Bakker380da532012-04-18 16:10:25 +00002937}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002938
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002939static void ssl_update_checksum_start( ssl_context *ssl,
2940 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002941{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002942#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2943 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002944 md5_update( &ssl->handshake->fin_md5 , buf, len );
2945 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#endif
2947#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2948#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002949 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002950#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002951#if defined(POLARSSL_SHA512_C)
2952 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002953#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002954#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002955}
2956
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002957#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2958 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002959static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2960 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002961{
Paul Bakker48916f92012-09-16 19:57:18 +00002962 md5_update( &ssl->handshake->fin_md5 , buf, len );
2963 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002964}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002965#endif
Paul Bakker380da532012-04-18 16:10:25 +00002966
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002967#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2968#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002969static void ssl_update_checksum_sha256( ssl_context *ssl,
2970 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002971{
Paul Bakker9e36f042013-06-30 14:34:05 +02002972 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002973}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002974#endif
Paul Bakker380da532012-04-18 16:10:25 +00002975
Paul Bakker9e36f042013-06-30 14:34:05 +02002976#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002977static void ssl_update_checksum_sha384( ssl_context *ssl,
2978 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002979{
Paul Bakker9e36f042013-06-30 14:34:05 +02002980 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002981}
Paul Bakker769075d2012-11-24 11:26:46 +01002982#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002983#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002984
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002985#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986static void ssl_calc_finished_ssl(
2987 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002988{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002989 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002990 md5_context md5;
2991 sha1_context sha1;
2992
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 unsigned char padbuf[48];
2994 unsigned char md5sum[16];
2995 unsigned char sha1sum[20];
2996
Paul Bakker48916f92012-09-16 19:57:18 +00002997 ssl_session *session = ssl->session_negotiate;
2998 if( !session )
2999 session = ssl->session;
3000
Paul Bakker1ef83d62012-04-11 12:09:53 +00003001 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3002
Paul Bakker48916f92012-09-16 19:57:18 +00003003 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3004 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003005
3006 /*
3007 * SSLv3:
3008 * hash =
3009 * MD5( master + pad2 +
3010 * MD5( handshake + sender + master + pad1 ) )
3011 * + SHA1( master + pad2 +
3012 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 */
3014
Paul Bakker90995b52013-06-24 19:20:35 +02003015#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003016 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003017 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003018#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003019
Paul Bakker90995b52013-06-24 19:20:35 +02003020#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003022 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003023#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003024
Paul Bakker3c2122f2013-06-24 19:03:14 +02003025 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3026 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
Paul Bakker1ef83d62012-04-11 12:09:53 +00003028 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Paul Bakker3c2122f2013-06-24 19:03:14 +02003030 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003031 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032 md5_update( &md5, padbuf, 48 );
3033 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003034
Paul Bakker3c2122f2013-06-24 19:03:14 +02003035 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003036 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003037 sha1_update( &sha1, padbuf, 40 );
3038 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
Paul Bakker1ef83d62012-04-11 12:09:53 +00003040 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003041
Paul Bakker1ef83d62012-04-11 12:09:53 +00003042 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003043 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003044 md5_update( &md5, padbuf, 48 );
3045 md5_update( &md5, md5sum, 16 );
3046 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003049 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003050 sha1_update( &sha1, padbuf , 40 );
3051 sha1_update( &sha1, sha1sum, 20 );
3052 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003053
Paul Bakker1ef83d62012-04-11 12:09:53 +00003054 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Paul Bakker5b4af392014-06-26 12:09:34 +02003056 md5_free( &md5 );
3057 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Paul Bakker34617722014-06-13 17:20:13 +02003059 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3060 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3061 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
3063 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3064}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003065#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003067#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003068static void ssl_calc_finished_tls(
3069 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003070{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003071 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003072 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003073 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003076
Paul Bakker48916f92012-09-16 19:57:18 +00003077 ssl_session *session = ssl->session_negotiate;
3078 if( !session )
3079 session = ssl->session;
3080
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003082
Paul Bakker48916f92012-09-16 19:57:18 +00003083 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3084 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003085
Paul Bakker1ef83d62012-04-11 12:09:53 +00003086 /*
3087 * TLSv1:
3088 * hash = PRF( master, finished_label,
3089 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3090 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
Paul Bakker90995b52013-06-24 19:20:35 +02003092#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003093 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3094 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003095#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003096
Paul Bakker90995b52013-06-24 19:20:35 +02003097#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003098 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3099 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003100#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003101
3102 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003103 ? "client finished"
3104 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003105
3106 md5_finish( &md5, padbuf );
3107 sha1_finish( &sha1, padbuf + 16 );
3108
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003109 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003110 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003111
3112 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3113
Paul Bakker5b4af392014-06-26 12:09:34 +02003114 md5_free( &md5 );
3115 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003116
Paul Bakker34617722014-06-13 17:20:13 +02003117 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003118
3119 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3120}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003121#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003122
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003123#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3124#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003125static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003126 ssl_context *ssl, unsigned char *buf, int from )
3127{
3128 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003129 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003130 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003131 unsigned char padbuf[32];
3132
Paul Bakker48916f92012-09-16 19:57:18 +00003133 ssl_session *session = ssl->session_negotiate;
3134 if( !session )
3135 session = ssl->session;
3136
Paul Bakker380da532012-04-18 16:10:25 +00003137 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003138
Paul Bakker9e36f042013-06-30 14:34:05 +02003139 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003140
3141 /*
3142 * TLSv1.2:
3143 * hash = PRF( master, finished_label,
3144 * Hash( handshake ) )[0.11]
3145 */
3146
Paul Bakker9e36f042013-06-30 14:34:05 +02003147#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003148 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003149 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003150#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003151
3152 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003153 ? "client finished"
3154 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155
Paul Bakker9e36f042013-06-30 14:34:05 +02003156 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003157
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003158 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003159 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003160
3161 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3162
Paul Bakker5b4af392014-06-26 12:09:34 +02003163 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003164
Paul Bakker34617722014-06-13 17:20:13 +02003165 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003166
3167 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3168}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003169#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003170
Paul Bakker9e36f042013-06-30 14:34:05 +02003171#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003172static void ssl_calc_finished_tls_sha384(
3173 ssl_context *ssl, unsigned char *buf, int from )
3174{
3175 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003176 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003177 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003178 unsigned char padbuf[48];
3179
Paul Bakker48916f92012-09-16 19:57:18 +00003180 ssl_session *session = ssl->session_negotiate;
3181 if( !session )
3182 session = ssl->session;
3183
Paul Bakker380da532012-04-18 16:10:25 +00003184 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003185
Paul Bakker9e36f042013-06-30 14:34:05 +02003186 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003187
3188 /*
3189 * TLSv1.2:
3190 * hash = PRF( master, finished_label,
3191 * Hash( handshake ) )[0.11]
3192 */
3193
Paul Bakker9e36f042013-06-30 14:34:05 +02003194#if !defined(POLARSSL_SHA512_ALT)
3195 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3196 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003197#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003198
3199 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003200 ? "client finished"
3201 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003202
Paul Bakker9e36f042013-06-30 14:34:05 +02003203 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003204
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003205 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003206 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003207
3208 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3209
Paul Bakker5b4af392014-06-26 12:09:34 +02003210 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003211
Paul Bakker34617722014-06-13 17:20:13 +02003212 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003213
3214 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3215}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003216#endif /* POLARSSL_SHA512_C */
3217#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003218
Paul Bakker48916f92012-09-16 19:57:18 +00003219void ssl_handshake_wrapup( ssl_context *ssl )
3220{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003221 int resume = ssl->handshake->resume;
3222
Paul Bakker48916f92012-09-16 19:57:18 +00003223 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3224
3225 /*
3226 * Free our handshake params
3227 */
3228 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003229 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003230 ssl->handshake = NULL;
3231
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003232 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003233 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003234 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003235 ssl->renego_records_seen = 0;
3236 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003237
Paul Bakker48916f92012-09-16 19:57:18 +00003238 /*
3239 * Switch in our now active transform context
3240 */
3241 if( ssl->transform )
3242 {
3243 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003244 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003245 }
3246 ssl->transform = ssl->transform_negotiate;
3247 ssl->transform_negotiate = NULL;
3248
Paul Bakker0a597072012-09-25 21:55:46 +00003249 if( ssl->session )
3250 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003251#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3252 /* RFC 7366 3.1: keep the EtM state */
3253 ssl->session_negotiate->encrypt_then_mac =
3254 ssl->session->encrypt_then_mac;
3255#endif
3256
Paul Bakker0a597072012-09-25 21:55:46 +00003257 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003258 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003259 }
3260 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003261 ssl->session_negotiate = NULL;
3262
Paul Bakker0a597072012-09-25 21:55:46 +00003263 /*
3264 * Add cache entry
3265 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003266 if( ssl->f_set_cache != NULL &&
3267 ssl->session->length != 0 &&
3268 resume == 0 )
3269 {
Paul Bakker0a597072012-09-25 21:55:46 +00003270 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3271 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003272 }
Paul Bakker0a597072012-09-25 21:55:46 +00003273
Paul Bakker48916f92012-09-16 19:57:18 +00003274 ssl->state++;
3275
3276 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3277}
3278
Paul Bakker1ef83d62012-04-11 12:09:53 +00003279int ssl_write_finished( ssl_context *ssl )
3280{
3281 int ret, hash_len;
3282
3283 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3284
Paul Bakker92be97b2013-01-02 17:30:03 +01003285 /*
3286 * Set the out_msg pointer to the correct location based on IV length
3287 */
3288 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3289 {
3290 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3291 ssl->transform_negotiate->fixed_ivlen;
3292 }
3293 else
3294 ssl->out_msg = ssl->out_iv;
3295
Paul Bakker48916f92012-09-16 19:57:18 +00003296 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003297
3298 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3300
Paul Bakker48916f92012-09-16 19:57:18 +00003301 ssl->verify_data_len = hash_len;
3302 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3303
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 ssl->out_msglen = 4 + hash_len;
3305 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3306 ssl->out_msg[0] = SSL_HS_FINISHED;
3307
3308 /*
3309 * In case of session resuming, invert the client and server
3310 * ChangeCipherSpec messages order.
3311 */
Paul Bakker0a597072012-09-25 21:55:46 +00003312 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003313 {
3314 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003315 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003316 else
3317 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3318 }
3319 else
3320 ssl->state++;
3321
Paul Bakker48916f92012-09-16 19:57:18 +00003322 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003323 * Switch to our negotiated transform and session parameters for outbound
3324 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003325 */
3326 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3327 ssl->transform_out = ssl->transform_negotiate;
3328 ssl->session_out = ssl->session_negotiate;
3329 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003330
Paul Bakker07eb38b2012-12-19 14:42:06 +01003331#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003332 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003333 {
3334 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3335 {
3336 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3337 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3338 }
3339 }
3340#endif
3341
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3343 {
3344 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3345 return( ret );
3346 }
3347
3348 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3349
3350 return( 0 );
3351}
3352
3353int ssl_parse_finished( ssl_context *ssl )
3354{
Paul Bakker23986e52011-04-24 08:57:21 +00003355 int ret;
3356 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 unsigned char buf[36];
3358
3359 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3360
Paul Bakker48916f92012-09-16 19:57:18 +00003361 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003362
Paul Bakker48916f92012-09-16 19:57:18 +00003363 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003364 * Switch to our negotiated transform and session parameters for inbound
3365 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003366 */
3367 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3368 ssl->transform_in = ssl->transform_negotiate;
3369 ssl->session_in = ssl->session_negotiate;
3370 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003371
Paul Bakker92be97b2013-01-02 17:30:03 +01003372 /*
3373 * Set the in_msg pointer to the correct location based on IV length
3374 */
3375 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3376 {
3377 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3378 ssl->transform_negotiate->fixed_ivlen;
3379 }
3380 else
3381 ssl->in_msg = ssl->in_iv;
3382
Paul Bakker07eb38b2012-12-19 14:42:06 +01003383#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003384 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003385 {
3386 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3387 {
3388 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3389 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3390 }
3391 }
3392#endif
3393
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3395 {
3396 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3397 return( ret );
3398 }
3399
3400 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3401 {
3402 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003403 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 }
3405
Paul Bakker1ef83d62012-04-11 12:09:53 +00003406 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003407 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3408
3409 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3410 ssl->in_hslen != 4 + hash_len )
3411 {
3412 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003413 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003414 }
3415
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003416 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003417 {
3418 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003419 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003420 }
3421
Paul Bakker48916f92012-09-16 19:57:18 +00003422 ssl->verify_data_len = hash_len;
3423 memcpy( ssl->peer_verify_data, buf, hash_len );
3424
Paul Bakker0a597072012-09-25 21:55:46 +00003425 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 {
3427 if( ssl->endpoint == SSL_IS_CLIENT )
3428 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3429
3430 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003431 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003432 }
3433 else
3434 ssl->state++;
3435
3436 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3437
3438 return( 0 );
3439}
3440
Paul Bakker968afaa2014-07-09 11:09:24 +02003441static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003442{
3443 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3444
3445#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3446 defined(POLARSSL_SSL_PROTO_TLS1_1)
3447 md5_init( &handshake->fin_md5 );
3448 sha1_init( &handshake->fin_sha1 );
3449 md5_starts( &handshake->fin_md5 );
3450 sha1_starts( &handshake->fin_sha1 );
3451#endif
3452#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3453#if defined(POLARSSL_SHA256_C)
3454 sha256_init( &handshake->fin_sha256 );
3455 sha256_starts( &handshake->fin_sha256, 0 );
3456#endif
3457#if defined(POLARSSL_SHA512_C)
3458 sha512_init( &handshake->fin_sha512 );
3459 sha512_starts( &handshake->fin_sha512, 1 );
3460#endif
3461#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3462
3463 handshake->update_checksum = ssl_update_checksum_start;
3464 handshake->sig_alg = SSL_HASH_SHA1;
3465
3466#if defined(POLARSSL_DHM_C)
3467 dhm_init( &handshake->dhm_ctx );
3468#endif
3469#if defined(POLARSSL_ECDH_C)
3470 ecdh_init( &handshake->ecdh_ctx );
3471#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003472}
3473
3474static void ssl_transform_init( ssl_transform *transform )
3475{
3476 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003477
3478 cipher_init( &transform->cipher_ctx_enc );
3479 cipher_init( &transform->cipher_ctx_dec );
3480
3481 md_init( &transform->md_ctx_enc );
3482 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003483}
3484
3485void ssl_session_init( ssl_session *session )
3486{
3487 memset( session, 0, sizeof(ssl_session) );
3488}
3489
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003490static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003491{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003492 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003493 if( ssl->transform_negotiate )
3494 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003495 if( ssl->session_negotiate )
3496 ssl_session_free( ssl->session_negotiate );
3497 if( ssl->handshake )
3498 ssl_handshake_free( ssl->handshake );
3499
3500 /*
3501 * Either the pointers are now NULL or cleared properly and can be freed.
3502 * Now allocate missing structures.
3503 */
3504 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003505 {
3506 ssl->transform_negotiate =
3507 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3508 }
Paul Bakker48916f92012-09-16 19:57:18 +00003509
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003510 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003511 {
3512 ssl->session_negotiate =
3513 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3514 }
Paul Bakker48916f92012-09-16 19:57:18 +00003515
Paul Bakker82788fb2014-10-20 13:59:19 +02003516 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003517 {
3518 ssl->handshake = (ssl_handshake_params *)
3519 polarssl_malloc( sizeof(ssl_handshake_params) );
3520 }
Paul Bakker48916f92012-09-16 19:57:18 +00003521
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003522 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003523 if( ssl->handshake == NULL ||
3524 ssl->transform_negotiate == NULL ||
3525 ssl->session_negotiate == NULL )
3526 {
3527 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003528
3529 polarssl_free( ssl->handshake );
3530 polarssl_free( ssl->transform_negotiate );
3531 polarssl_free( ssl->session_negotiate );
3532
3533 ssl->handshake = NULL;
3534 ssl->transform_negotiate = NULL;
3535 ssl->session_negotiate = NULL;
3536
Paul Bakker48916f92012-09-16 19:57:18 +00003537 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3538 }
3539
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003540 /* Initialize structures */
3541 ssl_session_init( ssl->session_negotiate );
3542 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003543 ssl_handshake_params_init( ssl->handshake );
3544
3545#if defined(POLARSSL_X509_CRT_PARSE_C)
3546 ssl->handshake->key_cert = ssl->key_cert;
3547#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003548
Paul Bakker48916f92012-09-16 19:57:18 +00003549 return( 0 );
3550}
3551
Paul Bakker5121ce52009-01-03 21:22:43 +00003552/*
3553 * Initialize an SSL context
3554 */
3555int ssl_init( ssl_context *ssl )
3556{
Paul Bakker48916f92012-09-16 19:57:18 +00003557 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 int len = SSL_BUFFER_LEN;
3559
3560 memset( ssl, 0, sizeof( ssl_context ) );
3561
Paul Bakker62f2dee2012-09-28 07:31:51 +00003562 /*
3563 * Sane defaults
3564 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003565 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3566 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3567 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3568 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003569
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003570 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003571
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003572 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3573
Paul Bakker62f2dee2012-09-28 07:31:51 +00003574#if defined(POLARSSL_DHM_C)
3575 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3576 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3577 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3578 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3579 {
3580 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3581 return( ret );
3582 }
3583#endif
3584
3585 /*
3586 * Prepare base structures
3587 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003588 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003589 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003590 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 ssl->in_msg = ssl->in_ctr + 13;
3592
3593 if( ssl->in_ctr == NULL )
3594 {
3595 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003596 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 }
3598
Paul Bakker6e339b52013-07-03 13:37:05 +02003599 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003601 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003602 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003603
3604 if( ssl->out_ctr == NULL )
3605 {
3606 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003607 polarssl_free( ssl->in_ctr );
3608 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003609 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003610 }
3611
3612 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3613 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3614
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003615#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3616 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3617#endif
3618
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003619#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3620 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3621#endif
3622
Paul Bakker606b4ba2013-08-14 16:52:14 +02003623#if defined(POLARSSL_SSL_SESSION_TICKETS)
3624 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3625#endif
3626
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003627#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003628 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003629#endif
3630
Paul Bakker48916f92012-09-16 19:57:18 +00003631 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3632 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003633
3634 return( 0 );
3635}
3636
3637/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003638 * Reset an initialized and used SSL context for re-use while retaining
3639 * all application-set variables, function pointers and data.
3640 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003641int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003642{
Paul Bakker48916f92012-09-16 19:57:18 +00003643 int ret;
3644
Paul Bakker7eb013f2011-10-06 12:37:39 +00003645 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003646 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3647 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3648
3649 ssl->verify_data_len = 0;
3650 memset( ssl->own_verify_data, 0, 36 );
3651 memset( ssl->peer_verify_data, 0, 36 );
3652
Paul Bakker7eb013f2011-10-06 12:37:39 +00003653 ssl->in_offt = NULL;
3654
Paul Bakker92be97b2013-01-02 17:30:03 +01003655 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003656 ssl->in_msgtype = 0;
3657 ssl->in_msglen = 0;
3658 ssl->in_left = 0;
3659
3660 ssl->in_hslen = 0;
3661 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003662 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003663
Paul Bakker92be97b2013-01-02 17:30:03 +01003664 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003665 ssl->out_msgtype = 0;
3666 ssl->out_msglen = 0;
3667 ssl->out_left = 0;
3668
Paul Bakker48916f92012-09-16 19:57:18 +00003669 ssl->transform_in = NULL;
3670 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003671
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003672 ssl->renego_records_seen = 0;
3673
Paul Bakker7eb013f2011-10-06 12:37:39 +00003674 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3675 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003676
3677#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003678 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003679 {
3680 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003681 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003682 {
3683 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3684 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3685 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003686 }
3687#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003688
Paul Bakker48916f92012-09-16 19:57:18 +00003689 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003690 {
Paul Bakker48916f92012-09-16 19:57:18 +00003691 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003692 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003693 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003694 }
Paul Bakker48916f92012-09-16 19:57:18 +00003695
Paul Bakkerc0463502013-02-14 11:19:38 +01003696 if( ssl->session )
3697 {
3698 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003699 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003700 ssl->session = NULL;
3701 }
3702
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003703#if defined(POLARSSL_SSL_ALPN)
3704 ssl->alpn_chosen = NULL;
3705#endif
3706
Paul Bakker48916f92012-09-16 19:57:18 +00003707 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3708 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003709
3710 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003711}
3712
Paul Bakkera503a632013-08-14 13:48:06 +02003713#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003714static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3715{
3716 aes_free( &tkeys->enc );
3717 aes_free( &tkeys->dec );
3718
3719 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3720}
3721
Paul Bakker7eb013f2011-10-06 12:37:39 +00003722/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003723 * Allocate and initialize ticket keys
3724 */
3725static int ssl_ticket_keys_init( ssl_context *ssl )
3726{
3727 int ret;
3728 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003729 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003730
3731 if( ssl->ticket_keys != NULL )
3732 return( 0 );
3733
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003734 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3735 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003736 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3737
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003738 aes_init( &tkeys->enc );
3739 aes_init( &tkeys->dec );
3740
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003741 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003742 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003743 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003744 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003745 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003746 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003747
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003748 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3749 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3750 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3751 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003752 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003753 polarssl_free( tkeys );
3754 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003755 }
3756
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003757 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003758 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003759 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003760 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003761 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003762 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003763
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003764 ssl->ticket_keys = tkeys;
3765
3766 return( 0 );
3767}
Paul Bakkera503a632013-08-14 13:48:06 +02003768#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003769
3770/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003771 * SSL set accessors
3772 */
3773void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3774{
3775 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003776
Paul Bakker606b4ba2013-08-14 16:52:14 +02003777#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003778 if( endpoint == SSL_IS_CLIENT )
3779 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003780#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003781}
3782
3783void ssl_set_authmode( ssl_context *ssl, int authmode )
3784{
3785 ssl->authmode = authmode;
3786}
3787
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003788#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003789void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003790 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003791 void *p_vrfy )
3792{
3793 ssl->f_vrfy = f_vrfy;
3794 ssl->p_vrfy = p_vrfy;
3795}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003796#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003797
Paul Bakker5121ce52009-01-03 21:22:43 +00003798void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003799 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003800 void *p_rng )
3801{
3802 ssl->f_rng = f_rng;
3803 ssl->p_rng = p_rng;
3804}
3805
3806void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003807 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003808 void *p_dbg )
3809{
3810 ssl->f_dbg = f_dbg;
3811 ssl->p_dbg = p_dbg;
3812}
3813
3814void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003815 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003816 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003817{
3818 ssl->f_recv = f_recv;
3819 ssl->f_send = f_send;
3820 ssl->p_recv = p_recv;
3821 ssl->p_send = p_send;
3822}
3823
Paul Bakker0a597072012-09-25 21:55:46 +00003824void ssl_set_session_cache( ssl_context *ssl,
3825 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3826 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003827{
Paul Bakker0a597072012-09-25 21:55:46 +00003828 ssl->f_get_cache = f_get_cache;
3829 ssl->p_get_cache = p_get_cache;
3830 ssl->f_set_cache = f_set_cache;
3831 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003832}
3833
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003834int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003835{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003836 int ret;
3837
3838 if( ssl == NULL ||
3839 session == NULL ||
3840 ssl->session_negotiate == NULL ||
3841 ssl->endpoint != SSL_IS_CLIENT )
3842 {
3843 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3844 }
3845
3846 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3847 return( ret );
3848
Paul Bakker0a597072012-09-25 21:55:46 +00003849 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003850
3851 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003852}
3853
Paul Bakkerb68cad62012-08-23 08:34:18 +00003854void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003855{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003856 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3857 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3858 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3859 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3860}
3861
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003862void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3863 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003864 int major, int minor )
3865{
3866 if( major != SSL_MAJOR_VERSION_3 )
3867 return;
3868
3869 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3870 return;
3871
3872 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003873}
3874
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003875#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003876/* Add a new (empty) key_cert entry an return a pointer to it */
3877static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3878{
3879 ssl_key_cert *key_cert, *last;
3880
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003881 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3882 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003883 return( NULL );
3884
3885 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3886
3887 /* Append the new key_cert to the (possibly empty) current list */
3888 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003889 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003890 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003891 if( ssl->handshake != NULL )
3892 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003893 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003894 else
3895 {
3896 last = ssl->key_cert;
3897 while( last->next != NULL )
3898 last = last->next;
3899 last->next = key_cert;
3900 }
3901
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003902 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003903}
3904
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003905void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003906 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003907{
3908 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003909 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003910 ssl->peer_cn = peer_cn;
3911}
3912
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003913int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003914 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003915{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003916 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3917
3918 if( key_cert == NULL )
3919 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3920
3921 key_cert->cert = own_cert;
3922 key_cert->key = pk_key;
3923
3924 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003925}
3926
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003927#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003928int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003929 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003930{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003931 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003932 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003933
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003934 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003935 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3936
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003937 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3938 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003939 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003940
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003941 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003942
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003943 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003944 if( ret != 0 )
3945 return( ret );
3946
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003947 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003948 return( ret );
3949
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003950 key_cert->cert = own_cert;
3951 key_cert->key_own_alloc = 1;
3952
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003953 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003954}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003955#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003956
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003957int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003958 void *rsa_key,
3959 rsa_decrypt_func rsa_decrypt,
3960 rsa_sign_func rsa_sign,
3961 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003962{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003963 int ret;
3964 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003965
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003966 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003967 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3968
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003969 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3970 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003971 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003972
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003973 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003974
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003975 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3976 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3977 return( ret );
3978
3979 key_cert->cert = own_cert;
3980 key_cert->key_own_alloc = 1;
3981
3982 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003983}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003984#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003985
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003986#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003987int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3988 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003989{
Paul Bakker6db455e2013-09-18 17:29:31 +02003990 if( psk == NULL || psk_identity == NULL )
3991 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3992
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003993 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003994 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3995
Paul Bakker6db455e2013-09-18 17:29:31 +02003996 if( ssl->psk != NULL )
3997 {
3998 polarssl_free( ssl->psk );
3999 polarssl_free( ssl->psk_identity );
4000 }
4001
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004002 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004003 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004004
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004005 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004006 ssl->psk_identity = (unsigned char *)
4007 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004008
4009 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4010 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4011
4012 memcpy( ssl->psk, psk, ssl->psk_len );
4013 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004014
4015 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004016}
4017
4018void ssl_set_psk_cb( ssl_context *ssl,
4019 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4020 size_t),
4021 void *p_psk )
4022{
4023 ssl->f_psk = f_psk;
4024 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004025}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004026#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004027
Paul Bakker48916f92012-09-16 19:57:18 +00004028#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004029int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004030{
4031 int ret;
4032
Paul Bakker48916f92012-09-16 19:57:18 +00004033 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004034 {
4035 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4036 return( ret );
4037 }
4038
Paul Bakker48916f92012-09-16 19:57:18 +00004039 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004040 {
4041 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4042 return( ret );
4043 }
4044
4045 return( 0 );
4046}
4047
Paul Bakker1b57b062011-01-06 15:48:19 +00004048int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4049{
4050 int ret;
4051
Paul Bakker66d5d072014-06-17 16:39:18 +02004052 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004053 {
4054 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4055 return( ret );
4056 }
4057
Paul Bakker66d5d072014-06-17 16:39:18 +02004058 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004059 {
4060 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4061 return( ret );
4062 }
4063
4064 return( 0 );
4065}
Paul Bakker48916f92012-09-16 19:57:18 +00004066#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004067
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004068#if defined(POLARSSL_SSL_SET_CURVES)
4069/*
4070 * Set the allowed elliptic curves
4071 */
4072void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4073{
4074 ssl->curve_list = curve_list;
4075}
4076#endif
4077
Paul Bakker0be444a2013-08-27 21:55:01 +02004078#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004079int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004080{
4081 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004082 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004083
4084 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004085
4086 if( ssl->hostname_len + 1 == 0 )
4087 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4088
Paul Bakker6e339b52013-07-03 13:37:05 +02004089 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004090
Paul Bakkerb15b8512012-01-13 13:44:06 +00004091 if( ssl->hostname == NULL )
4092 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4093
Paul Bakker3c2122f2013-06-24 19:03:14 +02004094 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004095 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004096
Paul Bakker40ea7de2009-05-03 10:18:48 +00004097 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004098
4099 return( 0 );
4100}
4101
Paul Bakker5701cdc2012-09-27 21:49:42 +00004102void ssl_set_sni( ssl_context *ssl,
4103 int (*f_sni)(void *, ssl_context *,
4104 const unsigned char *, size_t),
4105 void *p_sni )
4106{
4107 ssl->f_sni = f_sni;
4108 ssl->p_sni = p_sni;
4109}
Paul Bakker0be444a2013-08-27 21:55:01 +02004110#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004111
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004112#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004113int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004114{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004115 size_t cur_len, tot_len;
4116 const char **p;
4117
4118 /*
4119 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4120 * truncated". Check lengths now rather than later.
4121 */
4122 tot_len = 0;
4123 for( p = protos; *p != NULL; p++ )
4124 {
4125 cur_len = strlen( *p );
4126 tot_len += cur_len;
4127
4128 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4129 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4130 }
4131
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004132 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004133
4134 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004135}
4136
4137const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4138{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004139 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004140}
4141#endif /* POLARSSL_SSL_ALPN */
4142
Paul Bakker490ecc82011-10-06 13:04:09 +00004143void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4144{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004145 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4146 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4147 {
4148 ssl->max_major_ver = major;
4149 ssl->max_minor_ver = minor;
4150 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004151}
4152
Paul Bakker1d29fb52012-09-28 13:28:45 +00004153void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4154{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004155 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4156 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4157 {
4158 ssl->min_major_ver = major;
4159 ssl->min_minor_ver = minor;
4160 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004161}
4162
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004163#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4164void ssl_set_fallback( ssl_context *ssl, char fallback )
4165{
4166 ssl->fallback = fallback;
4167}
4168#endif
4169
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004170#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4171void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4172{
4173 ssl->encrypt_then_mac = etm;
4174}
4175#endif
4176
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004177#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4178void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4179{
4180 ssl->extended_ms = ems;
4181}
4182#endif
4183
Paul Bakker05decb22013-08-15 13:33:48 +02004184#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004185int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4186{
Paul Bakker77e257e2013-12-16 15:29:52 +01004187 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004188 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004189 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004190 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004191 }
4192
4193 ssl->mfl_code = mfl_code;
4194
4195 return( 0 );
4196}
Paul Bakker05decb22013-08-15 13:33:48 +02004197#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004198
Paul Bakker1f2bc622013-08-15 13:45:55 +02004199#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004200int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004201{
4202 if( ssl->endpoint != SSL_IS_CLIENT )
4203 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4204
Paul Bakker8c1ede62013-07-19 14:14:37 +02004205 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004206
4207 return( 0 );
4208}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004209#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004210
Paul Bakker48916f92012-09-16 19:57:18 +00004211void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4212{
4213 ssl->disable_renegotiation = renegotiation;
4214}
4215
4216void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4217{
4218 ssl->allow_legacy_renegotiation = allow_legacy;
4219}
4220
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004221void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4222{
4223 ssl->renego_max_records = max_records;
4224}
4225
Paul Bakkera503a632013-08-14 13:48:06 +02004226#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004227int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4228{
4229 ssl->session_tickets = use_tickets;
4230
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004231 if( ssl->endpoint == SSL_IS_CLIENT )
4232 return( 0 );
4233
4234 if( ssl->f_rng == NULL )
4235 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4236
4237 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004238}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004239
4240void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4241{
4242 ssl->ticket_lifetime = lifetime;
4243}
Paul Bakkera503a632013-08-14 13:48:06 +02004244#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004245
Paul Bakker5121ce52009-01-03 21:22:43 +00004246/*
4247 * SSL get accessors
4248 */
Paul Bakker23986e52011-04-24 08:57:21 +00004249size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004250{
4251 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4252}
4253
Paul Bakkerff60ee62010-03-16 21:09:09 +00004254int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004255{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004256 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004257}
4258
Paul Bakkere3166ce2011-01-27 17:40:50 +00004259const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004260{
Paul Bakker926c8e42013-03-06 10:23:34 +01004261 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004262 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004263
Paul Bakkere3166ce2011-01-27 17:40:50 +00004264 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004265}
4266
Paul Bakker43ca69c2011-01-15 17:35:19 +00004267const char *ssl_get_version( const ssl_context *ssl )
4268{
4269 switch( ssl->minor_ver )
4270 {
4271 case SSL_MINOR_VERSION_0:
4272 return( "SSLv3.0" );
4273
4274 case SSL_MINOR_VERSION_1:
4275 return( "TLSv1.0" );
4276
4277 case SSL_MINOR_VERSION_2:
4278 return( "TLSv1.1" );
4279
Paul Bakker1ef83d62012-04-11 12:09:53 +00004280 case SSL_MINOR_VERSION_3:
4281 return( "TLSv1.2" );
4282
Paul Bakker43ca69c2011-01-15 17:35:19 +00004283 default:
4284 break;
4285 }
4286 return( "unknown" );
4287}
4288
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004289#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004290const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004291{
4292 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004293 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004294
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004295 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004296}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004297#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004298
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004299int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4300{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004301 if( ssl == NULL ||
4302 dst == NULL ||
4303 ssl->session == NULL ||
4304 ssl->endpoint != SSL_IS_CLIENT )
4305 {
4306 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4307 }
4308
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004309 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004310}
4311
Paul Bakker5121ce52009-01-03 21:22:43 +00004312/*
Paul Bakker1961b702013-01-25 14:49:24 +01004313 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004314 */
Paul Bakker1961b702013-01-25 14:49:24 +01004315int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004316{
Paul Bakker40e46942009-01-03 21:51:57 +00004317 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004318
Paul Bakker40e46942009-01-03 21:51:57 +00004319#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004320 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004321 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004322#endif
4323
Paul Bakker40e46942009-01-03 21:51:57 +00004324#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004325 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004326 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004327#endif
4328
Paul Bakker1961b702013-01-25 14:49:24 +01004329 return( ret );
4330}
4331
4332/*
4333 * Perform the SSL handshake
4334 */
4335int ssl_handshake( ssl_context *ssl )
4336{
4337 int ret = 0;
4338
4339 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4340
4341 while( ssl->state != SSL_HANDSHAKE_OVER )
4342 {
4343 ret = ssl_handshake_step( ssl );
4344
4345 if( ret != 0 )
4346 break;
4347 }
4348
Paul Bakker5121ce52009-01-03 21:22:43 +00004349 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4350
4351 return( ret );
4352}
4353
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004354#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004355/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004356 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004357 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004358static int ssl_write_hello_request( ssl_context *ssl )
4359{
4360 int ret;
4361
4362 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4363
4364 ssl->out_msglen = 4;
4365 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4366 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4367
4368 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4369 {
4370 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4371 return( ret );
4372 }
4373
4374 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4375
4376 return( 0 );
4377}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004378#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004379
4380/*
4381 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004382 * - any side: calling ssl_renegotiate(),
4383 * - client: receiving a HelloRequest during ssl_read(),
4384 * - server: receiving any handshake message on server during ssl_read() after
4385 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004386 * If the handshake doesn't complete due to waiting for I/O, it will continue
4387 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004388 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004389static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004390{
4391 int ret;
4392
4393 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4394
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004395 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4396 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004397
4398 ssl->state = SSL_HELLO_REQUEST;
4399 ssl->renegotiation = SSL_RENEGOTIATION;
4400
Paul Bakker48916f92012-09-16 19:57:18 +00004401 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4402 {
4403 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4404 return( ret );
4405 }
4406
4407 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4408
4409 return( 0 );
4410}
4411
4412/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004413 * Renegotiate current connection on client,
4414 * or request renegotiation on server
4415 */
4416int ssl_renegotiate( ssl_context *ssl )
4417{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004418 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004419
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004420#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004421 /* On server, just send the request */
4422 if( ssl->endpoint == SSL_IS_SERVER )
4423 {
4424 if( ssl->state != SSL_HANDSHAKE_OVER )
4425 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4426
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004427 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4428
4429 /* Did we already try/start sending HelloRequest? */
4430 if( ssl->out_left != 0 )
4431 return( ssl_flush_output( ssl ) );
4432
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004433 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004434 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004435#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004436
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004437#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004438 /*
4439 * On client, either start the renegotiation process or,
4440 * if already in progress, continue the handshake
4441 */
4442 if( ssl->renegotiation != SSL_RENEGOTIATION )
4443 {
4444 if( ssl->state != SSL_HANDSHAKE_OVER )
4445 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4446
4447 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4448 {
4449 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4450 return( ret );
4451 }
4452 }
4453 else
4454 {
4455 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4456 {
4457 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4458 return( ret );
4459 }
4460 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004461#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004462
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004463 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004464}
4465
4466/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004467 * Receive application data decrypted from the SSL layer
4468 */
Paul Bakker23986e52011-04-24 08:57:21 +00004469int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004470{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004471 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004472 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004473
4474 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4475
4476 if( ssl->state != SSL_HANDSHAKE_OVER )
4477 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004478 ret = ssl_handshake( ssl );
4479 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4480 {
4481 record_read = 1;
4482 }
4483 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004484 {
4485 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4486 return( ret );
4487 }
4488 }
4489
4490 if( ssl->in_offt == NULL )
4491 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004492 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004493 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004494 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4495 {
4496 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4497 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004498
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004499 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4500 return( ret );
4501 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004502 }
4503
4504 if( ssl->in_msglen == 0 &&
4505 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4506 {
4507 /*
4508 * OpenSSL sends empty messages to randomize the IV
4509 */
4510 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4511 {
Paul Bakker831a7552011-05-18 13:32:51 +00004512 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4513 return( 0 );
4514
Paul Bakker5121ce52009-01-03 21:22:43 +00004515 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4516 return( ret );
4517 }
4518 }
4519
Paul Bakker48916f92012-09-16 19:57:18 +00004520 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4521 {
4522 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4523
4524 if( ssl->endpoint == SSL_IS_CLIENT &&
4525 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4526 ssl->in_hslen != 4 ) )
4527 {
4528 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4529 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4530 }
4531
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004532 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4533 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004534 ssl->allow_legacy_renegotiation ==
4535 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004536 {
4537 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4538
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004539#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004540 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004541 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004542 /*
4543 * SSLv3 does not have a "no_renegotiation" alert
4544 */
4545 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4546 return( ret );
4547 }
4548 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004549#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004550#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4551 defined(POLARSSL_SSL_PROTO_TLS1_2)
4552 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004553 {
4554 if( ( ret = ssl_send_alert_message( ssl,
4555 SSL_ALERT_LEVEL_WARNING,
4556 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4557 {
4558 return( ret );
4559 }
Paul Bakker48916f92012-09-16 19:57:18 +00004560 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004561 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004562#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4563 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004564 {
4565 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004566 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004567 }
Paul Bakker48916f92012-09-16 19:57:18 +00004568 }
4569 else
4570 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004571 ret = ssl_start_renegotiation( ssl );
4572 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4573 {
4574 record_read = 1;
4575 }
4576 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004577 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004578 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004579 return( ret );
4580 }
Paul Bakker48916f92012-09-16 19:57:18 +00004581 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004582
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004583 /* If a non-handshake record was read during renego, fallthrough,
4584 * else tell the user they should call ssl_read() again */
4585 if( ! record_read )
4586 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004587 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004588 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4589 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004590 ssl->renego_records_seen++;
4591
4592 if( ssl->renego_max_records >= 0 &&
4593 ssl->renego_records_seen > ssl->renego_max_records )
4594 {
4595 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4596 "but not honored by client" ) );
4597 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4598 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004599 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004600
4601 /* Fatal and closure alerts handled by ssl_read_record() */
4602 if( ssl->in_msgtype == SSL_MSG_ALERT )
4603 {
4604 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4605 return( POLARSSL_ERR_NET_WANT_READ );
4606 }
4607
4608 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004609 {
4610 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004611 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004612 }
4613
4614 ssl->in_offt = ssl->in_msg;
4615 }
4616
4617 n = ( len < ssl->in_msglen )
4618 ? len : ssl->in_msglen;
4619
4620 memcpy( buf, ssl->in_offt, n );
4621 ssl->in_msglen -= n;
4622
4623 if( ssl->in_msglen == 0 )
4624 /* all bytes consumed */
4625 ssl->in_offt = NULL;
4626 else
4627 /* more data available */
4628 ssl->in_offt += n;
4629
4630 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4631
Paul Bakker23986e52011-04-24 08:57:21 +00004632 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004633}
4634
4635/*
4636 * Send application data to be encrypted by the SSL layer
4637 */
Paul Bakker23986e52011-04-24 08:57:21 +00004638int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004639{
Paul Bakker23986e52011-04-24 08:57:21 +00004640 int ret;
4641 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004642 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004643
4644 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4645
4646 if( ssl->state != SSL_HANDSHAKE_OVER )
4647 {
4648 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4649 {
4650 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4651 return( ret );
4652 }
4653 }
4654
Paul Bakker05decb22013-08-15 13:33:48 +02004655#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004656 /*
4657 * Assume mfl_code is correct since it was checked when set
4658 */
4659 max_len = mfl_code_to_length[ssl->mfl_code];
4660
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004661 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004662 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004663 */
4664 if( ssl->session_out != NULL &&
4665 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4666 {
4667 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4668 }
Paul Bakker05decb22013-08-15 13:33:48 +02004669#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004670
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004671 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004672
Paul Bakker5121ce52009-01-03 21:22:43 +00004673 if( ssl->out_left != 0 )
4674 {
4675 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4676 {
4677 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4678 return( ret );
4679 }
4680 }
Paul Bakker887bd502011-06-08 13:10:54 +00004681 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004682 {
Paul Bakker887bd502011-06-08 13:10:54 +00004683 ssl->out_msglen = n;
4684 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4685 memcpy( ssl->out_msg, buf, n );
4686
4687 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4688 {
4689 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4690 return( ret );
4691 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004692 }
4693
4694 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4695
Paul Bakker23986e52011-04-24 08:57:21 +00004696 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004697}
4698
4699/*
4700 * Notify the peer that the connection is being closed
4701 */
4702int ssl_close_notify( ssl_context *ssl )
4703{
4704 int ret;
4705
4706 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4707
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004708 if( ssl->out_left != 0 )
4709 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004710
4711 if( ssl->state == SSL_HANDSHAKE_OVER )
4712 {
Paul Bakker48916f92012-09-16 19:57:18 +00004713 if( ( ret = ssl_send_alert_message( ssl,
4714 SSL_ALERT_LEVEL_WARNING,
4715 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004716 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004717 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004718 return( ret );
4719 }
4720 }
4721
4722 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4723
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004724 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004725}
4726
Paul Bakker48916f92012-09-16 19:57:18 +00004727void ssl_transform_free( ssl_transform *transform )
4728{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004729 if( transform == NULL )
4730 return;
4731
Paul Bakker48916f92012-09-16 19:57:18 +00004732#if defined(POLARSSL_ZLIB_SUPPORT)
4733 deflateEnd( &transform->ctx_deflate );
4734 inflateEnd( &transform->ctx_inflate );
4735#endif
4736
Paul Bakker84bbeb52014-07-01 14:53:22 +02004737 cipher_free( &transform->cipher_ctx_enc );
4738 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004739
Paul Bakker84bbeb52014-07-01 14:53:22 +02004740 md_free( &transform->md_ctx_enc );
4741 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004742
Paul Bakker34617722014-06-13 17:20:13 +02004743 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004744}
4745
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004746#if defined(POLARSSL_X509_CRT_PARSE_C)
4747static void ssl_key_cert_free( ssl_key_cert *key_cert )
4748{
4749 ssl_key_cert *cur = key_cert, *next;
4750
4751 while( cur != NULL )
4752 {
4753 next = cur->next;
4754
4755 if( cur->key_own_alloc )
4756 {
4757 pk_free( cur->key );
4758 polarssl_free( cur->key );
4759 }
4760 polarssl_free( cur );
4761
4762 cur = next;
4763 }
4764}
4765#endif /* POLARSSL_X509_CRT_PARSE_C */
4766
Paul Bakker48916f92012-09-16 19:57:18 +00004767void ssl_handshake_free( ssl_handshake_params *handshake )
4768{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004769 if( handshake == NULL )
4770 return;
4771
Paul Bakker48916f92012-09-16 19:57:18 +00004772#if defined(POLARSSL_DHM_C)
4773 dhm_free( &handshake->dhm_ctx );
4774#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004775#if defined(POLARSSL_ECDH_C)
4776 ecdh_free( &handshake->ecdh_ctx );
4777#endif
4778
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004779#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004780 /* explicit void pointer cast for buggy MS compiler */
4781 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004782#endif
4783
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004784#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4785 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4786 /*
4787 * Free only the linked list wrapper, not the keys themselves
4788 * since the belong to the SNI callback
4789 */
4790 if( handshake->sni_key_cert != NULL )
4791 {
4792 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4793
4794 while( cur != NULL )
4795 {
4796 next = cur->next;
4797 polarssl_free( cur );
4798 cur = next;
4799 }
4800 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004801#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004802
Paul Bakker34617722014-06-13 17:20:13 +02004803 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004804}
4805
4806void ssl_session_free( ssl_session *session )
4807{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004808 if( session == NULL )
4809 return;
4810
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004811#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004812 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004813 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004814 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004815 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004816 }
Paul Bakkered27a042013-04-18 22:46:23 +02004817#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004818
Paul Bakkera503a632013-08-14 13:48:06 +02004819#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004820 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004821#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004822
Paul Bakker34617722014-06-13 17:20:13 +02004823 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004824}
4825
Paul Bakker5121ce52009-01-03 21:22:43 +00004826/*
4827 * Free an SSL context
4828 */
4829void ssl_free( ssl_context *ssl )
4830{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004831 if( ssl == NULL )
4832 return;
4833
Paul Bakker5121ce52009-01-03 21:22:43 +00004834 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4835
Paul Bakker5121ce52009-01-03 21:22:43 +00004836 if( ssl->out_ctr != NULL )
4837 {
Paul Bakker34617722014-06-13 17:20:13 +02004838 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004839 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004840 }
4841
4842 if( ssl->in_ctr != NULL )
4843 {
Paul Bakker34617722014-06-13 17:20:13 +02004844 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004845 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 }
4847
Paul Bakker16770332013-10-11 09:59:44 +02004848#if defined(POLARSSL_ZLIB_SUPPORT)
4849 if( ssl->compress_buf != NULL )
4850 {
Paul Bakker34617722014-06-13 17:20:13 +02004851 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004852 polarssl_free( ssl->compress_buf );
4853 }
4854#endif
4855
Paul Bakker40e46942009-01-03 21:51:57 +00004856#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004857 mpi_free( &ssl->dhm_P );
4858 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004859#endif
4860
Paul Bakker48916f92012-09-16 19:57:18 +00004861 if( ssl->transform )
4862 {
4863 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004864 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004865 }
4866
4867 if( ssl->handshake )
4868 {
4869 ssl_handshake_free( ssl->handshake );
4870 ssl_transform_free( ssl->transform_negotiate );
4871 ssl_session_free( ssl->session_negotiate );
4872
Paul Bakker6e339b52013-07-03 13:37:05 +02004873 polarssl_free( ssl->handshake );
4874 polarssl_free( ssl->transform_negotiate );
4875 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004876 }
4877
Paul Bakkerc0463502013-02-14 11:19:38 +01004878 if( ssl->session )
4879 {
4880 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004881 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004882 }
4883
Paul Bakkera503a632013-08-14 13:48:06 +02004884#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004885 if( ssl->ticket_keys )
4886 {
4887 ssl_ticket_keys_free( ssl->ticket_keys );
4888 polarssl_free( ssl->ticket_keys );
4889 }
Paul Bakkera503a632013-08-14 13:48:06 +02004890#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004891
Paul Bakker0be444a2013-08-27 21:55:01 +02004892#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004893 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004894 {
Paul Bakker34617722014-06-13 17:20:13 +02004895 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004896 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004897 ssl->hostname_len = 0;
4898 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004899#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004900
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004901#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004902 if( ssl->psk != NULL )
4903 {
Paul Bakker34617722014-06-13 17:20:13 +02004904 polarssl_zeroize( ssl->psk, ssl->psk_len );
4905 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004906 polarssl_free( ssl->psk );
4907 polarssl_free( ssl->psk_identity );
4908 ssl->psk_len = 0;
4909 ssl->psk_identity_len = 0;
4910 }
4911#endif
4912
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004913#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004914 ssl_key_cert_free( ssl->key_cert );
4915#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004916
Paul Bakker05ef8352012-05-08 09:17:57 +00004917#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4918 if( ssl_hw_record_finish != NULL )
4919 {
4920 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4921 ssl_hw_record_finish( ssl );
4922 }
4923#endif
4924
Paul Bakker5121ce52009-01-03 21:22:43 +00004925 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004926
Paul Bakker86f04f42013-02-14 11:20:09 +01004927 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004928 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004929}
4930
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004931#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004932/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004933 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004934 */
4935unsigned char ssl_sig_from_pk( pk_context *pk )
4936{
4937#if defined(POLARSSL_RSA_C)
4938 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4939 return( SSL_SIG_RSA );
4940#endif
4941#if defined(POLARSSL_ECDSA_C)
4942 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4943 return( SSL_SIG_ECDSA );
4944#endif
4945 return( SSL_SIG_ANON );
4946}
4947
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004948pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4949{
4950 switch( sig )
4951 {
4952#if defined(POLARSSL_RSA_C)
4953 case SSL_SIG_RSA:
4954 return( POLARSSL_PK_RSA );
4955#endif
4956#if defined(POLARSSL_ECDSA_C)
4957 case SSL_SIG_ECDSA:
4958 return( POLARSSL_PK_ECDSA );
4959#endif
4960 default:
4961 return( POLARSSL_PK_NONE );
4962 }
4963}
Paul Bakker9af723c2014-05-01 13:03:14 +02004964#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004965
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004966/*
4967 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4968 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004969md_type_t ssl_md_alg_from_hash( unsigned char hash )
4970{
4971 switch( hash )
4972 {
4973#if defined(POLARSSL_MD5_C)
4974 case SSL_HASH_MD5:
4975 return( POLARSSL_MD_MD5 );
4976#endif
4977#if defined(POLARSSL_SHA1_C)
4978 case SSL_HASH_SHA1:
4979 return( POLARSSL_MD_SHA1 );
4980#endif
4981#if defined(POLARSSL_SHA256_C)
4982 case SSL_HASH_SHA224:
4983 return( POLARSSL_MD_SHA224 );
4984 case SSL_HASH_SHA256:
4985 return( POLARSSL_MD_SHA256 );
4986#endif
4987#if defined(POLARSSL_SHA512_C)
4988 case SSL_HASH_SHA384:
4989 return( POLARSSL_MD_SHA384 );
4990 case SSL_HASH_SHA512:
4991 return( POLARSSL_MD_SHA512 );
4992#endif
4993 default:
4994 return( POLARSSL_MD_NONE );
4995 }
4996}
4997
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004998#if defined(POLARSSL_SSL_SET_CURVES)
4999/*
5000 * Check is a curve proposed by the peer is in our list.
5001 * Return 1 if we're willing to use it, 0 otherwise.
5002 */
5003int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5004{
5005 const ecp_group_id *gid;
5006
5007 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5008 if( *gid == grp_id )
5009 return( 1 );
5010
5011 return( 0 );
5012}
Paul Bakker9af723c2014-05-01 13:03:14 +02005013#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005014
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005015#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005016int ssl_check_cert_usage( const x509_crt *cert,
5017 const ssl_ciphersuite_t *ciphersuite,
5018 int cert_endpoint )
5019{
5020#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5021 int usage = 0;
5022#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005023#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5024 const char *ext_oid;
5025 size_t ext_len;
5026#endif
5027
5028#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5029 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5030 ((void) cert);
5031 ((void) cert_endpoint);
5032#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005033
5034#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5035 if( cert_endpoint == SSL_IS_SERVER )
5036 {
5037 /* Server part of the key exchange */
5038 switch( ciphersuite->key_exchange )
5039 {
5040 case POLARSSL_KEY_EXCHANGE_RSA:
5041 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5042 usage = KU_KEY_ENCIPHERMENT;
5043 break;
5044
5045 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5046 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5047 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5048 usage = KU_DIGITAL_SIGNATURE;
5049 break;
5050
5051 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5052 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5053 usage = KU_KEY_AGREEMENT;
5054 break;
5055
5056 /* Don't use default: we want warnings when adding new values */
5057 case POLARSSL_KEY_EXCHANGE_NONE:
5058 case POLARSSL_KEY_EXCHANGE_PSK:
5059 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5060 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5061 usage = 0;
5062 }
5063 }
5064 else
5065 {
5066 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5067 usage = KU_DIGITAL_SIGNATURE;
5068 }
5069
5070 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5071 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005072#else
5073 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005074#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5075
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005076#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5077 if( cert_endpoint == SSL_IS_SERVER )
5078 {
5079 ext_oid = OID_SERVER_AUTH;
5080 ext_len = OID_SIZE( OID_SERVER_AUTH );
5081 }
5082 else
5083 {
5084 ext_oid = OID_CLIENT_AUTH;
5085 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5086 }
5087
5088 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5089 return( -1 );
5090#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5091
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005092 return( 0 );
5093}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005094#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005095
5096#endif /* POLARSSL_SSL_TLS_C */