blob: 6462ad0bd8802c026aeefc97a3dd3251f927610b [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
Manuel Pégourié-Gonnard9d7821d2014-11-06 01:19:52 +01001106 /* Unused if AEAD is the only option */
1107 ((void) ssl);
1108 ((void) session);
1109 ((void) mode);
1110
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001111 return( MAC_NONE );
1112}
1113
Paul Bakker5121ce52009-01-03 21:22:43 +00001114/*
1115 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001116 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001117static int ssl_encrypt_buf( ssl_context *ssl )
1118{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001119 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001120 const cipher_mode_t mode = cipher_get_cipher_mode(
1121 &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001122 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001123
1124 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1125
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001126 mac_order = ssl_get_mac_order( ssl, ssl->session_out, mode );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001127
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001129 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001131#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1132 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1133 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001134 if( mac_order == MAC_PLAINTEXT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001136#if defined(POLARSSL_SSL_PROTO_SSL3)
1137 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1138 {
1139 ssl_mac( &ssl->transform_out->md_ctx_enc,
1140 ssl->transform_out->mac_enc,
1141 ssl->out_msg, ssl->out_msglen,
1142 ssl->out_ctr, ssl->out_msgtype );
1143 }
1144 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001145#endif
1146#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001147 defined(POLARSSL_SSL_PROTO_TLS1_2)
1148 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1149 {
1150 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1151 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1152 ssl->out_msg, ssl->out_msglen );
1153 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1154 ssl->out_msg + ssl->out_msglen );
1155 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1156 }
1157 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001158#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001159 {
1160 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001161 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001162 }
1163
1164 SSL_DEBUG_BUF( 4, "computed mac",
1165 ssl->out_msg + ssl->out_msglen,
1166 ssl->transform_out->maclen );
1167
1168 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001169 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001170#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001171
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001172 /*
1173 * Encrypt
1174 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001175#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001176 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001178 int ret;
1179 size_t olen = 0;
1180
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1182 "including %d bytes of padding",
1183 ssl->out_msglen, 0 ) );
1184
1185 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1186 ssl->out_msg, ssl->out_msglen );
1187
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001188 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001189 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001190 ssl->transform_out->ivlen,
1191 ssl->out_msg, ssl->out_msglen,
1192 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001193 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001194 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001195 return( ret );
1196 }
1197
1198 if( ssl->out_msglen != olen )
1199 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001200 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001201 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001202 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 }
Paul Bakker68884e32013-01-07 18:20:04 +01001204 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001205#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001206#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1207 if( mode == POLARSSL_MODE_GCM ||
1208 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001209 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001210 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001211 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001212 unsigned char *enc_msg;
1213 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001214 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1215 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001216
Paul Bakkerca4ab492012-04-18 14:23:57 +00001217 memcpy( add_data, ssl->out_ctr, 8 );
1218 add_data[8] = ssl->out_msgtype;
1219 add_data[9] = ssl->major_ver;
1220 add_data[10] = ssl->minor_ver;
1221 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1222 add_data[12] = ssl->out_msglen & 0xFF;
1223
1224 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1225 add_data, 13 );
1226
Paul Bakker68884e32013-01-07 18:20:04 +01001227 /*
1228 * Generate IV
1229 */
1230 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001231 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1232 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001233 if( ret != 0 )
1234 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001235
Paul Bakker68884e32013-01-07 18:20:04 +01001236 memcpy( ssl->out_iv,
1237 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1238 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001239
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001240 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001241 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001242
Paul Bakker68884e32013-01-07 18:20:04 +01001243 /*
1244 * Fix pointer positions and message length with added IV
1245 */
1246 enc_msg = ssl->out_msg;
1247 enc_msglen = ssl->out_msglen;
1248 ssl->out_msglen += ssl->transform_out->ivlen -
1249 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001250
Paul Bakker68884e32013-01-07 18:20:04 +01001251 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1252 "including %d bytes of padding",
1253 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001254
Paul Bakker68884e32013-01-07 18:20:04 +01001255 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1256 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001257
Paul Bakker68884e32013-01-07 18:20:04 +01001258 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001259 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001260 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001261 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1262 ssl->transform_out->iv_enc,
1263 ssl->transform_out->ivlen,
1264 add_data, 13,
1265 enc_msg, enc_msglen,
1266 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001267 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001268 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001269 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001270 return( ret );
1271 }
1272
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001273 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001274 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001275 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001276 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001277 }
1278
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001279 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001280
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001281 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001282 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001284#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001285#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1286 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001287 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001289 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001290 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001291 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001292
Paul Bakker48916f92012-09-16 19:57:18 +00001293 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1294 ssl->transform_out->ivlen;
1295 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 padlen = 0;
1297
1298 for( i = 0; i <= padlen; i++ )
1299 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1300
1301 ssl->out_msglen += padlen + 1;
1302
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001303 enc_msglen = ssl->out_msglen;
1304 enc_msg = ssl->out_msg;
1305
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001306#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001307 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001308 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1309 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001310 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001311 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001312 {
1313 /*
1314 * Generate IV
1315 */
Paul Bakker48916f92012-09-16 19:57:18 +00001316 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1317 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001318 if( ret != 0 )
1319 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001320
Paul Bakker92be97b2013-01-02 17:30:03 +01001321 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001322 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001323
1324 /*
1325 * Fix pointer positions and message length with added IV
1326 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001327 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001328 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001329 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001330 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001331#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001332
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001334 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001335 ssl->out_msglen, ssl->transform_out->ivlen,
1336 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
1338 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001339 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001341 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001342 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001343 ssl->transform_out->ivlen,
1344 enc_msg, enc_msglen,
1345 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001346 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001347 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001348 return( ret );
1349 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001350
Paul Bakkercca5b812013-08-31 17:40:26 +02001351 if( enc_msglen != olen )
1352 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001353 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001354 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001355 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001356
1357#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001358 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1359 {
1360 /*
1361 * Save IV in SSL3 and TLS1
1362 */
1363 memcpy( ssl->transform_out->iv_enc,
1364 ssl->transform_out->cipher_ctx_enc.iv,
1365 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001367#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001368
1369#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1370 if( mac_order == MAC_CIPHERTEXT )
1371 {
1372 /*
1373 * MAC(MAC_write_key, seq_num +
1374 * TLSCipherText.type +
1375 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001376 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001377 * IV + // except for TLS 1.0
1378 * ENC(content + padding + padding_length));
1379 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001380 unsigned char pseudo_hdr[13];
1381
1382 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1383 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001384 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1385 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1386
1387 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001388
1389 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1390 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1391 ssl->out_iv, ssl->out_msglen );
1392 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1393 ssl->out_iv + ssl->out_msglen );
1394 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1395
1396 ssl->out_msglen += ssl->transform_out->maclen;
1397 }
1398#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001400 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001401#endif /* POLARSSL_CIPHER_MODE_CBC &&
1402 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001403 {
1404 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001405 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Paul Bakkerca4ab492012-04-18 14:23:57 +00001408 for( i = 8; i > 0; i-- )
1409 if( ++ssl->out_ctr[i - 1] != 0 )
1410 break;
1411
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001412 /* The loops goes to its end iff the counter is wrapping */
1413 if( i == 0 )
1414 {
1415 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1416 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1417 }
1418
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1420
1421 return( 0 );
1422}
1423
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001424#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001425
Paul Bakker5121ce52009-01-03 21:22:43 +00001426static int ssl_decrypt_buf( ssl_context *ssl )
1427{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001428 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001429 const cipher_mode_t mode = cipher_get_cipher_mode(
1430 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001431#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1432 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1433 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1434 size_t padlen = 0, correct = 1;
1435#endif
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001436 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
1438 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1439
Paul Bakker48916f92012-09-16 19:57:18 +00001440 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 {
1442 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001443 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001444 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 }
1446
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001447 mac_order = ssl_get_mac_order( ssl, ssl->session_in, mode );
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001448
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001449#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001450 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001451 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001452 int ret;
1453 size_t olen = 0;
1454
Paul Bakker68884e32013-01-07 18:20:04 +01001455 padlen = 0;
1456
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001457 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001458 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001459 ssl->transform_in->ivlen,
1460 ssl->in_msg, ssl->in_msglen,
1461 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001462 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001463 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001464 return( ret );
1465 }
1466
1467 if( ssl->in_msglen != olen )
1468 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001469 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001470 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001471 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 }
Paul Bakker68884e32013-01-07 18:20:04 +01001473 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001474#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001475#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1476 if( mode == POLARSSL_MODE_GCM ||
1477 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001478 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001479 int ret;
1480 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001481 unsigned char *dec_msg;
1482 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001483 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001484 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1485 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001486 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1487 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001488
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001489 if( ssl->in_msglen < explicit_iv_len + taglen )
1490 {
1491 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1492 "+ taglen (%d)", ssl->in_msglen,
1493 explicit_iv_len, taglen ) );
1494 return( POLARSSL_ERR_SSL_INVALID_MAC );
1495 }
1496 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1497
Paul Bakker68884e32013-01-07 18:20:04 +01001498 dec_msg = ssl->in_msg;
1499 dec_msg_result = ssl->in_msg;
1500 ssl->in_msglen = dec_msglen;
1501
1502 memcpy( add_data, ssl->in_ctr, 8 );
1503 add_data[8] = ssl->in_msgtype;
1504 add_data[9] = ssl->major_ver;
1505 add_data[10] = ssl->minor_ver;
1506 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1507 add_data[12] = ssl->in_msglen & 0xFF;
1508
1509 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1510 add_data, 13 );
1511
1512 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1513 ssl->in_iv,
1514 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1515
1516 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1517 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001518 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001519
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001520 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001521 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001522 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001523 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1524 ssl->transform_in->iv_dec,
1525 ssl->transform_in->ivlen,
1526 add_data, 13,
1527 dec_msg, dec_msglen,
1528 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001529 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001530 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001531 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1532
1533 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1534 return( POLARSSL_ERR_SSL_INVALID_MAC );
1535
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001536 return( ret );
1537 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001538
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001539 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001540 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001541 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001542 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001543 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001546#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001547#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1548 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001549 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001550 {
Paul Bakker45829992013-01-03 14:52:21 +01001551 /*
1552 * Decrypt and check the padding
1553 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001554 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001555 unsigned char *dec_msg;
1556 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001557 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001558 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001559 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001560
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 /*
Paul Bakker45829992013-01-03 14:52:21 +01001562 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001564#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001565 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1566 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001567#endif
Paul Bakker45829992013-01-03 14:52:21 +01001568
1569 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1570 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1571 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001572 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1573 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1574 ssl->transform_in->ivlen,
1575 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001576 return( POLARSSL_ERR_SSL_INVALID_MAC );
1577 }
1578
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001579 dec_msglen = ssl->in_msglen;
1580 dec_msg = ssl->in_msg;
1581 dec_msg_result = ssl->in_msg;
1582
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001583 /*
1584 * Authenticate before decrypt if enabled
1585 */
1586#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1587 if( mac_order == MAC_CIPHERTEXT )
1588 {
1589 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001590 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001591
1592 dec_msglen -= ssl->transform_in->maclen;
1593 ssl->in_msglen -= ssl->transform_in->maclen;
1594
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001595 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1596 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1597 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1598 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1599
1600 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1601
1602 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001603 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1604 ssl->in_iv, ssl->in_msglen );
1605 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1606 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1607
1608 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1609 ssl->transform_in->maclen );
1610 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1611 ssl->transform_in->maclen );
1612
1613 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1614 ssl->transform_in->maclen ) != 0 )
1615 {
1616 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1617
1618 return( POLARSSL_ERR_SSL_INVALID_MAC );
1619 }
1620 }
1621#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1622
1623 /*
1624 * Check length sanity
1625 */
1626 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1627 {
1628 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1629 ssl->in_msglen, ssl->transform_in->ivlen ) );
1630 return( POLARSSL_ERR_SSL_INVALID_MAC );
1631 }
1632
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001633#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001634 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001635 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001636 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001637 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001638 {
Paul Bakker48916f92012-09-16 19:57:18 +00001639 dec_msglen -= ssl->transform_in->ivlen;
1640 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001641
Paul Bakker48916f92012-09-16 19:57:18 +00001642 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001643 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001644 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001645#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001646
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001647 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001648 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001649 ssl->transform_in->ivlen,
1650 dec_msg, dec_msglen,
1651 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001652 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001653 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001654 return( ret );
1655 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001656
Paul Bakkercca5b812013-08-31 17:40:26 +02001657 if( dec_msglen != olen )
1658 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001659 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001660 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001661 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001662
1663#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001664 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1665 {
1666 /*
1667 * Save IV in SSL3 and TLS1
1668 */
1669 memcpy( ssl->transform_in->iv_dec,
1670 ssl->transform_in->cipher_ctx_dec.iv,
1671 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001672 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001673#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001674
1675 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001676
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001677 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
1678 mac_order == MAC_PLAINTEXT )
Paul Bakker45829992013-01-03 14:52:21 +01001679 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001680#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001681 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1682 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001683#endif
Paul Bakker45829992013-01-03 14:52:21 +01001684 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001685 correct = 0;
1686 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001688#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001689 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1690 {
Paul Bakker48916f92012-09-16 19:57:18 +00001691 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001692 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001693#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1695 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001696 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001697#endif
Paul Bakker45829992013-01-03 14:52:21 +01001698 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 }
1700 }
1701 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001702#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001703#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1704 defined(POLARSSL_SSL_PROTO_TLS1_2)
1705 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 {
1707 /*
Paul Bakker45829992013-01-03 14:52:21 +01001708 * TLSv1+: always check the padding up to the first failure
1709 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001710 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001711 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001712 size_t padding_idx = ssl->in_msglen - padlen - 1;
1713
Paul Bakker956c9e02013-12-19 14:42:28 +01001714 /*
1715 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001716 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001717 *
Paul Bakker61885c72014-04-25 12:59:03 +02001718 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1719 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001720 *
1721 * In both cases we reset padding_idx to a safe value (0) to
1722 * prevent out-of-buffer reads.
1723 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001724 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001725 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1726 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001727
1728 padding_idx *= correct;
1729
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001730 for( i = 1; i <= 256; i++ )
1731 {
1732 real_count &= ( i <= padlen );
1733 pad_count += real_count *
1734 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1735 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001736
1737 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001738
Paul Bakkerd66f0702013-01-31 16:57:45 +01001739#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001740 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001741 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001742#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001743 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001744 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001745 else
1746#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1747 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001748 {
1749 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001750 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001751 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001752
1753 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001754 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001755 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001756#endif /* POLARSSL_CIPHER_MODE_CBC &&
1757 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001758 {
1759 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001760 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001761 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
1763 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1764 ssl->in_msg, ssl->in_msglen );
1765
1766 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001767 * Authenticate if not done yet.
1768 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001770#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1771 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1772 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001773 if( mac_order == MAC_PLAINTEXT )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001774 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001775 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1776
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001777 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001779 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1780 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001782 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001784#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001785 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1786 {
1787 ssl_mac( &ssl->transform_in->md_ctx_dec,
1788 ssl->transform_in->mac_dec,
1789 ssl->in_msg, ssl->in_msglen,
1790 ssl->in_ctr, ssl->in_msgtype );
1791 }
1792 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001793#endif /* POLARSSL_SSL_PROTO_SSL3 */
1794#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001795 defined(POLARSSL_SSL_PROTO_TLS1_2)
1796 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1797 {
1798 /*
1799 * Process MAC and always update for padlen afterwards to make
1800 * total time independent of padlen
1801 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001802 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001803 *
1804 * Known timing attacks:
1805 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1806 *
1807 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1808 * correctly. (We round down instead of up, so -56 is the correct
1809 * value for our calculations instead of -55)
1810 */
1811 size_t j, extra_run = 0;
1812 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1813 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001814
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001815 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001816
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001817 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1818 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1819 ssl->in_msglen );
1820 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1821 ssl->in_msg + ssl->in_msglen );
1822 for( j = 0; j < extra_run; j++ )
1823 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001824
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001825 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1826 }
1827 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001828#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001829 POLARSSL_SSL_PROTO_TLS1_2 */
1830 {
1831 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001832 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001833 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001834
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001835 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1836 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1837 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001838
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001839 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001840 ssl->transform_in->maclen ) != 0 )
1841 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001842#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001843 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001844#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001845 correct = 0;
1846 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001848 /*
1849 * Finally check the correct flag
1850 */
1851 if( correct == 0 )
1852 return( POLARSSL_ERR_SSL_INVALID_MAC );
1853 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001854#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
1856 if( ssl->in_msglen == 0 )
1857 {
1858 ssl->nb_zero++;
1859
1860 /*
1861 * Three or more empty messages may be a DoS attack
1862 * (excessive CPU consumption).
1863 */
1864 if( ssl->nb_zero > 3 )
1865 {
1866 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1867 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001868 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001869 }
1870 }
1871 else
1872 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001873
Paul Bakker23986e52011-04-24 08:57:21 +00001874 for( i = 8; i > 0; i-- )
1875 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 break;
1877
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001878 /* The loops goes to its end iff the counter is wrapping */
1879 if( i == 0 )
1880 {
1881 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1882 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1883 }
1884
Paul Bakker5121ce52009-01-03 21:22:43 +00001885 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1886
1887 return( 0 );
1888}
1889
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001890#undef MAC_NONE
1891#undef MAC_PLAINTEXT
1892#undef MAC_CIPHERTEXT
1893
Paul Bakker2770fbd2012-07-03 13:30:23 +00001894#if defined(POLARSSL_ZLIB_SUPPORT)
1895/*
1896 * Compression/decompression functions
1897 */
1898static int ssl_compress_buf( ssl_context *ssl )
1899{
1900 int ret;
1901 unsigned char *msg_post = ssl->out_msg;
1902 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001903 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001904
1905 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1906
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001907 if( len_pre == 0 )
1908 return( 0 );
1909
Paul Bakker2770fbd2012-07-03 13:30:23 +00001910 memcpy( msg_pre, ssl->out_msg, len_pre );
1911
1912 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1913 ssl->out_msglen ) );
1914
1915 SSL_DEBUG_BUF( 4, "before compression: output payload",
1916 ssl->out_msg, ssl->out_msglen );
1917
Paul Bakker48916f92012-09-16 19:57:18 +00001918 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1919 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1920 ssl->transform_out->ctx_deflate.next_out = msg_post;
1921 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001922
Paul Bakker48916f92012-09-16 19:57:18 +00001923 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001924 if( ret != Z_OK )
1925 {
1926 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1927 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1928 }
1929
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001930 ssl->out_msglen = SSL_BUFFER_LEN -
1931 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001932
Paul Bakker2770fbd2012-07-03 13:30:23 +00001933 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1934 ssl->out_msglen ) );
1935
1936 SSL_DEBUG_BUF( 4, "after compression: output payload",
1937 ssl->out_msg, ssl->out_msglen );
1938
1939 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1940
1941 return( 0 );
1942}
1943
1944static int ssl_decompress_buf( ssl_context *ssl )
1945{
1946 int ret;
1947 unsigned char *msg_post = ssl->in_msg;
1948 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001949 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001950
1951 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1952
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001953 if( len_pre == 0 )
1954 return( 0 );
1955
Paul Bakker2770fbd2012-07-03 13:30:23 +00001956 memcpy( msg_pre, ssl->in_msg, len_pre );
1957
1958 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1959 ssl->in_msglen ) );
1960
1961 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1962 ssl->in_msg, ssl->in_msglen );
1963
Paul Bakker48916f92012-09-16 19:57:18 +00001964 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1965 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1966 ssl->transform_in->ctx_inflate.next_out = msg_post;
1967 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001968
Paul Bakker48916f92012-09-16 19:57:18 +00001969 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001970 if( ret != Z_OK )
1971 {
1972 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1973 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1974 }
1975
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001976 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1977 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001978
Paul Bakker2770fbd2012-07-03 13:30:23 +00001979 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1980 ssl->in_msglen ) );
1981
1982 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1983 ssl->in_msg, ssl->in_msglen );
1984
1985 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1986
1987 return( 0 );
1988}
1989#endif /* POLARSSL_ZLIB_SUPPORT */
1990
Paul Bakker5121ce52009-01-03 21:22:43 +00001991/*
1992 * Fill the input message buffer
1993 */
Paul Bakker23986e52011-04-24 08:57:21 +00001994int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001995{
Paul Bakker23986e52011-04-24 08:57:21 +00001996 int ret;
1997 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001998
1999 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2000
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002001 if( nb_want > SSL_BUFFER_LEN - 8 )
2002 {
2003 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2004 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2005 }
2006
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 while( ssl->in_left < nb_want )
2008 {
2009 len = nb_want - ssl->in_left;
2010 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2011
2012 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2013 ssl->in_left, nb_want ) );
2014 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2015
Paul Bakker831a7552011-05-18 13:32:51 +00002016 if( ret == 0 )
2017 return( POLARSSL_ERR_SSL_CONN_EOF );
2018
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 if( ret < 0 )
2020 return( ret );
2021
2022 ssl->in_left += ret;
2023 }
2024
2025 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2026
2027 return( 0 );
2028}
2029
2030/*
2031 * Flush any data not yet written
2032 */
2033int ssl_flush_output( ssl_context *ssl )
2034{
2035 int ret;
2036 unsigned char *buf;
2037
2038 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2039
2040 while( ssl->out_left > 0 )
2041 {
2042 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2043 5 + ssl->out_msglen, ssl->out_left ) );
2044
Paul Bakker5bd42292012-12-19 14:40:42 +01002045 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002047
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2049
2050 if( ret <= 0 )
2051 return( ret );
2052
2053 ssl->out_left -= ret;
2054 }
2055
2056 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2057
2058 return( 0 );
2059}
2060
2061/*
2062 * Record layer functions
2063 */
2064int ssl_write_record( ssl_context *ssl )
2065{
Paul Bakker05ef8352012-05-08 09:17:57 +00002066 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002067 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002068
2069 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2070
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2072 {
2073 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2074 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2075 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2076
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002077 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2078 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 }
2080
Paul Bakker2770fbd2012-07-03 13:30:23 +00002081#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002082 if( ssl->transform_out != NULL &&
2083 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002084 {
2085 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2086 {
2087 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2088 return( ret );
2089 }
2090
2091 len = ssl->out_msglen;
2092 }
2093#endif /*POLARSSL_ZLIB_SUPPORT */
2094
Paul Bakker05ef8352012-05-08 09:17:57 +00002095#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002096 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002098 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002099
Paul Bakker05ef8352012-05-08 09:17:57 +00002100 ret = ssl_hw_record_write( ssl );
2101 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2102 {
2103 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002104 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002105 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002106
2107 if( ret == 0 )
2108 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002109 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002110#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002111 if( !done )
2112 {
2113 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2114 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2115 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2117 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002118
Paul Bakker48916f92012-09-16 19:57:18 +00002119 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002120 {
2121 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2122 {
2123 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2124 return( ret );
2125 }
2126
2127 len = ssl->out_msglen;
2128 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2129 ssl->out_hdr[4] = (unsigned char)( len );
2130 }
2131
2132 ssl->out_left = 5 + ssl->out_msglen;
2133
2134 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2135 "version = [%d:%d], msglen = %d",
2136 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2137 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2138
2139 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002140 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 }
2142
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2144 {
2145 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2146 return( ret );
2147 }
2148
2149 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2150
2151 return( 0 );
2152}
2153
2154int ssl_read_record( ssl_context *ssl )
2155{
Paul Bakker05ef8352012-05-08 09:17:57 +00002156 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
2158 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2159
2160 if( ssl->in_hslen != 0 &&
2161 ssl->in_hslen < ssl->in_msglen )
2162 {
2163 /*
2164 * Get next Handshake message in the current record
2165 */
2166 ssl->in_msglen -= ssl->in_hslen;
2167
Paul Bakker8934a982011-08-05 11:11:53 +00002168 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002169 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
2171 ssl->in_hslen = 4;
2172 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2173
2174 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2175 " %d, type = %d, hslen = %d",
2176 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2177
2178 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2179 {
2180 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002181 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 }
2183
2184 if( ssl->in_msglen < ssl->in_hslen )
2185 {
2186 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002187 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 }
2189
Paul Bakker4224bc02014-04-08 14:36:50 +02002190 if( ssl->state != SSL_HANDSHAKE_OVER )
2191 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
2193 return( 0 );
2194 }
2195
2196 ssl->in_hslen = 0;
2197
2198 /*
2199 * Read the record header and validate it
2200 */
2201 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2202 {
2203 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2204 return( ret );
2205 }
2206
2207 ssl->in_msgtype = ssl->in_hdr[0];
2208 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2209
2210 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2211 "version = [%d:%d], msglen = %d",
2212 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2213 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2214
2215 if( ssl->in_hdr[1] != ssl->major_ver )
2216 {
2217 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002218 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 }
2220
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002221 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 {
2223 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002224 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 }
2226
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002227 /* Sanity check (outer boundaries) */
2228 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2229 {
2230 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2231 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2232 }
2233
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002235 * Make sure the message length is acceptable for the current transform
2236 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 */
Paul Bakker48916f92012-09-16 19:57:18 +00002238 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002240 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 {
2242 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002243 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 }
2245 }
2246 else
2247 {
Paul Bakker48916f92012-09-16 19:57:18 +00002248 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 {
2250 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002251 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 }
2253
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002254#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002256 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 {
2258 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002259 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002261#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002262
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002263#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2264 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 /*
2266 * TLS encrypted messages can have up to 256 bytes of padding
2267 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002268 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002269 ssl->in_msglen > ssl->transform_in->minlen +
2270 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 {
2272 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002273 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 }
2277
2278 /*
2279 * Read and optionally decrypt the message contents
2280 */
2281 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2282 {
2283 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2284 return( ret );
2285 }
2286
2287 SSL_DEBUG_BUF( 4, "input record from network",
2288 ssl->in_hdr, 5 + ssl->in_msglen );
2289
Paul Bakker05ef8352012-05-08 09:17:57 +00002290#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002291 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002292 {
2293 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2294
2295 ret = ssl_hw_record_read( ssl );
2296 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2297 {
2298 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002299 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002300 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002301
2302 if( ret == 0 )
2303 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002304 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002305#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002306 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002307 {
2308 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2309 {
Paul Bakker40865c82013-01-31 17:13:13 +01002310#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2311 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2312 {
2313 ssl_send_alert_message( ssl,
2314 SSL_ALERT_LEVEL_FATAL,
2315 SSL_ALERT_MSG_BAD_RECORD_MAC );
2316 }
2317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2319 return( ret );
2320 }
2321
2322 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2323 ssl->in_msg, ssl->in_msglen );
2324
2325 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2326 {
2327 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002328 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 }
2330 }
2331
Paul Bakker2770fbd2012-07-03 13:30:23 +00002332#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002333 if( ssl->transform_in != NULL &&
2334 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002335 {
2336 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2337 {
2338 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2339 return( ret );
2340 }
2341
2342 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2343 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2344 }
2345#endif /* POLARSSL_ZLIB_SUPPORT */
2346
Paul Bakker0a925182012-04-16 06:46:41 +00002347 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2348 ssl->in_msgtype != SSL_MSG_ALERT &&
2349 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2350 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2351 {
2352 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2353
Paul Bakker48916f92012-09-16 19:57:18 +00002354 if( ( ret = ssl_send_alert_message( ssl,
2355 SSL_ALERT_LEVEL_FATAL,
2356 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002357 {
2358 return( ret );
2359 }
2360
2361 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2362 }
2363
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2365 {
2366 ssl->in_hslen = 4;
2367 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2368
2369 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2370 " %d, type = %d, hslen = %d",
2371 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2372
2373 /*
2374 * Additional checks to validate the handshake header
2375 */
2376 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2377 {
2378 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002379 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 }
2381
2382 if( ssl->in_msglen < ssl->in_hslen )
2383 {
2384 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002385 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 }
2387
Paul Bakker48916f92012-09-16 19:57:18 +00002388 if( ssl->state != SSL_HANDSHAKE_OVER )
2389 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 }
2391
2392 if( ssl->in_msgtype == SSL_MSG_ALERT )
2393 {
2394 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2395 ssl->in_msg[0], ssl->in_msg[1] ) );
2396
2397 /*
2398 * Ignore non-fatal alerts, except close_notify
2399 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002400 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002402 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2403 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002404 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 }
2406
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002407 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2408 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 {
2410 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002411 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 }
2413 }
2414
2415 ssl->in_left = 0;
2416
2417 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2418
2419 return( 0 );
2420}
2421
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002422int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2423{
2424 int ret;
2425
2426 if( ( ret = ssl_send_alert_message( ssl,
2427 SSL_ALERT_LEVEL_FATAL,
2428 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2429 {
2430 return( ret );
2431 }
2432
2433 return( 0 );
2434}
2435
Paul Bakker0a925182012-04-16 06:46:41 +00002436int ssl_send_alert_message( ssl_context *ssl,
2437 unsigned char level,
2438 unsigned char message )
2439{
2440 int ret;
2441
2442 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2443
2444 ssl->out_msgtype = SSL_MSG_ALERT;
2445 ssl->out_msglen = 2;
2446 ssl->out_msg[0] = level;
2447 ssl->out_msg[1] = message;
2448
2449 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2450 {
2451 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2452 return( ret );
2453 }
2454
2455 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2456
2457 return( 0 );
2458}
2459
Paul Bakker5121ce52009-01-03 21:22:43 +00002460/*
2461 * Handshake functions
2462 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002463#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2464 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2465 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2466 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2467 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2468 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2469 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002470int ssl_write_certificate( ssl_context *ssl )
2471{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002472 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
2474 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2475
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002476 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002477 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2478 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002479 {
2480 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2481 ssl->state++;
2482 return( 0 );
2483 }
2484
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002485 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2486 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002487}
2488
2489int ssl_parse_certificate( ssl_context *ssl )
2490{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002491 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2492
2493 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2494
2495 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002496 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2497 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002498 {
2499 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2500 ssl->state++;
2501 return( 0 );
2502 }
2503
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002504 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2505 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002506}
2507#else
2508int ssl_write_certificate( ssl_context *ssl )
2509{
2510 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2511 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002512 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002513 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2514
2515 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2516
2517 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002518 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2519 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002520 {
2521 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2522 ssl->state++;
2523 return( 0 );
2524 }
2525
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 if( ssl->endpoint == SSL_IS_CLIENT )
2527 {
2528 if( ssl->client_auth == 0 )
2529 {
2530 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2531 ssl->state++;
2532 return( 0 );
2533 }
2534
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002535#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 /*
2537 * If using SSLv3 and got no cert, send an Alert message
2538 * (otherwise an empty Certificate message will be sent).
2539 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002540 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2542 {
2543 ssl->out_msglen = 2;
2544 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002545 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2546 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
2548 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2549 goto write_msg;
2550 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002551#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 }
2553 else /* SSL_IS_SERVER */
2554 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002555 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 {
2557 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002558 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 }
2560 }
2561
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002562 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
2564 /*
2565 * 0 . 0 handshake type
2566 * 1 . 3 handshake length
2567 * 4 . 6 length of all certs
2568 * 7 . 9 length of cert. 1
2569 * 10 . n-1 peer certificate
2570 * n . n+2 length of cert. 2
2571 * n+3 . ... upper level cert, etc.
2572 */
2573 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002574 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Paul Bakker29087132010-03-21 21:03:34 +00002576 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 {
2578 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002579 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 {
2581 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2582 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002583 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 }
2585
2586 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2587 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2588 ssl->out_msg[i + 2] = (unsigned char)( n );
2589
2590 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2591 i += n; crt = crt->next;
2592 }
2593
2594 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2595 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2596 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2597
2598 ssl->out_msglen = i;
2599 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2600 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2601
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002602#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002603write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
2606 ssl->state++;
2607
2608 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2609 {
2610 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2611 return( ret );
2612 }
2613
2614 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2615
Paul Bakkered27a042013-04-18 22:46:23 +02002616 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002617}
2618
2619int ssl_parse_certificate( ssl_context *ssl )
2620{
Paul Bakkered27a042013-04-18 22:46:23 +02002621 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002622 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002623 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
2625 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2626
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002627 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002628 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2629 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002630 {
2631 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2632 ssl->state++;
2633 return( 0 );
2634 }
2635
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002637 ( ssl->authmode == SSL_VERIFY_NONE ||
2638 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002640 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2642 ssl->state++;
2643 return( 0 );
2644 }
2645
2646 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2647 {
2648 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2649 return( ret );
2650 }
2651
2652 ssl->state++;
2653
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002654#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 /*
2656 * Check if the client sent an empty certificate
2657 */
2658 if( ssl->endpoint == SSL_IS_SERVER &&
2659 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2660 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002661 if( ssl->in_msglen == 2 &&
2662 ssl->in_msgtype == SSL_MSG_ALERT &&
2663 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2664 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 {
2666 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2667
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002668 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2670 return( 0 );
2671 else
Paul Bakker40e46942009-01-03 21:51:57 +00002672 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 }
2674 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002675#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002677#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2678 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 if( ssl->endpoint == SSL_IS_SERVER &&
2680 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2681 {
2682 if( ssl->in_hslen == 7 &&
2683 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2684 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2685 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2686 {
2687 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2688
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002689 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002691 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 else
2693 return( 0 );
2694 }
2695 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002696#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2697 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
2699 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2700 {
2701 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002702 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 }
2704
2705 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2706 {
2707 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002708 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 }
2710
2711 /*
2712 * Same message structure as in ssl_write_certificate()
2713 */
2714 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2715
2716 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2717 {
2718 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002719 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002722 /* In case we tried to reuse a session but it failed */
2723 if( ssl->session_negotiate->peer_cert != NULL )
2724 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002725 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002726 polarssl_free( ssl->session_negotiate->peer_cert );
2727 }
2728
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002729 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2730 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 {
2732 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002733 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002734 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 }
2736
Paul Bakkerb6b09562013-09-18 14:17:41 +02002737 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
2739 i = 7;
2740
2741 while( i < ssl->in_hslen )
2742 {
2743 if( ssl->in_msg[i] != 0 )
2744 {
2745 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002746 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002747 }
2748
2749 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2750 | (unsigned int) ssl->in_msg[i + 2];
2751 i += 3;
2752
2753 if( n < 128 || i + n > ssl->in_hslen )
2754 {
2755 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002756 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 }
2758
Paul Bakkerddf26b42013-09-18 13:46:23 +02002759 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2760 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761 if( ret != 0 )
2762 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002763 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 return( ret );
2765 }
2766
2767 i += n;
2768 }
2769
Paul Bakker48916f92012-09-16 19:57:18 +00002770 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002772 /*
2773 * On client, make sure the server cert doesn't change during renego to
2774 * avoid "triple handshake" attack: https://secure-resumption.com/
2775 */
2776 if( ssl->endpoint == SSL_IS_CLIENT &&
2777 ssl->renegotiation == SSL_RENEGOTIATION )
2778 {
2779 if( ssl->session->peer_cert == NULL )
2780 {
2781 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2782 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2783 }
2784
2785 if( ssl->session->peer_cert->raw.len !=
2786 ssl->session_negotiate->peer_cert->raw.len ||
2787 memcmp( ssl->session->peer_cert->raw.p,
2788 ssl->session_negotiate->peer_cert->raw.p,
2789 ssl->session->peer_cert->raw.len ) != 0 )
2790 {
2791 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2792 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2793 }
2794 }
2795
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 if( ssl->authmode != SSL_VERIFY_NONE )
2797 {
2798 if( ssl->ca_chain == NULL )
2799 {
2800 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002801 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 }
2803
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002804 /*
2805 * Main check: verify certificate
2806 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002807 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2808 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2809 &ssl->session_negotiate->verify_result,
2810 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
2812 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002813 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002815 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002816
2817 /*
2818 * Secondary checks: always done, but change 'ret' only if it was 0
2819 */
2820
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002821#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002822 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002823 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002824
2825 /* If certificate uses an EC key, make sure the curve is OK */
2826 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2827 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2828 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002829 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002830 if( ret == 0 )
2831 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002832 }
2833 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002834#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002836 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2837 ciphersuite_info,
2838 ! ssl->endpoint ) != 0 )
2839 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002840 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002841 if( ret == 0 )
2842 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2843 }
2844
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2846 ret = 0;
2847 }
2848
2849 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2850
2851 return( ret );
2852}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002853#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2854 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2855 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2856 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2857 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2858 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2859 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
2861int ssl_write_change_cipher_spec( ssl_context *ssl )
2862{
2863 int ret;
2864
2865 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2866
2867 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2868 ssl->out_msglen = 1;
2869 ssl->out_msg[0] = 1;
2870
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 ssl->state++;
2872
2873 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2874 {
2875 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2876 return( ret );
2877 }
2878
2879 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2880
2881 return( 0 );
2882}
2883
2884int ssl_parse_change_cipher_spec( ssl_context *ssl )
2885{
2886 int ret;
2887
2888 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2889
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2891 {
2892 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2893 return( ret );
2894 }
2895
2896 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2897 {
2898 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002899 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 }
2901
2902 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2903 {
2904 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002905 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002906 }
2907
2908 ssl->state++;
2909
2910 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2911
2912 return( 0 );
2913}
2914
Paul Bakker41c83d32013-03-20 14:39:14 +01002915void ssl_optimize_checksum( ssl_context *ssl,
2916 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002917{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002918 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002919
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002920#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2921 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002922 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002923 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002924 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002925#endif
2926#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2927#if defined(POLARSSL_SHA512_C)
2928 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2929 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2930 else
2931#endif
2932#if defined(POLARSSL_SHA256_C)
2933 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002934 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002935 else
2936#endif
2937#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002938 {
2939 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002940 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002941 }
Paul Bakker380da532012-04-18 16:10:25 +00002942}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002943
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002944static void ssl_update_checksum_start( ssl_context *ssl,
2945 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002946{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002947#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2948 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002949 md5_update( &ssl->handshake->fin_md5 , buf, len );
2950 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002951#endif
2952#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2953#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002954 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002955#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002956#if defined(POLARSSL_SHA512_C)
2957 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002958#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002959#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002960}
2961
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002962#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2963 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002964static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2965 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002966{
Paul Bakker48916f92012-09-16 19:57:18 +00002967 md5_update( &ssl->handshake->fin_md5 , buf, len );
2968 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002969}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002970#endif
Paul Bakker380da532012-04-18 16:10:25 +00002971
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002972#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2973#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002974static void ssl_update_checksum_sha256( ssl_context *ssl,
2975 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002976{
Paul Bakker9e36f042013-06-30 14:34:05 +02002977 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002978}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002979#endif
Paul Bakker380da532012-04-18 16:10:25 +00002980
Paul Bakker9e36f042013-06-30 14:34:05 +02002981#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002982static void ssl_update_checksum_sha384( ssl_context *ssl,
2983 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002984{
Paul Bakker9e36f042013-06-30 14:34:05 +02002985 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002986}
Paul Bakker769075d2012-11-24 11:26:46 +01002987#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002988#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002989
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002990#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002991static void ssl_calc_finished_ssl(
2992 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002993{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002994 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995 md5_context md5;
2996 sha1_context sha1;
2997
Paul Bakker5121ce52009-01-03 21:22:43 +00002998 unsigned char padbuf[48];
2999 unsigned char md5sum[16];
3000 unsigned char sha1sum[20];
3001
Paul Bakker48916f92012-09-16 19:57:18 +00003002 ssl_session *session = ssl->session_negotiate;
3003 if( !session )
3004 session = ssl->session;
3005
Paul Bakker1ef83d62012-04-11 12:09:53 +00003006 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3007
Paul Bakker48916f92012-09-16 19:57:18 +00003008 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3009 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003010
3011 /*
3012 * SSLv3:
3013 * hash =
3014 * MD5( master + pad2 +
3015 * MD5( handshake + sender + master + pad1 ) )
3016 * + SHA1( master + pad2 +
3017 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003018 */
3019
Paul Bakker90995b52013-06-24 19:20:35 +02003020#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003022 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003023#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003024
Paul Bakker90995b52013-06-24 19:20:35 +02003025#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003027 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003028#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Paul Bakker3c2122f2013-06-24 19:03:14 +02003030 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3031 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Paul Bakker1ef83d62012-04-11 12:09:53 +00003033 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003034
Paul Bakker3c2122f2013-06-24 19:03:14 +02003035 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003036 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003037 md5_update( &md5, padbuf, 48 );
3038 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
Paul Bakker3c2122f2013-06-24 19:03:14 +02003040 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003041 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003042 sha1_update( &sha1, padbuf, 40 );
3043 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003044
Paul Bakker1ef83d62012-04-11 12:09:53 +00003045 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003046
Paul Bakker1ef83d62012-04-11 12:09:53 +00003047 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003048 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003049 md5_update( &md5, padbuf, 48 );
3050 md5_update( &md5, md5sum, 16 );
3051 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003052
Paul Bakker1ef83d62012-04-11 12:09:53 +00003053 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003054 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003055 sha1_update( &sha1, padbuf , 40 );
3056 sha1_update( &sha1, sha1sum, 20 );
3057 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Paul Bakker1ef83d62012-04-11 12:09:53 +00003059 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Paul Bakker5b4af392014-06-26 12:09:34 +02003061 md5_free( &md5 );
3062 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
Paul Bakker34617722014-06-13 17:20:13 +02003064 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3065 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3066 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003067
3068 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3069}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003070#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003071
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003072#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003073static void ssl_calc_finished_tls(
3074 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003075{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003076 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003077 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003078 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003079 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003080 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003081
Paul Bakker48916f92012-09-16 19:57:18 +00003082 ssl_session *session = ssl->session_negotiate;
3083 if( !session )
3084 session = ssl->session;
3085
Paul Bakker1ef83d62012-04-11 12:09:53 +00003086 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003087
Paul Bakker48916f92012-09-16 19:57:18 +00003088 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3089 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
Paul Bakker1ef83d62012-04-11 12:09:53 +00003091 /*
3092 * TLSv1:
3093 * hash = PRF( master, finished_label,
3094 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3095 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Paul Bakker90995b52013-06-24 19:20:35 +02003097#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003098 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3099 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003100#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003101
Paul Bakker90995b52013-06-24 19:20:35 +02003102#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003103 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3104 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003105#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003106
3107 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003108 ? "client finished"
3109 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003110
3111 md5_finish( &md5, padbuf );
3112 sha1_finish( &sha1, padbuf + 16 );
3113
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003114 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003115 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003116
3117 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3118
Paul Bakker5b4af392014-06-26 12:09:34 +02003119 md5_free( &md5 );
3120 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003121
Paul Bakker34617722014-06-13 17:20:13 +02003122 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003123
3124 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3125}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003126#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003128#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3129#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003130static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003131 ssl_context *ssl, unsigned char *buf, int from )
3132{
3133 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003134 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003135 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003136 unsigned char padbuf[32];
3137
Paul Bakker48916f92012-09-16 19:57:18 +00003138 ssl_session *session = ssl->session_negotiate;
3139 if( !session )
3140 session = ssl->session;
3141
Paul Bakker380da532012-04-18 16:10:25 +00003142 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003143
Paul Bakker9e36f042013-06-30 14:34:05 +02003144 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003145
3146 /*
3147 * TLSv1.2:
3148 * hash = PRF( master, finished_label,
3149 * Hash( handshake ) )[0.11]
3150 */
3151
Paul Bakker9e36f042013-06-30 14:34:05 +02003152#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003153 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003154 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003155#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003156
3157 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003158 ? "client finished"
3159 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003160
Paul Bakker9e36f042013-06-30 14:34:05 +02003161 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003162
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003163 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003164 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003165
3166 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3167
Paul Bakker5b4af392014-06-26 12:09:34 +02003168 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003169
Paul Bakker34617722014-06-13 17:20:13 +02003170 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003171
3172 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3173}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003174#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003175
Paul Bakker9e36f042013-06-30 14:34:05 +02003176#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003177static void ssl_calc_finished_tls_sha384(
3178 ssl_context *ssl, unsigned char *buf, int from )
3179{
3180 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003181 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003182 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003183 unsigned char padbuf[48];
3184
Paul Bakker48916f92012-09-16 19:57:18 +00003185 ssl_session *session = ssl->session_negotiate;
3186 if( !session )
3187 session = ssl->session;
3188
Paul Bakker380da532012-04-18 16:10:25 +00003189 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003190
Paul Bakker9e36f042013-06-30 14:34:05 +02003191 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003192
3193 /*
3194 * TLSv1.2:
3195 * hash = PRF( master, finished_label,
3196 * Hash( handshake ) )[0.11]
3197 */
3198
Paul Bakker9e36f042013-06-30 14:34:05 +02003199#if !defined(POLARSSL_SHA512_ALT)
3200 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3201 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003202#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003203
3204 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003205 ? "client finished"
3206 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003207
Paul Bakker9e36f042013-06-30 14:34:05 +02003208 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003209
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003210 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003211 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003212
3213 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3214
Paul Bakker5b4af392014-06-26 12:09:34 +02003215 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003216
Paul Bakker34617722014-06-13 17:20:13 +02003217 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003218
3219 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3220}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003221#endif /* POLARSSL_SHA512_C */
3222#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003223
Paul Bakker48916f92012-09-16 19:57:18 +00003224void ssl_handshake_wrapup( ssl_context *ssl )
3225{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003226 int resume = ssl->handshake->resume;
3227
Paul Bakker48916f92012-09-16 19:57:18 +00003228 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3229
3230 /*
3231 * Free our handshake params
3232 */
3233 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003234 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003235 ssl->handshake = NULL;
3236
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003237 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003238 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003239 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003240 ssl->renego_records_seen = 0;
3241 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003242
Paul Bakker48916f92012-09-16 19:57:18 +00003243 /*
3244 * Switch in our now active transform context
3245 */
3246 if( ssl->transform )
3247 {
3248 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003249 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003250 }
3251 ssl->transform = ssl->transform_negotiate;
3252 ssl->transform_negotiate = NULL;
3253
Paul Bakker0a597072012-09-25 21:55:46 +00003254 if( ssl->session )
3255 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003256#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3257 /* RFC 7366 3.1: keep the EtM state */
3258 ssl->session_negotiate->encrypt_then_mac =
3259 ssl->session->encrypt_then_mac;
3260#endif
3261
Paul Bakker0a597072012-09-25 21:55:46 +00003262 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003263 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003264 }
3265 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003266 ssl->session_negotiate = NULL;
3267
Paul Bakker0a597072012-09-25 21:55:46 +00003268 /*
3269 * Add cache entry
3270 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003271 if( ssl->f_set_cache != NULL &&
3272 ssl->session->length != 0 &&
3273 resume == 0 )
3274 {
Paul Bakker0a597072012-09-25 21:55:46 +00003275 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3276 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003277 }
Paul Bakker0a597072012-09-25 21:55:46 +00003278
Paul Bakker48916f92012-09-16 19:57:18 +00003279 ssl->state++;
3280
3281 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3282}
3283
Paul Bakker1ef83d62012-04-11 12:09:53 +00003284int ssl_write_finished( ssl_context *ssl )
3285{
3286 int ret, hash_len;
3287
3288 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3289
Paul Bakker92be97b2013-01-02 17:30:03 +01003290 /*
3291 * Set the out_msg pointer to the correct location based on IV length
3292 */
3293 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3294 {
3295 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3296 ssl->transform_negotiate->fixed_ivlen;
3297 }
3298 else
3299 ssl->out_msg = ssl->out_iv;
3300
Paul Bakker48916f92012-09-16 19:57:18 +00003301 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003302
3303 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3305
Paul Bakker48916f92012-09-16 19:57:18 +00003306 ssl->verify_data_len = hash_len;
3307 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3308
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 ssl->out_msglen = 4 + hash_len;
3310 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3311 ssl->out_msg[0] = SSL_HS_FINISHED;
3312
3313 /*
3314 * In case of session resuming, invert the client and server
3315 * ChangeCipherSpec messages order.
3316 */
Paul Bakker0a597072012-09-25 21:55:46 +00003317 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 {
3319 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003320 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003321 else
3322 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3323 }
3324 else
3325 ssl->state++;
3326
Paul Bakker48916f92012-09-16 19:57:18 +00003327 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003328 * Switch to our negotiated transform and session parameters for outbound
3329 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003330 */
3331 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3332 ssl->transform_out = ssl->transform_negotiate;
3333 ssl->session_out = ssl->session_negotiate;
3334 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003335
Paul Bakker07eb38b2012-12-19 14:42:06 +01003336#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003337 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003338 {
3339 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3340 {
3341 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3342 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3343 }
3344 }
3345#endif
3346
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3348 {
3349 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3350 return( ret );
3351 }
3352
3353 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3354
3355 return( 0 );
3356}
3357
3358int ssl_parse_finished( ssl_context *ssl )
3359{
Paul Bakker23986e52011-04-24 08:57:21 +00003360 int ret;
3361 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003362 unsigned char buf[36];
3363
3364 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3365
Paul Bakker48916f92012-09-16 19:57:18 +00003366 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003367
Paul Bakker48916f92012-09-16 19:57:18 +00003368 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003369 * Switch to our negotiated transform and session parameters for inbound
3370 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003371 */
3372 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3373 ssl->transform_in = ssl->transform_negotiate;
3374 ssl->session_in = ssl->session_negotiate;
3375 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003376
Paul Bakker92be97b2013-01-02 17:30:03 +01003377 /*
3378 * Set the in_msg pointer to the correct location based on IV length
3379 */
3380 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3381 {
3382 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3383 ssl->transform_negotiate->fixed_ivlen;
3384 }
3385 else
3386 ssl->in_msg = ssl->in_iv;
3387
Paul Bakker07eb38b2012-12-19 14:42:06 +01003388#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003389 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003390 {
3391 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3392 {
3393 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3394 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3395 }
3396 }
3397#endif
3398
Paul Bakker5121ce52009-01-03 21:22:43 +00003399 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3400 {
3401 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3402 return( ret );
3403 }
3404
3405 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3406 {
3407 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003408 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003409 }
3410
Paul Bakker1ef83d62012-04-11 12:09:53 +00003411 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3413
3414 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3415 ssl->in_hslen != 4 + hash_len )
3416 {
3417 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003418 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003419 }
3420
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003421 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003422 {
3423 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003424 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003425 }
3426
Paul Bakker48916f92012-09-16 19:57:18 +00003427 ssl->verify_data_len = hash_len;
3428 memcpy( ssl->peer_verify_data, buf, hash_len );
3429
Paul Bakker0a597072012-09-25 21:55:46 +00003430 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003431 {
3432 if( ssl->endpoint == SSL_IS_CLIENT )
3433 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3434
3435 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003436 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003437 }
3438 else
3439 ssl->state++;
3440
3441 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3442
3443 return( 0 );
3444}
3445
Paul Bakker968afaa2014-07-09 11:09:24 +02003446static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003447{
3448 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3449
3450#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3451 defined(POLARSSL_SSL_PROTO_TLS1_1)
3452 md5_init( &handshake->fin_md5 );
3453 sha1_init( &handshake->fin_sha1 );
3454 md5_starts( &handshake->fin_md5 );
3455 sha1_starts( &handshake->fin_sha1 );
3456#endif
3457#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3458#if defined(POLARSSL_SHA256_C)
3459 sha256_init( &handshake->fin_sha256 );
3460 sha256_starts( &handshake->fin_sha256, 0 );
3461#endif
3462#if defined(POLARSSL_SHA512_C)
3463 sha512_init( &handshake->fin_sha512 );
3464 sha512_starts( &handshake->fin_sha512, 1 );
3465#endif
3466#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3467
3468 handshake->update_checksum = ssl_update_checksum_start;
3469 handshake->sig_alg = SSL_HASH_SHA1;
3470
3471#if defined(POLARSSL_DHM_C)
3472 dhm_init( &handshake->dhm_ctx );
3473#endif
3474#if defined(POLARSSL_ECDH_C)
3475 ecdh_init( &handshake->ecdh_ctx );
3476#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003477}
3478
3479static void ssl_transform_init( ssl_transform *transform )
3480{
3481 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003482
3483 cipher_init( &transform->cipher_ctx_enc );
3484 cipher_init( &transform->cipher_ctx_dec );
3485
3486 md_init( &transform->md_ctx_enc );
3487 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003488}
3489
3490void ssl_session_init( ssl_session *session )
3491{
3492 memset( session, 0, sizeof(ssl_session) );
3493}
3494
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003495static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003496{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003497 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003498 if( ssl->transform_negotiate )
3499 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003500 if( ssl->session_negotiate )
3501 ssl_session_free( ssl->session_negotiate );
3502 if( ssl->handshake )
3503 ssl_handshake_free( ssl->handshake );
3504
3505 /*
3506 * Either the pointers are now NULL or cleared properly and can be freed.
3507 * Now allocate missing structures.
3508 */
3509 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003510 {
3511 ssl->transform_negotiate =
3512 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3513 }
Paul Bakker48916f92012-09-16 19:57:18 +00003514
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003515 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003516 {
3517 ssl->session_negotiate =
3518 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3519 }
Paul Bakker48916f92012-09-16 19:57:18 +00003520
Paul Bakker82788fb2014-10-20 13:59:19 +02003521 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003522 {
3523 ssl->handshake = (ssl_handshake_params *)
3524 polarssl_malloc( sizeof(ssl_handshake_params) );
3525 }
Paul Bakker48916f92012-09-16 19:57:18 +00003526
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003527 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003528 if( ssl->handshake == NULL ||
3529 ssl->transform_negotiate == NULL ||
3530 ssl->session_negotiate == NULL )
3531 {
3532 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003533
3534 polarssl_free( ssl->handshake );
3535 polarssl_free( ssl->transform_negotiate );
3536 polarssl_free( ssl->session_negotiate );
3537
3538 ssl->handshake = NULL;
3539 ssl->transform_negotiate = NULL;
3540 ssl->session_negotiate = NULL;
3541
Paul Bakker48916f92012-09-16 19:57:18 +00003542 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3543 }
3544
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003545 /* Initialize structures */
3546 ssl_session_init( ssl->session_negotiate );
3547 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003548 ssl_handshake_params_init( ssl->handshake );
3549
3550#if defined(POLARSSL_X509_CRT_PARSE_C)
3551 ssl->handshake->key_cert = ssl->key_cert;
3552#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003553
Paul Bakker48916f92012-09-16 19:57:18 +00003554 return( 0 );
3555}
3556
Paul Bakker5121ce52009-01-03 21:22:43 +00003557/*
3558 * Initialize an SSL context
3559 */
3560int ssl_init( ssl_context *ssl )
3561{
Paul Bakker48916f92012-09-16 19:57:18 +00003562 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003563 int len = SSL_BUFFER_LEN;
3564
3565 memset( ssl, 0, sizeof( ssl_context ) );
3566
Paul Bakker62f2dee2012-09-28 07:31:51 +00003567 /*
3568 * Sane defaults
3569 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003570 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3571 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3572 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3573 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003574
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003575 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003576
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003577 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3578
Paul Bakker62f2dee2012-09-28 07:31:51 +00003579#if defined(POLARSSL_DHM_C)
3580 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3581 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3582 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3583 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3584 {
3585 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3586 return( ret );
3587 }
3588#endif
3589
3590 /*
3591 * Prepare base structures
3592 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003593 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003595 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 ssl->in_msg = ssl->in_ctr + 13;
3597
3598 if( ssl->in_ctr == NULL )
3599 {
3600 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003601 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 }
3603
Paul Bakker6e339b52013-07-03 13:37:05 +02003604 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003606 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003607 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
3609 if( ssl->out_ctr == NULL )
3610 {
3611 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003612 polarssl_free( ssl->in_ctr );
3613 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003614 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003615 }
3616
3617 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3618 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3619
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003620#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3621 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3622#endif
3623
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003624#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3625 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3626#endif
3627
Paul Bakker606b4ba2013-08-14 16:52:14 +02003628#if defined(POLARSSL_SSL_SESSION_TICKETS)
3629 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3630#endif
3631
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003632#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003633 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003634#endif
3635
Paul Bakker48916f92012-09-16 19:57:18 +00003636 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3637 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003638
3639 return( 0 );
3640}
3641
3642/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003643 * Reset an initialized and used SSL context for re-use while retaining
3644 * all application-set variables, function pointers and data.
3645 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003646int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003647{
Paul Bakker48916f92012-09-16 19:57:18 +00003648 int ret;
3649
Paul Bakker7eb013f2011-10-06 12:37:39 +00003650 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003651 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3652 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3653
3654 ssl->verify_data_len = 0;
3655 memset( ssl->own_verify_data, 0, 36 );
3656 memset( ssl->peer_verify_data, 0, 36 );
3657
Paul Bakker7eb013f2011-10-06 12:37:39 +00003658 ssl->in_offt = NULL;
3659
Paul Bakker92be97b2013-01-02 17:30:03 +01003660 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003661 ssl->in_msgtype = 0;
3662 ssl->in_msglen = 0;
3663 ssl->in_left = 0;
3664
3665 ssl->in_hslen = 0;
3666 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003667 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003668
Paul Bakker92be97b2013-01-02 17:30:03 +01003669 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003670 ssl->out_msgtype = 0;
3671 ssl->out_msglen = 0;
3672 ssl->out_left = 0;
3673
Paul Bakker48916f92012-09-16 19:57:18 +00003674 ssl->transform_in = NULL;
3675 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003676
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003677 ssl->renego_records_seen = 0;
3678
Paul Bakker7eb013f2011-10-06 12:37:39 +00003679 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3680 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003681
3682#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003683 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003684 {
3685 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003686 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003687 {
3688 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3689 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3690 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003691 }
3692#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003693
Paul Bakker48916f92012-09-16 19:57:18 +00003694 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003695 {
Paul Bakker48916f92012-09-16 19:57:18 +00003696 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003697 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003698 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003699 }
Paul Bakker48916f92012-09-16 19:57:18 +00003700
Paul Bakkerc0463502013-02-14 11:19:38 +01003701 if( ssl->session )
3702 {
3703 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003704 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003705 ssl->session = NULL;
3706 }
3707
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003708#if defined(POLARSSL_SSL_ALPN)
3709 ssl->alpn_chosen = NULL;
3710#endif
3711
Paul Bakker48916f92012-09-16 19:57:18 +00003712 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3713 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003714
3715 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003716}
3717
Paul Bakkera503a632013-08-14 13:48:06 +02003718#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003719static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3720{
3721 aes_free( &tkeys->enc );
3722 aes_free( &tkeys->dec );
3723
3724 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3725}
3726
Paul Bakker7eb013f2011-10-06 12:37:39 +00003727/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003728 * Allocate and initialize ticket keys
3729 */
3730static int ssl_ticket_keys_init( ssl_context *ssl )
3731{
3732 int ret;
3733 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003734 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003735
3736 if( ssl->ticket_keys != NULL )
3737 return( 0 );
3738
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003739 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3740 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003741 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3742
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003743 aes_init( &tkeys->enc );
3744 aes_init( &tkeys->dec );
3745
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003746 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003747 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003748 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003749 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003750 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003751 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003752
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003753 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3754 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3755 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3756 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003757 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003758 polarssl_free( tkeys );
3759 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003760 }
3761
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003762 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003763 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003764 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003765 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003766 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003767 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003768
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003769 ssl->ticket_keys = tkeys;
3770
3771 return( 0 );
3772}
Paul Bakkera503a632013-08-14 13:48:06 +02003773#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003774
3775/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 * SSL set accessors
3777 */
3778void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3779{
3780 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003781
Paul Bakker606b4ba2013-08-14 16:52:14 +02003782#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003783 if( endpoint == SSL_IS_CLIENT )
3784 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003785#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003786}
3787
3788void ssl_set_authmode( ssl_context *ssl, int authmode )
3789{
3790 ssl->authmode = authmode;
3791}
3792
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003793#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003794void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003795 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003796 void *p_vrfy )
3797{
3798 ssl->f_vrfy = f_vrfy;
3799 ssl->p_vrfy = p_vrfy;
3800}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003801#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003802
Paul Bakker5121ce52009-01-03 21:22:43 +00003803void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003804 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003805 void *p_rng )
3806{
3807 ssl->f_rng = f_rng;
3808 ssl->p_rng = p_rng;
3809}
3810
3811void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003812 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003813 void *p_dbg )
3814{
3815 ssl->f_dbg = f_dbg;
3816 ssl->p_dbg = p_dbg;
3817}
3818
3819void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003820 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003821 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003822{
3823 ssl->f_recv = f_recv;
3824 ssl->f_send = f_send;
3825 ssl->p_recv = p_recv;
3826 ssl->p_send = p_send;
3827}
3828
Paul Bakker0a597072012-09-25 21:55:46 +00003829void ssl_set_session_cache( ssl_context *ssl,
3830 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3831 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003832{
Paul Bakker0a597072012-09-25 21:55:46 +00003833 ssl->f_get_cache = f_get_cache;
3834 ssl->p_get_cache = p_get_cache;
3835 ssl->f_set_cache = f_set_cache;
3836 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003837}
3838
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003839int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003840{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003841 int ret;
3842
3843 if( ssl == NULL ||
3844 session == NULL ||
3845 ssl->session_negotiate == NULL ||
3846 ssl->endpoint != SSL_IS_CLIENT )
3847 {
3848 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3849 }
3850
3851 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3852 return( ret );
3853
Paul Bakker0a597072012-09-25 21:55:46 +00003854 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003855
3856 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003857}
3858
Paul Bakkerb68cad62012-08-23 08:34:18 +00003859void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003860{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003861 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3862 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3863 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3864 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3865}
3866
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003867void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3868 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003869 int major, int minor )
3870{
3871 if( major != SSL_MAJOR_VERSION_3 )
3872 return;
3873
3874 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3875 return;
3876
3877 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003878}
3879
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003880#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003881/* Add a new (empty) key_cert entry an return a pointer to it */
3882static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3883{
3884 ssl_key_cert *key_cert, *last;
3885
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003886 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3887 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003888 return( NULL );
3889
3890 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3891
3892 /* Append the new key_cert to the (possibly empty) current list */
3893 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003894 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003895 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003896 if( ssl->handshake != NULL )
3897 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003898 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003899 else
3900 {
3901 last = ssl->key_cert;
3902 while( last->next != NULL )
3903 last = last->next;
3904 last->next = key_cert;
3905 }
3906
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003907 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003908}
3909
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003910void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003911 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003912{
3913 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003914 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003915 ssl->peer_cn = peer_cn;
3916}
3917
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003918int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003919 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003920{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003921 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3922
3923 if( key_cert == NULL )
3924 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3925
3926 key_cert->cert = own_cert;
3927 key_cert->key = pk_key;
3928
3929 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003930}
3931
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003932#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003933int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003934 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003935{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003936 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003937 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003938
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003939 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003940 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3941
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003942 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3943 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003944 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003945
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003946 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003947
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003948 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003949 if( ret != 0 )
3950 return( ret );
3951
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003952 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003953 return( ret );
3954
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003955 key_cert->cert = own_cert;
3956 key_cert->key_own_alloc = 1;
3957
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003958 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003959}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003960#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003961
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003962int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003963 void *rsa_key,
3964 rsa_decrypt_func rsa_decrypt,
3965 rsa_sign_func rsa_sign,
3966 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003967{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003968 int ret;
3969 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003970
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003971 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003972 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3973
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003974 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3975 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003976 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003977
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003978 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003979
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003980 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3981 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3982 return( ret );
3983
3984 key_cert->cert = own_cert;
3985 key_cert->key_own_alloc = 1;
3986
3987 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003988}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003989#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003990
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003991#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003992int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3993 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003994{
Paul Bakker6db455e2013-09-18 17:29:31 +02003995 if( psk == NULL || psk_identity == NULL )
3996 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3997
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003998 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003999 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4000
Paul Bakker6db455e2013-09-18 17:29:31 +02004001 if( ssl->psk != NULL )
4002 {
4003 polarssl_free( ssl->psk );
4004 polarssl_free( ssl->psk_identity );
4005 }
4006
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004007 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004008 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004009
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004010 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004011 ssl->psk_identity = (unsigned char *)
4012 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004013
4014 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4015 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4016
4017 memcpy( ssl->psk, psk, ssl->psk_len );
4018 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004019
4020 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004021}
4022
4023void ssl_set_psk_cb( ssl_context *ssl,
4024 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4025 size_t),
4026 void *p_psk )
4027{
4028 ssl->f_psk = f_psk;
4029 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004030}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004031#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004032
Paul Bakker48916f92012-09-16 19:57:18 +00004033#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004034int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004035{
4036 int ret;
4037
Paul Bakker48916f92012-09-16 19:57:18 +00004038 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004039 {
4040 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4041 return( ret );
4042 }
4043
Paul Bakker48916f92012-09-16 19:57:18 +00004044 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004045 {
4046 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4047 return( ret );
4048 }
4049
4050 return( 0 );
4051}
4052
Paul Bakker1b57b062011-01-06 15:48:19 +00004053int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4054{
4055 int ret;
4056
Paul Bakker66d5d072014-06-17 16:39:18 +02004057 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004058 {
4059 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4060 return( ret );
4061 }
4062
Paul Bakker66d5d072014-06-17 16:39:18 +02004063 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004064 {
4065 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4066 return( ret );
4067 }
4068
4069 return( 0 );
4070}
Paul Bakker48916f92012-09-16 19:57:18 +00004071#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004072
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004073#if defined(POLARSSL_SSL_SET_CURVES)
4074/*
4075 * Set the allowed elliptic curves
4076 */
4077void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4078{
4079 ssl->curve_list = curve_list;
4080}
4081#endif
4082
Paul Bakker0be444a2013-08-27 21:55:01 +02004083#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004084int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004085{
4086 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004087 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004088
4089 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004090
4091 if( ssl->hostname_len + 1 == 0 )
4092 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4093
Paul Bakker6e339b52013-07-03 13:37:05 +02004094 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004095
Paul Bakkerb15b8512012-01-13 13:44:06 +00004096 if( ssl->hostname == NULL )
4097 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4098
Paul Bakker3c2122f2013-06-24 19:03:14 +02004099 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004100 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004101
Paul Bakker40ea7de2009-05-03 10:18:48 +00004102 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004103
4104 return( 0 );
4105}
4106
Paul Bakker5701cdc2012-09-27 21:49:42 +00004107void ssl_set_sni( ssl_context *ssl,
4108 int (*f_sni)(void *, ssl_context *,
4109 const unsigned char *, size_t),
4110 void *p_sni )
4111{
4112 ssl->f_sni = f_sni;
4113 ssl->p_sni = p_sni;
4114}
Paul Bakker0be444a2013-08-27 21:55:01 +02004115#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004116
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004117#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004118int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004119{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004120 size_t cur_len, tot_len;
4121 const char **p;
4122
4123 /*
4124 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4125 * truncated". Check lengths now rather than later.
4126 */
4127 tot_len = 0;
4128 for( p = protos; *p != NULL; p++ )
4129 {
4130 cur_len = strlen( *p );
4131 tot_len += cur_len;
4132
4133 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4134 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4135 }
4136
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004137 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004138
4139 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004140}
4141
4142const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4143{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004144 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004145}
4146#endif /* POLARSSL_SSL_ALPN */
4147
Paul Bakker490ecc82011-10-06 13:04:09 +00004148void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4149{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004150 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4151 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4152 {
4153 ssl->max_major_ver = major;
4154 ssl->max_minor_ver = minor;
4155 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004156}
4157
Paul Bakker1d29fb52012-09-28 13:28:45 +00004158void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4159{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004160 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4161 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4162 {
4163 ssl->min_major_ver = major;
4164 ssl->min_minor_ver = minor;
4165 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004166}
4167
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004168#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4169void ssl_set_fallback( ssl_context *ssl, char fallback )
4170{
4171 ssl->fallback = fallback;
4172}
4173#endif
4174
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004175#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4176void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4177{
4178 ssl->encrypt_then_mac = etm;
4179}
4180#endif
4181
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004182#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4183void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4184{
4185 ssl->extended_ms = ems;
4186}
4187#endif
4188
Paul Bakker05decb22013-08-15 13:33:48 +02004189#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004190int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4191{
Paul Bakker77e257e2013-12-16 15:29:52 +01004192 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004193 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004194 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004195 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004196 }
4197
4198 ssl->mfl_code = mfl_code;
4199
4200 return( 0 );
4201}
Paul Bakker05decb22013-08-15 13:33:48 +02004202#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004203
Paul Bakker1f2bc622013-08-15 13:45:55 +02004204#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004205int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004206{
4207 if( ssl->endpoint != SSL_IS_CLIENT )
4208 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4209
Paul Bakker8c1ede62013-07-19 14:14:37 +02004210 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004211
4212 return( 0 );
4213}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004214#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004215
Paul Bakker48916f92012-09-16 19:57:18 +00004216void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4217{
4218 ssl->disable_renegotiation = renegotiation;
4219}
4220
4221void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4222{
4223 ssl->allow_legacy_renegotiation = allow_legacy;
4224}
4225
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004226void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4227{
4228 ssl->renego_max_records = max_records;
4229}
4230
Paul Bakkera503a632013-08-14 13:48:06 +02004231#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004232int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4233{
4234 ssl->session_tickets = use_tickets;
4235
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004236 if( ssl->endpoint == SSL_IS_CLIENT )
4237 return( 0 );
4238
4239 if( ssl->f_rng == NULL )
4240 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4241
4242 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004243}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004244
4245void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4246{
4247 ssl->ticket_lifetime = lifetime;
4248}
Paul Bakkera503a632013-08-14 13:48:06 +02004249#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004250
Paul Bakker5121ce52009-01-03 21:22:43 +00004251/*
4252 * SSL get accessors
4253 */
Paul Bakker23986e52011-04-24 08:57:21 +00004254size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004255{
4256 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4257}
4258
Paul Bakkerff60ee62010-03-16 21:09:09 +00004259int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004260{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004261 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004262}
4263
Paul Bakkere3166ce2011-01-27 17:40:50 +00004264const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004265{
Paul Bakker926c8e42013-03-06 10:23:34 +01004266 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004267 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004268
Paul Bakkere3166ce2011-01-27 17:40:50 +00004269 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004270}
4271
Paul Bakker43ca69c2011-01-15 17:35:19 +00004272const char *ssl_get_version( const ssl_context *ssl )
4273{
4274 switch( ssl->minor_ver )
4275 {
4276 case SSL_MINOR_VERSION_0:
4277 return( "SSLv3.0" );
4278
4279 case SSL_MINOR_VERSION_1:
4280 return( "TLSv1.0" );
4281
4282 case SSL_MINOR_VERSION_2:
4283 return( "TLSv1.1" );
4284
Paul Bakker1ef83d62012-04-11 12:09:53 +00004285 case SSL_MINOR_VERSION_3:
4286 return( "TLSv1.2" );
4287
Paul Bakker43ca69c2011-01-15 17:35:19 +00004288 default:
4289 break;
4290 }
4291 return( "unknown" );
4292}
4293
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004294#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004295const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004296{
4297 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004298 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004299
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004300 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004301}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004302#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004303
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004304int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4305{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004306 if( ssl == NULL ||
4307 dst == NULL ||
4308 ssl->session == NULL ||
4309 ssl->endpoint != SSL_IS_CLIENT )
4310 {
4311 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4312 }
4313
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004314 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004315}
4316
Paul Bakker5121ce52009-01-03 21:22:43 +00004317/*
Paul Bakker1961b702013-01-25 14:49:24 +01004318 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004319 */
Paul Bakker1961b702013-01-25 14:49:24 +01004320int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004321{
Paul Bakker40e46942009-01-03 21:51:57 +00004322 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004323
Paul Bakker40e46942009-01-03 21:51:57 +00004324#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004325 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004326 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004327#endif
4328
Paul Bakker40e46942009-01-03 21:51:57 +00004329#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004330 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004331 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004332#endif
4333
Paul Bakker1961b702013-01-25 14:49:24 +01004334 return( ret );
4335}
4336
4337/*
4338 * Perform the SSL handshake
4339 */
4340int ssl_handshake( ssl_context *ssl )
4341{
4342 int ret = 0;
4343
4344 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4345
4346 while( ssl->state != SSL_HANDSHAKE_OVER )
4347 {
4348 ret = ssl_handshake_step( ssl );
4349
4350 if( ret != 0 )
4351 break;
4352 }
4353
Paul Bakker5121ce52009-01-03 21:22:43 +00004354 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4355
4356 return( ret );
4357}
4358
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004359#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004360/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004361 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004362 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004363static int ssl_write_hello_request( ssl_context *ssl )
4364{
4365 int ret;
4366
4367 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4368
4369 ssl->out_msglen = 4;
4370 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4371 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4372
4373 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4374 {
4375 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4376 return( ret );
4377 }
4378
4379 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4380
4381 return( 0 );
4382}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004383#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004384
4385/*
4386 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004387 * - any side: calling ssl_renegotiate(),
4388 * - client: receiving a HelloRequest during ssl_read(),
4389 * - server: receiving any handshake message on server during ssl_read() after
4390 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004391 * If the handshake doesn't complete due to waiting for I/O, it will continue
4392 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004393 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004394static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004395{
4396 int ret;
4397
4398 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4399
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004400 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4401 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004402
4403 ssl->state = SSL_HELLO_REQUEST;
4404 ssl->renegotiation = SSL_RENEGOTIATION;
4405
Paul Bakker48916f92012-09-16 19:57:18 +00004406 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4407 {
4408 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4409 return( ret );
4410 }
4411
4412 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4413
4414 return( 0 );
4415}
4416
4417/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004418 * Renegotiate current connection on client,
4419 * or request renegotiation on server
4420 */
4421int ssl_renegotiate( ssl_context *ssl )
4422{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004423 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004424
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004425#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004426 /* On server, just send the request */
4427 if( ssl->endpoint == SSL_IS_SERVER )
4428 {
4429 if( ssl->state != SSL_HANDSHAKE_OVER )
4430 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4431
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004432 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4433
4434 /* Did we already try/start sending HelloRequest? */
4435 if( ssl->out_left != 0 )
4436 return( ssl_flush_output( ssl ) );
4437
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004438 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004439 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004440#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004441
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004442#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004443 /*
4444 * On client, either start the renegotiation process or,
4445 * if already in progress, continue the handshake
4446 */
4447 if( ssl->renegotiation != SSL_RENEGOTIATION )
4448 {
4449 if( ssl->state != SSL_HANDSHAKE_OVER )
4450 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4451
4452 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4453 {
4454 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4455 return( ret );
4456 }
4457 }
4458 else
4459 {
4460 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4461 {
4462 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4463 return( ret );
4464 }
4465 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004466#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004467
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004468 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004469}
4470
4471/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004472 * Receive application data decrypted from the SSL layer
4473 */
Paul Bakker23986e52011-04-24 08:57:21 +00004474int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004475{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004476 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004477 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004478
4479 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4480
4481 if( ssl->state != SSL_HANDSHAKE_OVER )
4482 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004483 ret = ssl_handshake( ssl );
4484 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4485 {
4486 record_read = 1;
4487 }
4488 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004489 {
4490 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4491 return( ret );
4492 }
4493 }
4494
4495 if( ssl->in_offt == NULL )
4496 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004497 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004499 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4500 {
4501 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4502 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004503
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004504 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4505 return( ret );
4506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004507 }
4508
4509 if( ssl->in_msglen == 0 &&
4510 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4511 {
4512 /*
4513 * OpenSSL sends empty messages to randomize the IV
4514 */
4515 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4516 {
Paul Bakker831a7552011-05-18 13:32:51 +00004517 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4518 return( 0 );
4519
Paul Bakker5121ce52009-01-03 21:22:43 +00004520 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4521 return( ret );
4522 }
4523 }
4524
Paul Bakker48916f92012-09-16 19:57:18 +00004525 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4526 {
4527 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4528
4529 if( ssl->endpoint == SSL_IS_CLIENT &&
4530 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4531 ssl->in_hslen != 4 ) )
4532 {
4533 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4534 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4535 }
4536
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004537 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4538 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004539 ssl->allow_legacy_renegotiation ==
4540 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004541 {
4542 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4543
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004544#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004545 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004546 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004547 /*
4548 * SSLv3 does not have a "no_renegotiation" alert
4549 */
4550 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4551 return( ret );
4552 }
4553 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004554#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004555#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4556 defined(POLARSSL_SSL_PROTO_TLS1_2)
4557 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004558 {
4559 if( ( ret = ssl_send_alert_message( ssl,
4560 SSL_ALERT_LEVEL_WARNING,
4561 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4562 {
4563 return( ret );
4564 }
Paul Bakker48916f92012-09-16 19:57:18 +00004565 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004566 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004567#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4568 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004569 {
4570 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004571 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004572 }
Paul Bakker48916f92012-09-16 19:57:18 +00004573 }
4574 else
4575 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004576 ret = ssl_start_renegotiation( ssl );
4577 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4578 {
4579 record_read = 1;
4580 }
4581 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004582 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004583 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004584 return( ret );
4585 }
Paul Bakker48916f92012-09-16 19:57:18 +00004586 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004587
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004588 /* If a non-handshake record was read during renego, fallthrough,
4589 * else tell the user they should call ssl_read() again */
4590 if( ! record_read )
4591 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004592 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004593 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4594 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004595 ssl->renego_records_seen++;
4596
4597 if( ssl->renego_max_records >= 0 &&
4598 ssl->renego_records_seen > ssl->renego_max_records )
4599 {
4600 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4601 "but not honored by client" ) );
4602 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4603 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004604 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004605
4606 /* Fatal and closure alerts handled by ssl_read_record() */
4607 if( ssl->in_msgtype == SSL_MSG_ALERT )
4608 {
4609 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4610 return( POLARSSL_ERR_NET_WANT_READ );
4611 }
4612
4613 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004614 {
4615 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004616 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004617 }
4618
4619 ssl->in_offt = ssl->in_msg;
4620 }
4621
4622 n = ( len < ssl->in_msglen )
4623 ? len : ssl->in_msglen;
4624
4625 memcpy( buf, ssl->in_offt, n );
4626 ssl->in_msglen -= n;
4627
4628 if( ssl->in_msglen == 0 )
4629 /* all bytes consumed */
4630 ssl->in_offt = NULL;
4631 else
4632 /* more data available */
4633 ssl->in_offt += n;
4634
4635 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4636
Paul Bakker23986e52011-04-24 08:57:21 +00004637 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004638}
4639
4640/*
4641 * Send application data to be encrypted by the SSL layer
4642 */
Paul Bakker23986e52011-04-24 08:57:21 +00004643int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004644{
Paul Bakker23986e52011-04-24 08:57:21 +00004645 int ret;
4646 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004647 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004648
4649 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4650
4651 if( ssl->state != SSL_HANDSHAKE_OVER )
4652 {
4653 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4654 {
4655 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4656 return( ret );
4657 }
4658 }
4659
Paul Bakker05decb22013-08-15 13:33:48 +02004660#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004661 /*
4662 * Assume mfl_code is correct since it was checked when set
4663 */
4664 max_len = mfl_code_to_length[ssl->mfl_code];
4665
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004666 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004667 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004668 */
4669 if( ssl->session_out != NULL &&
4670 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4671 {
4672 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4673 }
Paul Bakker05decb22013-08-15 13:33:48 +02004674#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004675
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004676 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004677
Paul Bakker5121ce52009-01-03 21:22:43 +00004678 if( ssl->out_left != 0 )
4679 {
4680 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4681 {
4682 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4683 return( ret );
4684 }
4685 }
Paul Bakker887bd502011-06-08 13:10:54 +00004686 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004687 {
Paul Bakker887bd502011-06-08 13:10:54 +00004688 ssl->out_msglen = n;
4689 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4690 memcpy( ssl->out_msg, buf, n );
4691
4692 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4693 {
4694 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4695 return( ret );
4696 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004697 }
4698
4699 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4700
Paul Bakker23986e52011-04-24 08:57:21 +00004701 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004702}
4703
4704/*
4705 * Notify the peer that the connection is being closed
4706 */
4707int ssl_close_notify( ssl_context *ssl )
4708{
4709 int ret;
4710
4711 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4712
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004713 if( ssl->out_left != 0 )
4714 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004715
4716 if( ssl->state == SSL_HANDSHAKE_OVER )
4717 {
Paul Bakker48916f92012-09-16 19:57:18 +00004718 if( ( ret = ssl_send_alert_message( ssl,
4719 SSL_ALERT_LEVEL_WARNING,
4720 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004721 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004722 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004723 return( ret );
4724 }
4725 }
4726
4727 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4728
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004729 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004730}
4731
Paul Bakker48916f92012-09-16 19:57:18 +00004732void ssl_transform_free( ssl_transform *transform )
4733{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004734 if( transform == NULL )
4735 return;
4736
Paul Bakker48916f92012-09-16 19:57:18 +00004737#if defined(POLARSSL_ZLIB_SUPPORT)
4738 deflateEnd( &transform->ctx_deflate );
4739 inflateEnd( &transform->ctx_inflate );
4740#endif
4741
Paul Bakker84bbeb52014-07-01 14:53:22 +02004742 cipher_free( &transform->cipher_ctx_enc );
4743 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004744
Paul Bakker84bbeb52014-07-01 14:53:22 +02004745 md_free( &transform->md_ctx_enc );
4746 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004747
Paul Bakker34617722014-06-13 17:20:13 +02004748 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004749}
4750
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004751#if defined(POLARSSL_X509_CRT_PARSE_C)
4752static void ssl_key_cert_free( ssl_key_cert *key_cert )
4753{
4754 ssl_key_cert *cur = key_cert, *next;
4755
4756 while( cur != NULL )
4757 {
4758 next = cur->next;
4759
4760 if( cur->key_own_alloc )
4761 {
4762 pk_free( cur->key );
4763 polarssl_free( cur->key );
4764 }
4765 polarssl_free( cur );
4766
4767 cur = next;
4768 }
4769}
4770#endif /* POLARSSL_X509_CRT_PARSE_C */
4771
Paul Bakker48916f92012-09-16 19:57:18 +00004772void ssl_handshake_free( ssl_handshake_params *handshake )
4773{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004774 if( handshake == NULL )
4775 return;
4776
Paul Bakker48916f92012-09-16 19:57:18 +00004777#if defined(POLARSSL_DHM_C)
4778 dhm_free( &handshake->dhm_ctx );
4779#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004780#if defined(POLARSSL_ECDH_C)
4781 ecdh_free( &handshake->ecdh_ctx );
4782#endif
4783
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004784#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004785 /* explicit void pointer cast for buggy MS compiler */
4786 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004787#endif
4788
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004789#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4790 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4791 /*
4792 * Free only the linked list wrapper, not the keys themselves
4793 * since the belong to the SNI callback
4794 */
4795 if( handshake->sni_key_cert != NULL )
4796 {
4797 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4798
4799 while( cur != NULL )
4800 {
4801 next = cur->next;
4802 polarssl_free( cur );
4803 cur = next;
4804 }
4805 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004806#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004807
Paul Bakker34617722014-06-13 17:20:13 +02004808 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004809}
4810
4811void ssl_session_free( ssl_session *session )
4812{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004813 if( session == NULL )
4814 return;
4815
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004816#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004817 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004818 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004819 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004820 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004821 }
Paul Bakkered27a042013-04-18 22:46:23 +02004822#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004823
Paul Bakkera503a632013-08-14 13:48:06 +02004824#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004825 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004826#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004827
Paul Bakker34617722014-06-13 17:20:13 +02004828 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004829}
4830
Paul Bakker5121ce52009-01-03 21:22:43 +00004831/*
4832 * Free an SSL context
4833 */
4834void ssl_free( ssl_context *ssl )
4835{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004836 if( ssl == NULL )
4837 return;
4838
Paul Bakker5121ce52009-01-03 21:22:43 +00004839 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4840
Paul Bakker5121ce52009-01-03 21:22:43 +00004841 if( ssl->out_ctr != NULL )
4842 {
Paul Bakker34617722014-06-13 17:20:13 +02004843 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004844 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004845 }
4846
4847 if( ssl->in_ctr != NULL )
4848 {
Paul Bakker34617722014-06-13 17:20:13 +02004849 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004850 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004851 }
4852
Paul Bakker16770332013-10-11 09:59:44 +02004853#if defined(POLARSSL_ZLIB_SUPPORT)
4854 if( ssl->compress_buf != NULL )
4855 {
Paul Bakker34617722014-06-13 17:20:13 +02004856 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004857 polarssl_free( ssl->compress_buf );
4858 }
4859#endif
4860
Paul Bakker40e46942009-01-03 21:51:57 +00004861#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004862 mpi_free( &ssl->dhm_P );
4863 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004864#endif
4865
Paul Bakker48916f92012-09-16 19:57:18 +00004866 if( ssl->transform )
4867 {
4868 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004869 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004870 }
4871
4872 if( ssl->handshake )
4873 {
4874 ssl_handshake_free( ssl->handshake );
4875 ssl_transform_free( ssl->transform_negotiate );
4876 ssl_session_free( ssl->session_negotiate );
4877
Paul Bakker6e339b52013-07-03 13:37:05 +02004878 polarssl_free( ssl->handshake );
4879 polarssl_free( ssl->transform_negotiate );
4880 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004881 }
4882
Paul Bakkerc0463502013-02-14 11:19:38 +01004883 if( ssl->session )
4884 {
4885 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004886 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004887 }
4888
Paul Bakkera503a632013-08-14 13:48:06 +02004889#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004890 if( ssl->ticket_keys )
4891 {
4892 ssl_ticket_keys_free( ssl->ticket_keys );
4893 polarssl_free( ssl->ticket_keys );
4894 }
Paul Bakkera503a632013-08-14 13:48:06 +02004895#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004896
Paul Bakker0be444a2013-08-27 21:55:01 +02004897#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004898 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004899 {
Paul Bakker34617722014-06-13 17:20:13 +02004900 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004901 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004902 ssl->hostname_len = 0;
4903 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004904#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004905
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004906#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004907 if( ssl->psk != NULL )
4908 {
Paul Bakker34617722014-06-13 17:20:13 +02004909 polarssl_zeroize( ssl->psk, ssl->psk_len );
4910 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004911 polarssl_free( ssl->psk );
4912 polarssl_free( ssl->psk_identity );
4913 ssl->psk_len = 0;
4914 ssl->psk_identity_len = 0;
4915 }
4916#endif
4917
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004918#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004919 ssl_key_cert_free( ssl->key_cert );
4920#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004921
Paul Bakker05ef8352012-05-08 09:17:57 +00004922#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4923 if( ssl_hw_record_finish != NULL )
4924 {
4925 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4926 ssl_hw_record_finish( ssl );
4927 }
4928#endif
4929
Paul Bakker5121ce52009-01-03 21:22:43 +00004930 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004931
Paul Bakker86f04f42013-02-14 11:20:09 +01004932 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004933 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004934}
4935
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004936#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004937/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004938 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004939 */
4940unsigned char ssl_sig_from_pk( pk_context *pk )
4941{
4942#if defined(POLARSSL_RSA_C)
4943 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4944 return( SSL_SIG_RSA );
4945#endif
4946#if defined(POLARSSL_ECDSA_C)
4947 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4948 return( SSL_SIG_ECDSA );
4949#endif
4950 return( SSL_SIG_ANON );
4951}
4952
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004953pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4954{
4955 switch( sig )
4956 {
4957#if defined(POLARSSL_RSA_C)
4958 case SSL_SIG_RSA:
4959 return( POLARSSL_PK_RSA );
4960#endif
4961#if defined(POLARSSL_ECDSA_C)
4962 case SSL_SIG_ECDSA:
4963 return( POLARSSL_PK_ECDSA );
4964#endif
4965 default:
4966 return( POLARSSL_PK_NONE );
4967 }
4968}
Paul Bakker9af723c2014-05-01 13:03:14 +02004969#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004970
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004971/*
4972 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4973 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004974md_type_t ssl_md_alg_from_hash( unsigned char hash )
4975{
4976 switch( hash )
4977 {
4978#if defined(POLARSSL_MD5_C)
4979 case SSL_HASH_MD5:
4980 return( POLARSSL_MD_MD5 );
4981#endif
4982#if defined(POLARSSL_SHA1_C)
4983 case SSL_HASH_SHA1:
4984 return( POLARSSL_MD_SHA1 );
4985#endif
4986#if defined(POLARSSL_SHA256_C)
4987 case SSL_HASH_SHA224:
4988 return( POLARSSL_MD_SHA224 );
4989 case SSL_HASH_SHA256:
4990 return( POLARSSL_MD_SHA256 );
4991#endif
4992#if defined(POLARSSL_SHA512_C)
4993 case SSL_HASH_SHA384:
4994 return( POLARSSL_MD_SHA384 );
4995 case SSL_HASH_SHA512:
4996 return( POLARSSL_MD_SHA512 );
4997#endif
4998 default:
4999 return( POLARSSL_MD_NONE );
5000 }
5001}
5002
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005003#if defined(POLARSSL_SSL_SET_CURVES)
5004/*
5005 * Check is a curve proposed by the peer is in our list.
5006 * Return 1 if we're willing to use it, 0 otherwise.
5007 */
5008int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5009{
5010 const ecp_group_id *gid;
5011
5012 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5013 if( *gid == grp_id )
5014 return( 1 );
5015
5016 return( 0 );
5017}
Paul Bakker9af723c2014-05-01 13:03:14 +02005018#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005019
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005020#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005021int ssl_check_cert_usage( const x509_crt *cert,
5022 const ssl_ciphersuite_t *ciphersuite,
5023 int cert_endpoint )
5024{
5025#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5026 int usage = 0;
5027#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005028#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5029 const char *ext_oid;
5030 size_t ext_len;
5031#endif
5032
5033#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5034 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5035 ((void) cert);
5036 ((void) cert_endpoint);
5037#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005038
5039#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5040 if( cert_endpoint == SSL_IS_SERVER )
5041 {
5042 /* Server part of the key exchange */
5043 switch( ciphersuite->key_exchange )
5044 {
5045 case POLARSSL_KEY_EXCHANGE_RSA:
5046 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5047 usage = KU_KEY_ENCIPHERMENT;
5048 break;
5049
5050 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5051 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5052 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5053 usage = KU_DIGITAL_SIGNATURE;
5054 break;
5055
5056 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5057 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5058 usage = KU_KEY_AGREEMENT;
5059 break;
5060
5061 /* Don't use default: we want warnings when adding new values */
5062 case POLARSSL_KEY_EXCHANGE_NONE:
5063 case POLARSSL_KEY_EXCHANGE_PSK:
5064 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5065 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5066 usage = 0;
5067 }
5068 }
5069 else
5070 {
5071 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5072 usage = KU_DIGITAL_SIGNATURE;
5073 }
5074
5075 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5076 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005077#else
5078 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005079#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5080
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005081#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5082 if( cert_endpoint == SSL_IS_SERVER )
5083 {
5084 ext_oid = OID_SERVER_AUTH;
5085 ext_len = OID_SIZE( OID_SERVER_AUTH );
5086 }
5087 else
5088 {
5089 ext_oid = OID_CLIENT_AUTH;
5090 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5091 }
5092
5093 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5094 return( -1 );
5095#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5096
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005097 return( 0 );
5098}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005099#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005100
5101#endif /* POLARSSL_SSL_TLS_C */