blob: 8c981cf36ebbcc6be5fd71971a22c1e1a69c96e7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
424 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200473#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
474 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200475 {
476 unsigned char session_hash[48];
477 size_t hash_len;
478
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200479 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200480
481 ssl->handshake->calc_verify( ssl, session_hash );
482
483#if defined(POLARSSL_SSL_PROTO_TLS1_2)
484 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
485 {
486#if defined(POLARSSL_SHA512_C)
487 if( ssl->transform_negotiate->ciphersuite_info->mac ==
488 POLARSSL_MD_SHA384 )
489 {
490 hash_len = 48;
491 }
492 else
493#endif
494 hash_len = 32;
495 }
496 else
497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
498 hash_len = 36;
499
500 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
501
502 handshake->tls_prf( handshake->premaster, handshake->pmslen,
503 "extended master secret",
504 session_hash, hash_len, session->master, 48 );
505
506 }
507 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200508#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf( handshake->premaster, handshake->pmslen,
510 "master secret",
511 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200513
Paul Bakker34617722014-06-13 17:20:13 +0200514 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 }
516 else
517 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
518
519 /*
520 * Swap the client and server random values.
521 */
Paul Bakker48916f92012-09-16 19:57:18 +0000522 memcpy( tmp, handshake->randbytes, 64 );
523 memcpy( handshake->randbytes, tmp + 32, 32 );
524 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200525 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 /*
528 * SSLv3:
529 * key block =
530 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
532 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
533 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
534 * ...
535 *
536 * TLSv1:
537 * key block = PRF( master, "key expansion", randbytes )
538 */
Paul Bakker48916f92012-09-16 19:57:18 +0000539 handshake->tls_prf( session->master, 48, "key expansion",
540 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
543 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
544 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
545 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
547
Paul Bakker34617722014-06-13 17:20:13 +0200548 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 /*
551 * Determine the appropriate key, IV and MAC length.
552 */
Paul Bakker68884e32013-01-07 18:20:04 +0100553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 transform->keylen = cipher_info->key_length / 8;
555
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200556 if( cipher_info->mode == POLARSSL_MODE_GCM ||
557 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200559 transform->maclen = 0;
560
Paul Bakker68884e32013-01-07 18:20:04 +0100561 transform->ivlen = 12;
562 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200563
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200564 /* Minimum length is expicit IV + tag */
565 transform->minlen = transform->ivlen - transform->fixed_ivlen
566 + ( transform->ciphersuite_info->flags &
567 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100568 }
569 else
570 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200571 int ret;
572
573 /* Initialize HMAC contexts */
574 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
575 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100576 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
578 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100579 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200581 /* Get MAC length */
582 transform->maclen = md_get_size( md_info );
583
584#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
585 /*
586 * If HMAC is to be truncated, we shall keep the leftmost bytes,
587 * (rfc 6066 page 13 or rfc 2104 section 4),
588 * so we only need to adjust the length here.
589 */
590 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
591 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
592#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
593
594 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100595 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200597 /* Minimum length */
598 if( cipher_info->mode == POLARSSL_MODE_STREAM )
599 transform->minlen = transform->maclen;
600 else
Paul Bakker68884e32013-01-07 18:20:04 +0100601 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200602 /*
603 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100604 * 1. if EtM is in use: one block plus MAC
605 * otherwise: * first multiple of blocklen greater than maclen
606 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200607 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100608#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
609 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
610 {
611 transform->minlen = transform->maclen
612 + cipher_info->block_size;
613 }
614 else
615#endif
616 {
617 transform->minlen = transform->maclen
618 + cipher_info->block_size
619 - transform->maclen % cipher_info->block_size;
620 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200621
622#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
623 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
624 ssl->minor_ver == SSL_MINOR_VERSION_1 )
625 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100626 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200627#endif
628#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
629 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
630 ssl->minor_ver == SSL_MINOR_VERSION_3 )
631 {
632 transform->minlen += transform->ivlen;
633 }
634 else
635#endif
636 {
637 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
638 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
639 }
Paul Bakker68884e32013-01-07 18:20:04 +0100640 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 }
642
643 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000644 transform->keylen, transform->minlen, transform->ivlen,
645 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 /*
648 * Finally setup the cipher contexts, IVs and MAC secrets.
649 */
650 if( ssl->endpoint == SSL_IS_CLIENT )
651 {
Paul Bakker48916f92012-09-16 19:57:18 +0000652 key1 = keyblk + transform->maclen * 2;
653 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Paul Bakker68884e32013-01-07 18:20:04 +0100655 mac_enc = keyblk;
656 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000658 /*
659 * This is not used in TLS v1.1.
660 */
Paul Bakker48916f92012-09-16 19:57:18 +0000661 iv_copy_len = ( transform->fixed_ivlen ) ?
662 transform->fixed_ivlen : transform->ivlen;
663 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
664 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000665 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 }
667 else
668 {
Paul Bakker48916f92012-09-16 19:57:18 +0000669 key1 = keyblk + transform->maclen * 2 + transform->keylen;
670 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Paul Bakker68884e32013-01-07 18:20:04 +0100672 mac_enc = keyblk + transform->maclen;
673 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000675 /*
676 * This is not used in TLS v1.1.
677 */
Paul Bakker48916f92012-09-16 19:57:18 +0000678 iv_copy_len = ( transform->fixed_ivlen ) ?
679 transform->fixed_ivlen : transform->ivlen;
680 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
681 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000682 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 }
684
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200685#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100686 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
687 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100688 if( transform->maclen > sizeof transform->mac_enc )
689 {
690 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200691 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100692 }
693
Paul Bakker68884e32013-01-07 18:20:04 +0100694 memcpy( transform->mac_enc, mac_enc, transform->maclen );
695 memcpy( transform->mac_dec, mac_dec, transform->maclen );
696 }
697 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200698#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200699#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
700 defined(POLARSSL_SSL_PROTO_TLS1_2)
701 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100702 {
703 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
704 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
705 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200706 else
707#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200708 {
709 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200710 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200711 }
Paul Bakker68884e32013-01-07 18:20:04 +0100712
Paul Bakker05ef8352012-05-08 09:17:57 +0000713#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200714 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000715 {
716 int ret = 0;
717
718 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
719
Paul Bakker07eb38b2012-12-19 14:42:06 +0100720 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
721 transform->iv_enc, transform->iv_dec,
722 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100723 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100724 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000725 {
726 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200727 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000728 }
729 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200730#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000731
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200732 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
733 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200735 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
736 return( ret );
737 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200738
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200739 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
740 cipher_info ) ) != 0 )
741 {
742 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
743 return( ret );
744 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200745
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200746 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
747 cipher_info->key_length,
748 POLARSSL_ENCRYPT ) ) != 0 )
749 {
750 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
751 return( ret );
752 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200753
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200754 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
755 cipher_info->key_length,
756 POLARSSL_DECRYPT ) ) != 0 )
757 {
758 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
759 return( ret );
760 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200761
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200762#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200763 if( cipher_info->mode == POLARSSL_MODE_CBC )
764 {
765 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
766 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200767 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200768 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
769 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200770 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200771
772 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
773 POLARSSL_PADDING_NONE ) ) != 0 )
774 {
775 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
776 return( ret );
777 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200779#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000780
Paul Bakker34617722014-06-13 17:20:13 +0200781 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Paul Bakker2770fbd2012-07-03 13:30:23 +0000783#if defined(POLARSSL_ZLIB_SUPPORT)
784 // Initialize compression
785 //
Paul Bakker48916f92012-09-16 19:57:18 +0000786 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000787 {
Paul Bakker16770332013-10-11 09:59:44 +0200788 if( ssl->compress_buf == NULL )
789 {
790 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
791 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
792 if( ssl->compress_buf == NULL )
793 {
794 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
795 SSL_BUFFER_LEN ) );
796 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
797 }
798 }
799
Paul Bakker2770fbd2012-07-03 13:30:23 +0000800 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
801
Paul Bakker48916f92012-09-16 19:57:18 +0000802 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
803 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000804
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200805 if( deflateInit( &transform->ctx_deflate,
806 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000807 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000808 {
809 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
810 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
811 }
812 }
813#endif /* POLARSSL_ZLIB_SUPPORT */
814
Paul Bakker5121ce52009-01-03 21:22:43 +0000815 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
816
817 return( 0 );
818}
819
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200820#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000821void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000822{
823 md5_context md5;
824 sha1_context sha1;
825 unsigned char pad_1[48];
826 unsigned char pad_2[48];
827
Paul Bakker380da532012-04-18 16:10:25 +0000828 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Paul Bakker48916f92012-09-16 19:57:18 +0000830 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
831 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000832
Paul Bakker380da532012-04-18 16:10:25 +0000833 memset( pad_1, 0x36, 48 );
834 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Paul Bakker48916f92012-09-16 19:57:18 +0000836 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000837 md5_update( &md5, pad_1, 48 );
838 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Paul Bakker380da532012-04-18 16:10:25 +0000840 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000841 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000842 md5_update( &md5, pad_2, 48 );
843 md5_update( &md5, hash, 16 );
844 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Paul Bakker48916f92012-09-16 19:57:18 +0000846 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000847 sha1_update( &sha1, pad_1, 40 );
848 sha1_finish( &sha1, hash + 16 );
849
850 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000851 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000852 sha1_update( &sha1, pad_2, 40 );
853 sha1_update( &sha1, hash + 16, 20 );
854 sha1_finish( &sha1, hash + 16 );
855
856 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
857 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
858
Paul Bakker5b4af392014-06-26 12:09:34 +0200859 md5_free( &md5 );
860 sha1_free( &sha1 );
861
Paul Bakker380da532012-04-18 16:10:25 +0000862 return;
863}
Paul Bakker9af723c2014-05-01 13:03:14 +0200864#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000865
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200866#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000867void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
868{
869 md5_context md5;
870 sha1_context sha1;
871
872 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
873
Paul Bakker48916f92012-09-16 19:57:18 +0000874 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
875 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000876
Paul Bakker48916f92012-09-16 19:57:18 +0000877 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000878 sha1_finish( &sha1, hash + 16 );
879
880 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
881 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
882
Paul Bakker5b4af392014-06-26 12:09:34 +0200883 md5_free( &md5 );
884 sha1_free( &sha1 );
885
Paul Bakker380da532012-04-18 16:10:25 +0000886 return;
887}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200888#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000889
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200890#if defined(POLARSSL_SSL_PROTO_TLS1_2)
891#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000892void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
893{
Paul Bakker9e36f042013-06-30 14:34:05 +0200894 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000895
896 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
897
Paul Bakker9e36f042013-06-30 14:34:05 +0200898 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
899 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000900
901 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
902 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
903
Paul Bakker5b4af392014-06-26 12:09:34 +0200904 sha256_free( &sha256 );
905
Paul Bakker380da532012-04-18 16:10:25 +0000906 return;
907}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200908#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000909
Paul Bakker9e36f042013-06-30 14:34:05 +0200910#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000911void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
912{
Paul Bakker9e36f042013-06-30 14:34:05 +0200913 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000914
915 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
916
Paul Bakker9e36f042013-06-30 14:34:05 +0200917 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
918 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
Paul Bakkerca4ab492012-04-18 14:23:57 +0000920 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
922
Paul Bakker5b4af392014-06-26 12:09:34 +0200923 sha512_free( &sha512 );
924
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 return;
926}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200927#endif /* POLARSSL_SHA512_C */
928#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200930#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200931int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
932{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200933 unsigned char *p = ssl->handshake->premaster;
934 unsigned char *end = p + sizeof( ssl->handshake->premaster );
935
936 /*
937 * PMS = struct {
938 * opaque other_secret<0..2^16-1>;
939 * opaque psk<0..2^16-1>;
940 * };
941 * with "other_secret" depending on the particular key exchange
942 */
943#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
944 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
945 {
946 if( end - p < 2 + (int) ssl->psk_len )
947 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
948
949 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
950 *(p++) = (unsigned char)( ssl->psk_len );
951 p += ssl->psk_len;
952 }
953 else
954#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200955#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
956 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
957 {
958 /*
959 * other_secret already set by the ClientKeyExchange message,
960 * and is 48 bytes long
961 */
962 *p++ = 0;
963 *p++ = 48;
964 p += 48;
965 }
966 else
967#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200968#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
969 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
970 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200971 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200972 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200973
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200974 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200975 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200976 p + 2, &len,
977 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200978 {
979 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
980 return( ret );
981 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200982 *(p++) = (unsigned char)( len >> 8 );
983 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200984 p += len;
985
986 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
987 }
988 else
989#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
990#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
991 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
992 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200993 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200994 size_t zlen;
995
996 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200997 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200998 ssl->f_rng, ssl->p_rng ) ) != 0 )
999 {
1000 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1001 return( ret );
1002 }
1003
1004 *(p++) = (unsigned char)( zlen >> 8 );
1005 *(p++) = (unsigned char)( zlen );
1006 p += zlen;
1007
1008 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1009 }
1010 else
1011#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1012 {
1013 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001014 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001015 }
1016
1017 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001018 if( end - p < 2 + (int) ssl->psk_len )
1019 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1020
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001021 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1022 *(p++) = (unsigned char)( ssl->psk_len );
1023 memcpy( p, ssl->psk, ssl->psk_len );
1024 p += ssl->psk_len;
1025
1026 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1027
1028 return( 0 );
1029}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001030#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001031
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001032#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001033/*
1034 * SSLv3.0 MAC functions
1035 */
Paul Bakker68884e32013-01-07 18:20:04 +01001036static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1037 unsigned char *buf, size_t len,
1038 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001039{
1040 unsigned char header[11];
1041 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001042 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001043 int md_size = md_get_size( md_ctx->md_info );
1044 int md_type = md_get_type( md_ctx->md_info );
1045
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001046 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001047 if( md_type == POLARSSL_MD_MD5 )
1048 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001049 else
Paul Bakker68884e32013-01-07 18:20:04 +01001050 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001051
1052 memcpy( header, ctr, 8 );
1053 header[ 8] = (unsigned char) type;
1054 header[ 9] = (unsigned char)( len >> 8 );
1055 header[10] = (unsigned char)( len );
1056
Paul Bakker68884e32013-01-07 18:20:04 +01001057 memset( padding, 0x36, padlen );
1058 md_starts( md_ctx );
1059 md_update( md_ctx, secret, md_size );
1060 md_update( md_ctx, padding, padlen );
1061 md_update( md_ctx, header, 11 );
1062 md_update( md_ctx, buf, len );
1063 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001064
Paul Bakker68884e32013-01-07 18:20:04 +01001065 memset( padding, 0x5C, padlen );
1066 md_starts( md_ctx );
1067 md_update( md_ctx, secret, md_size );
1068 md_update( md_ctx, padding, padlen );
1069 md_update( md_ctx, buf + len, md_size );
1070 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001071}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001072#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001073
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001074#define MAC_NONE 0
1075#define MAC_PLAINTEXT 1
1076#define MAC_CIPHERTEXT 2
1077
1078/*
1079 * Is MAC applied on ciphertext, cleartext or not at all?
1080 */
1081static char ssl_get_mac_order( ssl_context *ssl,
1082 const ssl_session *session,
1083 cipher_mode_t mode )
1084{
1085#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
1086 if( mode == POLARSSL_MODE_STREAM )
1087 return( MAC_PLAINTEXT );
1088#endif
1089
1090#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1091 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
1092 if( mode == POLARSSL_MODE_CBC )
1093 {
1094#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1095 if( session != NULL && session->encrypt_then_mac == SSL_ETM_ENABLED )
1096 {
1097 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1098 return( MAC_CIPHERTEXT );
1099 }
1100#endif
1101
1102 return( MAC_PLAINTEXT );
1103 }
1104#endif
1105
1106 return( MAC_NONE );
1107}
1108
Paul Bakker5121ce52009-01-03 21:22:43 +00001109/*
1110 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001111 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001112static int ssl_encrypt_buf( ssl_context *ssl )
1113{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001114 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001115 const cipher_mode_t mode = cipher_get_cipher_mode(
1116 &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001117 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
1119 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1120
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001121 mac_order = ssl_get_mac_order( ssl, ssl->session_out, mode );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001122
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001124 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001125 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001126#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1127 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1128 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001129 if( mac_order == MAC_PLAINTEXT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001131#if defined(POLARSSL_SSL_PROTO_SSL3)
1132 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1133 {
1134 ssl_mac( &ssl->transform_out->md_ctx_enc,
1135 ssl->transform_out->mac_enc,
1136 ssl->out_msg, ssl->out_msglen,
1137 ssl->out_ctr, ssl->out_msgtype );
1138 }
1139 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001140#endif
1141#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001142 defined(POLARSSL_SSL_PROTO_TLS1_2)
1143 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1144 {
1145 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1146 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1147 ssl->out_msg, ssl->out_msglen );
1148 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1149 ssl->out_msg + ssl->out_msglen );
1150 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1151 }
1152 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001153#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001154 {
1155 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001156 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001157 }
1158
1159 SSL_DEBUG_BUF( 4, "computed mac",
1160 ssl->out_msg + ssl->out_msglen,
1161 ssl->transform_out->maclen );
1162
1163 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001164 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001165#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001167 /*
1168 * Encrypt
1169 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001170#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001171 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001173 int ret;
1174 size_t olen = 0;
1175
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1177 "including %d bytes of padding",
1178 ssl->out_msglen, 0 ) );
1179
1180 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1181 ssl->out_msg, ssl->out_msglen );
1182
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001183 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001184 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001185 ssl->transform_out->ivlen,
1186 ssl->out_msg, ssl->out_msglen,
1187 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001188 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001189 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001190 return( ret );
1191 }
1192
1193 if( ssl->out_msglen != olen )
1194 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001195 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001196 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 }
Paul Bakker68884e32013-01-07 18:20:04 +01001199 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001200#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001201#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1202 if( mode == POLARSSL_MODE_GCM ||
1203 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001204 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001205 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001206 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001207 unsigned char *enc_msg;
1208 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001209 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1210 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001211
Paul Bakkerca4ab492012-04-18 14:23:57 +00001212 memcpy( add_data, ssl->out_ctr, 8 );
1213 add_data[8] = ssl->out_msgtype;
1214 add_data[9] = ssl->major_ver;
1215 add_data[10] = ssl->minor_ver;
1216 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1217 add_data[12] = ssl->out_msglen & 0xFF;
1218
1219 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1220 add_data, 13 );
1221
Paul Bakker68884e32013-01-07 18:20:04 +01001222 /*
1223 * Generate IV
1224 */
1225 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001226 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1227 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001228 if( ret != 0 )
1229 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001230
Paul Bakker68884e32013-01-07 18:20:04 +01001231 memcpy( ssl->out_iv,
1232 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1233 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001234
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001235 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001236 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001237
Paul Bakker68884e32013-01-07 18:20:04 +01001238 /*
1239 * Fix pointer positions and message length with added IV
1240 */
1241 enc_msg = ssl->out_msg;
1242 enc_msglen = ssl->out_msglen;
1243 ssl->out_msglen += ssl->transform_out->ivlen -
1244 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001245
Paul Bakker68884e32013-01-07 18:20:04 +01001246 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1247 "including %d bytes of padding",
1248 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001249
Paul Bakker68884e32013-01-07 18:20:04 +01001250 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1251 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001252
Paul Bakker68884e32013-01-07 18:20:04 +01001253 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001254 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001255 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001256 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1257 ssl->transform_out->iv_enc,
1258 ssl->transform_out->ivlen,
1259 add_data, 13,
1260 enc_msg, enc_msglen,
1261 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001262 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001263 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001264 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001265 return( ret );
1266 }
1267
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001268 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001269 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001270 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001271 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001272 }
1273
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001274 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001275
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001276 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001279#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001280#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1281 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001282 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001284 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001285 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001286 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001287
Paul Bakker48916f92012-09-16 19:57:18 +00001288 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1289 ssl->transform_out->ivlen;
1290 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 padlen = 0;
1292
1293 for( i = 0; i <= padlen; i++ )
1294 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1295
1296 ssl->out_msglen += padlen + 1;
1297
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001298 enc_msglen = ssl->out_msglen;
1299 enc_msg = ssl->out_msg;
1300
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001301#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001302 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001303 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1304 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001306 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001307 {
1308 /*
1309 * Generate IV
1310 */
Paul Bakker48916f92012-09-16 19:57:18 +00001311 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1312 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001313 if( ret != 0 )
1314 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001315
Paul Bakker92be97b2013-01-02 17:30:03 +01001316 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001317 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001318
1319 /*
1320 * Fix pointer positions and message length with added IV
1321 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001322 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001323 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001324 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001325 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001326#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001327
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001329 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001330 ssl->out_msglen, ssl->transform_out->ivlen,
1331 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
1333 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001334 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001336 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001337 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001338 ssl->transform_out->ivlen,
1339 enc_msg, enc_msglen,
1340 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001341 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001342 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001343 return( ret );
1344 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001345
Paul Bakkercca5b812013-08-31 17:40:26 +02001346 if( enc_msglen != olen )
1347 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001348 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001349 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001350 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001351
1352#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001353 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1354 {
1355 /*
1356 * Save IV in SSL3 and TLS1
1357 */
1358 memcpy( ssl->transform_out->iv_enc,
1359 ssl->transform_out->cipher_ctx_enc.iv,
1360 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001362#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001363
1364#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1365 if( mac_order == MAC_CIPHERTEXT )
1366 {
1367 /*
1368 * MAC(MAC_write_key, seq_num +
1369 * TLSCipherText.type +
1370 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001371 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001372 * IV + // except for TLS 1.0
1373 * ENC(content + padding + padding_length));
1374 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001375 unsigned char pseudo_hdr[13];
1376
1377 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1378 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001379 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1380 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1381
1382 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001383
1384 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1385 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1386 ssl->out_iv, ssl->out_msglen );
1387 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1388 ssl->out_iv + ssl->out_msglen );
1389 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1390
1391 ssl->out_msglen += ssl->transform_out->maclen;
1392 }
1393#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001395 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001396#endif /* POLARSSL_CIPHER_MODE_CBC &&
1397 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001398 {
1399 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001400 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001401 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Paul Bakkerca4ab492012-04-18 14:23:57 +00001403 for( i = 8; i > 0; i-- )
1404 if( ++ssl->out_ctr[i - 1] != 0 )
1405 break;
1406
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001407 /* The loops goes to its end iff the counter is wrapping */
1408 if( i == 0 )
1409 {
1410 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1411 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1412 }
1413
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1415
1416 return( 0 );
1417}
1418
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001419#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001420
Paul Bakker5121ce52009-01-03 21:22:43 +00001421static int ssl_decrypt_buf( ssl_context *ssl )
1422{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001423 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001424 const cipher_mode_t mode = cipher_get_cipher_mode(
1425 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001426#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1427 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1428 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1429 size_t padlen = 0, correct = 1;
1430#endif
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001431 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
1433 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1434
Paul Bakker48916f92012-09-16 19:57:18 +00001435 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 {
1437 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001438 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001439 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
1441
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001442 mac_order = ssl_get_mac_order( ssl, ssl->session_in, mode );
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001443
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001444#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001445 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001446 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001447 int ret;
1448 size_t olen = 0;
1449
Paul Bakker68884e32013-01-07 18:20:04 +01001450 padlen = 0;
1451
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001452 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001453 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001454 ssl->transform_in->ivlen,
1455 ssl->in_msg, ssl->in_msglen,
1456 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001457 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001458 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001459 return( ret );
1460 }
1461
1462 if( ssl->in_msglen != olen )
1463 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001464 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001465 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 }
Paul Bakker68884e32013-01-07 18:20:04 +01001468 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001469#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001470#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1471 if( mode == POLARSSL_MODE_GCM ||
1472 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001473 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001474 int ret;
1475 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001476 unsigned char *dec_msg;
1477 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001478 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001479 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1480 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001481 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1482 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001483
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001484 if( ssl->in_msglen < explicit_iv_len + taglen )
1485 {
1486 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1487 "+ taglen (%d)", ssl->in_msglen,
1488 explicit_iv_len, taglen ) );
1489 return( POLARSSL_ERR_SSL_INVALID_MAC );
1490 }
1491 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1492
Paul Bakker68884e32013-01-07 18:20:04 +01001493 dec_msg = ssl->in_msg;
1494 dec_msg_result = ssl->in_msg;
1495 ssl->in_msglen = dec_msglen;
1496
1497 memcpy( add_data, ssl->in_ctr, 8 );
1498 add_data[8] = ssl->in_msgtype;
1499 add_data[9] = ssl->major_ver;
1500 add_data[10] = ssl->minor_ver;
1501 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1502 add_data[12] = ssl->in_msglen & 0xFF;
1503
1504 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1505 add_data, 13 );
1506
1507 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1508 ssl->in_iv,
1509 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1510
1511 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1512 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001513 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001514
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001515 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001516 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001517 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001518 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1519 ssl->transform_in->iv_dec,
1520 ssl->transform_in->ivlen,
1521 add_data, 13,
1522 dec_msg, dec_msglen,
1523 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001524 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001525 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001526 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1527
1528 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1529 return( POLARSSL_ERR_SSL_INVALID_MAC );
1530
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001531 return( ret );
1532 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001533
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001534 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001535 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001536 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001537 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001538 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001541#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001542#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1543 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001544 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 {
Paul Bakker45829992013-01-03 14:52:21 +01001546 /*
1547 * Decrypt and check the padding
1548 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001549 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001550 unsigned char *dec_msg;
1551 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001552 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001553 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001554 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001555
Paul Bakker5121ce52009-01-03 21:22:43 +00001556 /*
Paul Bakker45829992013-01-03 14:52:21 +01001557 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001559#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001560 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1561 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001562#endif
Paul Bakker45829992013-01-03 14:52:21 +01001563
1564 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1565 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1566 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001567 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1568 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1569 ssl->transform_in->ivlen,
1570 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001571 return( POLARSSL_ERR_SSL_INVALID_MAC );
1572 }
1573
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001574 dec_msglen = ssl->in_msglen;
1575 dec_msg = ssl->in_msg;
1576 dec_msg_result = ssl->in_msg;
1577
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001578 /*
1579 * Authenticate before decrypt if enabled
1580 */
1581#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1582 if( mac_order == MAC_CIPHERTEXT )
1583 {
1584 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001585 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001586
1587 dec_msglen -= ssl->transform_in->maclen;
1588 ssl->in_msglen -= ssl->transform_in->maclen;
1589
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001590 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1591 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1592 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1593 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1594
1595 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1596
1597 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001598 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1599 ssl->in_iv, ssl->in_msglen );
1600 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1601 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1602
1603 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1604 ssl->transform_in->maclen );
1605 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1606 ssl->transform_in->maclen );
1607
1608 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1609 ssl->transform_in->maclen ) != 0 )
1610 {
1611 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1612
1613 return( POLARSSL_ERR_SSL_INVALID_MAC );
1614 }
1615 }
1616#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1617
1618 /*
1619 * Check length sanity
1620 */
1621 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1622 {
1623 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1624 ssl->in_msglen, ssl->transform_in->ivlen ) );
1625 return( POLARSSL_ERR_SSL_INVALID_MAC );
1626 }
1627
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001628#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001629 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001630 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001631 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001632 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001633 {
Paul Bakker48916f92012-09-16 19:57:18 +00001634 dec_msglen -= ssl->transform_in->ivlen;
1635 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001636
Paul Bakker48916f92012-09-16 19:57:18 +00001637 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001638 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001639 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001640#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001641
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001642 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001643 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001644 ssl->transform_in->ivlen,
1645 dec_msg, dec_msglen,
1646 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001647 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001648 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001649 return( ret );
1650 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001651
Paul Bakkercca5b812013-08-31 17:40:26 +02001652 if( dec_msglen != olen )
1653 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001654 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001655 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001656 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001657
1658#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001659 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1660 {
1661 /*
1662 * Save IV in SSL3 and TLS1
1663 */
1664 memcpy( ssl->transform_in->iv_dec,
1665 ssl->transform_in->cipher_ctx_dec.iv,
1666 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001668#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
1670 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001671
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001672 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
1673 mac_order == MAC_PLAINTEXT )
Paul Bakker45829992013-01-03 14:52:21 +01001674 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001675#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001676 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1677 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001678#endif
Paul Bakker45829992013-01-03 14:52:21 +01001679 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001680 correct = 0;
1681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001683#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001684 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1685 {
Paul Bakker48916f92012-09-16 19:57:18 +00001686 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001688#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001689 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1690 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001691 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001692#endif
Paul Bakker45829992013-01-03 14:52:21 +01001693 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 }
1695 }
1696 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001697#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001698#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1699 defined(POLARSSL_SSL_PROTO_TLS1_2)
1700 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 {
1702 /*
Paul Bakker45829992013-01-03 14:52:21 +01001703 * TLSv1+: always check the padding up to the first failure
1704 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001706 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001707 size_t padding_idx = ssl->in_msglen - padlen - 1;
1708
Paul Bakker956c9e02013-12-19 14:42:28 +01001709 /*
1710 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001711 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001712 *
Paul Bakker61885c72014-04-25 12:59:03 +02001713 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1714 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001715 *
1716 * In both cases we reset padding_idx to a safe value (0) to
1717 * prevent out-of-buffer reads.
1718 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001719 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001720 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1721 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001722
1723 padding_idx *= correct;
1724
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001725 for( i = 1; i <= 256; i++ )
1726 {
1727 real_count &= ( i <= padlen );
1728 pad_count += real_count *
1729 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1730 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001731
1732 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001733
Paul Bakkerd66f0702013-01-31 16:57:45 +01001734#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001735 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001736 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001737#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001738 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001739 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001740 else
1741#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1742 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001743 {
1744 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001745 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001746 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001747
1748 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001749 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001750 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001751#endif /* POLARSSL_CIPHER_MODE_CBC &&
1752 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001753 {
1754 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001755 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001756 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001757
1758 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1759 ssl->in_msg, ssl->in_msglen );
1760
1761 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001762 * Authenticate if not done yet.
1763 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001765#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1766 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1767 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001768 if( mac_order == MAC_PLAINTEXT )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001769 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001770 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1771
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001772 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001774 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1775 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001776
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001777 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001779#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001780 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1781 {
1782 ssl_mac( &ssl->transform_in->md_ctx_dec,
1783 ssl->transform_in->mac_dec,
1784 ssl->in_msg, ssl->in_msglen,
1785 ssl->in_ctr, ssl->in_msgtype );
1786 }
1787 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001788#endif /* POLARSSL_SSL_PROTO_SSL3 */
1789#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001790 defined(POLARSSL_SSL_PROTO_TLS1_2)
1791 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1792 {
1793 /*
1794 * Process MAC and always update for padlen afterwards to make
1795 * total time independent of padlen
1796 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001797 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001798 *
1799 * Known timing attacks:
1800 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1801 *
1802 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1803 * correctly. (We round down instead of up, so -56 is the correct
1804 * value for our calculations instead of -55)
1805 */
1806 size_t j, extra_run = 0;
1807 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1808 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001809
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001810 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001811
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001812 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1813 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1814 ssl->in_msglen );
1815 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1816 ssl->in_msg + ssl->in_msglen );
1817 for( j = 0; j < extra_run; j++ )
1818 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001819
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1821 }
1822 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001823#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001824 POLARSSL_SSL_PROTO_TLS1_2 */
1825 {
1826 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001827 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001828 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001829
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1831 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1832 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001834 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001835 ssl->transform_in->maclen ) != 0 )
1836 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001837#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001838 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001839#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001840 correct = 0;
1841 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001843 /*
1844 * Finally check the correct flag
1845 */
1846 if( correct == 0 )
1847 return( POLARSSL_ERR_SSL_INVALID_MAC );
1848 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001849#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
1851 if( ssl->in_msglen == 0 )
1852 {
1853 ssl->nb_zero++;
1854
1855 /*
1856 * Three or more empty messages may be a DoS attack
1857 * (excessive CPU consumption).
1858 */
1859 if( ssl->nb_zero > 3 )
1860 {
1861 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1862 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001863 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 }
1865 }
1866 else
1867 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001868
Paul Bakker23986e52011-04-24 08:57:21 +00001869 for( i = 8; i > 0; i-- )
1870 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 break;
1872
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001873 /* The loops goes to its end iff the counter is wrapping */
1874 if( i == 0 )
1875 {
1876 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1877 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1878 }
1879
Paul Bakker5121ce52009-01-03 21:22:43 +00001880 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1881
1882 return( 0 );
1883}
1884
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001885#undef MAC_NONE
1886#undef MAC_PLAINTEXT
1887#undef MAC_CIPHERTEXT
1888
Paul Bakker2770fbd2012-07-03 13:30:23 +00001889#if defined(POLARSSL_ZLIB_SUPPORT)
1890/*
1891 * Compression/decompression functions
1892 */
1893static int ssl_compress_buf( ssl_context *ssl )
1894{
1895 int ret;
1896 unsigned char *msg_post = ssl->out_msg;
1897 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001898 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001899
1900 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1901
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001902 if( len_pre == 0 )
1903 return( 0 );
1904
Paul Bakker2770fbd2012-07-03 13:30:23 +00001905 memcpy( msg_pre, ssl->out_msg, len_pre );
1906
1907 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1908 ssl->out_msglen ) );
1909
1910 SSL_DEBUG_BUF( 4, "before compression: output payload",
1911 ssl->out_msg, ssl->out_msglen );
1912
Paul Bakker48916f92012-09-16 19:57:18 +00001913 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1914 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1915 ssl->transform_out->ctx_deflate.next_out = msg_post;
1916 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001917
Paul Bakker48916f92012-09-16 19:57:18 +00001918 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001919 if( ret != Z_OK )
1920 {
1921 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1922 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1923 }
1924
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001925 ssl->out_msglen = SSL_BUFFER_LEN -
1926 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001927
Paul Bakker2770fbd2012-07-03 13:30:23 +00001928 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1929 ssl->out_msglen ) );
1930
1931 SSL_DEBUG_BUF( 4, "after compression: output payload",
1932 ssl->out_msg, ssl->out_msglen );
1933
1934 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1935
1936 return( 0 );
1937}
1938
1939static int ssl_decompress_buf( ssl_context *ssl )
1940{
1941 int ret;
1942 unsigned char *msg_post = ssl->in_msg;
1943 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001944 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001945
1946 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1947
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001948 if( len_pre == 0 )
1949 return( 0 );
1950
Paul Bakker2770fbd2012-07-03 13:30:23 +00001951 memcpy( msg_pre, ssl->in_msg, len_pre );
1952
1953 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1954 ssl->in_msglen ) );
1955
1956 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1957 ssl->in_msg, ssl->in_msglen );
1958
Paul Bakker48916f92012-09-16 19:57:18 +00001959 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1960 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1961 ssl->transform_in->ctx_inflate.next_out = msg_post;
1962 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001963
Paul Bakker48916f92012-09-16 19:57:18 +00001964 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001965 if( ret != Z_OK )
1966 {
1967 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1968 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1969 }
1970
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001971 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1972 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001973
Paul Bakker2770fbd2012-07-03 13:30:23 +00001974 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1975 ssl->in_msglen ) );
1976
1977 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1978 ssl->in_msg, ssl->in_msglen );
1979
1980 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1981
1982 return( 0 );
1983}
1984#endif /* POLARSSL_ZLIB_SUPPORT */
1985
Paul Bakker5121ce52009-01-03 21:22:43 +00001986/*
1987 * Fill the input message buffer
1988 */
Paul Bakker23986e52011-04-24 08:57:21 +00001989int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001990{
Paul Bakker23986e52011-04-24 08:57:21 +00001991 int ret;
1992 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001993
1994 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1995
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001996 if( nb_want > SSL_BUFFER_LEN - 8 )
1997 {
1998 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1999 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2000 }
2001
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 while( ssl->in_left < nb_want )
2003 {
2004 len = nb_want - ssl->in_left;
2005 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2006
2007 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2008 ssl->in_left, nb_want ) );
2009 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2010
Paul Bakker831a7552011-05-18 13:32:51 +00002011 if( ret == 0 )
2012 return( POLARSSL_ERR_SSL_CONN_EOF );
2013
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 if( ret < 0 )
2015 return( ret );
2016
2017 ssl->in_left += ret;
2018 }
2019
2020 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2021
2022 return( 0 );
2023}
2024
2025/*
2026 * Flush any data not yet written
2027 */
2028int ssl_flush_output( ssl_context *ssl )
2029{
2030 int ret;
2031 unsigned char *buf;
2032
2033 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2034
2035 while( ssl->out_left > 0 )
2036 {
2037 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2038 5 + ssl->out_msglen, ssl->out_left ) );
2039
Paul Bakker5bd42292012-12-19 14:40:42 +01002040 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002042
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2044
2045 if( ret <= 0 )
2046 return( ret );
2047
2048 ssl->out_left -= ret;
2049 }
2050
2051 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2052
2053 return( 0 );
2054}
2055
2056/*
2057 * Record layer functions
2058 */
2059int ssl_write_record( ssl_context *ssl )
2060{
Paul Bakker05ef8352012-05-08 09:17:57 +00002061 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002062 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002063
2064 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2065
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2067 {
2068 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2069 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2070 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2071
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002072 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2073 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 }
2075
Paul Bakker2770fbd2012-07-03 13:30:23 +00002076#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002077 if( ssl->transform_out != NULL &&
2078 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002079 {
2080 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2081 {
2082 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2083 return( ret );
2084 }
2085
2086 len = ssl->out_msglen;
2087 }
2088#endif /*POLARSSL_ZLIB_SUPPORT */
2089
Paul Bakker05ef8352012-05-08 09:17:57 +00002090#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002091 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002093 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094
Paul Bakker05ef8352012-05-08 09:17:57 +00002095 ret = ssl_hw_record_write( ssl );
2096 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2097 {
2098 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002099 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002100 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002101
2102 if( ret == 0 )
2103 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002104 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002105#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002106 if( !done )
2107 {
2108 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2109 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2110 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2112 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002113
Paul Bakker48916f92012-09-16 19:57:18 +00002114 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002115 {
2116 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2117 {
2118 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2119 return( ret );
2120 }
2121
2122 len = ssl->out_msglen;
2123 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2124 ssl->out_hdr[4] = (unsigned char)( len );
2125 }
2126
2127 ssl->out_left = 5 + ssl->out_msglen;
2128
2129 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2130 "version = [%d:%d], msglen = %d",
2131 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2132 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2133
2134 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002135 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 }
2137
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2139 {
2140 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2141 return( ret );
2142 }
2143
2144 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2145
2146 return( 0 );
2147}
2148
2149int ssl_read_record( ssl_context *ssl )
2150{
Paul Bakker05ef8352012-05-08 09:17:57 +00002151 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
2153 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2154
2155 if( ssl->in_hslen != 0 &&
2156 ssl->in_hslen < ssl->in_msglen )
2157 {
2158 /*
2159 * Get next Handshake message in the current record
2160 */
2161 ssl->in_msglen -= ssl->in_hslen;
2162
Paul Bakker8934a982011-08-05 11:11:53 +00002163 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002164 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
2166 ssl->in_hslen = 4;
2167 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2168
2169 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2170 " %d, type = %d, hslen = %d",
2171 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2172
2173 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2174 {
2175 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002176 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 }
2178
2179 if( ssl->in_msglen < ssl->in_hslen )
2180 {
2181 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002182 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 }
2184
Paul Bakker4224bc02014-04-08 14:36:50 +02002185 if( ssl->state != SSL_HANDSHAKE_OVER )
2186 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187
2188 return( 0 );
2189 }
2190
2191 ssl->in_hslen = 0;
2192
2193 /*
2194 * Read the record header and validate it
2195 */
2196 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2197 {
2198 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2199 return( ret );
2200 }
2201
2202 ssl->in_msgtype = ssl->in_hdr[0];
2203 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2204
2205 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2206 "version = [%d:%d], msglen = %d",
2207 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2208 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2209
2210 if( ssl->in_hdr[1] != ssl->major_ver )
2211 {
2212 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002213 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 }
2215
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002216 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 {
2218 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002219 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 }
2221
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002222 /* Sanity check (outer boundaries) */
2223 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2224 {
2225 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2226 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2227 }
2228
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002230 * Make sure the message length is acceptable for the current transform
2231 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 */
Paul Bakker48916f92012-09-16 19:57:18 +00002233 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002235 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 {
2237 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002238 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
2240 }
2241 else
2242 {
Paul Bakker48916f92012-09-16 19:57:18 +00002243 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 {
2245 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002246 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 }
2248
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002249#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002251 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 {
2253 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002254 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002256#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002258#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2259 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 /*
2261 * TLS encrypted messages can have up to 256 bytes of padding
2262 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002263 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002264 ssl->in_msglen > ssl->transform_in->minlen +
2265 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 {
2267 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002268 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002270#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 }
2272
2273 /*
2274 * Read and optionally decrypt the message contents
2275 */
2276 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2277 {
2278 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2279 return( ret );
2280 }
2281
2282 SSL_DEBUG_BUF( 4, "input record from network",
2283 ssl->in_hdr, 5 + ssl->in_msglen );
2284
Paul Bakker05ef8352012-05-08 09:17:57 +00002285#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002286 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002287 {
2288 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2289
2290 ret = ssl_hw_record_read( ssl );
2291 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2292 {
2293 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002294 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002295 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002296
2297 if( ret == 0 )
2298 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002299 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002300#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002301 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 {
2303 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2304 {
Paul Bakker40865c82013-01-31 17:13:13 +01002305#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2306 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2307 {
2308 ssl_send_alert_message( ssl,
2309 SSL_ALERT_LEVEL_FATAL,
2310 SSL_ALERT_MSG_BAD_RECORD_MAC );
2311 }
2312#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2314 return( ret );
2315 }
2316
2317 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2318 ssl->in_msg, ssl->in_msglen );
2319
2320 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2321 {
2322 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002323 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 }
2325 }
2326
Paul Bakker2770fbd2012-07-03 13:30:23 +00002327#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002328 if( ssl->transform_in != NULL &&
2329 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002330 {
2331 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2332 {
2333 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2334 return( ret );
2335 }
2336
2337 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2338 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2339 }
2340#endif /* POLARSSL_ZLIB_SUPPORT */
2341
Paul Bakker0a925182012-04-16 06:46:41 +00002342 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2343 ssl->in_msgtype != SSL_MSG_ALERT &&
2344 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2345 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2346 {
2347 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2348
Paul Bakker48916f92012-09-16 19:57:18 +00002349 if( ( ret = ssl_send_alert_message( ssl,
2350 SSL_ALERT_LEVEL_FATAL,
2351 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002352 {
2353 return( ret );
2354 }
2355
2356 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2357 }
2358
Paul Bakker5121ce52009-01-03 21:22:43 +00002359 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2360 {
2361 ssl->in_hslen = 4;
2362 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2363
2364 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2365 " %d, type = %d, hslen = %d",
2366 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2367
2368 /*
2369 * Additional checks to validate the handshake header
2370 */
2371 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2372 {
2373 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002374 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 }
2376
2377 if( ssl->in_msglen < ssl->in_hslen )
2378 {
2379 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002380 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 }
2382
Paul Bakker48916f92012-09-16 19:57:18 +00002383 if( ssl->state != SSL_HANDSHAKE_OVER )
2384 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 }
2386
2387 if( ssl->in_msgtype == SSL_MSG_ALERT )
2388 {
2389 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2390 ssl->in_msg[0], ssl->in_msg[1] ) );
2391
2392 /*
2393 * Ignore non-fatal alerts, except close_notify
2394 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002395 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002397 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2398 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002399 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 }
2401
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002402 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2403 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 {
2405 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002406 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 }
2408 }
2409
2410 ssl->in_left = 0;
2411
2412 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2413
2414 return( 0 );
2415}
2416
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002417int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2418{
2419 int ret;
2420
2421 if( ( ret = ssl_send_alert_message( ssl,
2422 SSL_ALERT_LEVEL_FATAL,
2423 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2424 {
2425 return( ret );
2426 }
2427
2428 return( 0 );
2429}
2430
Paul Bakker0a925182012-04-16 06:46:41 +00002431int ssl_send_alert_message( ssl_context *ssl,
2432 unsigned char level,
2433 unsigned char message )
2434{
2435 int ret;
2436
2437 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2438
2439 ssl->out_msgtype = SSL_MSG_ALERT;
2440 ssl->out_msglen = 2;
2441 ssl->out_msg[0] = level;
2442 ssl->out_msg[1] = message;
2443
2444 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2445 {
2446 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2447 return( ret );
2448 }
2449
2450 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2451
2452 return( 0 );
2453}
2454
Paul Bakker5121ce52009-01-03 21:22:43 +00002455/*
2456 * Handshake functions
2457 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002458#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2459 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2460 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2461 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2462 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2463 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2464 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002465int ssl_write_certificate( ssl_context *ssl )
2466{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002467 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2470
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002471 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002472 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2473 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002474 {
2475 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2476 ssl->state++;
2477 return( 0 );
2478 }
2479
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002480 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2481 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002482}
2483
2484int ssl_parse_certificate( ssl_context *ssl )
2485{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002486 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2487
2488 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2489
2490 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002491 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2492 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002493 {
2494 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2495 ssl->state++;
2496 return( 0 );
2497 }
2498
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002499 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2500 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501}
2502#else
2503int ssl_write_certificate( ssl_context *ssl )
2504{
2505 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2506 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002507 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002508 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2509
2510 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2511
2512 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002513 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2514 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002515 {
2516 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2517 ssl->state++;
2518 return( 0 );
2519 }
2520
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 if( ssl->endpoint == SSL_IS_CLIENT )
2522 {
2523 if( ssl->client_auth == 0 )
2524 {
2525 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2526 ssl->state++;
2527 return( 0 );
2528 }
2529
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002530#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 /*
2532 * If using SSLv3 and got no cert, send an Alert message
2533 * (otherwise an empty Certificate message will be sent).
2534 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002535 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2537 {
2538 ssl->out_msglen = 2;
2539 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002540 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2541 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
2543 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2544 goto write_msg;
2545 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002546#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002547 }
2548 else /* SSL_IS_SERVER */
2549 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002550 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 {
2552 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002553 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555 }
2556
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002557 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
2559 /*
2560 * 0 . 0 handshake type
2561 * 1 . 3 handshake length
2562 * 4 . 6 length of all certs
2563 * 7 . 9 length of cert. 1
2564 * 10 . n-1 peer certificate
2565 * n . n+2 length of cert. 2
2566 * n+3 . ... upper level cert, etc.
2567 */
2568 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002569 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Paul Bakker29087132010-03-21 21:03:34 +00002571 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 {
2573 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002574 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 {
2576 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2577 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002578 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 }
2580
2581 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2582 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2583 ssl->out_msg[i + 2] = (unsigned char)( n );
2584
2585 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2586 i += n; crt = crt->next;
2587 }
2588
2589 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2590 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2591 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2592
2593 ssl->out_msglen = i;
2594 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2595 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2596
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002597#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002598write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002599#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
2601 ssl->state++;
2602
2603 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2604 {
2605 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2606 return( ret );
2607 }
2608
2609 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2610
Paul Bakkered27a042013-04-18 22:46:23 +02002611 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612}
2613
2614int ssl_parse_certificate( ssl_context *ssl )
2615{
Paul Bakkered27a042013-04-18 22:46:23 +02002616 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002617 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002618 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
2620 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2621
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002622 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002623 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2624 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002625 {
2626 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2627 ssl->state++;
2628 return( 0 );
2629 }
2630
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002632 ( ssl->authmode == SSL_VERIFY_NONE ||
2633 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002635 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2637 ssl->state++;
2638 return( 0 );
2639 }
2640
2641 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2642 {
2643 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2644 return( ret );
2645 }
2646
2647 ssl->state++;
2648
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002649#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 /*
2651 * Check if the client sent an empty certificate
2652 */
2653 if( ssl->endpoint == SSL_IS_SERVER &&
2654 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2655 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002656 if( ssl->in_msglen == 2 &&
2657 ssl->in_msgtype == SSL_MSG_ALERT &&
2658 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2659 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 {
2661 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2662
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002663 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2665 return( 0 );
2666 else
Paul Bakker40e46942009-01-03 21:51:57 +00002667 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 }
2669 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002670#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002672#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2673 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 if( ssl->endpoint == SSL_IS_SERVER &&
2675 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2676 {
2677 if( ssl->in_hslen == 7 &&
2678 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2679 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2680 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2681 {
2682 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2683
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002684 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002686 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 else
2688 return( 0 );
2689 }
2690 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002691#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2692 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
2694 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2695 {
2696 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002697 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 }
2699
2700 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2701 {
2702 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002703 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 }
2705
2706 /*
2707 * Same message structure as in ssl_write_certificate()
2708 */
2709 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2710
2711 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2712 {
2713 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002714 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 }
2716
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002717 /* In case we tried to reuse a session but it failed */
2718 if( ssl->session_negotiate->peer_cert != NULL )
2719 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002720 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002721 polarssl_free( ssl->session_negotiate->peer_cert );
2722 }
2723
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002724 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2725 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 {
2727 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002728 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002729 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 }
2731
Paul Bakkerb6b09562013-09-18 14:17:41 +02002732 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
2734 i = 7;
2735
2736 while( i < ssl->in_hslen )
2737 {
2738 if( ssl->in_msg[i] != 0 )
2739 {
2740 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002741 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 }
2743
2744 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2745 | (unsigned int) ssl->in_msg[i + 2];
2746 i += 3;
2747
2748 if( n < 128 || i + n > ssl->in_hslen )
2749 {
2750 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002751 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 }
2753
Paul Bakkerddf26b42013-09-18 13:46:23 +02002754 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2755 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 if( ret != 0 )
2757 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002758 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 return( ret );
2760 }
2761
2762 i += n;
2763 }
2764
Paul Bakker48916f92012-09-16 19:57:18 +00002765 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002767 /*
2768 * On client, make sure the server cert doesn't change during renego to
2769 * avoid "triple handshake" attack: https://secure-resumption.com/
2770 */
2771 if( ssl->endpoint == SSL_IS_CLIENT &&
2772 ssl->renegotiation == SSL_RENEGOTIATION )
2773 {
2774 if( ssl->session->peer_cert == NULL )
2775 {
2776 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2777 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2778 }
2779
2780 if( ssl->session->peer_cert->raw.len !=
2781 ssl->session_negotiate->peer_cert->raw.len ||
2782 memcmp( ssl->session->peer_cert->raw.p,
2783 ssl->session_negotiate->peer_cert->raw.p,
2784 ssl->session->peer_cert->raw.len ) != 0 )
2785 {
2786 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2787 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2788 }
2789 }
2790
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 if( ssl->authmode != SSL_VERIFY_NONE )
2792 {
2793 if( ssl->ca_chain == NULL )
2794 {
2795 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002796 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 }
2798
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002799 /*
2800 * Main check: verify certificate
2801 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002802 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2803 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2804 &ssl->session_negotiate->verify_result,
2805 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
2807 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002808 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002810 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002811
2812 /*
2813 * Secondary checks: always done, but change 'ret' only if it was 0
2814 */
2815
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002816#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002817 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002818 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002819
2820 /* If certificate uses an EC key, make sure the curve is OK */
2821 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2822 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2823 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002824 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002825 if( ret == 0 )
2826 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002827 }
2828 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002829#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002831 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2832 ciphersuite_info,
2833 ! ssl->endpoint ) != 0 )
2834 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002835 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002836 if( ret == 0 )
2837 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2838 }
2839
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2841 ret = 0;
2842 }
2843
2844 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2845
2846 return( ret );
2847}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002848#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2849 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2850 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2851 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2852 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2853 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2854 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
2856int ssl_write_change_cipher_spec( ssl_context *ssl )
2857{
2858 int ret;
2859
2860 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2861
2862 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2863 ssl->out_msglen = 1;
2864 ssl->out_msg[0] = 1;
2865
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 ssl->state++;
2867
2868 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2869 {
2870 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2871 return( ret );
2872 }
2873
2874 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2875
2876 return( 0 );
2877}
2878
2879int ssl_parse_change_cipher_spec( ssl_context *ssl )
2880{
2881 int ret;
2882
2883 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2884
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2886 {
2887 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2888 return( ret );
2889 }
2890
2891 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2892 {
2893 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002894 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002895 }
2896
2897 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2898 {
2899 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002900 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 }
2902
2903 ssl->state++;
2904
2905 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2906
2907 return( 0 );
2908}
2909
Paul Bakker41c83d32013-03-20 14:39:14 +01002910void ssl_optimize_checksum( ssl_context *ssl,
2911 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002912{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002913 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002914
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002915#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2916 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002917 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002918 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002919 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002920#endif
2921#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2922#if defined(POLARSSL_SHA512_C)
2923 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2924 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2925 else
2926#endif
2927#if defined(POLARSSL_SHA256_C)
2928 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002929 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002930 else
2931#endif
2932#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002933 {
2934 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002935 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002936 }
Paul Bakker380da532012-04-18 16:10:25 +00002937}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002938
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002939static void ssl_update_checksum_start( ssl_context *ssl,
2940 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002941{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002942#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2943 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002944 md5_update( &ssl->handshake->fin_md5 , buf, len );
2945 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#endif
2947#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2948#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002949 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002950#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002951#if defined(POLARSSL_SHA512_C)
2952 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002953#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002954#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002955}
2956
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002957#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2958 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002959static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2960 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002961{
Paul Bakker48916f92012-09-16 19:57:18 +00002962 md5_update( &ssl->handshake->fin_md5 , buf, len );
2963 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002964}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002965#endif
Paul Bakker380da532012-04-18 16:10:25 +00002966
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002967#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2968#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002969static void ssl_update_checksum_sha256( ssl_context *ssl,
2970 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002971{
Paul Bakker9e36f042013-06-30 14:34:05 +02002972 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002973}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002974#endif
Paul Bakker380da532012-04-18 16:10:25 +00002975
Paul Bakker9e36f042013-06-30 14:34:05 +02002976#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002977static void ssl_update_checksum_sha384( ssl_context *ssl,
2978 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002979{
Paul Bakker9e36f042013-06-30 14:34:05 +02002980 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002981}
Paul Bakker769075d2012-11-24 11:26:46 +01002982#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002983#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002984
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002985#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986static void ssl_calc_finished_ssl(
2987 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002988{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002989 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002990 md5_context md5;
2991 sha1_context sha1;
2992
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 unsigned char padbuf[48];
2994 unsigned char md5sum[16];
2995 unsigned char sha1sum[20];
2996
Paul Bakker48916f92012-09-16 19:57:18 +00002997 ssl_session *session = ssl->session_negotiate;
2998 if( !session )
2999 session = ssl->session;
3000
Paul Bakker1ef83d62012-04-11 12:09:53 +00003001 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3002
Paul Bakker48916f92012-09-16 19:57:18 +00003003 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3004 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003005
3006 /*
3007 * SSLv3:
3008 * hash =
3009 * MD5( master + pad2 +
3010 * MD5( handshake + sender + master + pad1 ) )
3011 * + SHA1( master + pad2 +
3012 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 */
3014
Paul Bakker90995b52013-06-24 19:20:35 +02003015#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003016 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003017 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003018#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003019
Paul Bakker90995b52013-06-24 19:20:35 +02003020#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003022 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003023#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003024
Paul Bakker3c2122f2013-06-24 19:03:14 +02003025 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3026 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
Paul Bakker1ef83d62012-04-11 12:09:53 +00003028 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Paul Bakker3c2122f2013-06-24 19:03:14 +02003030 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003031 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032 md5_update( &md5, padbuf, 48 );
3033 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003034
Paul Bakker3c2122f2013-06-24 19:03:14 +02003035 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003036 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003037 sha1_update( &sha1, padbuf, 40 );
3038 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
Paul Bakker1ef83d62012-04-11 12:09:53 +00003040 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003041
Paul Bakker1ef83d62012-04-11 12:09:53 +00003042 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003043 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003044 md5_update( &md5, padbuf, 48 );
3045 md5_update( &md5, md5sum, 16 );
3046 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003049 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003050 sha1_update( &sha1, padbuf , 40 );
3051 sha1_update( &sha1, sha1sum, 20 );
3052 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003053
Paul Bakker1ef83d62012-04-11 12:09:53 +00003054 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Paul Bakker5b4af392014-06-26 12:09:34 +02003056 md5_free( &md5 );
3057 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Paul Bakker34617722014-06-13 17:20:13 +02003059 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3060 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3061 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
3063 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3064}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003065#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003067#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003068static void ssl_calc_finished_tls(
3069 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003070{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003071 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003072 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003073 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003076
Paul Bakker48916f92012-09-16 19:57:18 +00003077 ssl_session *session = ssl->session_negotiate;
3078 if( !session )
3079 session = ssl->session;
3080
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003082
Paul Bakker48916f92012-09-16 19:57:18 +00003083 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3084 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003085
Paul Bakker1ef83d62012-04-11 12:09:53 +00003086 /*
3087 * TLSv1:
3088 * hash = PRF( master, finished_label,
3089 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3090 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
Paul Bakker90995b52013-06-24 19:20:35 +02003092#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003093 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3094 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003095#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003096
Paul Bakker90995b52013-06-24 19:20:35 +02003097#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003098 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3099 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003100#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003101
3102 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003103 ? "client finished"
3104 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003105
3106 md5_finish( &md5, padbuf );
3107 sha1_finish( &sha1, padbuf + 16 );
3108
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003109 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003110 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003111
3112 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3113
Paul Bakker5b4af392014-06-26 12:09:34 +02003114 md5_free( &md5 );
3115 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003116
Paul Bakker34617722014-06-13 17:20:13 +02003117 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003118
3119 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3120}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003121#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003122
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003123#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3124#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003125static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003126 ssl_context *ssl, unsigned char *buf, int from )
3127{
3128 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003129 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003130 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003131 unsigned char padbuf[32];
3132
Paul Bakker48916f92012-09-16 19:57:18 +00003133 ssl_session *session = ssl->session_negotiate;
3134 if( !session )
3135 session = ssl->session;
3136
Paul Bakker380da532012-04-18 16:10:25 +00003137 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003138
Paul Bakker9e36f042013-06-30 14:34:05 +02003139 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003140
3141 /*
3142 * TLSv1.2:
3143 * hash = PRF( master, finished_label,
3144 * Hash( handshake ) )[0.11]
3145 */
3146
Paul Bakker9e36f042013-06-30 14:34:05 +02003147#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003148 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003149 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003150#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003151
3152 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003153 ? "client finished"
3154 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155
Paul Bakker9e36f042013-06-30 14:34:05 +02003156 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003157
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003158 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003159 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003160
3161 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3162
Paul Bakker5b4af392014-06-26 12:09:34 +02003163 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003164
Paul Bakker34617722014-06-13 17:20:13 +02003165 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003166
3167 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3168}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003169#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003170
Paul Bakker9e36f042013-06-30 14:34:05 +02003171#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003172static void ssl_calc_finished_tls_sha384(
3173 ssl_context *ssl, unsigned char *buf, int from )
3174{
3175 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003176 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003177 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003178 unsigned char padbuf[48];
3179
Paul Bakker48916f92012-09-16 19:57:18 +00003180 ssl_session *session = ssl->session_negotiate;
3181 if( !session )
3182 session = ssl->session;
3183
Paul Bakker380da532012-04-18 16:10:25 +00003184 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003185
Paul Bakker9e36f042013-06-30 14:34:05 +02003186 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003187
3188 /*
3189 * TLSv1.2:
3190 * hash = PRF( master, finished_label,
3191 * Hash( handshake ) )[0.11]
3192 */
3193
Paul Bakker9e36f042013-06-30 14:34:05 +02003194#if !defined(POLARSSL_SHA512_ALT)
3195 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3196 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003197#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003198
3199 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003200 ? "client finished"
3201 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003202
Paul Bakker9e36f042013-06-30 14:34:05 +02003203 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003204
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003205 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003206 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003207
3208 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3209
Paul Bakker5b4af392014-06-26 12:09:34 +02003210 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003211
Paul Bakker34617722014-06-13 17:20:13 +02003212 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003213
3214 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3215}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003216#endif /* POLARSSL_SHA512_C */
3217#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003218
Paul Bakker48916f92012-09-16 19:57:18 +00003219void ssl_handshake_wrapup( ssl_context *ssl )
3220{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003221 int resume = ssl->handshake->resume;
3222
Paul Bakker48916f92012-09-16 19:57:18 +00003223 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3224
3225 /*
3226 * Free our handshake params
3227 */
3228 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003229 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003230 ssl->handshake = NULL;
3231
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003232 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003233 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003234 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003235 ssl->renego_records_seen = 0;
3236 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003237
Paul Bakker48916f92012-09-16 19:57:18 +00003238 /*
3239 * Switch in our now active transform context
3240 */
3241 if( ssl->transform )
3242 {
3243 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003244 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003245 }
3246 ssl->transform = ssl->transform_negotiate;
3247 ssl->transform_negotiate = NULL;
3248
Paul Bakker0a597072012-09-25 21:55:46 +00003249 if( ssl->session )
3250 {
3251 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003252 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003253 }
3254 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003255 ssl->session_negotiate = NULL;
3256
Paul Bakker0a597072012-09-25 21:55:46 +00003257 /*
3258 * Add cache entry
3259 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003260 if( ssl->f_set_cache != NULL &&
3261 ssl->session->length != 0 &&
3262 resume == 0 )
3263 {
Paul Bakker0a597072012-09-25 21:55:46 +00003264 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3265 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003266 }
Paul Bakker0a597072012-09-25 21:55:46 +00003267
Paul Bakker48916f92012-09-16 19:57:18 +00003268 ssl->state++;
3269
3270 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3271}
3272
Paul Bakker1ef83d62012-04-11 12:09:53 +00003273int ssl_write_finished( ssl_context *ssl )
3274{
3275 int ret, hash_len;
3276
3277 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3278
Paul Bakker92be97b2013-01-02 17:30:03 +01003279 /*
3280 * Set the out_msg pointer to the correct location based on IV length
3281 */
3282 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3283 {
3284 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3285 ssl->transform_negotiate->fixed_ivlen;
3286 }
3287 else
3288 ssl->out_msg = ssl->out_iv;
3289
Paul Bakker48916f92012-09-16 19:57:18 +00003290 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003291
3292 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003293 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3294
Paul Bakker48916f92012-09-16 19:57:18 +00003295 ssl->verify_data_len = hash_len;
3296 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3297
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 ssl->out_msglen = 4 + hash_len;
3299 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3300 ssl->out_msg[0] = SSL_HS_FINISHED;
3301
3302 /*
3303 * In case of session resuming, invert the client and server
3304 * ChangeCipherSpec messages order.
3305 */
Paul Bakker0a597072012-09-25 21:55:46 +00003306 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 {
3308 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003309 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 else
3311 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3312 }
3313 else
3314 ssl->state++;
3315
Paul Bakker48916f92012-09-16 19:57:18 +00003316 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003317 * Switch to our negotiated transform and session parameters for outbound
3318 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003319 */
3320 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3321 ssl->transform_out = ssl->transform_negotiate;
3322 ssl->session_out = ssl->session_negotiate;
3323 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003324
Paul Bakker07eb38b2012-12-19 14:42:06 +01003325#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003326 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003327 {
3328 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3329 {
3330 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3331 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3332 }
3333 }
3334#endif
3335
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3337 {
3338 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3339 return( ret );
3340 }
3341
3342 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3343
3344 return( 0 );
3345}
3346
3347int ssl_parse_finished( ssl_context *ssl )
3348{
Paul Bakker23986e52011-04-24 08:57:21 +00003349 int ret;
3350 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 unsigned char buf[36];
3352
3353 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3354
Paul Bakker48916f92012-09-16 19:57:18 +00003355 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003356
Paul Bakker48916f92012-09-16 19:57:18 +00003357 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003358 * Switch to our negotiated transform and session parameters for inbound
3359 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003360 */
3361 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3362 ssl->transform_in = ssl->transform_negotiate;
3363 ssl->session_in = ssl->session_negotiate;
3364 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003365
Paul Bakker92be97b2013-01-02 17:30:03 +01003366 /*
3367 * Set the in_msg pointer to the correct location based on IV length
3368 */
3369 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3370 {
3371 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3372 ssl->transform_negotiate->fixed_ivlen;
3373 }
3374 else
3375 ssl->in_msg = ssl->in_iv;
3376
Paul Bakker07eb38b2012-12-19 14:42:06 +01003377#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003378 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003379 {
3380 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3381 {
3382 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3383 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3384 }
3385 }
3386#endif
3387
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3389 {
3390 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3391 return( ret );
3392 }
3393
3394 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3395 {
3396 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003397 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 }
3399
Paul Bakker1ef83d62012-04-11 12:09:53 +00003400 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003401 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3402
3403 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3404 ssl->in_hslen != 4 + hash_len )
3405 {
3406 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003407 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 }
3409
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003410 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003411 {
3412 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003413 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003414 }
3415
Paul Bakker48916f92012-09-16 19:57:18 +00003416 ssl->verify_data_len = hash_len;
3417 memcpy( ssl->peer_verify_data, buf, hash_len );
3418
Paul Bakker0a597072012-09-25 21:55:46 +00003419 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003420 {
3421 if( ssl->endpoint == SSL_IS_CLIENT )
3422 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3423
3424 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003425 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 }
3427 else
3428 ssl->state++;
3429
3430 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3431
3432 return( 0 );
3433}
3434
Paul Bakker968afaa2014-07-09 11:09:24 +02003435static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003436{
3437 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3438
3439#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3440 defined(POLARSSL_SSL_PROTO_TLS1_1)
3441 md5_init( &handshake->fin_md5 );
3442 sha1_init( &handshake->fin_sha1 );
3443 md5_starts( &handshake->fin_md5 );
3444 sha1_starts( &handshake->fin_sha1 );
3445#endif
3446#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3447#if defined(POLARSSL_SHA256_C)
3448 sha256_init( &handshake->fin_sha256 );
3449 sha256_starts( &handshake->fin_sha256, 0 );
3450#endif
3451#if defined(POLARSSL_SHA512_C)
3452 sha512_init( &handshake->fin_sha512 );
3453 sha512_starts( &handshake->fin_sha512, 1 );
3454#endif
3455#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3456
3457 handshake->update_checksum = ssl_update_checksum_start;
3458 handshake->sig_alg = SSL_HASH_SHA1;
3459
3460#if defined(POLARSSL_DHM_C)
3461 dhm_init( &handshake->dhm_ctx );
3462#endif
3463#if defined(POLARSSL_ECDH_C)
3464 ecdh_init( &handshake->ecdh_ctx );
3465#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003466}
3467
3468static void ssl_transform_init( ssl_transform *transform )
3469{
3470 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003471
3472 cipher_init( &transform->cipher_ctx_enc );
3473 cipher_init( &transform->cipher_ctx_dec );
3474
3475 md_init( &transform->md_ctx_enc );
3476 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003477}
3478
3479void ssl_session_init( ssl_session *session )
3480{
3481 memset( session, 0, sizeof(ssl_session) );
3482}
3483
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003484static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003485{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003486 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003487 if( ssl->transform_negotiate )
3488 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003489 if( ssl->session_negotiate )
3490 ssl_session_free( ssl->session_negotiate );
3491 if( ssl->handshake )
3492 ssl_handshake_free( ssl->handshake );
3493
3494 /*
3495 * Either the pointers are now NULL or cleared properly and can be freed.
3496 * Now allocate missing structures.
3497 */
3498 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003499 {
3500 ssl->transform_negotiate =
3501 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3502 }
Paul Bakker48916f92012-09-16 19:57:18 +00003503
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003504 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003505 {
3506 ssl->session_negotiate =
3507 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3508 }
Paul Bakker48916f92012-09-16 19:57:18 +00003509
Paul Bakker82788fb2014-10-20 13:59:19 +02003510 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003511 {
3512 ssl->handshake = (ssl_handshake_params *)
3513 polarssl_malloc( sizeof(ssl_handshake_params) );
3514 }
Paul Bakker48916f92012-09-16 19:57:18 +00003515
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003516 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003517 if( ssl->handshake == NULL ||
3518 ssl->transform_negotiate == NULL ||
3519 ssl->session_negotiate == NULL )
3520 {
3521 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003522
3523 polarssl_free( ssl->handshake );
3524 polarssl_free( ssl->transform_negotiate );
3525 polarssl_free( ssl->session_negotiate );
3526
3527 ssl->handshake = NULL;
3528 ssl->transform_negotiate = NULL;
3529 ssl->session_negotiate = NULL;
3530
Paul Bakker48916f92012-09-16 19:57:18 +00003531 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3532 }
3533
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003534 /* Initialize structures */
3535 ssl_session_init( ssl->session_negotiate );
3536 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003537 ssl_handshake_params_init( ssl->handshake );
3538
3539#if defined(POLARSSL_X509_CRT_PARSE_C)
3540 ssl->handshake->key_cert = ssl->key_cert;
3541#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003542
Paul Bakker48916f92012-09-16 19:57:18 +00003543 return( 0 );
3544}
3545
Paul Bakker5121ce52009-01-03 21:22:43 +00003546/*
3547 * Initialize an SSL context
3548 */
3549int ssl_init( ssl_context *ssl )
3550{
Paul Bakker48916f92012-09-16 19:57:18 +00003551 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003552 int len = SSL_BUFFER_LEN;
3553
3554 memset( ssl, 0, sizeof( ssl_context ) );
3555
Paul Bakker62f2dee2012-09-28 07:31:51 +00003556 /*
3557 * Sane defaults
3558 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003559 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3560 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3561 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3562 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003563
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003564 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003565
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003566 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3567
Paul Bakker62f2dee2012-09-28 07:31:51 +00003568#if defined(POLARSSL_DHM_C)
3569 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3570 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3571 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3572 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3573 {
3574 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3575 return( ret );
3576 }
3577#endif
3578
3579 /*
3580 * Prepare base structures
3581 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003582 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003583 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003584 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003585 ssl->in_msg = ssl->in_ctr + 13;
3586
3587 if( ssl->in_ctr == NULL )
3588 {
3589 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003590 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 }
3592
Paul Bakker6e339b52013-07-03 13:37:05 +02003593 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003595 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003596 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003597
3598 if( ssl->out_ctr == NULL )
3599 {
3600 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003601 polarssl_free( ssl->in_ctr );
3602 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003603 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 }
3605
3606 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3607 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3608
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003609#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3610 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3611#endif
3612
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003613#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3614 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3615#endif
3616
Paul Bakker606b4ba2013-08-14 16:52:14 +02003617#if defined(POLARSSL_SSL_SESSION_TICKETS)
3618 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3619#endif
3620
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003621#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003622 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003623#endif
3624
Paul Bakker48916f92012-09-16 19:57:18 +00003625 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3626 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
3628 return( 0 );
3629}
3630
3631/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003632 * Reset an initialized and used SSL context for re-use while retaining
3633 * all application-set variables, function pointers and data.
3634 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003635int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003636{
Paul Bakker48916f92012-09-16 19:57:18 +00003637 int ret;
3638
Paul Bakker7eb013f2011-10-06 12:37:39 +00003639 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003640 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3641 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3642
3643 ssl->verify_data_len = 0;
3644 memset( ssl->own_verify_data, 0, 36 );
3645 memset( ssl->peer_verify_data, 0, 36 );
3646
Paul Bakker7eb013f2011-10-06 12:37:39 +00003647 ssl->in_offt = NULL;
3648
Paul Bakker92be97b2013-01-02 17:30:03 +01003649 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003650 ssl->in_msgtype = 0;
3651 ssl->in_msglen = 0;
3652 ssl->in_left = 0;
3653
3654 ssl->in_hslen = 0;
3655 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003656 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003657
Paul Bakker92be97b2013-01-02 17:30:03 +01003658 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003659 ssl->out_msgtype = 0;
3660 ssl->out_msglen = 0;
3661 ssl->out_left = 0;
3662
Paul Bakker48916f92012-09-16 19:57:18 +00003663 ssl->transform_in = NULL;
3664 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003665
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003666 ssl->renego_records_seen = 0;
3667
Paul Bakker7eb013f2011-10-06 12:37:39 +00003668 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3669 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003670
3671#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003672 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003673 {
3674 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003675 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003676 {
3677 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3678 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3679 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003680 }
3681#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003682
Paul Bakker48916f92012-09-16 19:57:18 +00003683 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003684 {
Paul Bakker48916f92012-09-16 19:57:18 +00003685 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003686 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003687 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003688 }
Paul Bakker48916f92012-09-16 19:57:18 +00003689
Paul Bakkerc0463502013-02-14 11:19:38 +01003690 if( ssl->session )
3691 {
3692 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003693 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003694 ssl->session = NULL;
3695 }
3696
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003697#if defined(POLARSSL_SSL_ALPN)
3698 ssl->alpn_chosen = NULL;
3699#endif
3700
Paul Bakker48916f92012-09-16 19:57:18 +00003701 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3702 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003703
3704 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003705}
3706
Paul Bakkera503a632013-08-14 13:48:06 +02003707#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003708static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3709{
3710 aes_free( &tkeys->enc );
3711 aes_free( &tkeys->dec );
3712
3713 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3714}
3715
Paul Bakker7eb013f2011-10-06 12:37:39 +00003716/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003717 * Allocate and initialize ticket keys
3718 */
3719static int ssl_ticket_keys_init( ssl_context *ssl )
3720{
3721 int ret;
3722 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003723 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003724
3725 if( ssl->ticket_keys != NULL )
3726 return( 0 );
3727
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003728 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3729 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003730 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3731
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003732 aes_init( &tkeys->enc );
3733 aes_init( &tkeys->dec );
3734
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003735 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003736 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003737 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003738 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003739 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003740 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003741
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003742 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3743 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3744 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3745 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003746 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003747 polarssl_free( tkeys );
3748 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003749 }
3750
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003751 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003752 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003753 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003754 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003755 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003756 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003757
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003758 ssl->ticket_keys = tkeys;
3759
3760 return( 0 );
3761}
Paul Bakkera503a632013-08-14 13:48:06 +02003762#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003763
3764/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003765 * SSL set accessors
3766 */
3767void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3768{
3769 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003770
Paul Bakker606b4ba2013-08-14 16:52:14 +02003771#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003772 if( endpoint == SSL_IS_CLIENT )
3773 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003774#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003775}
3776
3777void ssl_set_authmode( ssl_context *ssl, int authmode )
3778{
3779 ssl->authmode = authmode;
3780}
3781
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003782#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003783void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003784 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003785 void *p_vrfy )
3786{
3787 ssl->f_vrfy = f_vrfy;
3788 ssl->p_vrfy = p_vrfy;
3789}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003790#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003791
Paul Bakker5121ce52009-01-03 21:22:43 +00003792void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003793 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 void *p_rng )
3795{
3796 ssl->f_rng = f_rng;
3797 ssl->p_rng = p_rng;
3798}
3799
3800void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003801 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003802 void *p_dbg )
3803{
3804 ssl->f_dbg = f_dbg;
3805 ssl->p_dbg = p_dbg;
3806}
3807
3808void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003809 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003810 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003811{
3812 ssl->f_recv = f_recv;
3813 ssl->f_send = f_send;
3814 ssl->p_recv = p_recv;
3815 ssl->p_send = p_send;
3816}
3817
Paul Bakker0a597072012-09-25 21:55:46 +00003818void ssl_set_session_cache( ssl_context *ssl,
3819 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3820 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003821{
Paul Bakker0a597072012-09-25 21:55:46 +00003822 ssl->f_get_cache = f_get_cache;
3823 ssl->p_get_cache = p_get_cache;
3824 ssl->f_set_cache = f_set_cache;
3825 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003826}
3827
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003828int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003829{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003830 int ret;
3831
3832 if( ssl == NULL ||
3833 session == NULL ||
3834 ssl->session_negotiate == NULL ||
3835 ssl->endpoint != SSL_IS_CLIENT )
3836 {
3837 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3838 }
3839
3840 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3841 return( ret );
3842
Paul Bakker0a597072012-09-25 21:55:46 +00003843 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003844
3845 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003846}
3847
Paul Bakkerb68cad62012-08-23 08:34:18 +00003848void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003849{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003850 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3851 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3852 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3853 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3854}
3855
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003856void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3857 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003858 int major, int minor )
3859{
3860 if( major != SSL_MAJOR_VERSION_3 )
3861 return;
3862
3863 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3864 return;
3865
3866 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003867}
3868
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003869#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003870/* Add a new (empty) key_cert entry an return a pointer to it */
3871static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3872{
3873 ssl_key_cert *key_cert, *last;
3874
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003875 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3876 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003877 return( NULL );
3878
3879 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3880
3881 /* Append the new key_cert to the (possibly empty) current list */
3882 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003883 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003884 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003885 if( ssl->handshake != NULL )
3886 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003887 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003888 else
3889 {
3890 last = ssl->key_cert;
3891 while( last->next != NULL )
3892 last = last->next;
3893 last->next = key_cert;
3894 }
3895
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003896 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003897}
3898
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003899void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003900 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003901{
3902 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003903 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003904 ssl->peer_cn = peer_cn;
3905}
3906
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003907int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003908 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003909{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003910 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3911
3912 if( key_cert == NULL )
3913 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3914
3915 key_cert->cert = own_cert;
3916 key_cert->key = pk_key;
3917
3918 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003919}
3920
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003921#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003922int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003923 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003924{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003925 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003926 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003927
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003928 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003929 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3930
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003931 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3932 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003933 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003934
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003935 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003936
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003937 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003938 if( ret != 0 )
3939 return( ret );
3940
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003941 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003942 return( ret );
3943
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003944 key_cert->cert = own_cert;
3945 key_cert->key_own_alloc = 1;
3946
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003947 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003948}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003949#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003950
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003951int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003952 void *rsa_key,
3953 rsa_decrypt_func rsa_decrypt,
3954 rsa_sign_func rsa_sign,
3955 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003956{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003957 int ret;
3958 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003959
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003960 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003961 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3962
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003963 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3964 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003965 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003966
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003967 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003968
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003969 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3970 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3971 return( ret );
3972
3973 key_cert->cert = own_cert;
3974 key_cert->key_own_alloc = 1;
3975
3976 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003977}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003978#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003979
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003980#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003981int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3982 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003983{
Paul Bakker6db455e2013-09-18 17:29:31 +02003984 if( psk == NULL || psk_identity == NULL )
3985 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3986
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003987 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003988 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3989
Paul Bakker6db455e2013-09-18 17:29:31 +02003990 if( ssl->psk != NULL )
3991 {
3992 polarssl_free( ssl->psk );
3993 polarssl_free( ssl->psk_identity );
3994 }
3995
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003996 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003997 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003998
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003999 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004000 ssl->psk_identity = (unsigned char *)
4001 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004002
4003 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4004 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4005
4006 memcpy( ssl->psk, psk, ssl->psk_len );
4007 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004008
4009 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004010}
4011
4012void ssl_set_psk_cb( ssl_context *ssl,
4013 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4014 size_t),
4015 void *p_psk )
4016{
4017 ssl->f_psk = f_psk;
4018 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004019}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004020#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004021
Paul Bakker48916f92012-09-16 19:57:18 +00004022#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004023int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004024{
4025 int ret;
4026
Paul Bakker48916f92012-09-16 19:57:18 +00004027 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004028 {
4029 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4030 return( ret );
4031 }
4032
Paul Bakker48916f92012-09-16 19:57:18 +00004033 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004034 {
4035 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4036 return( ret );
4037 }
4038
4039 return( 0 );
4040}
4041
Paul Bakker1b57b062011-01-06 15:48:19 +00004042int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4043{
4044 int ret;
4045
Paul Bakker66d5d072014-06-17 16:39:18 +02004046 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004047 {
4048 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4049 return( ret );
4050 }
4051
Paul Bakker66d5d072014-06-17 16:39:18 +02004052 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004053 {
4054 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4055 return( ret );
4056 }
4057
4058 return( 0 );
4059}
Paul Bakker48916f92012-09-16 19:57:18 +00004060#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004061
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004062#if defined(POLARSSL_SSL_SET_CURVES)
4063/*
4064 * Set the allowed elliptic curves
4065 */
4066void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4067{
4068 ssl->curve_list = curve_list;
4069}
4070#endif
4071
Paul Bakker0be444a2013-08-27 21:55:01 +02004072#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004073int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004074{
4075 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004076 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004077
4078 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004079
4080 if( ssl->hostname_len + 1 == 0 )
4081 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4082
Paul Bakker6e339b52013-07-03 13:37:05 +02004083 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004084
Paul Bakkerb15b8512012-01-13 13:44:06 +00004085 if( ssl->hostname == NULL )
4086 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4087
Paul Bakker3c2122f2013-06-24 19:03:14 +02004088 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004089 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004090
Paul Bakker40ea7de2009-05-03 10:18:48 +00004091 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004092
4093 return( 0 );
4094}
4095
Paul Bakker5701cdc2012-09-27 21:49:42 +00004096void ssl_set_sni( ssl_context *ssl,
4097 int (*f_sni)(void *, ssl_context *,
4098 const unsigned char *, size_t),
4099 void *p_sni )
4100{
4101 ssl->f_sni = f_sni;
4102 ssl->p_sni = p_sni;
4103}
Paul Bakker0be444a2013-08-27 21:55:01 +02004104#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004105
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004106#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004107int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004108{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004109 size_t cur_len, tot_len;
4110 const char **p;
4111
4112 /*
4113 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4114 * truncated". Check lengths now rather than later.
4115 */
4116 tot_len = 0;
4117 for( p = protos; *p != NULL; p++ )
4118 {
4119 cur_len = strlen( *p );
4120 tot_len += cur_len;
4121
4122 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4123 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4124 }
4125
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004126 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004127
4128 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004129}
4130
4131const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4132{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004133 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004134}
4135#endif /* POLARSSL_SSL_ALPN */
4136
Paul Bakker490ecc82011-10-06 13:04:09 +00004137void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4138{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004139 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4140 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4141 {
4142 ssl->max_major_ver = major;
4143 ssl->max_minor_ver = minor;
4144 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004145}
4146
Paul Bakker1d29fb52012-09-28 13:28:45 +00004147void ssl_set_min_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->min_major_ver = major;
4153 ssl->min_minor_ver = minor;
4154 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004155}
4156
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004157#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4158void ssl_set_fallback( ssl_context *ssl, char fallback )
4159{
4160 ssl->fallback = fallback;
4161}
4162#endif
4163
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004164#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4165void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4166{
4167 ssl->encrypt_then_mac = etm;
4168}
4169#endif
4170
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004171#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4172void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4173{
4174 ssl->extended_ms = ems;
4175}
4176#endif
4177
Paul Bakker05decb22013-08-15 13:33:48 +02004178#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004179int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4180{
Paul Bakker77e257e2013-12-16 15:29:52 +01004181 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004182 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004183 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004184 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004185 }
4186
4187 ssl->mfl_code = mfl_code;
4188
4189 return( 0 );
4190}
Paul Bakker05decb22013-08-15 13:33:48 +02004191#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004192
Paul Bakker1f2bc622013-08-15 13:45:55 +02004193#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004194int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004195{
4196 if( ssl->endpoint != SSL_IS_CLIENT )
4197 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4198
Paul Bakker8c1ede62013-07-19 14:14:37 +02004199 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004200
4201 return( 0 );
4202}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004203#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004204
Paul Bakker48916f92012-09-16 19:57:18 +00004205void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4206{
4207 ssl->disable_renegotiation = renegotiation;
4208}
4209
4210void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4211{
4212 ssl->allow_legacy_renegotiation = allow_legacy;
4213}
4214
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004215void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4216{
4217 ssl->renego_max_records = max_records;
4218}
4219
Paul Bakkera503a632013-08-14 13:48:06 +02004220#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004221int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4222{
4223 ssl->session_tickets = use_tickets;
4224
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004225 if( ssl->endpoint == SSL_IS_CLIENT )
4226 return( 0 );
4227
4228 if( ssl->f_rng == NULL )
4229 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4230
4231 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004232}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004233
4234void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4235{
4236 ssl->ticket_lifetime = lifetime;
4237}
Paul Bakkera503a632013-08-14 13:48:06 +02004238#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004239
Paul Bakker5121ce52009-01-03 21:22:43 +00004240/*
4241 * SSL get accessors
4242 */
Paul Bakker23986e52011-04-24 08:57:21 +00004243size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004244{
4245 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4246}
4247
Paul Bakkerff60ee62010-03-16 21:09:09 +00004248int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004249{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004250 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004251}
4252
Paul Bakkere3166ce2011-01-27 17:40:50 +00004253const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004254{
Paul Bakker926c8e42013-03-06 10:23:34 +01004255 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004256 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004257
Paul Bakkere3166ce2011-01-27 17:40:50 +00004258 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004259}
4260
Paul Bakker43ca69c2011-01-15 17:35:19 +00004261const char *ssl_get_version( const ssl_context *ssl )
4262{
4263 switch( ssl->minor_ver )
4264 {
4265 case SSL_MINOR_VERSION_0:
4266 return( "SSLv3.0" );
4267
4268 case SSL_MINOR_VERSION_1:
4269 return( "TLSv1.0" );
4270
4271 case SSL_MINOR_VERSION_2:
4272 return( "TLSv1.1" );
4273
Paul Bakker1ef83d62012-04-11 12:09:53 +00004274 case SSL_MINOR_VERSION_3:
4275 return( "TLSv1.2" );
4276
Paul Bakker43ca69c2011-01-15 17:35:19 +00004277 default:
4278 break;
4279 }
4280 return( "unknown" );
4281}
4282
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004283#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004284const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004285{
4286 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004287 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004288
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004289 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004290}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004291#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004292
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004293int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4294{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004295 if( ssl == NULL ||
4296 dst == NULL ||
4297 ssl->session == NULL ||
4298 ssl->endpoint != SSL_IS_CLIENT )
4299 {
4300 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4301 }
4302
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004303 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004304}
4305
Paul Bakker5121ce52009-01-03 21:22:43 +00004306/*
Paul Bakker1961b702013-01-25 14:49:24 +01004307 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 */
Paul Bakker1961b702013-01-25 14:49:24 +01004309int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004310{
Paul Bakker40e46942009-01-03 21:51:57 +00004311 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004312
Paul Bakker40e46942009-01-03 21:51:57 +00004313#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004314 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004315 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004316#endif
4317
Paul Bakker40e46942009-01-03 21:51:57 +00004318#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004319 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004320 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004321#endif
4322
Paul Bakker1961b702013-01-25 14:49:24 +01004323 return( ret );
4324}
4325
4326/*
4327 * Perform the SSL handshake
4328 */
4329int ssl_handshake( ssl_context *ssl )
4330{
4331 int ret = 0;
4332
4333 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4334
4335 while( ssl->state != SSL_HANDSHAKE_OVER )
4336 {
4337 ret = ssl_handshake_step( ssl );
4338
4339 if( ret != 0 )
4340 break;
4341 }
4342
Paul Bakker5121ce52009-01-03 21:22:43 +00004343 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4344
4345 return( ret );
4346}
4347
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004348#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004349/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004350 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004351 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004352static int ssl_write_hello_request( ssl_context *ssl )
4353{
4354 int ret;
4355
4356 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4357
4358 ssl->out_msglen = 4;
4359 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4360 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4361
4362 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4363 {
4364 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4365 return( ret );
4366 }
4367
4368 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4369
4370 return( 0 );
4371}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004372#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004373
4374/*
4375 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004376 * - any side: calling ssl_renegotiate(),
4377 * - client: receiving a HelloRequest during ssl_read(),
4378 * - server: receiving any handshake message on server during ssl_read() after
4379 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004380 * If the handshake doesn't complete due to waiting for I/O, it will continue
4381 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004382 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004383static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004384{
4385 int ret;
4386
4387 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4388
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004389 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4390 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004391
4392 ssl->state = SSL_HELLO_REQUEST;
4393 ssl->renegotiation = SSL_RENEGOTIATION;
4394
Paul Bakker48916f92012-09-16 19:57:18 +00004395 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4396 {
4397 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4398 return( ret );
4399 }
4400
4401 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4402
4403 return( 0 );
4404}
4405
4406/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004407 * Renegotiate current connection on client,
4408 * or request renegotiation on server
4409 */
4410int ssl_renegotiate( ssl_context *ssl )
4411{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004412 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004413
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004414#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004415 /* On server, just send the request */
4416 if( ssl->endpoint == SSL_IS_SERVER )
4417 {
4418 if( ssl->state != SSL_HANDSHAKE_OVER )
4419 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4420
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004421 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4422
4423 /* Did we already try/start sending HelloRequest? */
4424 if( ssl->out_left != 0 )
4425 return( ssl_flush_output( ssl ) );
4426
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004427 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004428 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004429#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004430
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004431#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004432 /*
4433 * On client, either start the renegotiation process or,
4434 * if already in progress, continue the handshake
4435 */
4436 if( ssl->renegotiation != SSL_RENEGOTIATION )
4437 {
4438 if( ssl->state != SSL_HANDSHAKE_OVER )
4439 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4440
4441 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4442 {
4443 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4444 return( ret );
4445 }
4446 }
4447 else
4448 {
4449 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4450 {
4451 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4452 return( ret );
4453 }
4454 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004455#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004456
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004457 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004458}
4459
4460/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004461 * Receive application data decrypted from the SSL layer
4462 */
Paul Bakker23986e52011-04-24 08:57:21 +00004463int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004464{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004465 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004466 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004467
4468 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4469
4470 if( ssl->state != SSL_HANDSHAKE_OVER )
4471 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004472 ret = ssl_handshake( ssl );
4473 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4474 {
4475 record_read = 1;
4476 }
4477 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004478 {
4479 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4480 return( ret );
4481 }
4482 }
4483
4484 if( ssl->in_offt == NULL )
4485 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004486 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004487 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004488 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4489 {
4490 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4491 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004492
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004493 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4494 return( ret );
4495 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004496 }
4497
4498 if( ssl->in_msglen == 0 &&
4499 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4500 {
4501 /*
4502 * OpenSSL sends empty messages to randomize the IV
4503 */
4504 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4505 {
Paul Bakker831a7552011-05-18 13:32:51 +00004506 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4507 return( 0 );
4508
Paul Bakker5121ce52009-01-03 21:22:43 +00004509 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4510 return( ret );
4511 }
4512 }
4513
Paul Bakker48916f92012-09-16 19:57:18 +00004514 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4515 {
4516 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4517
4518 if( ssl->endpoint == SSL_IS_CLIENT &&
4519 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4520 ssl->in_hslen != 4 ) )
4521 {
4522 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4523 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4524 }
4525
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004526 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4527 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004528 ssl->allow_legacy_renegotiation ==
4529 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004530 {
4531 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4532
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004533#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004534 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004535 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004536 /*
4537 * SSLv3 does not have a "no_renegotiation" alert
4538 */
4539 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4540 return( ret );
4541 }
4542 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004543#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004544#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4545 defined(POLARSSL_SSL_PROTO_TLS1_2)
4546 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004547 {
4548 if( ( ret = ssl_send_alert_message( ssl,
4549 SSL_ALERT_LEVEL_WARNING,
4550 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4551 {
4552 return( ret );
4553 }
Paul Bakker48916f92012-09-16 19:57:18 +00004554 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004555 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004556#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4557 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004558 {
4559 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004560 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004561 }
Paul Bakker48916f92012-09-16 19:57:18 +00004562 }
4563 else
4564 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004565 ret = ssl_start_renegotiation( ssl );
4566 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4567 {
4568 record_read = 1;
4569 }
4570 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004571 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004572 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004573 return( ret );
4574 }
Paul Bakker48916f92012-09-16 19:57:18 +00004575 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004576
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004577 /* If a non-handshake record was read during renego, fallthrough,
4578 * else tell the user they should call ssl_read() again */
4579 if( ! record_read )
4580 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004581 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004582 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4583 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004584 ssl->renego_records_seen++;
4585
4586 if( ssl->renego_max_records >= 0 &&
4587 ssl->renego_records_seen > ssl->renego_max_records )
4588 {
4589 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4590 "but not honored by client" ) );
4591 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4592 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004593 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004594
4595 /* Fatal and closure alerts handled by ssl_read_record() */
4596 if( ssl->in_msgtype == SSL_MSG_ALERT )
4597 {
4598 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4599 return( POLARSSL_ERR_NET_WANT_READ );
4600 }
4601
4602 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004603 {
4604 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004605 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 }
4607
4608 ssl->in_offt = ssl->in_msg;
4609 }
4610
4611 n = ( len < ssl->in_msglen )
4612 ? len : ssl->in_msglen;
4613
4614 memcpy( buf, ssl->in_offt, n );
4615 ssl->in_msglen -= n;
4616
4617 if( ssl->in_msglen == 0 )
4618 /* all bytes consumed */
4619 ssl->in_offt = NULL;
4620 else
4621 /* more data available */
4622 ssl->in_offt += n;
4623
4624 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4625
Paul Bakker23986e52011-04-24 08:57:21 +00004626 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004627}
4628
4629/*
4630 * Send application data to be encrypted by the SSL layer
4631 */
Paul Bakker23986e52011-04-24 08:57:21 +00004632int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004633{
Paul Bakker23986e52011-04-24 08:57:21 +00004634 int ret;
4635 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004636 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004637
4638 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4639
4640 if( ssl->state != SSL_HANDSHAKE_OVER )
4641 {
4642 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4643 {
4644 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4645 return( ret );
4646 }
4647 }
4648
Paul Bakker05decb22013-08-15 13:33:48 +02004649#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004650 /*
4651 * Assume mfl_code is correct since it was checked when set
4652 */
4653 max_len = mfl_code_to_length[ssl->mfl_code];
4654
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004655 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004656 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004657 */
4658 if( ssl->session_out != NULL &&
4659 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4660 {
4661 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4662 }
Paul Bakker05decb22013-08-15 13:33:48 +02004663#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004664
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004665 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004666
Paul Bakker5121ce52009-01-03 21:22:43 +00004667 if( ssl->out_left != 0 )
4668 {
4669 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4670 {
4671 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4672 return( ret );
4673 }
4674 }
Paul Bakker887bd502011-06-08 13:10:54 +00004675 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004676 {
Paul Bakker887bd502011-06-08 13:10:54 +00004677 ssl->out_msglen = n;
4678 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4679 memcpy( ssl->out_msg, buf, n );
4680
4681 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4682 {
4683 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4684 return( ret );
4685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004686 }
4687
4688 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4689
Paul Bakker23986e52011-04-24 08:57:21 +00004690 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004691}
4692
4693/*
4694 * Notify the peer that the connection is being closed
4695 */
4696int ssl_close_notify( ssl_context *ssl )
4697{
4698 int ret;
4699
4700 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4701
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004702 if( ssl->out_left != 0 )
4703 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004704
4705 if( ssl->state == SSL_HANDSHAKE_OVER )
4706 {
Paul Bakker48916f92012-09-16 19:57:18 +00004707 if( ( ret = ssl_send_alert_message( ssl,
4708 SSL_ALERT_LEVEL_WARNING,
4709 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004710 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004711 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004712 return( ret );
4713 }
4714 }
4715
4716 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4717
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004718 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004719}
4720
Paul Bakker48916f92012-09-16 19:57:18 +00004721void ssl_transform_free( ssl_transform *transform )
4722{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004723 if( transform == NULL )
4724 return;
4725
Paul Bakker48916f92012-09-16 19:57:18 +00004726#if defined(POLARSSL_ZLIB_SUPPORT)
4727 deflateEnd( &transform->ctx_deflate );
4728 inflateEnd( &transform->ctx_inflate );
4729#endif
4730
Paul Bakker84bbeb52014-07-01 14:53:22 +02004731 cipher_free( &transform->cipher_ctx_enc );
4732 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004733
Paul Bakker84bbeb52014-07-01 14:53:22 +02004734 md_free( &transform->md_ctx_enc );
4735 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004736
Paul Bakker34617722014-06-13 17:20:13 +02004737 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004738}
4739
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004740#if defined(POLARSSL_X509_CRT_PARSE_C)
4741static void ssl_key_cert_free( ssl_key_cert *key_cert )
4742{
4743 ssl_key_cert *cur = key_cert, *next;
4744
4745 while( cur != NULL )
4746 {
4747 next = cur->next;
4748
4749 if( cur->key_own_alloc )
4750 {
4751 pk_free( cur->key );
4752 polarssl_free( cur->key );
4753 }
4754 polarssl_free( cur );
4755
4756 cur = next;
4757 }
4758}
4759#endif /* POLARSSL_X509_CRT_PARSE_C */
4760
Paul Bakker48916f92012-09-16 19:57:18 +00004761void ssl_handshake_free( ssl_handshake_params *handshake )
4762{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004763 if( handshake == NULL )
4764 return;
4765
Paul Bakker48916f92012-09-16 19:57:18 +00004766#if defined(POLARSSL_DHM_C)
4767 dhm_free( &handshake->dhm_ctx );
4768#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004769#if defined(POLARSSL_ECDH_C)
4770 ecdh_free( &handshake->ecdh_ctx );
4771#endif
4772
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004773#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004774 /* explicit void pointer cast for buggy MS compiler */
4775 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004776#endif
4777
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004778#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4779 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4780 /*
4781 * Free only the linked list wrapper, not the keys themselves
4782 * since the belong to the SNI callback
4783 */
4784 if( handshake->sni_key_cert != NULL )
4785 {
4786 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4787
4788 while( cur != NULL )
4789 {
4790 next = cur->next;
4791 polarssl_free( cur );
4792 cur = next;
4793 }
4794 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004795#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004796
Paul Bakker34617722014-06-13 17:20:13 +02004797 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004798}
4799
4800void ssl_session_free( ssl_session *session )
4801{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004802 if( session == NULL )
4803 return;
4804
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004805#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004806 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004807 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004808 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004809 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004810 }
Paul Bakkered27a042013-04-18 22:46:23 +02004811#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004812
Paul Bakkera503a632013-08-14 13:48:06 +02004813#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004814 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004815#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004816
Paul Bakker34617722014-06-13 17:20:13 +02004817 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004818}
4819
Paul Bakker5121ce52009-01-03 21:22:43 +00004820/*
4821 * Free an SSL context
4822 */
4823void ssl_free( ssl_context *ssl )
4824{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004825 if( ssl == NULL )
4826 return;
4827
Paul Bakker5121ce52009-01-03 21:22:43 +00004828 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4829
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 if( ssl->out_ctr != NULL )
4831 {
Paul Bakker34617722014-06-13 17:20:13 +02004832 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004833 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004834 }
4835
4836 if( ssl->in_ctr != NULL )
4837 {
Paul Bakker34617722014-06-13 17:20:13 +02004838 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004839 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004840 }
4841
Paul Bakker16770332013-10-11 09:59:44 +02004842#if defined(POLARSSL_ZLIB_SUPPORT)
4843 if( ssl->compress_buf != NULL )
4844 {
Paul Bakker34617722014-06-13 17:20:13 +02004845 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004846 polarssl_free( ssl->compress_buf );
4847 }
4848#endif
4849
Paul Bakker40e46942009-01-03 21:51:57 +00004850#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004851 mpi_free( &ssl->dhm_P );
4852 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004853#endif
4854
Paul Bakker48916f92012-09-16 19:57:18 +00004855 if( ssl->transform )
4856 {
4857 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004858 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004859 }
4860
4861 if( ssl->handshake )
4862 {
4863 ssl_handshake_free( ssl->handshake );
4864 ssl_transform_free( ssl->transform_negotiate );
4865 ssl_session_free( ssl->session_negotiate );
4866
Paul Bakker6e339b52013-07-03 13:37:05 +02004867 polarssl_free( ssl->handshake );
4868 polarssl_free( ssl->transform_negotiate );
4869 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004870 }
4871
Paul Bakkerc0463502013-02-14 11:19:38 +01004872 if( ssl->session )
4873 {
4874 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004875 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004876 }
4877
Paul Bakkera503a632013-08-14 13:48:06 +02004878#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004879 if( ssl->ticket_keys )
4880 {
4881 ssl_ticket_keys_free( ssl->ticket_keys );
4882 polarssl_free( ssl->ticket_keys );
4883 }
Paul Bakkera503a632013-08-14 13:48:06 +02004884#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004885
Paul Bakker0be444a2013-08-27 21:55:01 +02004886#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004887 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004888 {
Paul Bakker34617722014-06-13 17:20:13 +02004889 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004890 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004891 ssl->hostname_len = 0;
4892 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004893#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004894
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004895#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004896 if( ssl->psk != NULL )
4897 {
Paul Bakker34617722014-06-13 17:20:13 +02004898 polarssl_zeroize( ssl->psk, ssl->psk_len );
4899 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004900 polarssl_free( ssl->psk );
4901 polarssl_free( ssl->psk_identity );
4902 ssl->psk_len = 0;
4903 ssl->psk_identity_len = 0;
4904 }
4905#endif
4906
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004907#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004908 ssl_key_cert_free( ssl->key_cert );
4909#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004910
Paul Bakker05ef8352012-05-08 09:17:57 +00004911#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4912 if( ssl_hw_record_finish != NULL )
4913 {
4914 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4915 ssl_hw_record_finish( ssl );
4916 }
4917#endif
4918
Paul Bakker5121ce52009-01-03 21:22:43 +00004919 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004920
Paul Bakker86f04f42013-02-14 11:20:09 +01004921 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004922 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004923}
4924
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004925#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004926/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004927 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004928 */
4929unsigned char ssl_sig_from_pk( pk_context *pk )
4930{
4931#if defined(POLARSSL_RSA_C)
4932 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4933 return( SSL_SIG_RSA );
4934#endif
4935#if defined(POLARSSL_ECDSA_C)
4936 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4937 return( SSL_SIG_ECDSA );
4938#endif
4939 return( SSL_SIG_ANON );
4940}
4941
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004942pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4943{
4944 switch( sig )
4945 {
4946#if defined(POLARSSL_RSA_C)
4947 case SSL_SIG_RSA:
4948 return( POLARSSL_PK_RSA );
4949#endif
4950#if defined(POLARSSL_ECDSA_C)
4951 case SSL_SIG_ECDSA:
4952 return( POLARSSL_PK_ECDSA );
4953#endif
4954 default:
4955 return( POLARSSL_PK_NONE );
4956 }
4957}
Paul Bakker9af723c2014-05-01 13:03:14 +02004958#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004959
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004960/*
4961 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4962 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004963md_type_t ssl_md_alg_from_hash( unsigned char hash )
4964{
4965 switch( hash )
4966 {
4967#if defined(POLARSSL_MD5_C)
4968 case SSL_HASH_MD5:
4969 return( POLARSSL_MD_MD5 );
4970#endif
4971#if defined(POLARSSL_SHA1_C)
4972 case SSL_HASH_SHA1:
4973 return( POLARSSL_MD_SHA1 );
4974#endif
4975#if defined(POLARSSL_SHA256_C)
4976 case SSL_HASH_SHA224:
4977 return( POLARSSL_MD_SHA224 );
4978 case SSL_HASH_SHA256:
4979 return( POLARSSL_MD_SHA256 );
4980#endif
4981#if defined(POLARSSL_SHA512_C)
4982 case SSL_HASH_SHA384:
4983 return( POLARSSL_MD_SHA384 );
4984 case SSL_HASH_SHA512:
4985 return( POLARSSL_MD_SHA512 );
4986#endif
4987 default:
4988 return( POLARSSL_MD_NONE );
4989 }
4990}
4991
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004992#if defined(POLARSSL_SSL_SET_CURVES)
4993/*
4994 * Check is a curve proposed by the peer is in our list.
4995 * Return 1 if we're willing to use it, 0 otherwise.
4996 */
4997int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4998{
4999 const ecp_group_id *gid;
5000
5001 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5002 if( *gid == grp_id )
5003 return( 1 );
5004
5005 return( 0 );
5006}
Paul Bakker9af723c2014-05-01 13:03:14 +02005007#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005008
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005009#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005010int ssl_check_cert_usage( const x509_crt *cert,
5011 const ssl_ciphersuite_t *ciphersuite,
5012 int cert_endpoint )
5013{
5014#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5015 int usage = 0;
5016#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005017#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5018 const char *ext_oid;
5019 size_t ext_len;
5020#endif
5021
5022#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5023 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5024 ((void) cert);
5025 ((void) cert_endpoint);
5026#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005027
5028#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5029 if( cert_endpoint == SSL_IS_SERVER )
5030 {
5031 /* Server part of the key exchange */
5032 switch( ciphersuite->key_exchange )
5033 {
5034 case POLARSSL_KEY_EXCHANGE_RSA:
5035 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5036 usage = KU_KEY_ENCIPHERMENT;
5037 break;
5038
5039 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5040 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5041 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5042 usage = KU_DIGITAL_SIGNATURE;
5043 break;
5044
5045 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5046 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5047 usage = KU_KEY_AGREEMENT;
5048 break;
5049
5050 /* Don't use default: we want warnings when adding new values */
5051 case POLARSSL_KEY_EXCHANGE_NONE:
5052 case POLARSSL_KEY_EXCHANGE_PSK:
5053 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5054 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5055 usage = 0;
5056 }
5057 }
5058 else
5059 {
5060 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5061 usage = KU_DIGITAL_SIGNATURE;
5062 }
5063
5064 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5065 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005066#else
5067 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005068#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5069
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005070#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5071 if( cert_endpoint == SSL_IS_SERVER )
5072 {
5073 ext_oid = OID_SERVER_AUTH;
5074 ext_len = OID_SIZE( OID_SERVER_AUTH );
5075 }
5076 else
5077 {
5078 ext_oid = OID_CLIENT_AUTH;
5079 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5080 }
5081
5082 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5083 return( -1 );
5084#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5085
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005086 return( 0 );
5087}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005088#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005089
5090#endif /* POLARSSL_SSL_TLS_C */