blob: cc6a356a89f6b1de29003466ea5347c9dbe4bcbb [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é-Gonnard8e4b3372014-11-17 15:06:13 +01001074#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1075 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1076 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1077#define POLARSSL_SOME_MODES_USE_MAC
1078#endif
1079
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001080/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001082 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001083static int ssl_encrypt_buf( ssl_context *ssl )
1084{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001085 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001086 cipher_mode_t mode;
1087 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
1089 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1090
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001091 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1092 {
1093 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1094 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1095 }
1096
1097 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1098
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001100 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001102#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001103 if( mode == POLARSSL_MODE_STREAM ||
1104 ( mode == POLARSSL_MODE_CBC
1105#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1106 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1107#endif
1108 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001109 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001110#if defined(POLARSSL_SSL_PROTO_SSL3)
1111 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1112 {
1113 ssl_mac( &ssl->transform_out->md_ctx_enc,
1114 ssl->transform_out->mac_enc,
1115 ssl->out_msg, ssl->out_msglen,
1116 ssl->out_ctr, ssl->out_msgtype );
1117 }
1118 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001119#endif
1120#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001121 defined(POLARSSL_SSL_PROTO_TLS1_2)
1122 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1123 {
1124 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1125 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1126 ssl->out_msg, ssl->out_msglen );
1127 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1128 ssl->out_msg + ssl->out_msglen );
1129 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1130 }
1131 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001132#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001133 {
1134 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001135 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001136 }
1137
1138 SSL_DEBUG_BUF( 4, "computed mac",
1139 ssl->out_msg + ssl->out_msglen,
1140 ssl->transform_out->maclen );
1141
1142 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001143 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001144 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001145#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001147 /*
1148 * Encrypt
1149 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001150#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001151 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001152 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001153 int ret;
1154 size_t olen = 0;
1155
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1157 "including %d bytes of padding",
1158 ssl->out_msglen, 0 ) );
1159
1160 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1161 ssl->out_msg, ssl->out_msglen );
1162
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001163 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001164 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001165 ssl->transform_out->ivlen,
1166 ssl->out_msg, ssl->out_msglen,
1167 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001168 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001169 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001170 return( ret );
1171 }
1172
1173 if( ssl->out_msglen != olen )
1174 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001175 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001176 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001177 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 }
Paul Bakker68884e32013-01-07 18:20:04 +01001179 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001180#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001181#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1182 if( mode == POLARSSL_MODE_GCM ||
1183 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001184 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001185 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001186 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001187 unsigned char *enc_msg;
1188 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001189 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1190 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001191
Paul Bakkerca4ab492012-04-18 14:23:57 +00001192 memcpy( add_data, ssl->out_ctr, 8 );
1193 add_data[8] = ssl->out_msgtype;
1194 add_data[9] = ssl->major_ver;
1195 add_data[10] = ssl->minor_ver;
1196 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1197 add_data[12] = ssl->out_msglen & 0xFF;
1198
1199 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1200 add_data, 13 );
1201
Paul Bakker68884e32013-01-07 18:20:04 +01001202 /*
1203 * Generate IV
1204 */
1205 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001206 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1207 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001208 if( ret != 0 )
1209 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001210
Paul Bakker68884e32013-01-07 18:20:04 +01001211 memcpy( ssl->out_iv,
1212 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1213 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001214
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001215 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001216 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001217
Paul Bakker68884e32013-01-07 18:20:04 +01001218 /*
1219 * Fix pointer positions and message length with added IV
1220 */
1221 enc_msg = ssl->out_msg;
1222 enc_msglen = ssl->out_msglen;
1223 ssl->out_msglen += ssl->transform_out->ivlen -
1224 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001225
Paul Bakker68884e32013-01-07 18:20:04 +01001226 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1227 "including %d bytes of padding",
1228 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001229
Paul Bakker68884e32013-01-07 18:20:04 +01001230 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1231 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001232
Paul Bakker68884e32013-01-07 18:20:04 +01001233 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001234 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001235 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001236 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1237 ssl->transform_out->iv_enc,
1238 ssl->transform_out->ivlen,
1239 add_data, 13,
1240 enc_msg, enc_msglen,
1241 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001242 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001243 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001244 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001245 return( ret );
1246 }
1247
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001248 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001249 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001250 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001251 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001252 }
1253
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001254 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001255 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001256
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001257 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001258 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001260#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001261#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1262 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001263 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001265 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001266 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001267 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001268
Paul Bakker48916f92012-09-16 19:57:18 +00001269 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1270 ssl->transform_out->ivlen;
1271 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 padlen = 0;
1273
1274 for( i = 0; i <= padlen; i++ )
1275 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1276
1277 ssl->out_msglen += padlen + 1;
1278
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001279 enc_msglen = ssl->out_msglen;
1280 enc_msg = ssl->out_msg;
1281
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001282#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001283 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001284 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1285 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001286 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001287 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001288 {
1289 /*
1290 * Generate IV
1291 */
Paul Bakker48916f92012-09-16 19:57:18 +00001292 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1293 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001294 if( ret != 0 )
1295 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001296
Paul Bakker92be97b2013-01-02 17:30:03 +01001297 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001298 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001299
1300 /*
1301 * Fix pointer positions and message length with added IV
1302 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001303 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001304 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001305 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001306 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001307#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001308
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001310 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001311 ssl->out_msglen, ssl->transform_out->ivlen,
1312 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313
1314 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001315 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001317 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001318 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001319 ssl->transform_out->ivlen,
1320 enc_msg, enc_msglen,
1321 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001322 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001323 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001324 return( ret );
1325 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001326
Paul Bakkercca5b812013-08-31 17:40:26 +02001327 if( enc_msglen != olen )
1328 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001329 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001330 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001331 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001332
1333#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001334 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1335 {
1336 /*
1337 * Save IV in SSL3 and TLS1
1338 */
1339 memcpy( ssl->transform_out->iv_enc,
1340 ssl->transform_out->cipher_ctx_enc.iv,
1341 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001343#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001344
1345#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001346 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001347 {
1348 /*
1349 * MAC(MAC_write_key, seq_num +
1350 * TLSCipherText.type +
1351 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001352 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001353 * IV + // except for TLS 1.0
1354 * ENC(content + padding + padding_length));
1355 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001356 unsigned char pseudo_hdr[13];
1357
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001358 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1359
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001360 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1361 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001362 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1363 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1364
1365 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001366
1367 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1368 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1369 ssl->out_iv, ssl->out_msglen );
1370 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1371 ssl->out_iv + ssl->out_msglen );
1372 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1373
1374 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001375 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001376 }
1377#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001379 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001380#endif /* POLARSSL_CIPHER_MODE_CBC &&
1381 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001382 {
1383 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001384 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001387 /* Make extra sure authentication was performed, exactly once */
1388 if( auth_done != 1 )
1389 {
1390 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1391 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1392 }
1393
Paul Bakkerca4ab492012-04-18 14:23:57 +00001394 for( i = 8; i > 0; i-- )
1395 if( ++ssl->out_ctr[i - 1] != 0 )
1396 break;
1397
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001398 /* The loops goes to its end iff the counter is wrapping */
1399 if( i == 0 )
1400 {
1401 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1402 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1403 }
1404
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1406
1407 return( 0 );
1408}
1409
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001410#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001411
Paul Bakker5121ce52009-01-03 21:22:43 +00001412static int ssl_decrypt_buf( ssl_context *ssl )
1413{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001414 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001415 cipher_mode_t mode;
1416 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001417#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001418 size_t padlen = 0, correct = 1;
1419#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
1421 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1422
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001423 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1424 {
1425 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1426 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1427 }
1428
1429 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1430
Paul Bakker48916f92012-09-16 19:57:18 +00001431 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 {
1433 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001434 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001435 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 }
1437
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001438#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001439 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001440 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001441 int ret;
1442 size_t olen = 0;
1443
Paul Bakker68884e32013-01-07 18:20:04 +01001444 padlen = 0;
1445
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001446 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001447 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001448 ssl->transform_in->ivlen,
1449 ssl->in_msg, ssl->in_msglen,
1450 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001451 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001452 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001453 return( ret );
1454 }
1455
1456 if( ssl->in_msglen != olen )
1457 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001458 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001459 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001461 }
Paul Bakker68884e32013-01-07 18:20:04 +01001462 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001463#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001464#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1465 if( mode == POLARSSL_MODE_GCM ||
1466 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001467 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001468 int ret;
1469 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001470 unsigned char *dec_msg;
1471 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001472 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001473 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1474 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001475 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1476 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001477
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001478 if( ssl->in_msglen < explicit_iv_len + taglen )
1479 {
1480 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1481 "+ taglen (%d)", ssl->in_msglen,
1482 explicit_iv_len, taglen ) );
1483 return( POLARSSL_ERR_SSL_INVALID_MAC );
1484 }
1485 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1486
Paul Bakker68884e32013-01-07 18:20:04 +01001487 dec_msg = ssl->in_msg;
1488 dec_msg_result = ssl->in_msg;
1489 ssl->in_msglen = dec_msglen;
1490
1491 memcpy( add_data, ssl->in_ctr, 8 );
1492 add_data[8] = ssl->in_msgtype;
1493 add_data[9] = ssl->major_ver;
1494 add_data[10] = ssl->minor_ver;
1495 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1496 add_data[12] = ssl->in_msglen & 0xFF;
1497
1498 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1499 add_data, 13 );
1500
1501 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1502 ssl->in_iv,
1503 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1504
1505 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1506 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001507 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001508
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001509 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001510 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001511 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001512 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1513 ssl->transform_in->iv_dec,
1514 ssl->transform_in->ivlen,
1515 add_data, 13,
1516 dec_msg, dec_msglen,
1517 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001518 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001519 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001520 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1521
1522 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1523 return( POLARSSL_ERR_SSL_INVALID_MAC );
1524
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001525 return( ret );
1526 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001527 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001528
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001529 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001530 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001531 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001532 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001533 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001536#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001537#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1538 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001539 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 {
Paul Bakker45829992013-01-03 14:52:21 +01001541 /*
1542 * Decrypt and check the padding
1543 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001544 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001545 unsigned char *dec_msg;
1546 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001547 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001548 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001549 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001550
Paul Bakker5121ce52009-01-03 21:22:43 +00001551 /*
Paul Bakker45829992013-01-03 14:52:21 +01001552 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001553 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001554#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001555 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1556 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001557#endif
Paul Bakker45829992013-01-03 14:52:21 +01001558
1559 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1560 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1561 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001562 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1563 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1564 ssl->transform_in->ivlen,
1565 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001566 return( POLARSSL_ERR_SSL_INVALID_MAC );
1567 }
1568
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001569 dec_msglen = ssl->in_msglen;
1570 dec_msg = ssl->in_msg;
1571 dec_msg_result = ssl->in_msg;
1572
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001573 /*
1574 * Authenticate before decrypt if enabled
1575 */
1576#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001577 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001578 {
1579 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001580 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001581
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001582 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1583
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001584 dec_msglen -= ssl->transform_in->maclen;
1585 ssl->in_msglen -= ssl->transform_in->maclen;
1586
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001587 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1588 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1589 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1590 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1591
1592 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1593
1594 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001595 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1596 ssl->in_iv, ssl->in_msglen );
1597 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1598 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1599
1600 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1601 ssl->transform_in->maclen );
1602 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1603 ssl->transform_in->maclen );
1604
1605 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1606 ssl->transform_in->maclen ) != 0 )
1607 {
1608 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1609
1610 return( POLARSSL_ERR_SSL_INVALID_MAC );
1611 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001612 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001613 }
1614#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1615
1616 /*
1617 * Check length sanity
1618 */
1619 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1620 {
1621 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1622 ssl->in_msglen, ssl->transform_in->ivlen ) );
1623 return( POLARSSL_ERR_SSL_INVALID_MAC );
1624 }
1625
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001626#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001627 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001628 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001629 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001630 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001631 {
Paul Bakker48916f92012-09-16 19:57:18 +00001632 dec_msglen -= ssl->transform_in->ivlen;
1633 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001634
Paul Bakker48916f92012-09-16 19:57:18 +00001635 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001636 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001637 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001638#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001639
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001640 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001641 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001642 ssl->transform_in->ivlen,
1643 dec_msg, dec_msglen,
1644 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001645 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001646 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001647 return( ret );
1648 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001649
Paul Bakkercca5b812013-08-31 17:40:26 +02001650 if( dec_msglen != olen )
1651 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001652 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001653 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001654 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001655
1656#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001657 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1658 {
1659 /*
1660 * Save IV in SSL3 and TLS1
1661 */
1662 memcpy( ssl->transform_in->iv_dec,
1663 ssl->transform_in->cipher_ctx_dec.iv,
1664 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001666#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
1668 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001669
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001670 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001671 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001672 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001673#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001674 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1675 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001676#endif
Paul Bakker45829992013-01-03 14:52:21 +01001677 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001678 correct = 0;
1679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001681#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001682 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1683 {
Paul Bakker48916f92012-09-16 19:57:18 +00001684 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001685 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001686#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1688 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001689 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001690#endif
Paul Bakker45829992013-01-03 14:52:21 +01001691 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001692 }
1693 }
1694 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001695#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001696#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1697 defined(POLARSSL_SSL_PROTO_TLS1_2)
1698 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 {
1700 /*
Paul Bakker45829992013-01-03 14:52:21 +01001701 * TLSv1+: always check the padding up to the first failure
1702 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001704 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001705 size_t padding_idx = ssl->in_msglen - padlen - 1;
1706
Paul Bakker956c9e02013-12-19 14:42:28 +01001707 /*
1708 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001709 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001710 *
Paul Bakker61885c72014-04-25 12:59:03 +02001711 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1712 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001713 *
1714 * In both cases we reset padding_idx to a safe value (0) to
1715 * prevent out-of-buffer reads.
1716 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001717 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001718 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1719 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001720
1721 padding_idx *= correct;
1722
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001723 for( i = 1; i <= 256; i++ )
1724 {
1725 real_count &= ( i <= padlen );
1726 pad_count += real_count *
1727 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1728 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001729
1730 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001731
Paul Bakkerd66f0702013-01-31 16:57:45 +01001732#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001733 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001734 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001735#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001736 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001737 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001738 else
1739#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1740 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001741 {
1742 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001743 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001744 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001745
1746 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001747 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001748 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001749#endif /* POLARSSL_CIPHER_MODE_CBC &&
1750 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001751 {
1752 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001753 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001754 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
1756 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1757 ssl->in_msg, ssl->in_msglen );
1758
1759 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001760 * Authenticate if not done yet.
1761 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001763#if defined(POLARSSL_SOME_MODES_USE_MAC)
1764 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001765 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001766 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1767
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001768 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001769
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001770 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1771 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001773 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001775#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001776 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1777 {
1778 ssl_mac( &ssl->transform_in->md_ctx_dec,
1779 ssl->transform_in->mac_dec,
1780 ssl->in_msg, ssl->in_msglen,
1781 ssl->in_ctr, ssl->in_msgtype );
1782 }
1783 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001784#endif /* POLARSSL_SSL_PROTO_SSL3 */
1785#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001786 defined(POLARSSL_SSL_PROTO_TLS1_2)
1787 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1788 {
1789 /*
1790 * Process MAC and always update for padlen afterwards to make
1791 * total time independent of padlen
1792 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001793 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001794 *
1795 * Known timing attacks:
1796 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1797 *
1798 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1799 * correctly. (We round down instead of up, so -56 is the correct
1800 * value for our calculations instead of -55)
1801 */
1802 size_t j, extra_run = 0;
1803 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1804 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001805
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001806 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001807
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001808 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1809 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1810 ssl->in_msglen );
1811 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1812 ssl->in_msg + ssl->in_msglen );
1813 for( j = 0; j < extra_run; j++ )
1814 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001815
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001816 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1817 }
1818 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001819#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 POLARSSL_SSL_PROTO_TLS1_2 */
1821 {
1822 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001823 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001824 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001825
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001826 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1827 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1828 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001829
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001830 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001831 ssl->transform_in->maclen ) != 0 )
1832 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001833#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001834 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001835#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001836 correct = 0;
1837 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001838 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001840 /*
1841 * Finally check the correct flag
1842 */
1843 if( correct == 0 )
1844 return( POLARSSL_ERR_SSL_INVALID_MAC );
1845 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001846#endif /* POLARSSL_SOME_MODES_USE_MAC */
1847
1848 /* Make extra sure authentication was performed, exactly once */
1849 if( auth_done != 1 )
1850 {
1851 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1852 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
1855 if( ssl->in_msglen == 0 )
1856 {
1857 ssl->nb_zero++;
1858
1859 /*
1860 * Three or more empty messages may be a DoS attack
1861 * (excessive CPU consumption).
1862 */
1863 if( ssl->nb_zero > 3 )
1864 {
1865 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1866 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001867 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 }
1869 }
1870 else
1871 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001872
Paul Bakker23986e52011-04-24 08:57:21 +00001873 for( i = 8; i > 0; i-- )
1874 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 break;
1876
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001877 /* The loops goes to its end iff the counter is wrapping */
1878 if( i == 0 )
1879 {
1880 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1881 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1882 }
1883
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1885
1886 return( 0 );
1887}
1888
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001889#undef MAC_NONE
1890#undef MAC_PLAINTEXT
1891#undef MAC_CIPHERTEXT
1892
Paul Bakker2770fbd2012-07-03 13:30:23 +00001893#if defined(POLARSSL_ZLIB_SUPPORT)
1894/*
1895 * Compression/decompression functions
1896 */
1897static int ssl_compress_buf( ssl_context *ssl )
1898{
1899 int ret;
1900 unsigned char *msg_post = ssl->out_msg;
1901 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001902 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001903
1904 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1905
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001906 if( len_pre == 0 )
1907 return( 0 );
1908
Paul Bakker2770fbd2012-07-03 13:30:23 +00001909 memcpy( msg_pre, ssl->out_msg, len_pre );
1910
1911 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1912 ssl->out_msglen ) );
1913
1914 SSL_DEBUG_BUF( 4, "before compression: output payload",
1915 ssl->out_msg, ssl->out_msglen );
1916
Paul Bakker48916f92012-09-16 19:57:18 +00001917 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1918 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1919 ssl->transform_out->ctx_deflate.next_out = msg_post;
1920 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001921
Paul Bakker48916f92012-09-16 19:57:18 +00001922 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001923 if( ret != Z_OK )
1924 {
1925 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1926 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1927 }
1928
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001929 ssl->out_msglen = SSL_BUFFER_LEN -
1930 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001931
Paul Bakker2770fbd2012-07-03 13:30:23 +00001932 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1933 ssl->out_msglen ) );
1934
1935 SSL_DEBUG_BUF( 4, "after compression: output payload",
1936 ssl->out_msg, ssl->out_msglen );
1937
1938 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1939
1940 return( 0 );
1941}
1942
1943static int ssl_decompress_buf( ssl_context *ssl )
1944{
1945 int ret;
1946 unsigned char *msg_post = ssl->in_msg;
1947 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001948 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001949
1950 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1951
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001952 if( len_pre == 0 )
1953 return( 0 );
1954
Paul Bakker2770fbd2012-07-03 13:30:23 +00001955 memcpy( msg_pre, ssl->in_msg, len_pre );
1956
1957 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1958 ssl->in_msglen ) );
1959
1960 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1961 ssl->in_msg, ssl->in_msglen );
1962
Paul Bakker48916f92012-09-16 19:57:18 +00001963 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1964 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1965 ssl->transform_in->ctx_inflate.next_out = msg_post;
1966 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001967
Paul Bakker48916f92012-09-16 19:57:18 +00001968 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001969 if( ret != Z_OK )
1970 {
1971 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1972 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1973 }
1974
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001975 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1976 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001977
Paul Bakker2770fbd2012-07-03 13:30:23 +00001978 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1979 ssl->in_msglen ) );
1980
1981 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1982 ssl->in_msg, ssl->in_msglen );
1983
1984 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1985
1986 return( 0 );
1987}
1988#endif /* POLARSSL_ZLIB_SUPPORT */
1989
Paul Bakker5121ce52009-01-03 21:22:43 +00001990/*
1991 * Fill the input message buffer
1992 */
Paul Bakker23986e52011-04-24 08:57:21 +00001993int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001994{
Paul Bakker23986e52011-04-24 08:57:21 +00001995 int ret;
1996 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
1998 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1999
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002000 if( nb_want > SSL_BUFFER_LEN - 8 )
2001 {
2002 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2003 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2004 }
2005
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 while( ssl->in_left < nb_want )
2007 {
2008 len = nb_want - ssl->in_left;
2009 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2010
2011 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2012 ssl->in_left, nb_want ) );
2013 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2014
Paul Bakker831a7552011-05-18 13:32:51 +00002015 if( ret == 0 )
2016 return( POLARSSL_ERR_SSL_CONN_EOF );
2017
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 if( ret < 0 )
2019 return( ret );
2020
2021 ssl->in_left += ret;
2022 }
2023
2024 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2025
2026 return( 0 );
2027}
2028
2029/*
2030 * Flush any data not yet written
2031 */
2032int ssl_flush_output( ssl_context *ssl )
2033{
2034 int ret;
2035 unsigned char *buf;
2036
2037 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2038
2039 while( ssl->out_left > 0 )
2040 {
2041 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2042 5 + ssl->out_msglen, ssl->out_left ) );
2043
Paul Bakker5bd42292012-12-19 14:40:42 +01002044 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002046
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2048
2049 if( ret <= 0 )
2050 return( ret );
2051
2052 ssl->out_left -= ret;
2053 }
2054
2055 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2056
2057 return( 0 );
2058}
2059
2060/*
2061 * Record layer functions
2062 */
2063int ssl_write_record( ssl_context *ssl )
2064{
Paul Bakker05ef8352012-05-08 09:17:57 +00002065 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002066 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002067
2068 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2069
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2071 {
2072 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2073 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2074 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2075
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002076 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2077 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 }
2079
Paul Bakker2770fbd2012-07-03 13:30:23 +00002080#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002081 if( ssl->transform_out != NULL &&
2082 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002083 {
2084 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2085 {
2086 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2087 return( ret );
2088 }
2089
2090 len = ssl->out_msglen;
2091 }
2092#endif /*POLARSSL_ZLIB_SUPPORT */
2093
Paul Bakker05ef8352012-05-08 09:17:57 +00002094#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002095 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002097 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Paul Bakker05ef8352012-05-08 09:17:57 +00002099 ret = ssl_hw_record_write( ssl );
2100 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2101 {
2102 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002103 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002104 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002105
2106 if( ret == 0 )
2107 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002108 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002109#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002110 if( !done )
2111 {
2112 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2113 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2114 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2116 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002117
Paul Bakker48916f92012-09-16 19:57:18 +00002118 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002119 {
2120 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2121 {
2122 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2123 return( ret );
2124 }
2125
2126 len = ssl->out_msglen;
2127 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2128 ssl->out_hdr[4] = (unsigned char)( len );
2129 }
2130
2131 ssl->out_left = 5 + ssl->out_msglen;
2132
2133 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2134 "version = [%d:%d], msglen = %d",
2135 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2136 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2137
2138 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002139 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 }
2141
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2143 {
2144 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2145 return( ret );
2146 }
2147
2148 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2149
2150 return( 0 );
2151}
2152
2153int ssl_read_record( ssl_context *ssl )
2154{
Paul Bakker05ef8352012-05-08 09:17:57 +00002155 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
2157 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2158
2159 if( ssl->in_hslen != 0 &&
2160 ssl->in_hslen < ssl->in_msglen )
2161 {
2162 /*
2163 * Get next Handshake message in the current record
2164 */
2165 ssl->in_msglen -= ssl->in_hslen;
2166
Paul Bakker8934a982011-08-05 11:11:53 +00002167 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002168 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
2170 ssl->in_hslen = 4;
2171 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2172
2173 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2174 " %d, type = %d, hslen = %d",
2175 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2176
2177 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2178 {
2179 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002180 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 }
2182
2183 if( ssl->in_msglen < ssl->in_hslen )
2184 {
2185 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002186 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 }
2188
Paul Bakker4224bc02014-04-08 14:36:50 +02002189 if( ssl->state != SSL_HANDSHAKE_OVER )
2190 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191
2192 return( 0 );
2193 }
2194
2195 ssl->in_hslen = 0;
2196
2197 /*
2198 * Read the record header and validate it
2199 */
2200 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2201 {
2202 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2203 return( ret );
2204 }
2205
2206 ssl->in_msgtype = ssl->in_hdr[0];
2207 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2208
2209 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2210 "version = [%d:%d], msglen = %d",
2211 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2212 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2213
2214 if( ssl->in_hdr[1] != ssl->major_ver )
2215 {
2216 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002217 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 }
2219
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002220 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 {
2222 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002223 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 }
2225
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002226 /* Sanity check (outer boundaries) */
2227 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2228 {
2229 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2230 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2231 }
2232
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002234 * Make sure the message length is acceptable for the current transform
2235 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 */
Paul Bakker48916f92012-09-16 19:57:18 +00002237 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002239 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 {
2241 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002242 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 }
2244 }
2245 else
2246 {
Paul Bakker48916f92012-09-16 19:57:18 +00002247 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 {
2249 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002250 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 }
2252
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002253#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002255 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 {
2257 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002258 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002260#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002262#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2263 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 /*
2265 * TLS encrypted messages can have up to 256 bytes of padding
2266 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002267 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002268 ssl->in_msglen > ssl->transform_in->minlen +
2269 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 {
2271 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002272 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002274#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 }
2276
2277 /*
2278 * Read and optionally decrypt the message contents
2279 */
2280 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2281 {
2282 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2283 return( ret );
2284 }
2285
2286 SSL_DEBUG_BUF( 4, "input record from network",
2287 ssl->in_hdr, 5 + ssl->in_msglen );
2288
Paul Bakker05ef8352012-05-08 09:17:57 +00002289#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002290 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002291 {
2292 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2293
2294 ret = ssl_hw_record_read( ssl );
2295 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2296 {
2297 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002298 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002299 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002300
2301 if( ret == 0 )
2302 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002303 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002304#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002305 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 {
2307 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2308 {
Paul Bakker40865c82013-01-31 17:13:13 +01002309#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2310 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2311 {
2312 ssl_send_alert_message( ssl,
2313 SSL_ALERT_LEVEL_FATAL,
2314 SSL_ALERT_MSG_BAD_RECORD_MAC );
2315 }
2316#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2318 return( ret );
2319 }
2320
2321 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2322 ssl->in_msg, ssl->in_msglen );
2323
2324 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2325 {
2326 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002327 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 }
2329 }
2330
Paul Bakker2770fbd2012-07-03 13:30:23 +00002331#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002332 if( ssl->transform_in != NULL &&
2333 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002334 {
2335 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2336 {
2337 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2338 return( ret );
2339 }
2340
2341 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2342 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2343 }
2344#endif /* POLARSSL_ZLIB_SUPPORT */
2345
Paul Bakker0a925182012-04-16 06:46:41 +00002346 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2347 ssl->in_msgtype != SSL_MSG_ALERT &&
2348 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2349 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2350 {
2351 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2352
Paul Bakker48916f92012-09-16 19:57:18 +00002353 if( ( ret = ssl_send_alert_message( ssl,
2354 SSL_ALERT_LEVEL_FATAL,
2355 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002356 {
2357 return( ret );
2358 }
2359
2360 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2361 }
2362
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2364 {
2365 ssl->in_hslen = 4;
2366 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2367
2368 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2369 " %d, type = %d, hslen = %d",
2370 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2371
2372 /*
2373 * Additional checks to validate the handshake header
2374 */
2375 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2376 {
2377 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002378 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 }
2380
2381 if( ssl->in_msglen < ssl->in_hslen )
2382 {
2383 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002384 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 }
2386
Paul Bakker48916f92012-09-16 19:57:18 +00002387 if( ssl->state != SSL_HANDSHAKE_OVER )
2388 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 }
2390
2391 if( ssl->in_msgtype == SSL_MSG_ALERT )
2392 {
2393 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2394 ssl->in_msg[0], ssl->in_msg[1] ) );
2395
2396 /*
2397 * Ignore non-fatal alerts, except close_notify
2398 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002399 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002401 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2402 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002403 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 }
2405
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002406 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2407 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 {
2409 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002410 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 }
2412 }
2413
2414 ssl->in_left = 0;
2415
2416 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2417
2418 return( 0 );
2419}
2420
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002421int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2422{
2423 int ret;
2424
2425 if( ( ret = ssl_send_alert_message( ssl,
2426 SSL_ALERT_LEVEL_FATAL,
2427 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2428 {
2429 return( ret );
2430 }
2431
2432 return( 0 );
2433}
2434
Paul Bakker0a925182012-04-16 06:46:41 +00002435int ssl_send_alert_message( ssl_context *ssl,
2436 unsigned char level,
2437 unsigned char message )
2438{
2439 int ret;
2440
2441 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2442
2443 ssl->out_msgtype = SSL_MSG_ALERT;
2444 ssl->out_msglen = 2;
2445 ssl->out_msg[0] = level;
2446 ssl->out_msg[1] = message;
2447
2448 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2449 {
2450 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2451 return( ret );
2452 }
2453
2454 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2455
2456 return( 0 );
2457}
2458
Paul Bakker5121ce52009-01-03 21:22:43 +00002459/*
2460 * Handshake functions
2461 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002462#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2463 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2464 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2465 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2466 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2467 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2468 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002469int ssl_write_certificate( ssl_context *ssl )
2470{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002471 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
2473 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2474
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002475 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002476 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2477 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002478 {
2479 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2480 ssl->state++;
2481 return( 0 );
2482 }
2483
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002484 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2485 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002486}
2487
2488int ssl_parse_certificate( ssl_context *ssl )
2489{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2491
2492 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2493
2494 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002495 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2496 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002497 {
2498 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2499 ssl->state++;
2500 return( 0 );
2501 }
2502
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002503 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2504 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002505}
2506#else
2507int ssl_write_certificate( ssl_context *ssl )
2508{
2509 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2510 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002511 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002512 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2513
2514 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2515
2516 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002517 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2518 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002519 {
2520 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2521 ssl->state++;
2522 return( 0 );
2523 }
2524
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 if( ssl->endpoint == SSL_IS_CLIENT )
2526 {
2527 if( ssl->client_auth == 0 )
2528 {
2529 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2530 ssl->state++;
2531 return( 0 );
2532 }
2533
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002534#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 /*
2536 * If using SSLv3 and got no cert, send an Alert message
2537 * (otherwise an empty Certificate message will be sent).
2538 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002539 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2541 {
2542 ssl->out_msglen = 2;
2543 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002544 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2545 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
2547 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2548 goto write_msg;
2549 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002550#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 }
2552 else /* SSL_IS_SERVER */
2553 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002554 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 {
2556 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002557 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 }
2559 }
2560
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002561 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
2563 /*
2564 * 0 . 0 handshake type
2565 * 1 . 3 handshake length
2566 * 4 . 6 length of all certs
2567 * 7 . 9 length of cert. 1
2568 * 10 . n-1 peer certificate
2569 * n . n+2 length of cert. 2
2570 * n+3 . ... upper level cert, etc.
2571 */
2572 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002573 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Paul Bakker29087132010-03-21 21:03:34 +00002575 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 {
2577 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002578 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 {
2580 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2581 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002582 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 }
2584
2585 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2586 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2587 ssl->out_msg[i + 2] = (unsigned char)( n );
2588
2589 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2590 i += n; crt = crt->next;
2591 }
2592
2593 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2594 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2595 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2596
2597 ssl->out_msglen = i;
2598 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2599 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2600
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002601#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002602write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002603#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
2605 ssl->state++;
2606
2607 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2608 {
2609 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2610 return( ret );
2611 }
2612
2613 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2614
Paul Bakkered27a042013-04-18 22:46:23 +02002615 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616}
2617
2618int ssl_parse_certificate( ssl_context *ssl )
2619{
Paul Bakkered27a042013-04-18 22:46:23 +02002620 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002621 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002622 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
2624 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2625
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002626 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002627 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2628 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002629 {
2630 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2631 ssl->state++;
2632 return( 0 );
2633 }
2634
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002636 ( ssl->authmode == SSL_VERIFY_NONE ||
2637 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002639 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2641 ssl->state++;
2642 return( 0 );
2643 }
2644
2645 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2646 {
2647 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2648 return( ret );
2649 }
2650
2651 ssl->state++;
2652
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002653#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 /*
2655 * Check if the client sent an empty certificate
2656 */
2657 if( ssl->endpoint == SSL_IS_SERVER &&
2658 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2659 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002660 if( ssl->in_msglen == 2 &&
2661 ssl->in_msgtype == SSL_MSG_ALERT &&
2662 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2663 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 {
2665 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2666
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002667 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2669 return( 0 );
2670 else
Paul Bakker40e46942009-01-03 21:51:57 +00002671 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 }
2673 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002674#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002676#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2677 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 if( ssl->endpoint == SSL_IS_SERVER &&
2679 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2680 {
2681 if( ssl->in_hslen == 7 &&
2682 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2683 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2684 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2685 {
2686 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2687
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002688 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002690 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 else
2692 return( 0 );
2693 }
2694 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002695#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2696 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
2698 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2699 {
2700 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002701 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 }
2703
2704 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2705 {
2706 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002707 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 }
2709
2710 /*
2711 * Same message structure as in ssl_write_certificate()
2712 */
2713 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2714
2715 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2716 {
2717 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002718 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 }
2720
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002721 /* In case we tried to reuse a session but it failed */
2722 if( ssl->session_negotiate->peer_cert != NULL )
2723 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002724 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002725 polarssl_free( ssl->session_negotiate->peer_cert );
2726 }
2727
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002728 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2729 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 {
2731 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002732 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002733 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
2735
Paul Bakkerb6b09562013-09-18 14:17:41 +02002736 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
2738 i = 7;
2739
2740 while( i < ssl->in_hslen )
2741 {
2742 if( ssl->in_msg[i] != 0 )
2743 {
2744 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002745 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 }
2747
2748 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2749 | (unsigned int) ssl->in_msg[i + 2];
2750 i += 3;
2751
2752 if( n < 128 || i + n > ssl->in_hslen )
2753 {
2754 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002755 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 }
2757
Paul Bakkerddf26b42013-09-18 13:46:23 +02002758 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2759 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 if( ret != 0 )
2761 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002762 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 return( ret );
2764 }
2765
2766 i += n;
2767 }
2768
Paul Bakker48916f92012-09-16 19:57:18 +00002769 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002771 /*
2772 * On client, make sure the server cert doesn't change during renego to
2773 * avoid "triple handshake" attack: https://secure-resumption.com/
2774 */
2775 if( ssl->endpoint == SSL_IS_CLIENT &&
2776 ssl->renegotiation == SSL_RENEGOTIATION )
2777 {
2778 if( ssl->session->peer_cert == NULL )
2779 {
2780 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2781 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2782 }
2783
2784 if( ssl->session->peer_cert->raw.len !=
2785 ssl->session_negotiate->peer_cert->raw.len ||
2786 memcmp( ssl->session->peer_cert->raw.p,
2787 ssl->session_negotiate->peer_cert->raw.p,
2788 ssl->session->peer_cert->raw.len ) != 0 )
2789 {
2790 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2791 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2792 }
2793 }
2794
Paul Bakker5121ce52009-01-03 21:22:43 +00002795 if( ssl->authmode != SSL_VERIFY_NONE )
2796 {
2797 if( ssl->ca_chain == NULL )
2798 {
2799 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002800 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 }
2802
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002803 /*
2804 * Main check: verify certificate
2805 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002806 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2807 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2808 &ssl->session_negotiate->verify_result,
2809 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
2811 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002812 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002814 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002815
2816 /*
2817 * Secondary checks: always done, but change 'ret' only if it was 0
2818 */
2819
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002820#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002821 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002822 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002823
2824 /* If certificate uses an EC key, make sure the curve is OK */
2825 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2826 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2827 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002828 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002829 if( ret == 0 )
2830 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002831 }
2832 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002833#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002835 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2836 ciphersuite_info,
2837 ! ssl->endpoint ) != 0 )
2838 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002839 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002840 if( ret == 0 )
2841 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2842 }
2843
Paul Bakker5121ce52009-01-03 21:22:43 +00002844 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2845 ret = 0;
2846 }
2847
2848 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2849
2850 return( ret );
2851}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002852#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2853 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2854 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2855 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2856 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2857 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2858 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
2860int ssl_write_change_cipher_spec( ssl_context *ssl )
2861{
2862 int ret;
2863
2864 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2865
2866 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2867 ssl->out_msglen = 1;
2868 ssl->out_msg[0] = 1;
2869
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 ssl->state++;
2871
2872 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2873 {
2874 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2875 return( ret );
2876 }
2877
2878 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2879
2880 return( 0 );
2881}
2882
2883int ssl_parse_change_cipher_spec( ssl_context *ssl )
2884{
2885 int ret;
2886
2887 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2888
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2890 {
2891 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2892 return( ret );
2893 }
2894
2895 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2896 {
2897 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002898 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 }
2900
2901 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2902 {
2903 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002904 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 }
2906
2907 ssl->state++;
2908
2909 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2910
2911 return( 0 );
2912}
2913
Paul Bakker41c83d32013-03-20 14:39:14 +01002914void ssl_optimize_checksum( ssl_context *ssl,
2915 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002916{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002917 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002918
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002919#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2920 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002921 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002922 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002923 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002924#endif
2925#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2926#if defined(POLARSSL_SHA512_C)
2927 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2928 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2929 else
2930#endif
2931#if defined(POLARSSL_SHA256_C)
2932 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002933 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002934 else
2935#endif
2936#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002937 {
2938 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002939 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002940 }
Paul Bakker380da532012-04-18 16:10:25 +00002941}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002942
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002943static void ssl_update_checksum_start( ssl_context *ssl,
2944 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002945{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2947 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002948 md5_update( &ssl->handshake->fin_md5 , buf, len );
2949 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002950#endif
2951#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2952#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002953 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002954#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002955#if defined(POLARSSL_SHA512_C)
2956 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002957#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002958#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002959}
2960
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002961#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2962 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002963static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2964 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002965{
Paul Bakker48916f92012-09-16 19:57:18 +00002966 md5_update( &ssl->handshake->fin_md5 , buf, len );
2967 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002968}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002969#endif
Paul Bakker380da532012-04-18 16:10:25 +00002970
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002971#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2972#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002973static void ssl_update_checksum_sha256( ssl_context *ssl,
2974 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002975{
Paul Bakker9e36f042013-06-30 14:34:05 +02002976 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002977}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002978#endif
Paul Bakker380da532012-04-18 16:10:25 +00002979
Paul Bakker9e36f042013-06-30 14:34:05 +02002980#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002981static void ssl_update_checksum_sha384( ssl_context *ssl,
2982 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002983{
Paul Bakker9e36f042013-06-30 14:34:05 +02002984 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002985}
Paul Bakker769075d2012-11-24 11:26:46 +01002986#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002987#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002988
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002989#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002990static void ssl_calc_finished_ssl(
2991 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002992{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002993 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994 md5_context md5;
2995 sha1_context sha1;
2996
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 unsigned char padbuf[48];
2998 unsigned char md5sum[16];
2999 unsigned char sha1sum[20];
3000
Paul Bakker48916f92012-09-16 19:57:18 +00003001 ssl_session *session = ssl->session_negotiate;
3002 if( !session )
3003 session = ssl->session;
3004
Paul Bakker1ef83d62012-04-11 12:09:53 +00003005 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3006
Paul Bakker48916f92012-09-16 19:57:18 +00003007 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3008 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003009
3010 /*
3011 * SSLv3:
3012 * hash =
3013 * MD5( master + pad2 +
3014 * MD5( handshake + sender + master + pad1 ) )
3015 * + SHA1( master + pad2 +
3016 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003017 */
3018
Paul Bakker90995b52013-06-24 19:20:35 +02003019#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003020 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003022#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Paul Bakker90995b52013-06-24 19:20:35 +02003024#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003026 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003028
Paul Bakker3c2122f2013-06-24 19:03:14 +02003029 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3030 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003033
Paul Bakker3c2122f2013-06-24 19:03:14 +02003034 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003035 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003036 md5_update( &md5, padbuf, 48 );
3037 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Paul Bakker3c2122f2013-06-24 19:03:14 +02003039 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003040 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003041 sha1_update( &sha1, padbuf, 40 );
3042 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Paul Bakker1ef83d62012-04-11 12:09:53 +00003044 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
Paul Bakker1ef83d62012-04-11 12:09:53 +00003046 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003047 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048 md5_update( &md5, padbuf, 48 );
3049 md5_update( &md5, md5sum, 16 );
3050 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003051
Paul Bakker1ef83d62012-04-11 12:09:53 +00003052 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003053 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003054 sha1_update( &sha1, padbuf , 40 );
3055 sha1_update( &sha1, sha1sum, 20 );
3056 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Paul Bakker1ef83d62012-04-11 12:09:53 +00003058 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003059
Paul Bakker5b4af392014-06-26 12:09:34 +02003060 md5_free( &md5 );
3061 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Paul Bakker34617722014-06-13 17:20:13 +02003063 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3064 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3065 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
3067 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3068}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003069#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003070
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003071#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003072static void ssl_calc_finished_tls(
3073 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003074{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003076 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003077 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003079 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
Paul Bakker48916f92012-09-16 19:57:18 +00003081 ssl_session *session = ssl->session_negotiate;
3082 if( !session )
3083 session = ssl->session;
3084
Paul Bakker1ef83d62012-04-11 12:09:53 +00003085 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Paul Bakker48916f92012-09-16 19:57:18 +00003087 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3088 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
Paul Bakker1ef83d62012-04-11 12:09:53 +00003090 /*
3091 * TLSv1:
3092 * hash = PRF( master, finished_label,
3093 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3094 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Paul Bakker90995b52013-06-24 19:20:35 +02003096#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003097 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3098 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003099#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003100
Paul Bakker90995b52013-06-24 19:20:35 +02003101#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3103 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003104#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003105
3106 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003107 ? "client finished"
3108 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003109
3110 md5_finish( &md5, padbuf );
3111 sha1_finish( &sha1, padbuf + 16 );
3112
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003113 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003114 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003115
3116 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3117
Paul Bakker5b4af392014-06-26 12:09:34 +02003118 md5_free( &md5 );
3119 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003120
Paul Bakker34617722014-06-13 17:20:13 +02003121 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003122
3123 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3124}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003125#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003126
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003127#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3128#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003129static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003130 ssl_context *ssl, unsigned char *buf, int from )
3131{
3132 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003133 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003134 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003135 unsigned char padbuf[32];
3136
Paul Bakker48916f92012-09-16 19:57:18 +00003137 ssl_session *session = ssl->session_negotiate;
3138 if( !session )
3139 session = ssl->session;
3140
Paul Bakker380da532012-04-18 16:10:25 +00003141 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003142
Paul Bakker9e36f042013-06-30 14:34:05 +02003143 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003144
3145 /*
3146 * TLSv1.2:
3147 * hash = PRF( master, finished_label,
3148 * Hash( handshake ) )[0.11]
3149 */
3150
Paul Bakker9e36f042013-06-30 14:34:05 +02003151#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003152 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003153 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003154#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155
3156 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003157 ? "client finished"
3158 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003159
Paul Bakker9e36f042013-06-30 14:34:05 +02003160 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003161
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003162 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003163 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003164
3165 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3166
Paul Bakker5b4af392014-06-26 12:09:34 +02003167 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003168
Paul Bakker34617722014-06-13 17:20:13 +02003169 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003170
3171 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3172}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003173#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003174
Paul Bakker9e36f042013-06-30 14:34:05 +02003175#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003176static void ssl_calc_finished_tls_sha384(
3177 ssl_context *ssl, unsigned char *buf, int from )
3178{
3179 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003180 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003181 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003182 unsigned char padbuf[48];
3183
Paul Bakker48916f92012-09-16 19:57:18 +00003184 ssl_session *session = ssl->session_negotiate;
3185 if( !session )
3186 session = ssl->session;
3187
Paul Bakker380da532012-04-18 16:10:25 +00003188 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003189
Paul Bakker9e36f042013-06-30 14:34:05 +02003190 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003191
3192 /*
3193 * TLSv1.2:
3194 * hash = PRF( master, finished_label,
3195 * Hash( handshake ) )[0.11]
3196 */
3197
Paul Bakker9e36f042013-06-30 14:34:05 +02003198#if !defined(POLARSSL_SHA512_ALT)
3199 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3200 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003201#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003202
3203 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003204 ? "client finished"
3205 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003206
Paul Bakker9e36f042013-06-30 14:34:05 +02003207 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003208
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003209 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003210 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003211
3212 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3213
Paul Bakker5b4af392014-06-26 12:09:34 +02003214 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003215
Paul Bakker34617722014-06-13 17:20:13 +02003216 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003217
3218 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3219}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003220#endif /* POLARSSL_SHA512_C */
3221#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003222
Paul Bakker48916f92012-09-16 19:57:18 +00003223void ssl_handshake_wrapup( ssl_context *ssl )
3224{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003225 int resume = ssl->handshake->resume;
3226
Paul Bakker48916f92012-09-16 19:57:18 +00003227 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3228
3229 /*
3230 * Free our handshake params
3231 */
3232 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003233 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003234 ssl->handshake = NULL;
3235
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003236 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003237 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003238 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003239 ssl->renego_records_seen = 0;
3240 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003241
Paul Bakker48916f92012-09-16 19:57:18 +00003242 /*
3243 * Switch in our now active transform context
3244 */
3245 if( ssl->transform )
3246 {
3247 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003248 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003249 }
3250 ssl->transform = ssl->transform_negotiate;
3251 ssl->transform_negotiate = NULL;
3252
Paul Bakker0a597072012-09-25 21:55:46 +00003253 if( ssl->session )
3254 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003255#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3256 /* RFC 7366 3.1: keep the EtM state */
3257 ssl->session_negotiate->encrypt_then_mac =
3258 ssl->session->encrypt_then_mac;
3259#endif
3260
Paul Bakker0a597072012-09-25 21:55:46 +00003261 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003262 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003263 }
3264 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003265 ssl->session_negotiate = NULL;
3266
Paul Bakker0a597072012-09-25 21:55:46 +00003267 /*
3268 * Add cache entry
3269 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003270 if( ssl->f_set_cache != NULL &&
3271 ssl->session->length != 0 &&
3272 resume == 0 )
3273 {
Paul Bakker0a597072012-09-25 21:55:46 +00003274 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3275 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003276 }
Paul Bakker0a597072012-09-25 21:55:46 +00003277
Paul Bakker48916f92012-09-16 19:57:18 +00003278 ssl->state++;
3279
3280 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3281}
3282
Paul Bakker1ef83d62012-04-11 12:09:53 +00003283int ssl_write_finished( ssl_context *ssl )
3284{
3285 int ret, hash_len;
3286
3287 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3288
Paul Bakker92be97b2013-01-02 17:30:03 +01003289 /*
3290 * Set the out_msg pointer to the correct location based on IV length
3291 */
3292 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3293 {
3294 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3295 ssl->transform_negotiate->fixed_ivlen;
3296 }
3297 else
3298 ssl->out_msg = ssl->out_iv;
3299
Paul Bakker48916f92012-09-16 19:57:18 +00003300 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003301
3302 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3304
Paul Bakker48916f92012-09-16 19:57:18 +00003305 ssl->verify_data_len = hash_len;
3306 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3307
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 ssl->out_msglen = 4 + hash_len;
3309 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3310 ssl->out_msg[0] = SSL_HS_FINISHED;
3311
3312 /*
3313 * In case of session resuming, invert the client and server
3314 * ChangeCipherSpec messages order.
3315 */
Paul Bakker0a597072012-09-25 21:55:46 +00003316 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 {
3318 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003319 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003320 else
3321 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3322 }
3323 else
3324 ssl->state++;
3325
Paul Bakker48916f92012-09-16 19:57:18 +00003326 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003327 * Switch to our negotiated transform and session parameters for outbound
3328 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003329 */
3330 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3331 ssl->transform_out = ssl->transform_negotiate;
3332 ssl->session_out = ssl->session_negotiate;
3333 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
Paul Bakker07eb38b2012-12-19 14:42:06 +01003335#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003336 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003337 {
3338 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3339 {
3340 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3341 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3342 }
3343 }
3344#endif
3345
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3347 {
3348 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3349 return( ret );
3350 }
3351
3352 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3353
3354 return( 0 );
3355}
3356
3357int ssl_parse_finished( ssl_context *ssl )
3358{
Paul Bakker23986e52011-04-24 08:57:21 +00003359 int ret;
3360 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 unsigned char buf[36];
3362
3363 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3364
Paul Bakker48916f92012-09-16 19:57:18 +00003365 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366
Paul Bakker48916f92012-09-16 19:57:18 +00003367 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003368 * Switch to our negotiated transform and session parameters for inbound
3369 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003370 */
3371 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3372 ssl->transform_in = ssl->transform_negotiate;
3373 ssl->session_in = ssl->session_negotiate;
3374 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003375
Paul Bakker92be97b2013-01-02 17:30:03 +01003376 /*
3377 * Set the in_msg pointer to the correct location based on IV length
3378 */
3379 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3380 {
3381 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3382 ssl->transform_negotiate->fixed_ivlen;
3383 }
3384 else
3385 ssl->in_msg = ssl->in_iv;
3386
Paul Bakker07eb38b2012-12-19 14:42:06 +01003387#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003388 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003389 {
3390 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3391 {
3392 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3393 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3394 }
3395 }
3396#endif
3397
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3399 {
3400 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3401 return( ret );
3402 }
3403
3404 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3405 {
3406 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003407 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 }
3409
Paul Bakker1ef83d62012-04-11 12:09:53 +00003410 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003411 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3412
3413 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3414 ssl->in_hslen != 4 + hash_len )
3415 {
3416 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003417 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003418 }
3419
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003420 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003421 {
3422 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003423 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003424 }
3425
Paul Bakker48916f92012-09-16 19:57:18 +00003426 ssl->verify_data_len = hash_len;
3427 memcpy( ssl->peer_verify_data, buf, hash_len );
3428
Paul Bakker0a597072012-09-25 21:55:46 +00003429 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003430 {
3431 if( ssl->endpoint == SSL_IS_CLIENT )
3432 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3433
3434 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003435 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003436 }
3437 else
3438 ssl->state++;
3439
3440 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3441
3442 return( 0 );
3443}
3444
Paul Bakker968afaa2014-07-09 11:09:24 +02003445static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003446{
3447 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3448
3449#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3450 defined(POLARSSL_SSL_PROTO_TLS1_1)
3451 md5_init( &handshake->fin_md5 );
3452 sha1_init( &handshake->fin_sha1 );
3453 md5_starts( &handshake->fin_md5 );
3454 sha1_starts( &handshake->fin_sha1 );
3455#endif
3456#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3457#if defined(POLARSSL_SHA256_C)
3458 sha256_init( &handshake->fin_sha256 );
3459 sha256_starts( &handshake->fin_sha256, 0 );
3460#endif
3461#if defined(POLARSSL_SHA512_C)
3462 sha512_init( &handshake->fin_sha512 );
3463 sha512_starts( &handshake->fin_sha512, 1 );
3464#endif
3465#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3466
3467 handshake->update_checksum = ssl_update_checksum_start;
3468 handshake->sig_alg = SSL_HASH_SHA1;
3469
3470#if defined(POLARSSL_DHM_C)
3471 dhm_init( &handshake->dhm_ctx );
3472#endif
3473#if defined(POLARSSL_ECDH_C)
3474 ecdh_init( &handshake->ecdh_ctx );
3475#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003476}
3477
3478static void ssl_transform_init( ssl_transform *transform )
3479{
3480 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003481
3482 cipher_init( &transform->cipher_ctx_enc );
3483 cipher_init( &transform->cipher_ctx_dec );
3484
3485 md_init( &transform->md_ctx_enc );
3486 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003487}
3488
3489void ssl_session_init( ssl_session *session )
3490{
3491 memset( session, 0, sizeof(ssl_session) );
3492}
3493
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003494static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003495{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003496 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003497 if( ssl->transform_negotiate )
3498 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003499 if( ssl->session_negotiate )
3500 ssl_session_free( ssl->session_negotiate );
3501 if( ssl->handshake )
3502 ssl_handshake_free( ssl->handshake );
3503
3504 /*
3505 * Either the pointers are now NULL or cleared properly and can be freed.
3506 * Now allocate missing structures.
3507 */
3508 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003509 {
3510 ssl->transform_negotiate =
3511 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3512 }
Paul Bakker48916f92012-09-16 19:57:18 +00003513
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003514 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003515 {
3516 ssl->session_negotiate =
3517 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3518 }
Paul Bakker48916f92012-09-16 19:57:18 +00003519
Paul Bakker82788fb2014-10-20 13:59:19 +02003520 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003521 {
3522 ssl->handshake = (ssl_handshake_params *)
3523 polarssl_malloc( sizeof(ssl_handshake_params) );
3524 }
Paul Bakker48916f92012-09-16 19:57:18 +00003525
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003526 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003527 if( ssl->handshake == NULL ||
3528 ssl->transform_negotiate == NULL ||
3529 ssl->session_negotiate == NULL )
3530 {
3531 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003532
3533 polarssl_free( ssl->handshake );
3534 polarssl_free( ssl->transform_negotiate );
3535 polarssl_free( ssl->session_negotiate );
3536
3537 ssl->handshake = NULL;
3538 ssl->transform_negotiate = NULL;
3539 ssl->session_negotiate = NULL;
3540
Paul Bakker48916f92012-09-16 19:57:18 +00003541 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3542 }
3543
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003544 /* Initialize structures */
3545 ssl_session_init( ssl->session_negotiate );
3546 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003547 ssl_handshake_params_init( ssl->handshake );
3548
3549#if defined(POLARSSL_X509_CRT_PARSE_C)
3550 ssl->handshake->key_cert = ssl->key_cert;
3551#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003552
Paul Bakker48916f92012-09-16 19:57:18 +00003553 return( 0 );
3554}
3555
Paul Bakker5121ce52009-01-03 21:22:43 +00003556/*
3557 * Initialize an SSL context
3558 */
3559int ssl_init( ssl_context *ssl )
3560{
Paul Bakker48916f92012-09-16 19:57:18 +00003561 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 int len = SSL_BUFFER_LEN;
3563
3564 memset( ssl, 0, sizeof( ssl_context ) );
3565
Paul Bakker62f2dee2012-09-28 07:31:51 +00003566 /*
3567 * Sane defaults
3568 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003569 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3570 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3571 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3572 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003573
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003574 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003575
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003576 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3577
Paul Bakker62f2dee2012-09-28 07:31:51 +00003578#if defined(POLARSSL_DHM_C)
3579 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3580 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3581 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3582 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3583 {
3584 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3585 return( ret );
3586 }
3587#endif
3588
3589 /*
3590 * Prepare base structures
3591 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003592 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003594 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 ssl->in_msg = ssl->in_ctr + 13;
3596
3597 if( ssl->in_ctr == NULL )
3598 {
3599 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003600 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003601 }
3602
Paul Bakker6e339b52013-07-03 13:37:05 +02003603 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003605 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003606 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003607
3608 if( ssl->out_ctr == NULL )
3609 {
3610 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003611 polarssl_free( ssl->in_ctr );
3612 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003613 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 }
3615
3616 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3617 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3618
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003619#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3620 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3621#endif
3622
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003623#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3624 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3625#endif
3626
Paul Bakker606b4ba2013-08-14 16:52:14 +02003627#if defined(POLARSSL_SSL_SESSION_TICKETS)
3628 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3629#endif
3630
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003631#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003632 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003633#endif
3634
Paul Bakker48916f92012-09-16 19:57:18 +00003635 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3636 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003637
3638 return( 0 );
3639}
3640
3641/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003642 * Reset an initialized and used SSL context for re-use while retaining
3643 * all application-set variables, function pointers and data.
3644 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003645int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003646{
Paul Bakker48916f92012-09-16 19:57:18 +00003647 int ret;
3648
Paul Bakker7eb013f2011-10-06 12:37:39 +00003649 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003650 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3651 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3652
3653 ssl->verify_data_len = 0;
3654 memset( ssl->own_verify_data, 0, 36 );
3655 memset( ssl->peer_verify_data, 0, 36 );
3656
Paul Bakker7eb013f2011-10-06 12:37:39 +00003657 ssl->in_offt = NULL;
3658
Paul Bakker92be97b2013-01-02 17:30:03 +01003659 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003660 ssl->in_msgtype = 0;
3661 ssl->in_msglen = 0;
3662 ssl->in_left = 0;
3663
3664 ssl->in_hslen = 0;
3665 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003666 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003667
Paul Bakker92be97b2013-01-02 17:30:03 +01003668 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003669 ssl->out_msgtype = 0;
3670 ssl->out_msglen = 0;
3671 ssl->out_left = 0;
3672
Paul Bakker48916f92012-09-16 19:57:18 +00003673 ssl->transform_in = NULL;
3674 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003675
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003676 ssl->renego_records_seen = 0;
3677
Paul Bakker7eb013f2011-10-06 12:37:39 +00003678 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3679 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003680
3681#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003682 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003683 {
3684 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003685 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003686 {
3687 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3688 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3689 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003690 }
3691#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003692
Paul Bakker48916f92012-09-16 19:57:18 +00003693 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003694 {
Paul Bakker48916f92012-09-16 19:57:18 +00003695 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003696 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003697 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003698 }
Paul Bakker48916f92012-09-16 19:57:18 +00003699
Paul Bakkerc0463502013-02-14 11:19:38 +01003700 if( ssl->session )
3701 {
3702 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003703 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003704 ssl->session = NULL;
3705 }
3706
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003707#if defined(POLARSSL_SSL_ALPN)
3708 ssl->alpn_chosen = NULL;
3709#endif
3710
Paul Bakker48916f92012-09-16 19:57:18 +00003711 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3712 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003713
3714 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003715}
3716
Paul Bakkera503a632013-08-14 13:48:06 +02003717#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003718static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3719{
3720 aes_free( &tkeys->enc );
3721 aes_free( &tkeys->dec );
3722
3723 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3724}
3725
Paul Bakker7eb013f2011-10-06 12:37:39 +00003726/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003727 * Allocate and initialize ticket keys
3728 */
3729static int ssl_ticket_keys_init( ssl_context *ssl )
3730{
3731 int ret;
3732 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003733 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003734
3735 if( ssl->ticket_keys != NULL )
3736 return( 0 );
3737
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003738 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3739 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003740 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3741
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003742 aes_init( &tkeys->enc );
3743 aes_init( &tkeys->dec );
3744
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003745 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003746 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003747 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003748 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003749 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003750 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003751
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003752 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3753 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3754 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3755 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003756 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003757 polarssl_free( tkeys );
3758 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003759 }
3760
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003761 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003762 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003763 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003764 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003765 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003766 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003767
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003768 ssl->ticket_keys = tkeys;
3769
3770 return( 0 );
3771}
Paul Bakkera503a632013-08-14 13:48:06 +02003772#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003773
3774/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 * SSL set accessors
3776 */
3777void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3778{
3779 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003780
Paul Bakker606b4ba2013-08-14 16:52:14 +02003781#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003782 if( endpoint == SSL_IS_CLIENT )
3783 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003784#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003785}
3786
3787void ssl_set_authmode( ssl_context *ssl, int authmode )
3788{
3789 ssl->authmode = authmode;
3790}
3791
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003792#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003793void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003794 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003795 void *p_vrfy )
3796{
3797 ssl->f_vrfy = f_vrfy;
3798 ssl->p_vrfy = p_vrfy;
3799}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003800#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003801
Paul Bakker5121ce52009-01-03 21:22:43 +00003802void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003803 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003804 void *p_rng )
3805{
3806 ssl->f_rng = f_rng;
3807 ssl->p_rng = p_rng;
3808}
3809
3810void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003811 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003812 void *p_dbg )
3813{
3814 ssl->f_dbg = f_dbg;
3815 ssl->p_dbg = p_dbg;
3816}
3817
3818void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003819 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003820 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003821{
3822 ssl->f_recv = f_recv;
3823 ssl->f_send = f_send;
3824 ssl->p_recv = p_recv;
3825 ssl->p_send = p_send;
3826}
3827
Paul Bakker0a597072012-09-25 21:55:46 +00003828void ssl_set_session_cache( ssl_context *ssl,
3829 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3830 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003831{
Paul Bakker0a597072012-09-25 21:55:46 +00003832 ssl->f_get_cache = f_get_cache;
3833 ssl->p_get_cache = p_get_cache;
3834 ssl->f_set_cache = f_set_cache;
3835 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003836}
3837
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003838int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003839{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003840 int ret;
3841
3842 if( ssl == NULL ||
3843 session == NULL ||
3844 ssl->session_negotiate == NULL ||
3845 ssl->endpoint != SSL_IS_CLIENT )
3846 {
3847 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3848 }
3849
3850 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3851 return( ret );
3852
Paul Bakker0a597072012-09-25 21:55:46 +00003853 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003854
3855 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856}
3857
Paul Bakkerb68cad62012-08-23 08:34:18 +00003858void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003859{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003860 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3861 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3862 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3863 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3864}
3865
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003866void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3867 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003868 int major, int minor )
3869{
3870 if( major != SSL_MAJOR_VERSION_3 )
3871 return;
3872
3873 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3874 return;
3875
3876 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003877}
3878
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003879#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003880/* Add a new (empty) key_cert entry an return a pointer to it */
3881static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3882{
3883 ssl_key_cert *key_cert, *last;
3884
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003885 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3886 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003887 return( NULL );
3888
3889 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3890
3891 /* Append the new key_cert to the (possibly empty) current list */
3892 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003893 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003894 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003895 if( ssl->handshake != NULL )
3896 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003897 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003898 else
3899 {
3900 last = ssl->key_cert;
3901 while( last->next != NULL )
3902 last = last->next;
3903 last->next = key_cert;
3904 }
3905
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003906 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003907}
3908
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003909void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003910 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003911{
3912 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003913 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003914 ssl->peer_cn = peer_cn;
3915}
3916
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003917int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003918 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003919{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003920 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3921
3922 if( key_cert == NULL )
3923 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3924
3925 key_cert->cert = own_cert;
3926 key_cert->key = pk_key;
3927
3928 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003929}
3930
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003931#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003932int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003933 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003934{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003935 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003936 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003937
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003938 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003939 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3940
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003941 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3942 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003943 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003944
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003945 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003946
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003947 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003948 if( ret != 0 )
3949 return( ret );
3950
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003951 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003952 return( ret );
3953
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003954 key_cert->cert = own_cert;
3955 key_cert->key_own_alloc = 1;
3956
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003957 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003958}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003959#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003960
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003961int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003962 void *rsa_key,
3963 rsa_decrypt_func rsa_decrypt,
3964 rsa_sign_func rsa_sign,
3965 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003966{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003967 int ret;
3968 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003969
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003970 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003971 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3972
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003973 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3974 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003975 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003976
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003977 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003978
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003979 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3980 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3981 return( ret );
3982
3983 key_cert->cert = own_cert;
3984 key_cert->key_own_alloc = 1;
3985
3986 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003987}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003988#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003989
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003990#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003991int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3992 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003993{
Paul Bakker6db455e2013-09-18 17:29:31 +02003994 if( psk == NULL || psk_identity == NULL )
3995 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3996
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003997 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003998 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3999
Paul Bakker6db455e2013-09-18 17:29:31 +02004000 if( ssl->psk != NULL )
4001 {
4002 polarssl_free( ssl->psk );
4003 polarssl_free( ssl->psk_identity );
4004 }
4005
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004006 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004007 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004008
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004009 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004010 ssl->psk_identity = (unsigned char *)
4011 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004012
4013 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4014 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4015
4016 memcpy( ssl->psk, psk, ssl->psk_len );
4017 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004018
4019 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004020}
4021
4022void ssl_set_psk_cb( ssl_context *ssl,
4023 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4024 size_t),
4025 void *p_psk )
4026{
4027 ssl->f_psk = f_psk;
4028 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004029}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004030#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004031
Paul Bakker48916f92012-09-16 19:57:18 +00004032#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004033int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004034{
4035 int ret;
4036
Paul Bakker48916f92012-09-16 19:57:18 +00004037 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004038 {
4039 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4040 return( ret );
4041 }
4042
Paul Bakker48916f92012-09-16 19:57:18 +00004043 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 {
4045 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4046 return( ret );
4047 }
4048
4049 return( 0 );
4050}
4051
Paul Bakker1b57b062011-01-06 15:48:19 +00004052int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4053{
4054 int ret;
4055
Paul Bakker66d5d072014-06-17 16:39:18 +02004056 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004057 {
4058 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4059 return( ret );
4060 }
4061
Paul Bakker66d5d072014-06-17 16:39:18 +02004062 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004063 {
4064 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4065 return( ret );
4066 }
4067
4068 return( 0 );
4069}
Paul Bakker48916f92012-09-16 19:57:18 +00004070#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004071
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004072#if defined(POLARSSL_SSL_SET_CURVES)
4073/*
4074 * Set the allowed elliptic curves
4075 */
4076void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4077{
4078 ssl->curve_list = curve_list;
4079}
4080#endif
4081
Paul Bakker0be444a2013-08-27 21:55:01 +02004082#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004083int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004084{
4085 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004086 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004087
4088 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004089
4090 if( ssl->hostname_len + 1 == 0 )
4091 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4092
Paul Bakker6e339b52013-07-03 13:37:05 +02004093 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004094
Paul Bakkerb15b8512012-01-13 13:44:06 +00004095 if( ssl->hostname == NULL )
4096 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4097
Paul Bakker3c2122f2013-06-24 19:03:14 +02004098 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004099 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004100
Paul Bakker40ea7de2009-05-03 10:18:48 +00004101 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004102
4103 return( 0 );
4104}
4105
Paul Bakker5701cdc2012-09-27 21:49:42 +00004106void ssl_set_sni( ssl_context *ssl,
4107 int (*f_sni)(void *, ssl_context *,
4108 const unsigned char *, size_t),
4109 void *p_sni )
4110{
4111 ssl->f_sni = f_sni;
4112 ssl->p_sni = p_sni;
4113}
Paul Bakker0be444a2013-08-27 21:55:01 +02004114#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004115
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004116#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004117int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004118{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004119 size_t cur_len, tot_len;
4120 const char **p;
4121
4122 /*
4123 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4124 * truncated". Check lengths now rather than later.
4125 */
4126 tot_len = 0;
4127 for( p = protos; *p != NULL; p++ )
4128 {
4129 cur_len = strlen( *p );
4130 tot_len += cur_len;
4131
4132 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4133 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4134 }
4135
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004136 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004137
4138 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004139}
4140
4141const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4142{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004143 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004144}
4145#endif /* POLARSSL_SSL_ALPN */
4146
Paul Bakker490ecc82011-10-06 13:04:09 +00004147void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4148{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004149 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4150 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4151 {
4152 ssl->max_major_ver = major;
4153 ssl->max_minor_ver = minor;
4154 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004155}
4156
Paul Bakker1d29fb52012-09-28 13:28:45 +00004157void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4158{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004159 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4160 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4161 {
4162 ssl->min_major_ver = major;
4163 ssl->min_minor_ver = minor;
4164 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004165}
4166
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004167#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4168void ssl_set_fallback( ssl_context *ssl, char fallback )
4169{
4170 ssl->fallback = fallback;
4171}
4172#endif
4173
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004174#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4175void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4176{
4177 ssl->encrypt_then_mac = etm;
4178}
4179#endif
4180
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004181#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4182void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4183{
4184 ssl->extended_ms = ems;
4185}
4186#endif
4187
Paul Bakker05decb22013-08-15 13:33:48 +02004188#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004189int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4190{
Paul Bakker77e257e2013-12-16 15:29:52 +01004191 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004192 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004193 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004194 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004195 }
4196
4197 ssl->mfl_code = mfl_code;
4198
4199 return( 0 );
4200}
Paul Bakker05decb22013-08-15 13:33:48 +02004201#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004202
Paul Bakker1f2bc622013-08-15 13:45:55 +02004203#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004204int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004205{
4206 if( ssl->endpoint != SSL_IS_CLIENT )
4207 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4208
Paul Bakker8c1ede62013-07-19 14:14:37 +02004209 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004210
4211 return( 0 );
4212}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004213#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004214
Paul Bakker48916f92012-09-16 19:57:18 +00004215void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4216{
4217 ssl->disable_renegotiation = renegotiation;
4218}
4219
4220void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4221{
4222 ssl->allow_legacy_renegotiation = allow_legacy;
4223}
4224
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004225void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4226{
4227 ssl->renego_max_records = max_records;
4228}
4229
Paul Bakkera503a632013-08-14 13:48:06 +02004230#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004231int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4232{
4233 ssl->session_tickets = use_tickets;
4234
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004235 if( ssl->endpoint == SSL_IS_CLIENT )
4236 return( 0 );
4237
4238 if( ssl->f_rng == NULL )
4239 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4240
4241 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004242}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004243
4244void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4245{
4246 ssl->ticket_lifetime = lifetime;
4247}
Paul Bakkera503a632013-08-14 13:48:06 +02004248#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004249
Paul Bakker5121ce52009-01-03 21:22:43 +00004250/*
4251 * SSL get accessors
4252 */
Paul Bakker23986e52011-04-24 08:57:21 +00004253size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004254{
4255 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4256}
4257
Paul Bakkerff60ee62010-03-16 21:09:09 +00004258int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004259{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004260 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004261}
4262
Paul Bakkere3166ce2011-01-27 17:40:50 +00004263const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004264{
Paul Bakker926c8e42013-03-06 10:23:34 +01004265 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004266 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004267
Paul Bakkere3166ce2011-01-27 17:40:50 +00004268 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004269}
4270
Paul Bakker43ca69c2011-01-15 17:35:19 +00004271const char *ssl_get_version( const ssl_context *ssl )
4272{
4273 switch( ssl->minor_ver )
4274 {
4275 case SSL_MINOR_VERSION_0:
4276 return( "SSLv3.0" );
4277
4278 case SSL_MINOR_VERSION_1:
4279 return( "TLSv1.0" );
4280
4281 case SSL_MINOR_VERSION_2:
4282 return( "TLSv1.1" );
4283
Paul Bakker1ef83d62012-04-11 12:09:53 +00004284 case SSL_MINOR_VERSION_3:
4285 return( "TLSv1.2" );
4286
Paul Bakker43ca69c2011-01-15 17:35:19 +00004287 default:
4288 break;
4289 }
4290 return( "unknown" );
4291}
4292
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004293#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004294const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004295{
4296 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004297 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004298
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004299 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004300}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004301#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004302
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004303int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4304{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004305 if( ssl == NULL ||
4306 dst == NULL ||
4307 ssl->session == NULL ||
4308 ssl->endpoint != SSL_IS_CLIENT )
4309 {
4310 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4311 }
4312
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004313 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004314}
4315
Paul Bakker5121ce52009-01-03 21:22:43 +00004316/*
Paul Bakker1961b702013-01-25 14:49:24 +01004317 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004318 */
Paul Bakker1961b702013-01-25 14:49:24 +01004319int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004320{
Paul Bakker40e46942009-01-03 21:51:57 +00004321 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004322
Paul Bakker40e46942009-01-03 21:51:57 +00004323#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004325 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004326#endif
4327
Paul Bakker40e46942009-01-03 21:51:57 +00004328#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004329 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004330 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004331#endif
4332
Paul Bakker1961b702013-01-25 14:49:24 +01004333 return( ret );
4334}
4335
4336/*
4337 * Perform the SSL handshake
4338 */
4339int ssl_handshake( ssl_context *ssl )
4340{
4341 int ret = 0;
4342
4343 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4344
4345 while( ssl->state != SSL_HANDSHAKE_OVER )
4346 {
4347 ret = ssl_handshake_step( ssl );
4348
4349 if( ret != 0 )
4350 break;
4351 }
4352
Paul Bakker5121ce52009-01-03 21:22:43 +00004353 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4354
4355 return( ret );
4356}
4357
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004358#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004359/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004360 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004361 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004362static int ssl_write_hello_request( ssl_context *ssl )
4363{
4364 int ret;
4365
4366 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4367
4368 ssl->out_msglen = 4;
4369 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4370 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4371
4372 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4373 {
4374 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4375 return( ret );
4376 }
4377
4378 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4379
4380 return( 0 );
4381}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004382#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004383
4384/*
4385 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004386 * - any side: calling ssl_renegotiate(),
4387 * - client: receiving a HelloRequest during ssl_read(),
4388 * - server: receiving any handshake message on server during ssl_read() after
4389 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004390 * If the handshake doesn't complete due to waiting for I/O, it will continue
4391 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004392 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004393static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004394{
4395 int ret;
4396
4397 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4398
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004399 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4400 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004401
4402 ssl->state = SSL_HELLO_REQUEST;
4403 ssl->renegotiation = SSL_RENEGOTIATION;
4404
Paul Bakker48916f92012-09-16 19:57:18 +00004405 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4406 {
4407 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4408 return( ret );
4409 }
4410
4411 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4412
4413 return( 0 );
4414}
4415
4416/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004417 * Renegotiate current connection on client,
4418 * or request renegotiation on server
4419 */
4420int ssl_renegotiate( ssl_context *ssl )
4421{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004422 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004423
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004424#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004425 /* On server, just send the request */
4426 if( ssl->endpoint == SSL_IS_SERVER )
4427 {
4428 if( ssl->state != SSL_HANDSHAKE_OVER )
4429 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4430
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004431 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4432
4433 /* Did we already try/start sending HelloRequest? */
4434 if( ssl->out_left != 0 )
4435 return( ssl_flush_output( ssl ) );
4436
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004437 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004438 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004439#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004440
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004441#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004442 /*
4443 * On client, either start the renegotiation process or,
4444 * if already in progress, continue the handshake
4445 */
4446 if( ssl->renegotiation != SSL_RENEGOTIATION )
4447 {
4448 if( ssl->state != SSL_HANDSHAKE_OVER )
4449 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4450
4451 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4452 {
4453 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4454 return( ret );
4455 }
4456 }
4457 else
4458 {
4459 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4460 {
4461 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4462 return( ret );
4463 }
4464 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004465#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004466
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004467 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004468}
4469
4470/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004471 * Receive application data decrypted from the SSL layer
4472 */
Paul Bakker23986e52011-04-24 08:57:21 +00004473int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004474{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004475 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004476 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004477
4478 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4479
4480 if( ssl->state != SSL_HANDSHAKE_OVER )
4481 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004482 ret = ssl_handshake( ssl );
4483 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4484 {
4485 record_read = 1;
4486 }
4487 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004488 {
4489 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4490 return( ret );
4491 }
4492 }
4493
4494 if( ssl->in_offt == NULL )
4495 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004496 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004497 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004498 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4499 {
4500 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4501 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004502
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004503 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4504 return( ret );
4505 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004506 }
4507
4508 if( ssl->in_msglen == 0 &&
4509 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4510 {
4511 /*
4512 * OpenSSL sends empty messages to randomize the IV
4513 */
4514 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4515 {
Paul Bakker831a7552011-05-18 13:32:51 +00004516 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4517 return( 0 );
4518
Paul Bakker5121ce52009-01-03 21:22:43 +00004519 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4520 return( ret );
4521 }
4522 }
4523
Paul Bakker48916f92012-09-16 19:57:18 +00004524 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4525 {
4526 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4527
4528 if( ssl->endpoint == SSL_IS_CLIENT &&
4529 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4530 ssl->in_hslen != 4 ) )
4531 {
4532 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4533 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4534 }
4535
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004536 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4537 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004538 ssl->allow_legacy_renegotiation ==
4539 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004540 {
4541 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4542
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004543#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004544 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004545 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004546 /*
4547 * SSLv3 does not have a "no_renegotiation" alert
4548 */
4549 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4550 return( ret );
4551 }
4552 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004553#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004554#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4555 defined(POLARSSL_SSL_PROTO_TLS1_2)
4556 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004557 {
4558 if( ( ret = ssl_send_alert_message( ssl,
4559 SSL_ALERT_LEVEL_WARNING,
4560 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4561 {
4562 return( ret );
4563 }
Paul Bakker48916f92012-09-16 19:57:18 +00004564 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004565 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004566#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4567 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004568 {
4569 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004570 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004571 }
Paul Bakker48916f92012-09-16 19:57:18 +00004572 }
4573 else
4574 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004575 ret = ssl_start_renegotiation( ssl );
4576 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4577 {
4578 record_read = 1;
4579 }
4580 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004581 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004582 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004583 return( ret );
4584 }
Paul Bakker48916f92012-09-16 19:57:18 +00004585 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004586
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004587 /* If a non-handshake record was read during renego, fallthrough,
4588 * else tell the user they should call ssl_read() again */
4589 if( ! record_read )
4590 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004591 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004592 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4593 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004594 ssl->renego_records_seen++;
4595
4596 if( ssl->renego_max_records >= 0 &&
4597 ssl->renego_records_seen > ssl->renego_max_records )
4598 {
4599 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4600 "but not honored by client" ) );
4601 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4602 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004603 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004604
4605 /* Fatal and closure alerts handled by ssl_read_record() */
4606 if( ssl->in_msgtype == SSL_MSG_ALERT )
4607 {
4608 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4609 return( POLARSSL_ERR_NET_WANT_READ );
4610 }
4611
4612 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004613 {
4614 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004615 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004616 }
4617
4618 ssl->in_offt = ssl->in_msg;
4619 }
4620
4621 n = ( len < ssl->in_msglen )
4622 ? len : ssl->in_msglen;
4623
4624 memcpy( buf, ssl->in_offt, n );
4625 ssl->in_msglen -= n;
4626
4627 if( ssl->in_msglen == 0 )
4628 /* all bytes consumed */
4629 ssl->in_offt = NULL;
4630 else
4631 /* more data available */
4632 ssl->in_offt += n;
4633
4634 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4635
Paul Bakker23986e52011-04-24 08:57:21 +00004636 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004637}
4638
4639/*
4640 * Send application data to be encrypted by the SSL layer
4641 */
Paul Bakker23986e52011-04-24 08:57:21 +00004642int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004643{
Paul Bakker23986e52011-04-24 08:57:21 +00004644 int ret;
4645 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004646 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004647
4648 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4649
4650 if( ssl->state != SSL_HANDSHAKE_OVER )
4651 {
4652 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4653 {
4654 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4655 return( ret );
4656 }
4657 }
4658
Paul Bakker05decb22013-08-15 13:33:48 +02004659#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004660 /*
4661 * Assume mfl_code is correct since it was checked when set
4662 */
4663 max_len = mfl_code_to_length[ssl->mfl_code];
4664
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004665 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004666 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004667 */
4668 if( ssl->session_out != NULL &&
4669 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4670 {
4671 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4672 }
Paul Bakker05decb22013-08-15 13:33:48 +02004673#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004674
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004675 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004676
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 if( ssl->out_left != 0 )
4678 {
4679 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4680 {
4681 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4682 return( ret );
4683 }
4684 }
Paul Bakker887bd502011-06-08 13:10:54 +00004685 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004686 {
Paul Bakker887bd502011-06-08 13:10:54 +00004687 ssl->out_msglen = n;
4688 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4689 memcpy( ssl->out_msg, buf, n );
4690
4691 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4692 {
4693 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4694 return( ret );
4695 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004696 }
4697
4698 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4699
Paul Bakker23986e52011-04-24 08:57:21 +00004700 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004701}
4702
4703/*
4704 * Notify the peer that the connection is being closed
4705 */
4706int ssl_close_notify( ssl_context *ssl )
4707{
4708 int ret;
4709
4710 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4711
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004712 if( ssl->out_left != 0 )
4713 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004714
4715 if( ssl->state == SSL_HANDSHAKE_OVER )
4716 {
Paul Bakker48916f92012-09-16 19:57:18 +00004717 if( ( ret = ssl_send_alert_message( ssl,
4718 SSL_ALERT_LEVEL_WARNING,
4719 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004720 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004721 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 return( ret );
4723 }
4724 }
4725
4726 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4727
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004728 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004729}
4730
Paul Bakker48916f92012-09-16 19:57:18 +00004731void ssl_transform_free( ssl_transform *transform )
4732{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004733 if( transform == NULL )
4734 return;
4735
Paul Bakker48916f92012-09-16 19:57:18 +00004736#if defined(POLARSSL_ZLIB_SUPPORT)
4737 deflateEnd( &transform->ctx_deflate );
4738 inflateEnd( &transform->ctx_inflate );
4739#endif
4740
Paul Bakker84bbeb52014-07-01 14:53:22 +02004741 cipher_free( &transform->cipher_ctx_enc );
4742 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004743
Paul Bakker84bbeb52014-07-01 14:53:22 +02004744 md_free( &transform->md_ctx_enc );
4745 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004746
Paul Bakker34617722014-06-13 17:20:13 +02004747 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004748}
4749
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004750#if defined(POLARSSL_X509_CRT_PARSE_C)
4751static void ssl_key_cert_free( ssl_key_cert *key_cert )
4752{
4753 ssl_key_cert *cur = key_cert, *next;
4754
4755 while( cur != NULL )
4756 {
4757 next = cur->next;
4758
4759 if( cur->key_own_alloc )
4760 {
4761 pk_free( cur->key );
4762 polarssl_free( cur->key );
4763 }
4764 polarssl_free( cur );
4765
4766 cur = next;
4767 }
4768}
4769#endif /* POLARSSL_X509_CRT_PARSE_C */
4770
Paul Bakker48916f92012-09-16 19:57:18 +00004771void ssl_handshake_free( ssl_handshake_params *handshake )
4772{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004773 if( handshake == NULL )
4774 return;
4775
Paul Bakker48916f92012-09-16 19:57:18 +00004776#if defined(POLARSSL_DHM_C)
4777 dhm_free( &handshake->dhm_ctx );
4778#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004779#if defined(POLARSSL_ECDH_C)
4780 ecdh_free( &handshake->ecdh_ctx );
4781#endif
4782
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004783#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004784 /* explicit void pointer cast for buggy MS compiler */
4785 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004786#endif
4787
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004788#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4789 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4790 /*
4791 * Free only the linked list wrapper, not the keys themselves
4792 * since the belong to the SNI callback
4793 */
4794 if( handshake->sni_key_cert != NULL )
4795 {
4796 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4797
4798 while( cur != NULL )
4799 {
4800 next = cur->next;
4801 polarssl_free( cur );
4802 cur = next;
4803 }
4804 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004805#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004806
Paul Bakker34617722014-06-13 17:20:13 +02004807 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004808}
4809
4810void ssl_session_free( ssl_session *session )
4811{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004812 if( session == NULL )
4813 return;
4814
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004815#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004816 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004817 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004818 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004819 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004820 }
Paul Bakkered27a042013-04-18 22:46:23 +02004821#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004822
Paul Bakkera503a632013-08-14 13:48:06 +02004823#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004824 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004825#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004826
Paul Bakker34617722014-06-13 17:20:13 +02004827 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004828}
4829
Paul Bakker5121ce52009-01-03 21:22:43 +00004830/*
4831 * Free an SSL context
4832 */
4833void ssl_free( ssl_context *ssl )
4834{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004835 if( ssl == NULL )
4836 return;
4837
Paul Bakker5121ce52009-01-03 21:22:43 +00004838 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4839
Paul Bakker5121ce52009-01-03 21:22:43 +00004840 if( ssl->out_ctr != NULL )
4841 {
Paul Bakker34617722014-06-13 17:20:13 +02004842 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004843 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004844 }
4845
4846 if( ssl->in_ctr != NULL )
4847 {
Paul Bakker34617722014-06-13 17:20:13 +02004848 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004849 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004850 }
4851
Paul Bakker16770332013-10-11 09:59:44 +02004852#if defined(POLARSSL_ZLIB_SUPPORT)
4853 if( ssl->compress_buf != NULL )
4854 {
Paul Bakker34617722014-06-13 17:20:13 +02004855 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004856 polarssl_free( ssl->compress_buf );
4857 }
4858#endif
4859
Paul Bakker40e46942009-01-03 21:51:57 +00004860#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004861 mpi_free( &ssl->dhm_P );
4862 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004863#endif
4864
Paul Bakker48916f92012-09-16 19:57:18 +00004865 if( ssl->transform )
4866 {
4867 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004868 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004869 }
4870
4871 if( ssl->handshake )
4872 {
4873 ssl_handshake_free( ssl->handshake );
4874 ssl_transform_free( ssl->transform_negotiate );
4875 ssl_session_free( ssl->session_negotiate );
4876
Paul Bakker6e339b52013-07-03 13:37:05 +02004877 polarssl_free( ssl->handshake );
4878 polarssl_free( ssl->transform_negotiate );
4879 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004880 }
4881
Paul Bakkerc0463502013-02-14 11:19:38 +01004882 if( ssl->session )
4883 {
4884 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004885 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004886 }
4887
Paul Bakkera503a632013-08-14 13:48:06 +02004888#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004889 if( ssl->ticket_keys )
4890 {
4891 ssl_ticket_keys_free( ssl->ticket_keys );
4892 polarssl_free( ssl->ticket_keys );
4893 }
Paul Bakkera503a632013-08-14 13:48:06 +02004894#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004895
Paul Bakker0be444a2013-08-27 21:55:01 +02004896#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004897 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004898 {
Paul Bakker34617722014-06-13 17:20:13 +02004899 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004900 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004901 ssl->hostname_len = 0;
4902 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004903#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004904
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004905#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004906 if( ssl->psk != NULL )
4907 {
Paul Bakker34617722014-06-13 17:20:13 +02004908 polarssl_zeroize( ssl->psk, ssl->psk_len );
4909 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004910 polarssl_free( ssl->psk );
4911 polarssl_free( ssl->psk_identity );
4912 ssl->psk_len = 0;
4913 ssl->psk_identity_len = 0;
4914 }
4915#endif
4916
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004917#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004918 ssl_key_cert_free( ssl->key_cert );
4919#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004920
Paul Bakker05ef8352012-05-08 09:17:57 +00004921#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4922 if( ssl_hw_record_finish != NULL )
4923 {
4924 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4925 ssl_hw_record_finish( ssl );
4926 }
4927#endif
4928
Paul Bakker5121ce52009-01-03 21:22:43 +00004929 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004930
Paul Bakker86f04f42013-02-14 11:20:09 +01004931 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004932 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004933}
4934
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004935#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004936/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004937 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004938 */
4939unsigned char ssl_sig_from_pk( pk_context *pk )
4940{
4941#if defined(POLARSSL_RSA_C)
4942 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4943 return( SSL_SIG_RSA );
4944#endif
4945#if defined(POLARSSL_ECDSA_C)
4946 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4947 return( SSL_SIG_ECDSA );
4948#endif
4949 return( SSL_SIG_ANON );
4950}
4951
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004952pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4953{
4954 switch( sig )
4955 {
4956#if defined(POLARSSL_RSA_C)
4957 case SSL_SIG_RSA:
4958 return( POLARSSL_PK_RSA );
4959#endif
4960#if defined(POLARSSL_ECDSA_C)
4961 case SSL_SIG_ECDSA:
4962 return( POLARSSL_PK_ECDSA );
4963#endif
4964 default:
4965 return( POLARSSL_PK_NONE );
4966 }
4967}
Paul Bakker9af723c2014-05-01 13:03:14 +02004968#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004969
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004970/*
4971 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4972 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004973md_type_t ssl_md_alg_from_hash( unsigned char hash )
4974{
4975 switch( hash )
4976 {
4977#if defined(POLARSSL_MD5_C)
4978 case SSL_HASH_MD5:
4979 return( POLARSSL_MD_MD5 );
4980#endif
4981#if defined(POLARSSL_SHA1_C)
4982 case SSL_HASH_SHA1:
4983 return( POLARSSL_MD_SHA1 );
4984#endif
4985#if defined(POLARSSL_SHA256_C)
4986 case SSL_HASH_SHA224:
4987 return( POLARSSL_MD_SHA224 );
4988 case SSL_HASH_SHA256:
4989 return( POLARSSL_MD_SHA256 );
4990#endif
4991#if defined(POLARSSL_SHA512_C)
4992 case SSL_HASH_SHA384:
4993 return( POLARSSL_MD_SHA384 );
4994 case SSL_HASH_SHA512:
4995 return( POLARSSL_MD_SHA512 );
4996#endif
4997 default:
4998 return( POLARSSL_MD_NONE );
4999 }
5000}
5001
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005002#if defined(POLARSSL_SSL_SET_CURVES)
5003/*
5004 * Check is a curve proposed by the peer is in our list.
5005 * Return 1 if we're willing to use it, 0 otherwise.
5006 */
5007int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5008{
5009 const ecp_group_id *gid;
5010
5011 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5012 if( *gid == grp_id )
5013 return( 1 );
5014
5015 return( 0 );
5016}
Paul Bakker9af723c2014-05-01 13:03:14 +02005017#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005018
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005019#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005020int ssl_check_cert_usage( const x509_crt *cert,
5021 const ssl_ciphersuite_t *ciphersuite,
5022 int cert_endpoint )
5023{
5024#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5025 int usage = 0;
5026#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005027#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5028 const char *ext_oid;
5029 size_t ext_len;
5030#endif
5031
5032#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5033 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5034 ((void) cert);
5035 ((void) cert_endpoint);
5036#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005037
5038#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5039 if( cert_endpoint == SSL_IS_SERVER )
5040 {
5041 /* Server part of the key exchange */
5042 switch( ciphersuite->key_exchange )
5043 {
5044 case POLARSSL_KEY_EXCHANGE_RSA:
5045 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5046 usage = KU_KEY_ENCIPHERMENT;
5047 break;
5048
5049 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5050 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5051 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5052 usage = KU_DIGITAL_SIGNATURE;
5053 break;
5054
5055 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5056 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5057 usage = KU_KEY_AGREEMENT;
5058 break;
5059
5060 /* Don't use default: we want warnings when adding new values */
5061 case POLARSSL_KEY_EXCHANGE_NONE:
5062 case POLARSSL_KEY_EXCHANGE_PSK:
5063 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5064 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5065 usage = 0;
5066 }
5067 }
5068 else
5069 {
5070 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5071 usage = KU_DIGITAL_SIGNATURE;
5072 }
5073
5074 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5075 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005076#else
5077 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005078#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5079
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005080#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5081 if( cert_endpoint == SSL_IS_SERVER )
5082 {
5083 ext_oid = OID_SERVER_AUTH;
5084 ext_len = OID_SIZE( OID_SERVER_AUTH );
5085 }
5086 else
5087 {
5088 ext_oid = OID_CLIENT_AUTH;
5089 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5090 }
5091
5092 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5093 return( -1 );
5094#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5095
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005096 return( 0 );
5097}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005098#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005099
5100#endif /* POLARSSL_SSL_TLS_C */