blob: 4132e47aa41630a425685024ab2e14407c3420be [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:
604 * first multiple of blocklen greater than maclen
605 * + IV except for SSL3 and TLS 1.0
606 */
607 transform->minlen = transform->maclen
608 + cipher_info->block_size
609 - transform->maclen % cipher_info->block_size;
610
611#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
612 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
613 ssl->minor_ver == SSL_MINOR_VERSION_1 )
614 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100615 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200616#endif
617#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
618 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
619 ssl->minor_ver == SSL_MINOR_VERSION_3 )
620 {
621 transform->minlen += transform->ivlen;
622 }
623 else
624#endif
625 {
626 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
627 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
628 }
Paul Bakker68884e32013-01-07 18:20:04 +0100629 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 }
631
632 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000633 transform->keylen, transform->minlen, transform->ivlen,
634 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 /*
637 * Finally setup the cipher contexts, IVs and MAC secrets.
638 */
639 if( ssl->endpoint == SSL_IS_CLIENT )
640 {
Paul Bakker48916f92012-09-16 19:57:18 +0000641 key1 = keyblk + transform->maclen * 2;
642 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
Paul Bakker68884e32013-01-07 18:20:04 +0100644 mac_enc = keyblk;
645 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000647 /*
648 * This is not used in TLS v1.1.
649 */
Paul Bakker48916f92012-09-16 19:57:18 +0000650 iv_copy_len = ( transform->fixed_ivlen ) ?
651 transform->fixed_ivlen : transform->ivlen;
652 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
653 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000654 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 }
656 else
657 {
Paul Bakker48916f92012-09-16 19:57:18 +0000658 key1 = keyblk + transform->maclen * 2 + transform->keylen;
659 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Paul Bakker68884e32013-01-07 18:20:04 +0100661 mac_enc = keyblk + transform->maclen;
662 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000664 /*
665 * This is not used in TLS v1.1.
666 */
Paul Bakker48916f92012-09-16 19:57:18 +0000667 iv_copy_len = ( transform->fixed_ivlen ) ?
668 transform->fixed_ivlen : transform->ivlen;
669 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
670 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000671 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 }
673
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200674#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100675 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
676 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100677 if( transform->maclen > sizeof transform->mac_enc )
678 {
679 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200680 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100681 }
682
Paul Bakker68884e32013-01-07 18:20:04 +0100683 memcpy( transform->mac_enc, mac_enc, transform->maclen );
684 memcpy( transform->mac_dec, mac_dec, transform->maclen );
685 }
686 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200687#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200688#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
689 defined(POLARSSL_SSL_PROTO_TLS1_2)
690 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100691 {
692 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
693 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
694 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200695 else
696#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200697 {
698 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200699 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200700 }
Paul Bakker68884e32013-01-07 18:20:04 +0100701
Paul Bakker05ef8352012-05-08 09:17:57 +0000702#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200703 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000704 {
705 int ret = 0;
706
707 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
708
Paul Bakker07eb38b2012-12-19 14:42:06 +0100709 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
710 transform->iv_enc, transform->iv_dec,
711 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100712 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100713 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000714 {
715 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200716 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000717 }
718 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200719#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000720
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200721 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
722 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200724 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
725 return( ret );
726 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200727
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200728 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
729 cipher_info ) ) != 0 )
730 {
731 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
732 return( ret );
733 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200734
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200735 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
736 cipher_info->key_length,
737 POLARSSL_ENCRYPT ) ) != 0 )
738 {
739 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
740 return( ret );
741 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200742
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200743 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
744 cipher_info->key_length,
745 POLARSSL_DECRYPT ) ) != 0 )
746 {
747 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
748 return( ret );
749 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200750
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200751#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200752 if( cipher_info->mode == POLARSSL_MODE_CBC )
753 {
754 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
755 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200756 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200757 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
758 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200759 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200760
761 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
762 POLARSSL_PADDING_NONE ) ) != 0 )
763 {
764 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
765 return( ret );
766 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200768#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Paul Bakker34617722014-06-13 17:20:13 +0200770 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
Paul Bakker2770fbd2012-07-03 13:30:23 +0000772#if defined(POLARSSL_ZLIB_SUPPORT)
773 // Initialize compression
774 //
Paul Bakker48916f92012-09-16 19:57:18 +0000775 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000776 {
Paul Bakker16770332013-10-11 09:59:44 +0200777 if( ssl->compress_buf == NULL )
778 {
779 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
780 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
781 if( ssl->compress_buf == NULL )
782 {
783 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
784 SSL_BUFFER_LEN ) );
785 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
786 }
787 }
788
Paul Bakker2770fbd2012-07-03 13:30:23 +0000789 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
790
Paul Bakker48916f92012-09-16 19:57:18 +0000791 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
792 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000793
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200794 if( deflateInit( &transform->ctx_deflate,
795 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000796 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000797 {
798 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
799 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
800 }
801 }
802#endif /* POLARSSL_ZLIB_SUPPORT */
803
Paul Bakker5121ce52009-01-03 21:22:43 +0000804 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
805
806 return( 0 );
807}
808
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200809#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000810void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000811{
812 md5_context md5;
813 sha1_context sha1;
814 unsigned char pad_1[48];
815 unsigned char pad_2[48];
816
Paul Bakker380da532012-04-18 16:10:25 +0000817 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000818
Paul Bakker48916f92012-09-16 19:57:18 +0000819 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
820 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821
Paul Bakker380da532012-04-18 16:10:25 +0000822 memset( pad_1, 0x36, 48 );
823 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Paul Bakker48916f92012-09-16 19:57:18 +0000825 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000826 md5_update( &md5, pad_1, 48 );
827 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000828
Paul Bakker380da532012-04-18 16:10:25 +0000829 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000830 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000831 md5_update( &md5, pad_2, 48 );
832 md5_update( &md5, hash, 16 );
833 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Paul Bakker48916f92012-09-16 19:57:18 +0000835 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000836 sha1_update( &sha1, pad_1, 40 );
837 sha1_finish( &sha1, hash + 16 );
838
839 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000840 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000841 sha1_update( &sha1, pad_2, 40 );
842 sha1_update( &sha1, hash + 16, 20 );
843 sha1_finish( &sha1, hash + 16 );
844
845 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
846 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
847
Paul Bakker5b4af392014-06-26 12:09:34 +0200848 md5_free( &md5 );
849 sha1_free( &sha1 );
850
Paul Bakker380da532012-04-18 16:10:25 +0000851 return;
852}
Paul Bakker9af723c2014-05-01 13:03:14 +0200853#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000854
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000856void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
857{
858 md5_context md5;
859 sha1_context sha1;
860
861 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
862
Paul Bakker48916f92012-09-16 19:57:18 +0000863 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
864 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000865
Paul Bakker48916f92012-09-16 19:57:18 +0000866 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000867 sha1_finish( &sha1, hash + 16 );
868
869 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
870 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
871
Paul Bakker5b4af392014-06-26 12:09:34 +0200872 md5_free( &md5 );
873 sha1_free( &sha1 );
874
Paul Bakker380da532012-04-18 16:10:25 +0000875 return;
876}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200877#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000878
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200879#if defined(POLARSSL_SSL_PROTO_TLS1_2)
880#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000881void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
882{
Paul Bakker9e36f042013-06-30 14:34:05 +0200883 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000884
885 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
886
Paul Bakker9e36f042013-06-30 14:34:05 +0200887 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
888 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000889
890 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
891 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
892
Paul Bakker5b4af392014-06-26 12:09:34 +0200893 sha256_free( &sha256 );
894
Paul Bakker380da532012-04-18 16:10:25 +0000895 return;
896}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200897#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000898
Paul Bakker9e36f042013-06-30 14:34:05 +0200899#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000900void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
901{
Paul Bakker9e36f042013-06-30 14:34:05 +0200902 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000903
904 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
905
Paul Bakker9e36f042013-06-30 14:34:05 +0200906 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
907 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Paul Bakkerca4ab492012-04-18 14:23:57 +0000909 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
911
Paul Bakker5b4af392014-06-26 12:09:34 +0200912 sha512_free( &sha512 );
913
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 return;
915}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#endif /* POLARSSL_SHA512_C */
917#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200919#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200920int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
921{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200922 unsigned char *p = ssl->handshake->premaster;
923 unsigned char *end = p + sizeof( ssl->handshake->premaster );
924
925 /*
926 * PMS = struct {
927 * opaque other_secret<0..2^16-1>;
928 * opaque psk<0..2^16-1>;
929 * };
930 * with "other_secret" depending on the particular key exchange
931 */
932#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
933 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
934 {
935 if( end - p < 2 + (int) ssl->psk_len )
936 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
937
938 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
939 *(p++) = (unsigned char)( ssl->psk_len );
940 p += ssl->psk_len;
941 }
942 else
943#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200944#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
945 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
946 {
947 /*
948 * other_secret already set by the ClientKeyExchange message,
949 * and is 48 bytes long
950 */
951 *p++ = 0;
952 *p++ = 48;
953 p += 48;
954 }
955 else
956#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200957#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
958 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
959 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200960 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200961 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200963 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200964 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200965 p + 2, &len,
966 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200967 {
968 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
969 return( ret );
970 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200971 *(p++) = (unsigned char)( len >> 8 );
972 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200973 p += len;
974
975 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
976 }
977 else
978#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
979#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
980 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
981 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200982 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983 size_t zlen;
984
985 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200986 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200987 ssl->f_rng, ssl->p_rng ) ) != 0 )
988 {
989 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
990 return( ret );
991 }
992
993 *(p++) = (unsigned char)( zlen >> 8 );
994 *(p++) = (unsigned char)( zlen );
995 p += zlen;
996
997 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
998 }
999 else
1000#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1001 {
1002 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001003 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 }
1005
1006 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001007 if( end - p < 2 + (int) ssl->psk_len )
1008 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1009
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001010 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1011 *(p++) = (unsigned char)( ssl->psk_len );
1012 memcpy( p, ssl->psk, ssl->psk_len );
1013 p += ssl->psk_len;
1014
1015 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1016
1017 return( 0 );
1018}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001019#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001020
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001021#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001022/*
1023 * SSLv3.0 MAC functions
1024 */
Paul Bakker68884e32013-01-07 18:20:04 +01001025static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1026 unsigned char *buf, size_t len,
1027 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001028{
1029 unsigned char header[11];
1030 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001031 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001032 int md_size = md_get_size( md_ctx->md_info );
1033 int md_type = md_get_type( md_ctx->md_info );
1034
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001035 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001036 if( md_type == POLARSSL_MD_MD5 )
1037 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001038 else
Paul Bakker68884e32013-01-07 18:20:04 +01001039 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
1041 memcpy( header, ctr, 8 );
1042 header[ 8] = (unsigned char) type;
1043 header[ 9] = (unsigned char)( len >> 8 );
1044 header[10] = (unsigned char)( len );
1045
Paul Bakker68884e32013-01-07 18:20:04 +01001046 memset( padding, 0x36, padlen );
1047 md_starts( md_ctx );
1048 md_update( md_ctx, secret, md_size );
1049 md_update( md_ctx, padding, padlen );
1050 md_update( md_ctx, header, 11 );
1051 md_update( md_ctx, buf, len );
1052 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Paul Bakker68884e32013-01-07 18:20:04 +01001054 memset( padding, 0x5C, padlen );
1055 md_starts( md_ctx );
1056 md_update( md_ctx, secret, md_size );
1057 md_update( md_ctx, padding, padlen );
1058 md_update( md_ctx, buf + len, md_size );
1059 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001060}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001061#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001062
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001063#define MAC_NONE 0
1064#define MAC_PLAINTEXT 1
1065#define MAC_CIPHERTEXT 2
1066
1067/*
1068 * Is MAC applied on ciphertext, cleartext or not at all?
1069 */
1070static char ssl_get_mac_order( ssl_context *ssl,
1071 const ssl_session *session,
1072 cipher_mode_t mode )
1073{
1074#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
1075 if( mode == POLARSSL_MODE_STREAM )
1076 return( MAC_PLAINTEXT );
1077#endif
1078
1079#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1080 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
1081 if( mode == POLARSSL_MODE_CBC )
1082 {
1083#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1084 if( session != NULL && session->encrypt_then_mac == SSL_ETM_ENABLED )
1085 {
1086 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1087 return( MAC_CIPHERTEXT );
1088 }
1089#endif
1090
1091 return( MAC_PLAINTEXT );
1092 }
1093#endif
1094
1095 return( MAC_NONE );
1096}
1097
Paul Bakker5121ce52009-01-03 21:22:43 +00001098/*
1099 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001100 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001101static int ssl_encrypt_buf( ssl_context *ssl )
1102{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001103 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001104 const cipher_mode_t mode = cipher_get_cipher_mode(
1105 &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001106 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
1108 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1109
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001110 mac_order = ssl_get_mac_order( ssl, ssl->session_out, mode );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001111
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001113 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001115#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1116 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1117 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001118 if( mac_order == MAC_PLAINTEXT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001120#if defined(POLARSSL_SSL_PROTO_SSL3)
1121 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1122 {
1123 ssl_mac( &ssl->transform_out->md_ctx_enc,
1124 ssl->transform_out->mac_enc,
1125 ssl->out_msg, ssl->out_msglen,
1126 ssl->out_ctr, ssl->out_msgtype );
1127 }
1128 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001129#endif
1130#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001131 defined(POLARSSL_SSL_PROTO_TLS1_2)
1132 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1133 {
1134 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1135 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1136 ssl->out_msg, ssl->out_msglen );
1137 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1138 ssl->out_msg + ssl->out_msglen );
1139 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1140 }
1141 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001142#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001143 {
1144 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001145 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001146 }
1147
1148 SSL_DEBUG_BUF( 4, "computed mac",
1149 ssl->out_msg + ssl->out_msglen,
1150 ssl->transform_out->maclen );
1151
1152 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001153 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001154#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001156 /*
1157 * Encrypt
1158 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001159#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001160 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001162 int ret;
1163 size_t olen = 0;
1164
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1166 "including %d bytes of padding",
1167 ssl->out_msglen, 0 ) );
1168
1169 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1170 ssl->out_msg, ssl->out_msglen );
1171
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001172 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001173 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001174 ssl->transform_out->ivlen,
1175 ssl->out_msg, ssl->out_msglen,
1176 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001177 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001178 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001179 return( ret );
1180 }
1181
1182 if( ssl->out_msglen != olen )
1183 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001184 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001185 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001186 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 }
Paul Bakker68884e32013-01-07 18:20:04 +01001188 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001189#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001190#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1191 if( mode == POLARSSL_MODE_GCM ||
1192 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001193 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001194 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001195 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001196 unsigned char *enc_msg;
1197 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001198 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1199 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001200
Paul Bakkerca4ab492012-04-18 14:23:57 +00001201 memcpy( add_data, ssl->out_ctr, 8 );
1202 add_data[8] = ssl->out_msgtype;
1203 add_data[9] = ssl->major_ver;
1204 add_data[10] = ssl->minor_ver;
1205 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1206 add_data[12] = ssl->out_msglen & 0xFF;
1207
1208 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1209 add_data, 13 );
1210
Paul Bakker68884e32013-01-07 18:20:04 +01001211 /*
1212 * Generate IV
1213 */
1214 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001215 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1216 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001217 if( ret != 0 )
1218 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001219
Paul Bakker68884e32013-01-07 18:20:04 +01001220 memcpy( ssl->out_iv,
1221 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1222 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001223
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001224 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001225 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001226
Paul Bakker68884e32013-01-07 18:20:04 +01001227 /*
1228 * Fix pointer positions and message length with added IV
1229 */
1230 enc_msg = ssl->out_msg;
1231 enc_msglen = ssl->out_msglen;
1232 ssl->out_msglen += ssl->transform_out->ivlen -
1233 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001234
Paul Bakker68884e32013-01-07 18:20:04 +01001235 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1236 "including %d bytes of padding",
1237 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001238
Paul Bakker68884e32013-01-07 18:20:04 +01001239 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1240 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001241
Paul Bakker68884e32013-01-07 18:20:04 +01001242 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001243 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001244 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001245 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1246 ssl->transform_out->iv_enc,
1247 ssl->transform_out->ivlen,
1248 add_data, 13,
1249 enc_msg, enc_msglen,
1250 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001251 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001252 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001253 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001254 return( ret );
1255 }
1256
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001257 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001258 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001259 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001260 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001261 }
1262
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001263 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001264
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001265 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001268#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001269#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1270 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001271 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001273 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001274 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001275 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001276
Paul Bakker48916f92012-09-16 19:57:18 +00001277 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1278 ssl->transform_out->ivlen;
1279 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 padlen = 0;
1281
1282 for( i = 0; i <= padlen; i++ )
1283 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1284
1285 ssl->out_msglen += padlen + 1;
1286
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001287 enc_msglen = ssl->out_msglen;
1288 enc_msg = ssl->out_msg;
1289
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001290#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001291 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001292 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1293 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001294 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001295 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001296 {
1297 /*
1298 * Generate IV
1299 */
Paul Bakker48916f92012-09-16 19:57:18 +00001300 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1301 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001302 if( ret != 0 )
1303 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001304
Paul Bakker92be97b2013-01-02 17:30:03 +01001305 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001306 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001307
1308 /*
1309 * Fix pointer positions and message length with added IV
1310 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001311 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001312 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001313 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001314 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001315#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001316
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001318 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001319 ssl->out_msglen, ssl->transform_out->ivlen,
1320 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
1322 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001323 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001325 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001326 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001327 ssl->transform_out->ivlen,
1328 enc_msg, enc_msglen,
1329 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001330 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001331 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001332 return( ret );
1333 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001334
Paul Bakkercca5b812013-08-31 17:40:26 +02001335 if( enc_msglen != olen )
1336 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001337 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001338 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001339 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001340
1341#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001342 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1343 {
1344 /*
1345 * Save IV in SSL3 and TLS1
1346 */
1347 memcpy( ssl->transform_out->iv_enc,
1348 ssl->transform_out->cipher_ctx_enc.iv,
1349 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001351#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001352
1353#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1354 if( mac_order == MAC_CIPHERTEXT )
1355 {
1356 /*
1357 * MAC(MAC_write_key, seq_num +
1358 * TLSCipherText.type +
1359 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001360 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001361 * IV + // except for TLS 1.0
1362 * ENC(content + padding + padding_length));
1363 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001364 unsigned char pseudo_hdr[13];
1365
1366 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1367 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001368 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1369 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1370
1371 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001372
1373 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1374 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1375 ssl->out_iv, ssl->out_msglen );
1376 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1377 ssl->out_iv + ssl->out_msglen );
1378 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1379
1380 ssl->out_msglen += ssl->transform_out->maclen;
1381 }
1382#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001384 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001385#endif /* POLARSSL_CIPHER_MODE_CBC &&
1386 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001387 {
1388 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001389 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001390 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Paul Bakkerca4ab492012-04-18 14:23:57 +00001392 for( i = 8; i > 0; i-- )
1393 if( ++ssl->out_ctr[i - 1] != 0 )
1394 break;
1395
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001396 /* The loops goes to its end iff the counter is wrapping */
1397 if( i == 0 )
1398 {
1399 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1400 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1401 }
1402
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1404
1405 return( 0 );
1406}
1407
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001408#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001409
Paul Bakker5121ce52009-01-03 21:22:43 +00001410static int ssl_decrypt_buf( ssl_context *ssl )
1411{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001412 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001413 const cipher_mode_t mode = cipher_get_cipher_mode(
1414 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001415#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1416 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1417 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1418 size_t padlen = 0, correct = 1;
1419#endif
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001420 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1423
Paul Bakker48916f92012-09-16 19:57:18 +00001424 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 {
1426 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001427 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001428 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 }
1430
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001431 mac_order = ssl_get_mac_order( ssl, ssl->session_in, mode );
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001432
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001433#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001434 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001435 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001436 int ret;
1437 size_t olen = 0;
1438
Paul Bakker68884e32013-01-07 18:20:04 +01001439 padlen = 0;
1440
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001441 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001442 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001443 ssl->transform_in->ivlen,
1444 ssl->in_msg, ssl->in_msglen,
1445 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001446 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001447 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001448 return( ret );
1449 }
1450
1451 if( ssl->in_msglen != olen )
1452 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001453 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001454 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001455 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 }
Paul Bakker68884e32013-01-07 18:20:04 +01001457 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001458#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001459#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1460 if( mode == POLARSSL_MODE_GCM ||
1461 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001462 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001463 int ret;
1464 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001465 unsigned char *dec_msg;
1466 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001467 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001468 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1469 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001470 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1471 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001472
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001473 if( ssl->in_msglen < explicit_iv_len + taglen )
1474 {
1475 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1476 "+ taglen (%d)", ssl->in_msglen,
1477 explicit_iv_len, taglen ) );
1478 return( POLARSSL_ERR_SSL_INVALID_MAC );
1479 }
1480 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1481
Paul Bakker68884e32013-01-07 18:20:04 +01001482 dec_msg = ssl->in_msg;
1483 dec_msg_result = ssl->in_msg;
1484 ssl->in_msglen = dec_msglen;
1485
1486 memcpy( add_data, ssl->in_ctr, 8 );
1487 add_data[8] = ssl->in_msgtype;
1488 add_data[9] = ssl->major_ver;
1489 add_data[10] = ssl->minor_ver;
1490 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1491 add_data[12] = ssl->in_msglen & 0xFF;
1492
1493 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1494 add_data, 13 );
1495
1496 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1497 ssl->in_iv,
1498 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1499
1500 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1501 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001502 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001503
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001504 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001505 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001506 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001507 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1508 ssl->transform_in->iv_dec,
1509 ssl->transform_in->ivlen,
1510 add_data, 13,
1511 dec_msg, dec_msglen,
1512 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001513 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001514 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001515 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1516
1517 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1518 return( POLARSSL_ERR_SSL_INVALID_MAC );
1519
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001520 return( ret );
1521 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001522
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001523 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001524 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001525 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001526 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001527 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001528 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001529 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001530#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001531#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1532 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001533 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 {
Paul Bakker45829992013-01-03 14:52:21 +01001535 /*
1536 * Decrypt and check the padding
1537 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001538 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001539 unsigned char *dec_msg;
1540 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001541 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001542 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001543 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001544
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 /*
Paul Bakker45829992013-01-03 14:52:21 +01001546 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001548#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001549 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1550 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001551#endif
Paul Bakker45829992013-01-03 14:52:21 +01001552
1553 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1554 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1555 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001556 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1557 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1558 ssl->transform_in->ivlen,
1559 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001560 return( POLARSSL_ERR_SSL_INVALID_MAC );
1561 }
1562
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001563 dec_msglen = ssl->in_msglen;
1564 dec_msg = ssl->in_msg;
1565 dec_msg_result = ssl->in_msg;
1566
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001567 /*
1568 * Authenticate before decrypt if enabled
1569 */
1570#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1571 if( mac_order == MAC_CIPHERTEXT )
1572 {
1573 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001574 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001575
1576 dec_msglen -= ssl->transform_in->maclen;
1577 ssl->in_msglen -= ssl->transform_in->maclen;
1578
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001579 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1580 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1581 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1582 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1583
1584 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1585
1586 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001587 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1588 ssl->in_iv, ssl->in_msglen );
1589 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1590 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1591
1592 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1593 ssl->transform_in->maclen );
1594 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1595 ssl->transform_in->maclen );
1596
1597 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1598 ssl->transform_in->maclen ) != 0 )
1599 {
1600 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1601
1602 return( POLARSSL_ERR_SSL_INVALID_MAC );
1603 }
1604 }
1605#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1606
1607 /*
1608 * Check length sanity
1609 */
1610 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1611 {
1612 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1613 ssl->in_msglen, ssl->transform_in->ivlen ) );
1614 return( POLARSSL_ERR_SSL_INVALID_MAC );
1615 }
1616
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001617#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001618 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001619 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001620 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001621 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001622 {
Paul Bakker48916f92012-09-16 19:57:18 +00001623 dec_msglen -= ssl->transform_in->ivlen;
1624 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001625
Paul Bakker48916f92012-09-16 19:57:18 +00001626 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001627 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001628 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001629#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001630
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001631 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001632 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001633 ssl->transform_in->ivlen,
1634 dec_msg, dec_msglen,
1635 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001636 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001637 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001638 return( ret );
1639 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001640
Paul Bakkercca5b812013-08-31 17:40:26 +02001641 if( dec_msglen != olen )
1642 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001643 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001644 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001645 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001646
1647#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001648 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1649 {
1650 /*
1651 * Save IV in SSL3 and TLS1
1652 */
1653 memcpy( ssl->transform_in->iv_dec,
1654 ssl->transform_in->cipher_ctx_dec.iv,
1655 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001657#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
1659 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001660
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001661 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
1662 mac_order == MAC_PLAINTEXT )
Paul Bakker45829992013-01-03 14:52:21 +01001663 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001664#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001665 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1666 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001667#endif
Paul Bakker45829992013-01-03 14:52:21 +01001668 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001669 correct = 0;
1670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001672#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001673 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1674 {
Paul Bakker48916f92012-09-16 19:57:18 +00001675 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001676 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001677#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1679 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001680 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001681#endif
Paul Bakker45829992013-01-03 14:52:21 +01001682 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001683 }
1684 }
1685 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001686#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001687#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1688 defined(POLARSSL_SSL_PROTO_TLS1_2)
1689 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001690 {
1691 /*
Paul Bakker45829992013-01-03 14:52:21 +01001692 * TLSv1+: always check the padding up to the first failure
1693 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001694 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001695 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001696 size_t padding_idx = ssl->in_msglen - padlen - 1;
1697
Paul Bakker956c9e02013-12-19 14:42:28 +01001698 /*
1699 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001700 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001701 *
Paul Bakker61885c72014-04-25 12:59:03 +02001702 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1703 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001704 *
1705 * In both cases we reset padding_idx to a safe value (0) to
1706 * prevent out-of-buffer reads.
1707 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001708 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001709 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1710 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001711
1712 padding_idx *= correct;
1713
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001714 for( i = 1; i <= 256; i++ )
1715 {
1716 real_count &= ( i <= padlen );
1717 pad_count += real_count *
1718 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1719 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001720
1721 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001722
Paul Bakkerd66f0702013-01-31 16:57:45 +01001723#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001724 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001725 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001726#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001727 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001728 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001729 else
1730#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1731 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001732 {
1733 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001734 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001735 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001736
1737 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001738 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001739 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001740#endif /* POLARSSL_CIPHER_MODE_CBC &&
1741 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001742 {
1743 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001744 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001745 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001746
1747 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1748 ssl->in_msg, ssl->in_msglen );
1749
1750 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001751 * Authenticate if not done yet.
1752 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001753 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001754#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1755 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1756 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001757 if( mac_order == MAC_PLAINTEXT )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001758 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001759 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1760
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001761 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001763 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1764 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001766 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001768#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001769 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1770 {
1771 ssl_mac( &ssl->transform_in->md_ctx_dec,
1772 ssl->transform_in->mac_dec,
1773 ssl->in_msg, ssl->in_msglen,
1774 ssl->in_ctr, ssl->in_msgtype );
1775 }
1776 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001777#endif /* POLARSSL_SSL_PROTO_SSL3 */
1778#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001779 defined(POLARSSL_SSL_PROTO_TLS1_2)
1780 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1781 {
1782 /*
1783 * Process MAC and always update for padlen afterwards to make
1784 * total time independent of padlen
1785 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001786 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001787 *
1788 * Known timing attacks:
1789 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1790 *
1791 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1792 * correctly. (We round down instead of up, so -56 is the correct
1793 * value for our calculations instead of -55)
1794 */
1795 size_t j, extra_run = 0;
1796 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1797 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001798
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001799 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001800
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001801 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1802 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1803 ssl->in_msglen );
1804 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1805 ssl->in_msg + ssl->in_msglen );
1806 for( j = 0; j < extra_run; j++ )
1807 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001808
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001809 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1810 }
1811 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001812#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001813 POLARSSL_SSL_PROTO_TLS1_2 */
1814 {
1815 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001816 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001817 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001818
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001819 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1820 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1821 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001823 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001824 ssl->transform_in->maclen ) != 0 )
1825 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001826#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001827 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001828#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001829 correct = 0;
1830 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001831
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001832 /*
1833 * Finally check the correct flag
1834 */
1835 if( correct == 0 )
1836 return( POLARSSL_ERR_SSL_INVALID_MAC );
1837 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001838#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
1840 if( ssl->in_msglen == 0 )
1841 {
1842 ssl->nb_zero++;
1843
1844 /*
1845 * Three or more empty messages may be a DoS attack
1846 * (excessive CPU consumption).
1847 */
1848 if( ssl->nb_zero > 3 )
1849 {
1850 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1851 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001852 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 }
1854 }
1855 else
1856 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001857
Paul Bakker23986e52011-04-24 08:57:21 +00001858 for( i = 8; i > 0; i-- )
1859 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001860 break;
1861
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001862 /* The loops goes to its end iff the counter is wrapping */
1863 if( i == 0 )
1864 {
1865 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1866 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1867 }
1868
Paul Bakker5121ce52009-01-03 21:22:43 +00001869 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1870
1871 return( 0 );
1872}
1873
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001874#undef MAC_NONE
1875#undef MAC_PLAINTEXT
1876#undef MAC_CIPHERTEXT
1877
Paul Bakker2770fbd2012-07-03 13:30:23 +00001878#if defined(POLARSSL_ZLIB_SUPPORT)
1879/*
1880 * Compression/decompression functions
1881 */
1882static int ssl_compress_buf( ssl_context *ssl )
1883{
1884 int ret;
1885 unsigned char *msg_post = ssl->out_msg;
1886 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001887 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001888
1889 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1890
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001891 if( len_pre == 0 )
1892 return( 0 );
1893
Paul Bakker2770fbd2012-07-03 13:30:23 +00001894 memcpy( msg_pre, ssl->out_msg, len_pre );
1895
1896 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1897 ssl->out_msglen ) );
1898
1899 SSL_DEBUG_BUF( 4, "before compression: output payload",
1900 ssl->out_msg, ssl->out_msglen );
1901
Paul Bakker48916f92012-09-16 19:57:18 +00001902 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1903 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1904 ssl->transform_out->ctx_deflate.next_out = msg_post;
1905 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001906
Paul Bakker48916f92012-09-16 19:57:18 +00001907 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001908 if( ret != Z_OK )
1909 {
1910 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1911 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1912 }
1913
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001914 ssl->out_msglen = SSL_BUFFER_LEN -
1915 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001916
Paul Bakker2770fbd2012-07-03 13:30:23 +00001917 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1918 ssl->out_msglen ) );
1919
1920 SSL_DEBUG_BUF( 4, "after compression: output payload",
1921 ssl->out_msg, ssl->out_msglen );
1922
1923 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1924
1925 return( 0 );
1926}
1927
1928static int ssl_decompress_buf( ssl_context *ssl )
1929{
1930 int ret;
1931 unsigned char *msg_post = ssl->in_msg;
1932 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001933 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001934
1935 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1936
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001937 if( len_pre == 0 )
1938 return( 0 );
1939
Paul Bakker2770fbd2012-07-03 13:30:23 +00001940 memcpy( msg_pre, ssl->in_msg, len_pre );
1941
1942 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1943 ssl->in_msglen ) );
1944
1945 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1946 ssl->in_msg, ssl->in_msglen );
1947
Paul Bakker48916f92012-09-16 19:57:18 +00001948 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1949 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1950 ssl->transform_in->ctx_inflate.next_out = msg_post;
1951 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001952
Paul Bakker48916f92012-09-16 19:57:18 +00001953 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001954 if( ret != Z_OK )
1955 {
1956 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1957 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1958 }
1959
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001960 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1961 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001962
Paul Bakker2770fbd2012-07-03 13:30:23 +00001963 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1964 ssl->in_msglen ) );
1965
1966 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1967 ssl->in_msg, ssl->in_msglen );
1968
1969 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1970
1971 return( 0 );
1972}
1973#endif /* POLARSSL_ZLIB_SUPPORT */
1974
Paul Bakker5121ce52009-01-03 21:22:43 +00001975/*
1976 * Fill the input message buffer
1977 */
Paul Bakker23986e52011-04-24 08:57:21 +00001978int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001979{
Paul Bakker23986e52011-04-24 08:57:21 +00001980 int ret;
1981 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001982
1983 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1984
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001985 if( nb_want > SSL_BUFFER_LEN - 8 )
1986 {
1987 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1988 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1989 }
1990
Paul Bakker5121ce52009-01-03 21:22:43 +00001991 while( ssl->in_left < nb_want )
1992 {
1993 len = nb_want - ssl->in_left;
1994 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1995
1996 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1997 ssl->in_left, nb_want ) );
1998 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1999
Paul Bakker831a7552011-05-18 13:32:51 +00002000 if( ret == 0 )
2001 return( POLARSSL_ERR_SSL_CONN_EOF );
2002
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 if( ret < 0 )
2004 return( ret );
2005
2006 ssl->in_left += ret;
2007 }
2008
2009 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2010
2011 return( 0 );
2012}
2013
2014/*
2015 * Flush any data not yet written
2016 */
2017int ssl_flush_output( ssl_context *ssl )
2018{
2019 int ret;
2020 unsigned char *buf;
2021
2022 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2023
2024 while( ssl->out_left > 0 )
2025 {
2026 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2027 5 + ssl->out_msglen, ssl->out_left ) );
2028
Paul Bakker5bd42292012-12-19 14:40:42 +01002029 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002031
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2033
2034 if( ret <= 0 )
2035 return( ret );
2036
2037 ssl->out_left -= ret;
2038 }
2039
2040 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2041
2042 return( 0 );
2043}
2044
2045/*
2046 * Record layer functions
2047 */
2048int ssl_write_record( ssl_context *ssl )
2049{
Paul Bakker05ef8352012-05-08 09:17:57 +00002050 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002051 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002052
2053 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2054
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2056 {
2057 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2058 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2059 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2060
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002061 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2062 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 }
2064
Paul Bakker2770fbd2012-07-03 13:30:23 +00002065#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002066 if( ssl->transform_out != NULL &&
2067 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002068 {
2069 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2070 {
2071 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2072 return( ret );
2073 }
2074
2075 len = ssl->out_msglen;
2076 }
2077#endif /*POLARSSL_ZLIB_SUPPORT */
2078
Paul Bakker05ef8352012-05-08 09:17:57 +00002079#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002080 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002082 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
Paul Bakker05ef8352012-05-08 09:17:57 +00002084 ret = ssl_hw_record_write( ssl );
2085 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2086 {
2087 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002088 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002089 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002090
2091 if( ret == 0 )
2092 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002093 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002094#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002095 if( !done )
2096 {
2097 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2098 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2099 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2101 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002102
Paul Bakker48916f92012-09-16 19:57:18 +00002103 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002104 {
2105 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2106 {
2107 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2108 return( ret );
2109 }
2110
2111 len = ssl->out_msglen;
2112 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2113 ssl->out_hdr[4] = (unsigned char)( len );
2114 }
2115
2116 ssl->out_left = 5 + ssl->out_msglen;
2117
2118 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2119 "version = [%d:%d], msglen = %d",
2120 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2121 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2122
2123 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002124 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 }
2126
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2128 {
2129 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2130 return( ret );
2131 }
2132
2133 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2134
2135 return( 0 );
2136}
2137
2138int ssl_read_record( ssl_context *ssl )
2139{
Paul Bakker05ef8352012-05-08 09:17:57 +00002140 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
2142 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2143
2144 if( ssl->in_hslen != 0 &&
2145 ssl->in_hslen < ssl->in_msglen )
2146 {
2147 /*
2148 * Get next Handshake message in the current record
2149 */
2150 ssl->in_msglen -= ssl->in_hslen;
2151
Paul Bakker8934a982011-08-05 11:11:53 +00002152 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002153 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002154
2155 ssl->in_hslen = 4;
2156 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2157
2158 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2159 " %d, type = %d, hslen = %d",
2160 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2161
2162 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2163 {
2164 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002165 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 }
2167
2168 if( ssl->in_msglen < ssl->in_hslen )
2169 {
2170 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002171 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 }
2173
Paul Bakker4224bc02014-04-08 14:36:50 +02002174 if( ssl->state != SSL_HANDSHAKE_OVER )
2175 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002176
2177 return( 0 );
2178 }
2179
2180 ssl->in_hslen = 0;
2181
2182 /*
2183 * Read the record header and validate it
2184 */
2185 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2186 {
2187 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2188 return( ret );
2189 }
2190
2191 ssl->in_msgtype = ssl->in_hdr[0];
2192 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2193
2194 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2195 "version = [%d:%d], msglen = %d",
2196 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2197 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2198
2199 if( ssl->in_hdr[1] != ssl->major_ver )
2200 {
2201 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002202 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 }
2204
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002205 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 {
2207 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002208 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 }
2210
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002211 /* Sanity check (outer boundaries) */
2212 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2213 {
2214 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2215 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2216 }
2217
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002219 * Make sure the message length is acceptable for the current transform
2220 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 */
Paul Bakker48916f92012-09-16 19:57:18 +00002222 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002224 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 {
2226 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002227 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002228 }
2229 }
2230 else
2231 {
Paul Bakker48916f92012-09-16 19:57:18 +00002232 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 {
2234 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002235 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 }
2237
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002238#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002240 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 {
2242 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002243 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002245#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002246
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002247#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2248 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 /*
2250 * TLS encrypted messages can have up to 256 bytes of padding
2251 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002252 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002253 ssl->in_msglen > ssl->transform_in->minlen +
2254 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 {
2256 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002257 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002259#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 }
2261
2262 /*
2263 * Read and optionally decrypt the message contents
2264 */
2265 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2266 {
2267 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2268 return( ret );
2269 }
2270
2271 SSL_DEBUG_BUF( 4, "input record from network",
2272 ssl->in_hdr, 5 + ssl->in_msglen );
2273
Paul Bakker05ef8352012-05-08 09:17:57 +00002274#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002275 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002276 {
2277 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2278
2279 ret = ssl_hw_record_read( ssl );
2280 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2281 {
2282 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002283 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002284 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002285
2286 if( ret == 0 )
2287 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002288 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002289#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002290 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 {
2292 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2293 {
Paul Bakker40865c82013-01-31 17:13:13 +01002294#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2295 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2296 {
2297 ssl_send_alert_message( ssl,
2298 SSL_ALERT_LEVEL_FATAL,
2299 SSL_ALERT_MSG_BAD_RECORD_MAC );
2300 }
2301#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2303 return( ret );
2304 }
2305
2306 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2307 ssl->in_msg, ssl->in_msglen );
2308
2309 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2310 {
2311 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002312 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 }
2314 }
2315
Paul Bakker2770fbd2012-07-03 13:30:23 +00002316#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002317 if( ssl->transform_in != NULL &&
2318 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002319 {
2320 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2321 {
2322 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2323 return( ret );
2324 }
2325
2326 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2327 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2328 }
2329#endif /* POLARSSL_ZLIB_SUPPORT */
2330
Paul Bakker0a925182012-04-16 06:46:41 +00002331 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2332 ssl->in_msgtype != SSL_MSG_ALERT &&
2333 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2334 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2335 {
2336 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2337
Paul Bakker48916f92012-09-16 19:57:18 +00002338 if( ( ret = ssl_send_alert_message( ssl,
2339 SSL_ALERT_LEVEL_FATAL,
2340 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002341 {
2342 return( ret );
2343 }
2344
2345 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2346 }
2347
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2349 {
2350 ssl->in_hslen = 4;
2351 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2352
2353 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2354 " %d, type = %d, hslen = %d",
2355 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2356
2357 /*
2358 * Additional checks to validate the handshake header
2359 */
2360 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2361 {
2362 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002363 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 }
2365
2366 if( ssl->in_msglen < ssl->in_hslen )
2367 {
2368 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002369 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 }
2371
Paul Bakker48916f92012-09-16 19:57:18 +00002372 if( ssl->state != SSL_HANDSHAKE_OVER )
2373 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 }
2375
2376 if( ssl->in_msgtype == SSL_MSG_ALERT )
2377 {
2378 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2379 ssl->in_msg[0], ssl->in_msg[1] ) );
2380
2381 /*
2382 * Ignore non-fatal alerts, except close_notify
2383 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002384 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2387 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 }
2390
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002391 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2392 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 {
2394 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002395 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 }
2397 }
2398
2399 ssl->in_left = 0;
2400
2401 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2402
2403 return( 0 );
2404}
2405
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002406int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2407{
2408 int ret;
2409
2410 if( ( ret = ssl_send_alert_message( ssl,
2411 SSL_ALERT_LEVEL_FATAL,
2412 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2413 {
2414 return( ret );
2415 }
2416
2417 return( 0 );
2418}
2419
Paul Bakker0a925182012-04-16 06:46:41 +00002420int ssl_send_alert_message( ssl_context *ssl,
2421 unsigned char level,
2422 unsigned char message )
2423{
2424 int ret;
2425
2426 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2427
2428 ssl->out_msgtype = SSL_MSG_ALERT;
2429 ssl->out_msglen = 2;
2430 ssl->out_msg[0] = level;
2431 ssl->out_msg[1] = message;
2432
2433 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2434 {
2435 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2436 return( ret );
2437 }
2438
2439 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2440
2441 return( 0 );
2442}
2443
Paul Bakker5121ce52009-01-03 21:22:43 +00002444/*
2445 * Handshake functions
2446 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002447#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2448 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2449 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2450 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2451 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2452 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2453 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002454int ssl_write_certificate( ssl_context *ssl )
2455{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002456 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
2458 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2459
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002460 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002461 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2462 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002463 {
2464 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2465 ssl->state++;
2466 return( 0 );
2467 }
2468
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002469 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2470 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002471}
2472
2473int ssl_parse_certificate( ssl_context *ssl )
2474{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002475 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2476
2477 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2478
2479 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002480 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2481 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002482 {
2483 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2484 ssl->state++;
2485 return( 0 );
2486 }
2487
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002488 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2489 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490}
2491#else
2492int ssl_write_certificate( ssl_context *ssl )
2493{
2494 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2495 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002496 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002497 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2498
2499 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2500
2501 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002502 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2503 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002504 {
2505 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2506 ssl->state++;
2507 return( 0 );
2508 }
2509
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 if( ssl->endpoint == SSL_IS_CLIENT )
2511 {
2512 if( ssl->client_auth == 0 )
2513 {
2514 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2515 ssl->state++;
2516 return( 0 );
2517 }
2518
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002519#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 /*
2521 * If using SSLv3 and got no cert, send an Alert message
2522 * (otherwise an empty Certificate message will be sent).
2523 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002524 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2526 {
2527 ssl->out_msglen = 2;
2528 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002529 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2530 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
2532 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2533 goto write_msg;
2534 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002535#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 }
2537 else /* SSL_IS_SERVER */
2538 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002539 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 {
2541 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002542 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 }
2544 }
2545
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002546 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
2548 /*
2549 * 0 . 0 handshake type
2550 * 1 . 3 handshake length
2551 * 4 . 6 length of all certs
2552 * 7 . 9 length of cert. 1
2553 * 10 . n-1 peer certificate
2554 * n . n+2 length of cert. 2
2555 * n+3 . ... upper level cert, etc.
2556 */
2557 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002558 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Paul Bakker29087132010-03-21 21:03:34 +00002560 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 {
2562 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002563 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 {
2565 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2566 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002567 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 }
2569
2570 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2571 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2572 ssl->out_msg[i + 2] = (unsigned char)( n );
2573
2574 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2575 i += n; crt = crt->next;
2576 }
2577
2578 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2579 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2580 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2581
2582 ssl->out_msglen = i;
2583 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2584 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2585
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002586#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002587write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002588#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
2590 ssl->state++;
2591
2592 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2593 {
2594 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2595 return( ret );
2596 }
2597
2598 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2599
Paul Bakkered27a042013-04-18 22:46:23 +02002600 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601}
2602
2603int ssl_parse_certificate( ssl_context *ssl )
2604{
Paul Bakkered27a042013-04-18 22:46:23 +02002605 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002606 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002607 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
2609 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2610
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002611 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002612 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2613 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002614 {
2615 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2616 ssl->state++;
2617 return( 0 );
2618 }
2619
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002621 ( ssl->authmode == SSL_VERIFY_NONE ||
2622 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002624 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2626 ssl->state++;
2627 return( 0 );
2628 }
2629
2630 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2631 {
2632 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2633 return( ret );
2634 }
2635
2636 ssl->state++;
2637
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002638#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 /*
2640 * Check if the client sent an empty certificate
2641 */
2642 if( ssl->endpoint == SSL_IS_SERVER &&
2643 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2644 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002645 if( ssl->in_msglen == 2 &&
2646 ssl->in_msgtype == SSL_MSG_ALERT &&
2647 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2648 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 {
2650 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2651
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002652 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2654 return( 0 );
2655 else
Paul Bakker40e46942009-01-03 21:51:57 +00002656 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 }
2658 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002659#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002661#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2662 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 if( ssl->endpoint == SSL_IS_SERVER &&
2664 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2665 {
2666 if( ssl->in_hslen == 7 &&
2667 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2668 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2669 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2670 {
2671 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2672
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002673 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002675 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 else
2677 return( 0 );
2678 }
2679 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002680#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2681 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
2683 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2684 {
2685 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002686 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 }
2688
2689 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2690 {
2691 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002692 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 }
2694
2695 /*
2696 * Same message structure as in ssl_write_certificate()
2697 */
2698 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2699
2700 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
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
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002706 /* In case we tried to reuse a session but it failed */
2707 if( ssl->session_negotiate->peer_cert != NULL )
2708 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002709 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002710 polarssl_free( ssl->session_negotiate->peer_cert );
2711 }
2712
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002713 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2714 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 {
2716 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002717 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002718 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 }
2720
Paul Bakkerb6b09562013-09-18 14:17:41 +02002721 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
2723 i = 7;
2724
2725 while( i < ssl->in_hslen )
2726 {
2727 if( ssl->in_msg[i] != 0 )
2728 {
2729 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002730 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 }
2732
2733 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2734 | (unsigned int) ssl->in_msg[i + 2];
2735 i += 3;
2736
2737 if( n < 128 || i + n > ssl->in_hslen )
2738 {
2739 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002740 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 }
2742
Paul Bakkerddf26b42013-09-18 13:46:23 +02002743 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2744 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 if( ret != 0 )
2746 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002747 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 return( ret );
2749 }
2750
2751 i += n;
2752 }
2753
Paul Bakker48916f92012-09-16 19:57:18 +00002754 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002756 /*
2757 * On client, make sure the server cert doesn't change during renego to
2758 * avoid "triple handshake" attack: https://secure-resumption.com/
2759 */
2760 if( ssl->endpoint == SSL_IS_CLIENT &&
2761 ssl->renegotiation == SSL_RENEGOTIATION )
2762 {
2763 if( ssl->session->peer_cert == NULL )
2764 {
2765 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2766 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2767 }
2768
2769 if( ssl->session->peer_cert->raw.len !=
2770 ssl->session_negotiate->peer_cert->raw.len ||
2771 memcmp( ssl->session->peer_cert->raw.p,
2772 ssl->session_negotiate->peer_cert->raw.p,
2773 ssl->session->peer_cert->raw.len ) != 0 )
2774 {
2775 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2776 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2777 }
2778 }
2779
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 if( ssl->authmode != SSL_VERIFY_NONE )
2781 {
2782 if( ssl->ca_chain == NULL )
2783 {
2784 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002785 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 }
2787
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002788 /*
2789 * Main check: verify certificate
2790 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002791 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2792 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2793 &ssl->session_negotiate->verify_result,
2794 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
2796 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002797 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002799 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002800
2801 /*
2802 * Secondary checks: always done, but change 'ret' only if it was 0
2803 */
2804
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002805#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002806 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002807 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002808
2809 /* If certificate uses an EC key, make sure the curve is OK */
2810 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2811 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2812 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002813 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002814 if( ret == 0 )
2815 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002816 }
2817 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002818#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002820 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2821 ciphersuite_info,
2822 ! ssl->endpoint ) != 0 )
2823 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002824 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002825 if( ret == 0 )
2826 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2827 }
2828
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2830 ret = 0;
2831 }
2832
2833 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2834
2835 return( ret );
2836}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002837#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2838 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2839 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2840 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2841 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2842 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2843 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
2845int ssl_write_change_cipher_spec( ssl_context *ssl )
2846{
2847 int ret;
2848
2849 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2850
2851 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2852 ssl->out_msglen = 1;
2853 ssl->out_msg[0] = 1;
2854
Paul Bakker5121ce52009-01-03 21:22:43 +00002855 ssl->state++;
2856
2857 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2858 {
2859 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2860 return( ret );
2861 }
2862
2863 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2864
2865 return( 0 );
2866}
2867
2868int ssl_parse_change_cipher_spec( ssl_context *ssl )
2869{
2870 int ret;
2871
2872 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2873
Paul Bakker5121ce52009-01-03 21:22:43 +00002874 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2875 {
2876 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2877 return( ret );
2878 }
2879
2880 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2881 {
2882 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002883 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 }
2885
2886 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2887 {
2888 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002889 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 }
2891
2892 ssl->state++;
2893
2894 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2895
2896 return( 0 );
2897}
2898
Paul Bakker41c83d32013-03-20 14:39:14 +01002899void ssl_optimize_checksum( ssl_context *ssl,
2900 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002901{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002902 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002903
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002904#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2905 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002906 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002907 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002908 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002909#endif
2910#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2911#if defined(POLARSSL_SHA512_C)
2912 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2913 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2914 else
2915#endif
2916#if defined(POLARSSL_SHA256_C)
2917 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002918 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002919 else
2920#endif
2921#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002922 {
2923 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002924 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002925 }
Paul Bakker380da532012-04-18 16:10:25 +00002926}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002927
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002928static void ssl_update_checksum_start( ssl_context *ssl,
2929 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002930{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002931#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2932 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002933 md5_update( &ssl->handshake->fin_md5 , buf, len );
2934 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002935#endif
2936#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2937#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002938 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002939#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002940#if defined(POLARSSL_SHA512_C)
2941 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002942#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002943#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002944}
2945
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2947 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002948static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2949 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002950{
Paul Bakker48916f92012-09-16 19:57:18 +00002951 md5_update( &ssl->handshake->fin_md5 , buf, len );
2952 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002953}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002954#endif
Paul Bakker380da532012-04-18 16:10:25 +00002955
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002956#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2957#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002958static void ssl_update_checksum_sha256( ssl_context *ssl,
2959 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002960{
Paul Bakker9e36f042013-06-30 14:34:05 +02002961 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002962}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002963#endif
Paul Bakker380da532012-04-18 16:10:25 +00002964
Paul Bakker9e36f042013-06-30 14:34:05 +02002965#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002966static void ssl_update_checksum_sha384( ssl_context *ssl,
2967 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002968{
Paul Bakker9e36f042013-06-30 14:34:05 +02002969 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002970}
Paul Bakker769075d2012-11-24 11:26:46 +01002971#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002972#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002973
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002974#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002975static void ssl_calc_finished_ssl(
2976 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002977{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002978 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979 md5_context md5;
2980 sha1_context sha1;
2981
Paul Bakker5121ce52009-01-03 21:22:43 +00002982 unsigned char padbuf[48];
2983 unsigned char md5sum[16];
2984 unsigned char sha1sum[20];
2985
Paul Bakker48916f92012-09-16 19:57:18 +00002986 ssl_session *session = ssl->session_negotiate;
2987 if( !session )
2988 session = ssl->session;
2989
Paul Bakker1ef83d62012-04-11 12:09:53 +00002990 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2991
Paul Bakker48916f92012-09-16 19:57:18 +00002992 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2993 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002994
2995 /*
2996 * SSLv3:
2997 * hash =
2998 * MD5( master + pad2 +
2999 * MD5( handshake + sender + master + pad1 ) )
3000 * + SHA1( master + pad2 +
3001 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003002 */
3003
Paul Bakker90995b52013-06-24 19:20:35 +02003004#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003005 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003006 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003007#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003008
Paul Bakker90995b52013-06-24 19:20:35 +02003009#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003011 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003012#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003013
Paul Bakker3c2122f2013-06-24 19:03:14 +02003014 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3015 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Paul Bakker1ef83d62012-04-11 12:09:53 +00003017 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
Paul Bakker3c2122f2013-06-24 19:03:14 +02003019 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003020 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021 md5_update( &md5, padbuf, 48 );
3022 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Paul Bakker3c2122f2013-06-24 19:03:14 +02003024 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003025 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003026 sha1_update( &sha1, padbuf, 40 );
3027 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003028
Paul Bakker1ef83d62012-04-11 12:09:53 +00003029 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Paul Bakker1ef83d62012-04-11 12:09:53 +00003031 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003032 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003033 md5_update( &md5, padbuf, 48 );
3034 md5_update( &md5, md5sum, 16 );
3035 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
Paul Bakker1ef83d62012-04-11 12:09:53 +00003037 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003038 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003039 sha1_update( &sha1, padbuf , 40 );
3040 sha1_update( &sha1, sha1sum, 20 );
3041 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
Paul Bakker1ef83d62012-04-11 12:09:53 +00003043 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003044
Paul Bakker5b4af392014-06-26 12:09:34 +02003045 md5_free( &md5 );
3046 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Paul Bakker34617722014-06-13 17:20:13 +02003048 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3049 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3050 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003051
3052 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3053}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003054#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003056#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003057static void ssl_calc_finished_tls(
3058 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003059{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003060 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003061 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003062 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003063 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003064 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
Paul Bakker48916f92012-09-16 19:57:18 +00003066 ssl_session *session = ssl->session_negotiate;
3067 if( !session )
3068 session = ssl->session;
3069
Paul Bakker1ef83d62012-04-11 12:09:53 +00003070 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003071
Paul Bakker48916f92012-09-16 19:57:18 +00003072 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3073 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003074
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 /*
3076 * TLSv1:
3077 * hash = PRF( master, finished_label,
3078 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3079 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
Paul Bakker90995b52013-06-24 19:20:35 +02003081#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003082 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3083 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003084#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003085
Paul Bakker90995b52013-06-24 19:20:35 +02003086#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003087 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3088 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003089#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003090
3091 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003092 ? "client finished"
3093 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003094
3095 md5_finish( &md5, padbuf );
3096 sha1_finish( &sha1, padbuf + 16 );
3097
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003098 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003099 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003100
3101 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3102
Paul Bakker5b4af392014-06-26 12:09:34 +02003103 md5_free( &md5 );
3104 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003105
Paul Bakker34617722014-06-13 17:20:13 +02003106 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003107
3108 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3109}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003110#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003111
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003112#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3113#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003114static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003115 ssl_context *ssl, unsigned char *buf, int from )
3116{
3117 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003118 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003119 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003120 unsigned char padbuf[32];
3121
Paul Bakker48916f92012-09-16 19:57:18 +00003122 ssl_session *session = ssl->session_negotiate;
3123 if( !session )
3124 session = ssl->session;
3125
Paul Bakker380da532012-04-18 16:10:25 +00003126 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127
Paul Bakker9e36f042013-06-30 14:34:05 +02003128 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003129
3130 /*
3131 * TLSv1.2:
3132 * hash = PRF( master, finished_label,
3133 * Hash( handshake ) )[0.11]
3134 */
3135
Paul Bakker9e36f042013-06-30 14:34:05 +02003136#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003137 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003138 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003139#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003140
3141 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003142 ? "client finished"
3143 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003144
Paul Bakker9e36f042013-06-30 14:34:05 +02003145 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003146
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003147 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003148 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003149
3150 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3151
Paul Bakker5b4af392014-06-26 12:09:34 +02003152 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003153
Paul Bakker34617722014-06-13 17:20:13 +02003154 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155
3156 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3157}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003158#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003159
Paul Bakker9e36f042013-06-30 14:34:05 +02003160#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003161static void ssl_calc_finished_tls_sha384(
3162 ssl_context *ssl, unsigned char *buf, int from )
3163{
3164 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003165 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003166 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003167 unsigned char padbuf[48];
3168
Paul Bakker48916f92012-09-16 19:57:18 +00003169 ssl_session *session = ssl->session_negotiate;
3170 if( !session )
3171 session = ssl->session;
3172
Paul Bakker380da532012-04-18 16:10:25 +00003173 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003174
Paul Bakker9e36f042013-06-30 14:34:05 +02003175 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003176
3177 /*
3178 * TLSv1.2:
3179 * hash = PRF( master, finished_label,
3180 * Hash( handshake ) )[0.11]
3181 */
3182
Paul Bakker9e36f042013-06-30 14:34:05 +02003183#if !defined(POLARSSL_SHA512_ALT)
3184 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3185 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003186#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003187
3188 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003189 ? "client finished"
3190 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003191
Paul Bakker9e36f042013-06-30 14:34:05 +02003192 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003193
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003194 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003195 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003196
3197 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3198
Paul Bakker5b4af392014-06-26 12:09:34 +02003199 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003200
Paul Bakker34617722014-06-13 17:20:13 +02003201 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003202
3203 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3204}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003205#endif /* POLARSSL_SHA512_C */
3206#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003207
Paul Bakker48916f92012-09-16 19:57:18 +00003208void ssl_handshake_wrapup( ssl_context *ssl )
3209{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003210 int resume = ssl->handshake->resume;
3211
Paul Bakker48916f92012-09-16 19:57:18 +00003212 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3213
3214 /*
3215 * Free our handshake params
3216 */
3217 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003218 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003219 ssl->handshake = NULL;
3220
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003221 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003222 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003223 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003224 ssl->renego_records_seen = 0;
3225 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003226
Paul Bakker48916f92012-09-16 19:57:18 +00003227 /*
3228 * Switch in our now active transform context
3229 */
3230 if( ssl->transform )
3231 {
3232 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003233 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003234 }
3235 ssl->transform = ssl->transform_negotiate;
3236 ssl->transform_negotiate = NULL;
3237
Paul Bakker0a597072012-09-25 21:55:46 +00003238 if( ssl->session )
3239 {
3240 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003241 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003242 }
3243 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003244 ssl->session_negotiate = NULL;
3245
Paul Bakker0a597072012-09-25 21:55:46 +00003246 /*
3247 * Add cache entry
3248 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003249 if( ssl->f_set_cache != NULL &&
3250 ssl->session->length != 0 &&
3251 resume == 0 )
3252 {
Paul Bakker0a597072012-09-25 21:55:46 +00003253 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3254 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003255 }
Paul Bakker0a597072012-09-25 21:55:46 +00003256
Paul Bakker48916f92012-09-16 19:57:18 +00003257 ssl->state++;
3258
3259 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3260}
3261
Paul Bakker1ef83d62012-04-11 12:09:53 +00003262int ssl_write_finished( ssl_context *ssl )
3263{
3264 int ret, hash_len;
3265
3266 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3267
Paul Bakker92be97b2013-01-02 17:30:03 +01003268 /*
3269 * Set the out_msg pointer to the correct location based on IV length
3270 */
3271 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3272 {
3273 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3274 ssl->transform_negotiate->fixed_ivlen;
3275 }
3276 else
3277 ssl->out_msg = ssl->out_iv;
3278
Paul Bakker48916f92012-09-16 19:57:18 +00003279 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003280
3281 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3283
Paul Bakker48916f92012-09-16 19:57:18 +00003284 ssl->verify_data_len = hash_len;
3285 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3286
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 ssl->out_msglen = 4 + hash_len;
3288 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3289 ssl->out_msg[0] = SSL_HS_FINISHED;
3290
3291 /*
3292 * In case of session resuming, invert the client and server
3293 * ChangeCipherSpec messages order.
3294 */
Paul Bakker0a597072012-09-25 21:55:46 +00003295 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 {
3297 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003298 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 else
3300 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3301 }
3302 else
3303 ssl->state++;
3304
Paul Bakker48916f92012-09-16 19:57:18 +00003305 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003306 * Switch to our negotiated transform and session parameters for outbound
3307 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003308 */
3309 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3310 ssl->transform_out = ssl->transform_negotiate;
3311 ssl->session_out = ssl->session_negotiate;
3312 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003313
Paul Bakker07eb38b2012-12-19 14:42:06 +01003314#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003315 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003316 {
3317 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3318 {
3319 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3320 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3321 }
3322 }
3323#endif
3324
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3326 {
3327 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3328 return( ret );
3329 }
3330
3331 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3332
3333 return( 0 );
3334}
3335
3336int ssl_parse_finished( ssl_context *ssl )
3337{
Paul Bakker23986e52011-04-24 08:57:21 +00003338 int ret;
3339 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003340 unsigned char buf[36];
3341
3342 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3343
Paul Bakker48916f92012-09-16 19:57:18 +00003344 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003345
Paul Bakker48916f92012-09-16 19:57:18 +00003346 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003347 * Switch to our negotiated transform and session parameters for inbound
3348 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003349 */
3350 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3351 ssl->transform_in = ssl->transform_negotiate;
3352 ssl->session_in = ssl->session_negotiate;
3353 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003354
Paul Bakker92be97b2013-01-02 17:30:03 +01003355 /*
3356 * Set the in_msg pointer to the correct location based on IV length
3357 */
3358 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3359 {
3360 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3361 ssl->transform_negotiate->fixed_ivlen;
3362 }
3363 else
3364 ssl->in_msg = ssl->in_iv;
3365
Paul Bakker07eb38b2012-12-19 14:42:06 +01003366#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003367 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003368 {
3369 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3370 {
3371 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3372 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3373 }
3374 }
3375#endif
3376
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3378 {
3379 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3380 return( ret );
3381 }
3382
3383 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3384 {
3385 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003386 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003387 }
3388
Paul Bakker1ef83d62012-04-11 12:09:53 +00003389 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3391
3392 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3393 ssl->in_hslen != 4 + hash_len )
3394 {
3395 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003396 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003397 }
3398
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003399 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003400 {
3401 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003402 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 }
3404
Paul Bakker48916f92012-09-16 19:57:18 +00003405 ssl->verify_data_len = hash_len;
3406 memcpy( ssl->peer_verify_data, buf, hash_len );
3407
Paul Bakker0a597072012-09-25 21:55:46 +00003408 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003409 {
3410 if( ssl->endpoint == SSL_IS_CLIENT )
3411 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3412
3413 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003414 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003415 }
3416 else
3417 ssl->state++;
3418
3419 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3420
3421 return( 0 );
3422}
3423
Paul Bakker968afaa2014-07-09 11:09:24 +02003424static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003425{
3426 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3427
3428#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3429 defined(POLARSSL_SSL_PROTO_TLS1_1)
3430 md5_init( &handshake->fin_md5 );
3431 sha1_init( &handshake->fin_sha1 );
3432 md5_starts( &handshake->fin_md5 );
3433 sha1_starts( &handshake->fin_sha1 );
3434#endif
3435#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3436#if defined(POLARSSL_SHA256_C)
3437 sha256_init( &handshake->fin_sha256 );
3438 sha256_starts( &handshake->fin_sha256, 0 );
3439#endif
3440#if defined(POLARSSL_SHA512_C)
3441 sha512_init( &handshake->fin_sha512 );
3442 sha512_starts( &handshake->fin_sha512, 1 );
3443#endif
3444#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3445
3446 handshake->update_checksum = ssl_update_checksum_start;
3447 handshake->sig_alg = SSL_HASH_SHA1;
3448
3449#if defined(POLARSSL_DHM_C)
3450 dhm_init( &handshake->dhm_ctx );
3451#endif
3452#if defined(POLARSSL_ECDH_C)
3453 ecdh_init( &handshake->ecdh_ctx );
3454#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003455}
3456
3457static void ssl_transform_init( ssl_transform *transform )
3458{
3459 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003460
3461 cipher_init( &transform->cipher_ctx_enc );
3462 cipher_init( &transform->cipher_ctx_dec );
3463
3464 md_init( &transform->md_ctx_enc );
3465 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003466}
3467
3468void ssl_session_init( ssl_session *session )
3469{
3470 memset( session, 0, sizeof(ssl_session) );
3471}
3472
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003473static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003474{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003475 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003476 if( ssl->transform_negotiate )
3477 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003478 if( ssl->session_negotiate )
3479 ssl_session_free( ssl->session_negotiate );
3480 if( ssl->handshake )
3481 ssl_handshake_free( ssl->handshake );
3482
3483 /*
3484 * Either the pointers are now NULL or cleared properly and can be freed.
3485 * Now allocate missing structures.
3486 */
3487 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003488 {
3489 ssl->transform_negotiate =
3490 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3491 }
Paul Bakker48916f92012-09-16 19:57:18 +00003492
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003493 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003494 {
3495 ssl->session_negotiate =
3496 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3497 }
Paul Bakker48916f92012-09-16 19:57:18 +00003498
Paul Bakker82788fb2014-10-20 13:59:19 +02003499 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003500 {
3501 ssl->handshake = (ssl_handshake_params *)
3502 polarssl_malloc( sizeof(ssl_handshake_params) );
3503 }
Paul Bakker48916f92012-09-16 19:57:18 +00003504
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003505 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003506 if( ssl->handshake == NULL ||
3507 ssl->transform_negotiate == NULL ||
3508 ssl->session_negotiate == NULL )
3509 {
3510 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003511
3512 polarssl_free( ssl->handshake );
3513 polarssl_free( ssl->transform_negotiate );
3514 polarssl_free( ssl->session_negotiate );
3515
3516 ssl->handshake = NULL;
3517 ssl->transform_negotiate = NULL;
3518 ssl->session_negotiate = NULL;
3519
Paul Bakker48916f92012-09-16 19:57:18 +00003520 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3521 }
3522
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003523 /* Initialize structures */
3524 ssl_session_init( ssl->session_negotiate );
3525 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003526 ssl_handshake_params_init( ssl->handshake );
3527
3528#if defined(POLARSSL_X509_CRT_PARSE_C)
3529 ssl->handshake->key_cert = ssl->key_cert;
3530#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003531
Paul Bakker48916f92012-09-16 19:57:18 +00003532 return( 0 );
3533}
3534
Paul Bakker5121ce52009-01-03 21:22:43 +00003535/*
3536 * Initialize an SSL context
3537 */
3538int ssl_init( ssl_context *ssl )
3539{
Paul Bakker48916f92012-09-16 19:57:18 +00003540 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 int len = SSL_BUFFER_LEN;
3542
3543 memset( ssl, 0, sizeof( ssl_context ) );
3544
Paul Bakker62f2dee2012-09-28 07:31:51 +00003545 /*
3546 * Sane defaults
3547 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003548 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3549 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3550 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3551 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003552
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003553 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003554
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003555 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3556
Paul Bakker62f2dee2012-09-28 07:31:51 +00003557#if defined(POLARSSL_DHM_C)
3558 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3559 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3560 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3561 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3562 {
3563 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3564 return( ret );
3565 }
3566#endif
3567
3568 /*
3569 * Prepare base structures
3570 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003571 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003573 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003574 ssl->in_msg = ssl->in_ctr + 13;
3575
3576 if( ssl->in_ctr == NULL )
3577 {
3578 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003579 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003580 }
3581
Paul Bakker6e339b52013-07-03 13:37:05 +02003582 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003583 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003584 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003585 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003586
3587 if( ssl->out_ctr == NULL )
3588 {
3589 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003590 polarssl_free( ssl->in_ctr );
3591 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003592 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 }
3594
3595 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3596 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3597
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003598#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3599 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3600#endif
3601
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003602#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3603 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3604#endif
3605
Paul Bakker606b4ba2013-08-14 16:52:14 +02003606#if defined(POLARSSL_SSL_SESSION_TICKETS)
3607 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3608#endif
3609
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003610#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003611 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003612#endif
3613
Paul Bakker48916f92012-09-16 19:57:18 +00003614 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3615 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003616
3617 return( 0 );
3618}
3619
3620/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003621 * Reset an initialized and used SSL context for re-use while retaining
3622 * all application-set variables, function pointers and data.
3623 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003624int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003625{
Paul Bakker48916f92012-09-16 19:57:18 +00003626 int ret;
3627
Paul Bakker7eb013f2011-10-06 12:37:39 +00003628 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003629 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3630 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3631
3632 ssl->verify_data_len = 0;
3633 memset( ssl->own_verify_data, 0, 36 );
3634 memset( ssl->peer_verify_data, 0, 36 );
3635
Paul Bakker7eb013f2011-10-06 12:37:39 +00003636 ssl->in_offt = NULL;
3637
Paul Bakker92be97b2013-01-02 17:30:03 +01003638 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003639 ssl->in_msgtype = 0;
3640 ssl->in_msglen = 0;
3641 ssl->in_left = 0;
3642
3643 ssl->in_hslen = 0;
3644 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003645 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003646
Paul Bakker92be97b2013-01-02 17:30:03 +01003647 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003648 ssl->out_msgtype = 0;
3649 ssl->out_msglen = 0;
3650 ssl->out_left = 0;
3651
Paul Bakker48916f92012-09-16 19:57:18 +00003652 ssl->transform_in = NULL;
3653 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003654
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003655 ssl->renego_records_seen = 0;
3656
Paul Bakker7eb013f2011-10-06 12:37:39 +00003657 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3658 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003659
3660#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003661 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003662 {
3663 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003664 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003665 {
3666 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3667 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3668 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003669 }
3670#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003671
Paul Bakker48916f92012-09-16 19:57:18 +00003672 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003673 {
Paul Bakker48916f92012-09-16 19:57:18 +00003674 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003675 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003676 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003677 }
Paul Bakker48916f92012-09-16 19:57:18 +00003678
Paul Bakkerc0463502013-02-14 11:19:38 +01003679 if( ssl->session )
3680 {
3681 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003682 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003683 ssl->session = NULL;
3684 }
3685
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003686#if defined(POLARSSL_SSL_ALPN)
3687 ssl->alpn_chosen = NULL;
3688#endif
3689
Paul Bakker48916f92012-09-16 19:57:18 +00003690 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3691 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003692
3693 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003694}
3695
Paul Bakkera503a632013-08-14 13:48:06 +02003696#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003697static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3698{
3699 aes_free( &tkeys->enc );
3700 aes_free( &tkeys->dec );
3701
3702 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3703}
3704
Paul Bakker7eb013f2011-10-06 12:37:39 +00003705/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003706 * Allocate and initialize ticket keys
3707 */
3708static int ssl_ticket_keys_init( ssl_context *ssl )
3709{
3710 int ret;
3711 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003712 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003713
3714 if( ssl->ticket_keys != NULL )
3715 return( 0 );
3716
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003717 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3718 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003719 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3720
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003721 aes_init( &tkeys->enc );
3722 aes_init( &tkeys->dec );
3723
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003724 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003725 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003726 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003727 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003728 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003729 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003730
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003731 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3732 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3733 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3734 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003735 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003736 polarssl_free( tkeys );
3737 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003738 }
3739
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003740 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003741 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003742 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003743 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003744 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003745 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003746
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003747 ssl->ticket_keys = tkeys;
3748
3749 return( 0 );
3750}
Paul Bakkera503a632013-08-14 13:48:06 +02003751#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003752
3753/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003754 * SSL set accessors
3755 */
3756void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3757{
3758 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003759
Paul Bakker606b4ba2013-08-14 16:52:14 +02003760#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003761 if( endpoint == SSL_IS_CLIENT )
3762 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003763#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003764}
3765
3766void ssl_set_authmode( ssl_context *ssl, int authmode )
3767{
3768 ssl->authmode = authmode;
3769}
3770
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003771#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003772void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003773 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003774 void *p_vrfy )
3775{
3776 ssl->f_vrfy = f_vrfy;
3777 ssl->p_vrfy = p_vrfy;
3778}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003779#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003780
Paul Bakker5121ce52009-01-03 21:22:43 +00003781void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003782 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003783 void *p_rng )
3784{
3785 ssl->f_rng = f_rng;
3786 ssl->p_rng = p_rng;
3787}
3788
3789void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003790 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003791 void *p_dbg )
3792{
3793 ssl->f_dbg = f_dbg;
3794 ssl->p_dbg = p_dbg;
3795}
3796
3797void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003798 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003799 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003800{
3801 ssl->f_recv = f_recv;
3802 ssl->f_send = f_send;
3803 ssl->p_recv = p_recv;
3804 ssl->p_send = p_send;
3805}
3806
Paul Bakker0a597072012-09-25 21:55:46 +00003807void ssl_set_session_cache( ssl_context *ssl,
3808 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3809 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003810{
Paul Bakker0a597072012-09-25 21:55:46 +00003811 ssl->f_get_cache = f_get_cache;
3812 ssl->p_get_cache = p_get_cache;
3813 ssl->f_set_cache = f_set_cache;
3814 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003815}
3816
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003817int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003818{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003819 int ret;
3820
3821 if( ssl == NULL ||
3822 session == NULL ||
3823 ssl->session_negotiate == NULL ||
3824 ssl->endpoint != SSL_IS_CLIENT )
3825 {
3826 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3827 }
3828
3829 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3830 return( ret );
3831
Paul Bakker0a597072012-09-25 21:55:46 +00003832 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003833
3834 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003835}
3836
Paul Bakkerb68cad62012-08-23 08:34:18 +00003837void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003838{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003839 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3840 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3841 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3842 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3843}
3844
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003845void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3846 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003847 int major, int minor )
3848{
3849 if( major != SSL_MAJOR_VERSION_3 )
3850 return;
3851
3852 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3853 return;
3854
3855 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003856}
3857
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003858#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003859/* Add a new (empty) key_cert entry an return a pointer to it */
3860static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3861{
3862 ssl_key_cert *key_cert, *last;
3863
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003864 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3865 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003866 return( NULL );
3867
3868 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3869
3870 /* Append the new key_cert to the (possibly empty) current list */
3871 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003872 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003873 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003874 if( ssl->handshake != NULL )
3875 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003876 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003877 else
3878 {
3879 last = ssl->key_cert;
3880 while( last->next != NULL )
3881 last = last->next;
3882 last->next = key_cert;
3883 }
3884
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003885 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003886}
3887
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003888void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003889 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003890{
3891 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003892 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003893 ssl->peer_cn = peer_cn;
3894}
3895
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003896int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003897 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003898{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003899 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3900
3901 if( key_cert == NULL )
3902 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3903
3904 key_cert->cert = own_cert;
3905 key_cert->key = pk_key;
3906
3907 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003908}
3909
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003910#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003911int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003912 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003913{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003914 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003915 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003916
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003917 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003918 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3919
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003920 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3921 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003922 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003923
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003924 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003925
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003926 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003927 if( ret != 0 )
3928 return( ret );
3929
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003930 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003931 return( ret );
3932
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003933 key_cert->cert = own_cert;
3934 key_cert->key_own_alloc = 1;
3935
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003936 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003937}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003938#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003939
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003940int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003941 void *rsa_key,
3942 rsa_decrypt_func rsa_decrypt,
3943 rsa_sign_func rsa_sign,
3944 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003945{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003946 int ret;
3947 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003948
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003949 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003950 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3951
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003952 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3953 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003954 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003955
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003956 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003957
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003958 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3959 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3960 return( ret );
3961
3962 key_cert->cert = own_cert;
3963 key_cert->key_own_alloc = 1;
3964
3965 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003966}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003967#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003968
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003969#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003970int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3971 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003972{
Paul Bakker6db455e2013-09-18 17:29:31 +02003973 if( psk == NULL || psk_identity == NULL )
3974 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3975
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003976 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003977 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3978
Paul Bakker6db455e2013-09-18 17:29:31 +02003979 if( ssl->psk != NULL )
3980 {
3981 polarssl_free( ssl->psk );
3982 polarssl_free( ssl->psk_identity );
3983 }
3984
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003985 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003986 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003987
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003988 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003989 ssl->psk_identity = (unsigned char *)
3990 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003991
3992 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3993 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3994
3995 memcpy( ssl->psk, psk, ssl->psk_len );
3996 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003997
3998 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003999}
4000
4001void ssl_set_psk_cb( ssl_context *ssl,
4002 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4003 size_t),
4004 void *p_psk )
4005{
4006 ssl->f_psk = f_psk;
4007 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004008}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004009#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004010
Paul Bakker48916f92012-09-16 19:57:18 +00004011#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004012int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004013{
4014 int ret;
4015
Paul Bakker48916f92012-09-16 19:57:18 +00004016 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 {
4018 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4019 return( ret );
4020 }
4021
Paul Bakker48916f92012-09-16 19:57:18 +00004022 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004023 {
4024 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4025 return( ret );
4026 }
4027
4028 return( 0 );
4029}
4030
Paul Bakker1b57b062011-01-06 15:48:19 +00004031int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4032{
4033 int ret;
4034
Paul Bakker66d5d072014-06-17 16:39:18 +02004035 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004036 {
4037 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4038 return( ret );
4039 }
4040
Paul Bakker66d5d072014-06-17 16:39:18 +02004041 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004042 {
4043 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4044 return( ret );
4045 }
4046
4047 return( 0 );
4048}
Paul Bakker48916f92012-09-16 19:57:18 +00004049#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004050
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004051#if defined(POLARSSL_SSL_SET_CURVES)
4052/*
4053 * Set the allowed elliptic curves
4054 */
4055void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4056{
4057 ssl->curve_list = curve_list;
4058}
4059#endif
4060
Paul Bakker0be444a2013-08-27 21:55:01 +02004061#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004062int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004063{
4064 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004065 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004066
4067 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004068
4069 if( ssl->hostname_len + 1 == 0 )
4070 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4071
Paul Bakker6e339b52013-07-03 13:37:05 +02004072 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004073
Paul Bakkerb15b8512012-01-13 13:44:06 +00004074 if( ssl->hostname == NULL )
4075 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4076
Paul Bakker3c2122f2013-06-24 19:03:14 +02004077 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004078 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004079
Paul Bakker40ea7de2009-05-03 10:18:48 +00004080 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004081
4082 return( 0 );
4083}
4084
Paul Bakker5701cdc2012-09-27 21:49:42 +00004085void ssl_set_sni( ssl_context *ssl,
4086 int (*f_sni)(void *, ssl_context *,
4087 const unsigned char *, size_t),
4088 void *p_sni )
4089{
4090 ssl->f_sni = f_sni;
4091 ssl->p_sni = p_sni;
4092}
Paul Bakker0be444a2013-08-27 21:55:01 +02004093#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004094
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004095#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004096int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004097{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004098 size_t cur_len, tot_len;
4099 const char **p;
4100
4101 /*
4102 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4103 * truncated". Check lengths now rather than later.
4104 */
4105 tot_len = 0;
4106 for( p = protos; *p != NULL; p++ )
4107 {
4108 cur_len = strlen( *p );
4109 tot_len += cur_len;
4110
4111 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4112 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4113 }
4114
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004115 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004116
4117 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004118}
4119
4120const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4121{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004122 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004123}
4124#endif /* POLARSSL_SSL_ALPN */
4125
Paul Bakker490ecc82011-10-06 13:04:09 +00004126void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4127{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004128 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4129 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4130 {
4131 ssl->max_major_ver = major;
4132 ssl->max_minor_ver = minor;
4133 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004134}
4135
Paul Bakker1d29fb52012-09-28 13:28:45 +00004136void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4137{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004138 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4139 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4140 {
4141 ssl->min_major_ver = major;
4142 ssl->min_minor_ver = minor;
4143 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004144}
4145
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004146#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4147void ssl_set_fallback( ssl_context *ssl, char fallback )
4148{
4149 ssl->fallback = fallback;
4150}
4151#endif
4152
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004153#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4154void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4155{
4156 ssl->encrypt_then_mac = etm;
4157}
4158#endif
4159
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004160#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4161void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4162{
4163 ssl->extended_ms = ems;
4164}
4165#endif
4166
Paul Bakker05decb22013-08-15 13:33:48 +02004167#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004168int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4169{
Paul Bakker77e257e2013-12-16 15:29:52 +01004170 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004171 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004172 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004173 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004174 }
4175
4176 ssl->mfl_code = mfl_code;
4177
4178 return( 0 );
4179}
Paul Bakker05decb22013-08-15 13:33:48 +02004180#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004181
Paul Bakker1f2bc622013-08-15 13:45:55 +02004182#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004183int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004184{
4185 if( ssl->endpoint != SSL_IS_CLIENT )
4186 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4187
Paul Bakker8c1ede62013-07-19 14:14:37 +02004188 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004189
4190 return( 0 );
4191}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004192#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004193
Paul Bakker48916f92012-09-16 19:57:18 +00004194void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4195{
4196 ssl->disable_renegotiation = renegotiation;
4197}
4198
4199void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4200{
4201 ssl->allow_legacy_renegotiation = allow_legacy;
4202}
4203
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004204void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4205{
4206 ssl->renego_max_records = max_records;
4207}
4208
Paul Bakkera503a632013-08-14 13:48:06 +02004209#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004210int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4211{
4212 ssl->session_tickets = use_tickets;
4213
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004214 if( ssl->endpoint == SSL_IS_CLIENT )
4215 return( 0 );
4216
4217 if( ssl->f_rng == NULL )
4218 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4219
4220 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004221}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004222
4223void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4224{
4225 ssl->ticket_lifetime = lifetime;
4226}
Paul Bakkera503a632013-08-14 13:48:06 +02004227#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004228
Paul Bakker5121ce52009-01-03 21:22:43 +00004229/*
4230 * SSL get accessors
4231 */
Paul Bakker23986e52011-04-24 08:57:21 +00004232size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004233{
4234 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4235}
4236
Paul Bakkerff60ee62010-03-16 21:09:09 +00004237int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004238{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004239 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004240}
4241
Paul Bakkere3166ce2011-01-27 17:40:50 +00004242const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004243{
Paul Bakker926c8e42013-03-06 10:23:34 +01004244 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004245 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004246
Paul Bakkere3166ce2011-01-27 17:40:50 +00004247 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004248}
4249
Paul Bakker43ca69c2011-01-15 17:35:19 +00004250const char *ssl_get_version( const ssl_context *ssl )
4251{
4252 switch( ssl->minor_ver )
4253 {
4254 case SSL_MINOR_VERSION_0:
4255 return( "SSLv3.0" );
4256
4257 case SSL_MINOR_VERSION_1:
4258 return( "TLSv1.0" );
4259
4260 case SSL_MINOR_VERSION_2:
4261 return( "TLSv1.1" );
4262
Paul Bakker1ef83d62012-04-11 12:09:53 +00004263 case SSL_MINOR_VERSION_3:
4264 return( "TLSv1.2" );
4265
Paul Bakker43ca69c2011-01-15 17:35:19 +00004266 default:
4267 break;
4268 }
4269 return( "unknown" );
4270}
4271
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004272#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004273const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004274{
4275 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004276 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004277
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004278 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004279}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004280#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004281
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004282int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4283{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004284 if( ssl == NULL ||
4285 dst == NULL ||
4286 ssl->session == NULL ||
4287 ssl->endpoint != SSL_IS_CLIENT )
4288 {
4289 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4290 }
4291
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004292 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004293}
4294
Paul Bakker5121ce52009-01-03 21:22:43 +00004295/*
Paul Bakker1961b702013-01-25 14:49:24 +01004296 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004297 */
Paul Bakker1961b702013-01-25 14:49:24 +01004298int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004299{
Paul Bakker40e46942009-01-03 21:51:57 +00004300 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004301
Paul Bakker40e46942009-01-03 21:51:57 +00004302#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004303 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004304 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004305#endif
4306
Paul Bakker40e46942009-01-03 21:51:57 +00004307#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004309 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004310#endif
4311
Paul Bakker1961b702013-01-25 14:49:24 +01004312 return( ret );
4313}
4314
4315/*
4316 * Perform the SSL handshake
4317 */
4318int ssl_handshake( ssl_context *ssl )
4319{
4320 int ret = 0;
4321
4322 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4323
4324 while( ssl->state != SSL_HANDSHAKE_OVER )
4325 {
4326 ret = ssl_handshake_step( ssl );
4327
4328 if( ret != 0 )
4329 break;
4330 }
4331
Paul Bakker5121ce52009-01-03 21:22:43 +00004332 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4333
4334 return( ret );
4335}
4336
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004337#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004338/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004339 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004340 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004341static int ssl_write_hello_request( ssl_context *ssl )
4342{
4343 int ret;
4344
4345 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4346
4347 ssl->out_msglen = 4;
4348 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4349 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4350
4351 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4352 {
4353 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4354 return( ret );
4355 }
4356
4357 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4358
4359 return( 0 );
4360}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004361#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004362
4363/*
4364 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004365 * - any side: calling ssl_renegotiate(),
4366 * - client: receiving a HelloRequest during ssl_read(),
4367 * - server: receiving any handshake message on server during ssl_read() after
4368 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004369 * If the handshake doesn't complete due to waiting for I/O, it will continue
4370 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004371 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004372static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004373{
4374 int ret;
4375
4376 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4377
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004378 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4379 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004380
4381 ssl->state = SSL_HELLO_REQUEST;
4382 ssl->renegotiation = SSL_RENEGOTIATION;
4383
Paul Bakker48916f92012-09-16 19:57:18 +00004384 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4385 {
4386 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4387 return( ret );
4388 }
4389
4390 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4391
4392 return( 0 );
4393}
4394
4395/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004396 * Renegotiate current connection on client,
4397 * or request renegotiation on server
4398 */
4399int ssl_renegotiate( ssl_context *ssl )
4400{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004401 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004402
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004403#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004404 /* On server, just send the request */
4405 if( ssl->endpoint == SSL_IS_SERVER )
4406 {
4407 if( ssl->state != SSL_HANDSHAKE_OVER )
4408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4409
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004410 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4411
4412 /* Did we already try/start sending HelloRequest? */
4413 if( ssl->out_left != 0 )
4414 return( ssl_flush_output( ssl ) );
4415
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004416 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004417 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004418#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004419
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004420#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004421 /*
4422 * On client, either start the renegotiation process or,
4423 * if already in progress, continue the handshake
4424 */
4425 if( ssl->renegotiation != SSL_RENEGOTIATION )
4426 {
4427 if( ssl->state != SSL_HANDSHAKE_OVER )
4428 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4429
4430 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4431 {
4432 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4433 return( ret );
4434 }
4435 }
4436 else
4437 {
4438 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4439 {
4440 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4441 return( ret );
4442 }
4443 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004444#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004445
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004446 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004447}
4448
4449/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004450 * Receive application data decrypted from the SSL layer
4451 */
Paul Bakker23986e52011-04-24 08:57:21 +00004452int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004453{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004454 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004455 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004456
4457 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4458
4459 if( ssl->state != SSL_HANDSHAKE_OVER )
4460 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004461 ret = ssl_handshake( ssl );
4462 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4463 {
4464 record_read = 1;
4465 }
4466 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004467 {
4468 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4469 return( ret );
4470 }
4471 }
4472
4473 if( ssl->in_offt == NULL )
4474 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004475 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004476 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004477 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4478 {
4479 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4480 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004481
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004482 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4483 return( ret );
4484 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004485 }
4486
4487 if( ssl->in_msglen == 0 &&
4488 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4489 {
4490 /*
4491 * OpenSSL sends empty messages to randomize the IV
4492 */
4493 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4494 {
Paul Bakker831a7552011-05-18 13:32:51 +00004495 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4496 return( 0 );
4497
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4499 return( ret );
4500 }
4501 }
4502
Paul Bakker48916f92012-09-16 19:57:18 +00004503 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4504 {
4505 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4506
4507 if( ssl->endpoint == SSL_IS_CLIENT &&
4508 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4509 ssl->in_hslen != 4 ) )
4510 {
4511 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4512 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4513 }
4514
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004515 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4516 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004517 ssl->allow_legacy_renegotiation ==
4518 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004519 {
4520 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4521
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004522#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004523 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004524 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004525 /*
4526 * SSLv3 does not have a "no_renegotiation" alert
4527 */
4528 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4529 return( ret );
4530 }
4531 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004532#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004533#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4534 defined(POLARSSL_SSL_PROTO_TLS1_2)
4535 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004536 {
4537 if( ( ret = ssl_send_alert_message( ssl,
4538 SSL_ALERT_LEVEL_WARNING,
4539 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4540 {
4541 return( ret );
4542 }
Paul Bakker48916f92012-09-16 19:57:18 +00004543 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004544 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004545#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4546 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004547 {
4548 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004549 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004550 }
Paul Bakker48916f92012-09-16 19:57:18 +00004551 }
4552 else
4553 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004554 ret = ssl_start_renegotiation( ssl );
4555 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4556 {
4557 record_read = 1;
4558 }
4559 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004560 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004561 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004562 return( ret );
4563 }
Paul Bakker48916f92012-09-16 19:57:18 +00004564 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004565
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004566 /* If a non-handshake record was read during renego, fallthrough,
4567 * else tell the user they should call ssl_read() again */
4568 if( ! record_read )
4569 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004570 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004571 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4572 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004573 ssl->renego_records_seen++;
4574
4575 if( ssl->renego_max_records >= 0 &&
4576 ssl->renego_records_seen > ssl->renego_max_records )
4577 {
4578 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4579 "but not honored by client" ) );
4580 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4581 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004582 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004583
4584 /* Fatal and closure alerts handled by ssl_read_record() */
4585 if( ssl->in_msgtype == SSL_MSG_ALERT )
4586 {
4587 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4588 return( POLARSSL_ERR_NET_WANT_READ );
4589 }
4590
4591 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004592 {
4593 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004594 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004595 }
4596
4597 ssl->in_offt = ssl->in_msg;
4598 }
4599
4600 n = ( len < ssl->in_msglen )
4601 ? len : ssl->in_msglen;
4602
4603 memcpy( buf, ssl->in_offt, n );
4604 ssl->in_msglen -= n;
4605
4606 if( ssl->in_msglen == 0 )
4607 /* all bytes consumed */
4608 ssl->in_offt = NULL;
4609 else
4610 /* more data available */
4611 ssl->in_offt += n;
4612
4613 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4614
Paul Bakker23986e52011-04-24 08:57:21 +00004615 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004616}
4617
4618/*
4619 * Send application data to be encrypted by the SSL layer
4620 */
Paul Bakker23986e52011-04-24 08:57:21 +00004621int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004622{
Paul Bakker23986e52011-04-24 08:57:21 +00004623 int ret;
4624 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004625 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004626
4627 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4628
4629 if( ssl->state != SSL_HANDSHAKE_OVER )
4630 {
4631 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4632 {
4633 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4634 return( ret );
4635 }
4636 }
4637
Paul Bakker05decb22013-08-15 13:33:48 +02004638#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004639 /*
4640 * Assume mfl_code is correct since it was checked when set
4641 */
4642 max_len = mfl_code_to_length[ssl->mfl_code];
4643
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004644 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004645 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004646 */
4647 if( ssl->session_out != NULL &&
4648 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4649 {
4650 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4651 }
Paul Bakker05decb22013-08-15 13:33:48 +02004652#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004653
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004654 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004655
Paul Bakker5121ce52009-01-03 21:22:43 +00004656 if( ssl->out_left != 0 )
4657 {
4658 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4659 {
4660 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4661 return( ret );
4662 }
4663 }
Paul Bakker887bd502011-06-08 13:10:54 +00004664 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004665 {
Paul Bakker887bd502011-06-08 13:10:54 +00004666 ssl->out_msglen = n;
4667 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4668 memcpy( ssl->out_msg, buf, n );
4669
4670 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4671 {
4672 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4673 return( ret );
4674 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004675 }
4676
4677 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4678
Paul Bakker23986e52011-04-24 08:57:21 +00004679 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004680}
4681
4682/*
4683 * Notify the peer that the connection is being closed
4684 */
4685int ssl_close_notify( ssl_context *ssl )
4686{
4687 int ret;
4688
4689 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4690
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004691 if( ssl->out_left != 0 )
4692 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004693
4694 if( ssl->state == SSL_HANDSHAKE_OVER )
4695 {
Paul Bakker48916f92012-09-16 19:57:18 +00004696 if( ( ret = ssl_send_alert_message( ssl,
4697 SSL_ALERT_LEVEL_WARNING,
4698 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004699 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004700 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004701 return( ret );
4702 }
4703 }
4704
4705 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4706
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004707 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004708}
4709
Paul Bakker48916f92012-09-16 19:57:18 +00004710void ssl_transform_free( ssl_transform *transform )
4711{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004712 if( transform == NULL )
4713 return;
4714
Paul Bakker48916f92012-09-16 19:57:18 +00004715#if defined(POLARSSL_ZLIB_SUPPORT)
4716 deflateEnd( &transform->ctx_deflate );
4717 inflateEnd( &transform->ctx_inflate );
4718#endif
4719
Paul Bakker84bbeb52014-07-01 14:53:22 +02004720 cipher_free( &transform->cipher_ctx_enc );
4721 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004722
Paul Bakker84bbeb52014-07-01 14:53:22 +02004723 md_free( &transform->md_ctx_enc );
4724 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004725
Paul Bakker34617722014-06-13 17:20:13 +02004726 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004727}
4728
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004729#if defined(POLARSSL_X509_CRT_PARSE_C)
4730static void ssl_key_cert_free( ssl_key_cert *key_cert )
4731{
4732 ssl_key_cert *cur = key_cert, *next;
4733
4734 while( cur != NULL )
4735 {
4736 next = cur->next;
4737
4738 if( cur->key_own_alloc )
4739 {
4740 pk_free( cur->key );
4741 polarssl_free( cur->key );
4742 }
4743 polarssl_free( cur );
4744
4745 cur = next;
4746 }
4747}
4748#endif /* POLARSSL_X509_CRT_PARSE_C */
4749
Paul Bakker48916f92012-09-16 19:57:18 +00004750void ssl_handshake_free( ssl_handshake_params *handshake )
4751{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004752 if( handshake == NULL )
4753 return;
4754
Paul Bakker48916f92012-09-16 19:57:18 +00004755#if defined(POLARSSL_DHM_C)
4756 dhm_free( &handshake->dhm_ctx );
4757#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004758#if defined(POLARSSL_ECDH_C)
4759 ecdh_free( &handshake->ecdh_ctx );
4760#endif
4761
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004762#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004763 /* explicit void pointer cast for buggy MS compiler */
4764 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004765#endif
4766
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004767#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4768 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4769 /*
4770 * Free only the linked list wrapper, not the keys themselves
4771 * since the belong to the SNI callback
4772 */
4773 if( handshake->sni_key_cert != NULL )
4774 {
4775 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4776
4777 while( cur != NULL )
4778 {
4779 next = cur->next;
4780 polarssl_free( cur );
4781 cur = next;
4782 }
4783 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004784#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004785
Paul Bakker34617722014-06-13 17:20:13 +02004786 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004787}
4788
4789void ssl_session_free( ssl_session *session )
4790{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004791 if( session == NULL )
4792 return;
4793
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004794#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004795 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004796 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004797 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004798 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004799 }
Paul Bakkered27a042013-04-18 22:46:23 +02004800#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004801
Paul Bakkera503a632013-08-14 13:48:06 +02004802#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004803 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004804#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004805
Paul Bakker34617722014-06-13 17:20:13 +02004806 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004807}
4808
Paul Bakker5121ce52009-01-03 21:22:43 +00004809/*
4810 * Free an SSL context
4811 */
4812void ssl_free( ssl_context *ssl )
4813{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004814 if( ssl == NULL )
4815 return;
4816
Paul Bakker5121ce52009-01-03 21:22:43 +00004817 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4818
Paul Bakker5121ce52009-01-03 21:22:43 +00004819 if( ssl->out_ctr != NULL )
4820 {
Paul Bakker34617722014-06-13 17:20:13 +02004821 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004822 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004823 }
4824
4825 if( ssl->in_ctr != NULL )
4826 {
Paul Bakker34617722014-06-13 17:20:13 +02004827 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004828 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004829 }
4830
Paul Bakker16770332013-10-11 09:59:44 +02004831#if defined(POLARSSL_ZLIB_SUPPORT)
4832 if( ssl->compress_buf != NULL )
4833 {
Paul Bakker34617722014-06-13 17:20:13 +02004834 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004835 polarssl_free( ssl->compress_buf );
4836 }
4837#endif
4838
Paul Bakker40e46942009-01-03 21:51:57 +00004839#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004840 mpi_free( &ssl->dhm_P );
4841 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004842#endif
4843
Paul Bakker48916f92012-09-16 19:57:18 +00004844 if( ssl->transform )
4845 {
4846 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004847 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004848 }
4849
4850 if( ssl->handshake )
4851 {
4852 ssl_handshake_free( ssl->handshake );
4853 ssl_transform_free( ssl->transform_negotiate );
4854 ssl_session_free( ssl->session_negotiate );
4855
Paul Bakker6e339b52013-07-03 13:37:05 +02004856 polarssl_free( ssl->handshake );
4857 polarssl_free( ssl->transform_negotiate );
4858 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004859 }
4860
Paul Bakkerc0463502013-02-14 11:19:38 +01004861 if( ssl->session )
4862 {
4863 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004864 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004865 }
4866
Paul Bakkera503a632013-08-14 13:48:06 +02004867#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004868 if( ssl->ticket_keys )
4869 {
4870 ssl_ticket_keys_free( ssl->ticket_keys );
4871 polarssl_free( ssl->ticket_keys );
4872 }
Paul Bakkera503a632013-08-14 13:48:06 +02004873#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004874
Paul Bakker0be444a2013-08-27 21:55:01 +02004875#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004876 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004877 {
Paul Bakker34617722014-06-13 17:20:13 +02004878 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004879 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004880 ssl->hostname_len = 0;
4881 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004882#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004883
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004884#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004885 if( ssl->psk != NULL )
4886 {
Paul Bakker34617722014-06-13 17:20:13 +02004887 polarssl_zeroize( ssl->psk, ssl->psk_len );
4888 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004889 polarssl_free( ssl->psk );
4890 polarssl_free( ssl->psk_identity );
4891 ssl->psk_len = 0;
4892 ssl->psk_identity_len = 0;
4893 }
4894#endif
4895
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004896#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004897 ssl_key_cert_free( ssl->key_cert );
4898#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004899
Paul Bakker05ef8352012-05-08 09:17:57 +00004900#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4901 if( ssl_hw_record_finish != NULL )
4902 {
4903 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4904 ssl_hw_record_finish( ssl );
4905 }
4906#endif
4907
Paul Bakker5121ce52009-01-03 21:22:43 +00004908 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004909
Paul Bakker86f04f42013-02-14 11:20:09 +01004910 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004911 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004912}
4913
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004914#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004915/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004916 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004917 */
4918unsigned char ssl_sig_from_pk( pk_context *pk )
4919{
4920#if defined(POLARSSL_RSA_C)
4921 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4922 return( SSL_SIG_RSA );
4923#endif
4924#if defined(POLARSSL_ECDSA_C)
4925 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4926 return( SSL_SIG_ECDSA );
4927#endif
4928 return( SSL_SIG_ANON );
4929}
4930
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004931pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4932{
4933 switch( sig )
4934 {
4935#if defined(POLARSSL_RSA_C)
4936 case SSL_SIG_RSA:
4937 return( POLARSSL_PK_RSA );
4938#endif
4939#if defined(POLARSSL_ECDSA_C)
4940 case SSL_SIG_ECDSA:
4941 return( POLARSSL_PK_ECDSA );
4942#endif
4943 default:
4944 return( POLARSSL_PK_NONE );
4945 }
4946}
Paul Bakker9af723c2014-05-01 13:03:14 +02004947#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004948
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004949/*
4950 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4951 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004952md_type_t ssl_md_alg_from_hash( unsigned char hash )
4953{
4954 switch( hash )
4955 {
4956#if defined(POLARSSL_MD5_C)
4957 case SSL_HASH_MD5:
4958 return( POLARSSL_MD_MD5 );
4959#endif
4960#if defined(POLARSSL_SHA1_C)
4961 case SSL_HASH_SHA1:
4962 return( POLARSSL_MD_SHA1 );
4963#endif
4964#if defined(POLARSSL_SHA256_C)
4965 case SSL_HASH_SHA224:
4966 return( POLARSSL_MD_SHA224 );
4967 case SSL_HASH_SHA256:
4968 return( POLARSSL_MD_SHA256 );
4969#endif
4970#if defined(POLARSSL_SHA512_C)
4971 case SSL_HASH_SHA384:
4972 return( POLARSSL_MD_SHA384 );
4973 case SSL_HASH_SHA512:
4974 return( POLARSSL_MD_SHA512 );
4975#endif
4976 default:
4977 return( POLARSSL_MD_NONE );
4978 }
4979}
4980
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004981#if defined(POLARSSL_SSL_SET_CURVES)
4982/*
4983 * Check is a curve proposed by the peer is in our list.
4984 * Return 1 if we're willing to use it, 0 otherwise.
4985 */
4986int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4987{
4988 const ecp_group_id *gid;
4989
4990 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4991 if( *gid == grp_id )
4992 return( 1 );
4993
4994 return( 0 );
4995}
Paul Bakker9af723c2014-05-01 13:03:14 +02004996#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004997
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004998#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004999int ssl_check_cert_usage( const x509_crt *cert,
5000 const ssl_ciphersuite_t *ciphersuite,
5001 int cert_endpoint )
5002{
5003#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5004 int usage = 0;
5005#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005006#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5007 const char *ext_oid;
5008 size_t ext_len;
5009#endif
5010
5011#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5012 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5013 ((void) cert);
5014 ((void) cert_endpoint);
5015#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005016
5017#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5018 if( cert_endpoint == SSL_IS_SERVER )
5019 {
5020 /* Server part of the key exchange */
5021 switch( ciphersuite->key_exchange )
5022 {
5023 case POLARSSL_KEY_EXCHANGE_RSA:
5024 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5025 usage = KU_KEY_ENCIPHERMENT;
5026 break;
5027
5028 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5029 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5030 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5031 usage = KU_DIGITAL_SIGNATURE;
5032 break;
5033
5034 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5035 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5036 usage = KU_KEY_AGREEMENT;
5037 break;
5038
5039 /* Don't use default: we want warnings when adding new values */
5040 case POLARSSL_KEY_EXCHANGE_NONE:
5041 case POLARSSL_KEY_EXCHANGE_PSK:
5042 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5043 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5044 usage = 0;
5045 }
5046 }
5047 else
5048 {
5049 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5050 usage = KU_DIGITAL_SIGNATURE;
5051 }
5052
5053 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5054 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005055#else
5056 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005057#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5058
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005059#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5060 if( cert_endpoint == SSL_IS_SERVER )
5061 {
5062 ext_oid = OID_SERVER_AUTH;
5063 ext_len = OID_SIZE( OID_SERVER_AUTH );
5064 }
5065 else
5066 {
5067 ext_oid = OID_CLIENT_AUTH;
5068 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5069 }
5070
5071 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5072 return( -1 );
5073#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5074
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005075 return( 0 );
5076}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005077#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005078
5079#endif /* POLARSSL_SSL_TLS_C */