blob: b59f7af6256d2258ac111667fce2aff4b7f5f5b4 [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
Paul Bakker48916f92012-09-16 19:57:18 +0000473 handshake->tls_prf( handshake->premaster, handshake->pmslen,
474 "master secret",
475 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
Paul Bakker34617722014-06-13 17:20:13 +0200477 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 }
479 else
480 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
481
482 /*
483 * Swap the client and server random values.
484 */
Paul Bakker48916f92012-09-16 19:57:18 +0000485 memcpy( tmp, handshake->randbytes, 64 );
486 memcpy( handshake->randbytes, tmp + 32, 32 );
487 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200488 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 /*
491 * SSLv3:
492 * key block =
493 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
494 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
495 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
496 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
497 * ...
498 *
499 * TLSv1:
500 * key block = PRF( master, "key expansion", randbytes )
501 */
Paul Bakker48916f92012-09-16 19:57:18 +0000502 handshake->tls_prf( session->master, 48, "key expansion",
503 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Paul Bakker48916f92012-09-16 19:57:18 +0000505 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
506 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
507 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
508 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
510
Paul Bakker34617722014-06-13 17:20:13 +0200511 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 /*
514 * Determine the appropriate key, IV and MAC length.
515 */
Paul Bakker68884e32013-01-07 18:20:04 +0100516
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200517 transform->keylen = cipher_info->key_length / 8;
518
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200519 if( cipher_info->mode == POLARSSL_MODE_GCM ||
520 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200522 transform->maclen = 0;
523
Paul Bakker68884e32013-01-07 18:20:04 +0100524 transform->ivlen = 12;
525 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200526
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200527 /* Minimum length is expicit IV + tag */
528 transform->minlen = transform->ivlen - transform->fixed_ivlen
529 + ( transform->ciphersuite_info->flags &
530 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100531 }
532 else
533 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200534 int ret;
535
536 /* Initialize HMAC contexts */
537 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
538 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100539 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200540 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
541 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100542 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200544 /* Get MAC length */
545 transform->maclen = md_get_size( md_info );
546
547#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
548 /*
549 * If HMAC is to be truncated, we shall keep the leftmost bytes,
550 * (rfc 6066 page 13 or rfc 2104 section 4),
551 * so we only need to adjust the length here.
552 */
553 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
554 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
555#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
556
557 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100558 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200560 /* Minimum length */
561 if( cipher_info->mode == POLARSSL_MODE_STREAM )
562 transform->minlen = transform->maclen;
563 else
Paul Bakker68884e32013-01-07 18:20:04 +0100564 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200565 /*
566 * GenericBlockCipher:
567 * first multiple of blocklen greater than maclen
568 * + IV except for SSL3 and TLS 1.0
569 */
570 transform->minlen = transform->maclen
571 + cipher_info->block_size
572 - transform->maclen % cipher_info->block_size;
573
574#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
575 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
576 ssl->minor_ver == SSL_MINOR_VERSION_1 )
577 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100578 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200579#endif
580#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
581 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
582 ssl->minor_ver == SSL_MINOR_VERSION_3 )
583 {
584 transform->minlen += transform->ivlen;
585 }
586 else
587#endif
588 {
589 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
590 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
591 }
Paul Bakker68884e32013-01-07 18:20:04 +0100592 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 }
594
595 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000596 transform->keylen, transform->minlen, transform->ivlen,
597 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 /*
600 * Finally setup the cipher contexts, IVs and MAC secrets.
601 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100602#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 if( ssl->endpoint == SSL_IS_CLIENT )
604 {
Paul Bakker48916f92012-09-16 19:57:18 +0000605 key1 = keyblk + transform->maclen * 2;
606 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
Paul Bakker68884e32013-01-07 18:20:04 +0100608 mac_enc = keyblk;
609 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000611 /*
612 * This is not used in TLS v1.1.
613 */
Paul Bakker48916f92012-09-16 19:57:18 +0000614 iv_copy_len = ( transform->fixed_ivlen ) ?
615 transform->fixed_ivlen : transform->ivlen;
616 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
617 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000618 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 }
620 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100621#endif /* POLARSSL_SSL_CLI_C */
622#if defined(POLARSSL_SSL_SRV_C)
623 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 {
Paul Bakker48916f92012-09-16 19:57:18 +0000625 key1 = keyblk + transform->maclen * 2 + transform->keylen;
626 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Paul Bakker68884e32013-01-07 18:20:04 +0100628 mac_enc = keyblk + transform->maclen;
629 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000631 /*
632 * This is not used in TLS v1.1.
633 */
Paul Bakker48916f92012-09-16 19:57:18 +0000634 iv_copy_len = ( transform->fixed_ivlen ) ?
635 transform->fixed_ivlen : transform->ivlen;
636 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
637 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000638 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100640 else
641#endif /* POLARSSL_SSL_SRV_C */
642 {
643 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
644 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
645 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200647#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100648 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
649 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100650 if( transform->maclen > sizeof transform->mac_enc )
651 {
652 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200653 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100654 }
655
Paul Bakker68884e32013-01-07 18:20:04 +0100656 memcpy( transform->mac_enc, mac_enc, transform->maclen );
657 memcpy( transform->mac_dec, mac_dec, transform->maclen );
658 }
659 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200660#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200661#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
662 defined(POLARSSL_SSL_PROTO_TLS1_2)
663 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100664 {
665 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
666 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
667 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200668 else
669#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200670 {
671 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200672 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200673 }
Paul Bakker68884e32013-01-07 18:20:04 +0100674
Paul Bakker05ef8352012-05-08 09:17:57 +0000675#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200676 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000677 {
678 int ret = 0;
679
680 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
681
Paul Bakker07eb38b2012-12-19 14:42:06 +0100682 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
683 transform->iv_enc, transform->iv_dec,
684 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100685 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100686 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000687 {
688 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200689 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000690 }
691 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200692#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000693
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200694 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
695 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200697 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
698 return( ret );
699 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200700
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200701 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
702 cipher_info ) ) != 0 )
703 {
704 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
705 return( ret );
706 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200707
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200708 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
709 cipher_info->key_length,
710 POLARSSL_ENCRYPT ) ) != 0 )
711 {
712 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
713 return( ret );
714 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200715
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200716 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
717 cipher_info->key_length,
718 POLARSSL_DECRYPT ) ) != 0 )
719 {
720 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
721 return( ret );
722 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200723
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200724#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200725 if( cipher_info->mode == POLARSSL_MODE_CBC )
726 {
727 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
728 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200729 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200730 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
731 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200732 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200733
734 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
735 POLARSSL_PADDING_NONE ) ) != 0 )
736 {
737 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
738 return( ret );
739 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200741#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Paul Bakker34617722014-06-13 17:20:13 +0200743 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Paul Bakker2770fbd2012-07-03 13:30:23 +0000745#if defined(POLARSSL_ZLIB_SUPPORT)
746 // Initialize compression
747 //
Paul Bakker48916f92012-09-16 19:57:18 +0000748 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000749 {
Paul Bakker16770332013-10-11 09:59:44 +0200750 if( ssl->compress_buf == NULL )
751 {
752 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
753 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
754 if( ssl->compress_buf == NULL )
755 {
756 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
757 SSL_BUFFER_LEN ) );
758 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
759 }
760 }
761
Paul Bakker2770fbd2012-07-03 13:30:23 +0000762 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
763
Paul Bakker48916f92012-09-16 19:57:18 +0000764 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
765 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000766
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200767 if( deflateInit( &transform->ctx_deflate,
768 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000769 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000770 {
771 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
772 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
773 }
774 }
775#endif /* POLARSSL_ZLIB_SUPPORT */
776
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
778
779 return( 0 );
780}
781
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200782#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000783void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000784{
785 md5_context md5;
786 sha1_context sha1;
787 unsigned char pad_1[48];
788 unsigned char pad_2[48];
789
Paul Bakker380da532012-04-18 16:10:25 +0000790 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
Paul Bakker48916f92012-09-16 19:57:18 +0000792 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
793 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Paul Bakker380da532012-04-18 16:10:25 +0000795 memset( pad_1, 0x36, 48 );
796 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Paul Bakker48916f92012-09-16 19:57:18 +0000798 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000799 md5_update( &md5, pad_1, 48 );
800 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000801
Paul Bakker380da532012-04-18 16:10:25 +0000802 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000803 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000804 md5_update( &md5, pad_2, 48 );
805 md5_update( &md5, hash, 16 );
806 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000807
Paul Bakker48916f92012-09-16 19:57:18 +0000808 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000809 sha1_update( &sha1, pad_1, 40 );
810 sha1_finish( &sha1, hash + 16 );
811
812 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000813 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000814 sha1_update( &sha1, pad_2, 40 );
815 sha1_update( &sha1, hash + 16, 20 );
816 sha1_finish( &sha1, hash + 16 );
817
818 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
819 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
820
Paul Bakker5b4af392014-06-26 12:09:34 +0200821 md5_free( &md5 );
822 sha1_free( &sha1 );
823
Paul Bakker380da532012-04-18 16:10:25 +0000824 return;
825}
Paul Bakker9af723c2014-05-01 13:03:14 +0200826#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000827
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000829void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
830{
831 md5_context md5;
832 sha1_context sha1;
833
834 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
835
Paul Bakker48916f92012-09-16 19:57:18 +0000836 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
837 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000838
Paul Bakker48916f92012-09-16 19:57:18 +0000839 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000840 sha1_finish( &sha1, hash + 16 );
841
842 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
843 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
844
Paul Bakker5b4af392014-06-26 12:09:34 +0200845 md5_free( &md5 );
846 sha1_free( &sha1 );
847
Paul Bakker380da532012-04-18 16:10:25 +0000848 return;
849}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200850#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000851
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200852#if defined(POLARSSL_SSL_PROTO_TLS1_2)
853#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000854void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
855{
Paul Bakker9e36f042013-06-30 14:34:05 +0200856 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000857
858 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
859
Paul Bakker9e36f042013-06-30 14:34:05 +0200860 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
861 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000862
863 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
864 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
865
Paul Bakker5b4af392014-06-26 12:09:34 +0200866 sha256_free( &sha256 );
867
Paul Bakker380da532012-04-18 16:10:25 +0000868 return;
869}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200870#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000871
Paul Bakker9e36f042013-06-30 14:34:05 +0200872#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000873void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
874{
Paul Bakker9e36f042013-06-30 14:34:05 +0200875 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000876
877 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
878
Paul Bakker9e36f042013-06-30 14:34:05 +0200879 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
880 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
Paul Bakkerca4ab492012-04-18 14:23:57 +0000882 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
884
Paul Bakker5b4af392014-06-26 12:09:34 +0200885 sha512_free( &sha512 );
886
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 return;
888}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200889#endif /* POLARSSL_SHA512_C */
890#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200892#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200893int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
894{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200895 unsigned char *p = ssl->handshake->premaster;
896 unsigned char *end = p + sizeof( ssl->handshake->premaster );
897
898 /*
899 * PMS = struct {
900 * opaque other_secret<0..2^16-1>;
901 * opaque psk<0..2^16-1>;
902 * };
903 * with "other_secret" depending on the particular key exchange
904 */
905#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
906 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
907 {
908 if( end - p < 2 + (int) ssl->psk_len )
909 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
910
911 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
912 *(p++) = (unsigned char)( ssl->psk_len );
913 p += ssl->psk_len;
914 }
915 else
916#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200917#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
918 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
919 {
920 /*
921 * other_secret already set by the ClientKeyExchange message,
922 * and is 48 bytes long
923 */
924 *p++ = 0;
925 *p++ = 48;
926 p += 48;
927 }
928 else
929#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200930#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
931 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
932 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200933 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200934 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200935
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200936 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200938 p + 2, &len,
939 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200940 {
941 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
942 return( ret );
943 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200944 *(p++) = (unsigned char)( len >> 8 );
945 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200946 p += len;
947
948 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
949 }
950 else
951#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
952#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
953 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
954 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200955 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200956 size_t zlen;
957
958 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200959 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200960 ssl->f_rng, ssl->p_rng ) ) != 0 )
961 {
962 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
963 return( ret );
964 }
965
966 *(p++) = (unsigned char)( zlen >> 8 );
967 *(p++) = (unsigned char)( zlen );
968 p += zlen;
969
970 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
971 }
972 else
973#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
974 {
975 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200976 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200977 }
978
979 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100980 if( end - p < 2 + (int) ssl->psk_len )
981 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
982
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
984 *(p++) = (unsigned char)( ssl->psk_len );
985 memcpy( p, ssl->psk, ssl->psk_len );
986 p += ssl->psk_len;
987
988 ssl->handshake->pmslen = p - ssl->handshake->premaster;
989
990 return( 0 );
991}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200992#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200993
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200994#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000995/*
996 * SSLv3.0 MAC functions
997 */
Paul Bakker68884e32013-01-07 18:20:04 +0100998static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
999 unsigned char *buf, size_t len,
1000 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001001{
1002 unsigned char header[11];
1003 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001004 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001005 int md_size = md_get_size( md_ctx->md_info );
1006 int md_type = md_get_type( md_ctx->md_info );
1007
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001008 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001009 if( md_type == POLARSSL_MD_MD5 )
1010 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001011 else
Paul Bakker68884e32013-01-07 18:20:04 +01001012 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
1014 memcpy( header, ctr, 8 );
1015 header[ 8] = (unsigned char) type;
1016 header[ 9] = (unsigned char)( len >> 8 );
1017 header[10] = (unsigned char)( len );
1018
Paul Bakker68884e32013-01-07 18:20:04 +01001019 memset( padding, 0x36, padlen );
1020 md_starts( md_ctx );
1021 md_update( md_ctx, secret, md_size );
1022 md_update( md_ctx, padding, padlen );
1023 md_update( md_ctx, header, 11 );
1024 md_update( md_ctx, buf, len );
1025 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Paul Bakker68884e32013-01-07 18:20:04 +01001027 memset( padding, 0x5C, padlen );
1028 md_starts( md_ctx );
1029 md_update( md_ctx, secret, md_size );
1030 md_update( md_ctx, padding, padlen );
1031 md_update( md_ctx, buf + len, md_size );
1032 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001033}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001034#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001035
Paul Bakker5121ce52009-01-03 21:22:43 +00001036/*
1037 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001038 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001039static int ssl_encrypt_buf( ssl_context *ssl )
1040{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001041 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001042 const cipher_mode_t mode = cipher_get_cipher_mode(
1043 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
1045 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1046
1047 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001048 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001049 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001050#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1051 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1052 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001053 if( mode != POLARSSL_MODE_GCM &&
1054 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001056#if defined(POLARSSL_SSL_PROTO_SSL3)
1057 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1058 {
1059 ssl_mac( &ssl->transform_out->md_ctx_enc,
1060 ssl->transform_out->mac_enc,
1061 ssl->out_msg, ssl->out_msglen,
1062 ssl->out_ctr, ssl->out_msgtype );
1063 }
1064 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001065#endif
1066#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001067 defined(POLARSSL_SSL_PROTO_TLS1_2)
1068 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1069 {
1070 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1071 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1072 ssl->out_msg, ssl->out_msglen );
1073 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1074 ssl->out_msg + ssl->out_msglen );
1075 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1076 }
1077 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001078#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001079 {
1080 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001081 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001082 }
1083
1084 SSL_DEBUG_BUF( 4, "computed mac",
1085 ssl->out_msg + ssl->out_msglen,
1086 ssl->transform_out->maclen );
1087
1088 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001089 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001090#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001092 /*
1093 * Encrypt
1094 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001095#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001096 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001098 int ret;
1099 size_t olen = 0;
1100
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1102 "including %d bytes of padding",
1103 ssl->out_msglen, 0 ) );
1104
1105 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1106 ssl->out_msg, ssl->out_msglen );
1107
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001108 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001109 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001110 ssl->transform_out->ivlen,
1111 ssl->out_msg, ssl->out_msglen,
1112 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001113 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001114 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001115 return( ret );
1116 }
1117
1118 if( ssl->out_msglen != olen )
1119 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001120 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001121 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 }
Paul Bakker68884e32013-01-07 18:20:04 +01001124 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001126#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1127 if( mode == POLARSSL_MODE_GCM ||
1128 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001129 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001130 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001131 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001132 unsigned char *enc_msg;
1133 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001134 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1135 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001136
Paul Bakkerca4ab492012-04-18 14:23:57 +00001137 memcpy( add_data, ssl->out_ctr, 8 );
1138 add_data[8] = ssl->out_msgtype;
1139 add_data[9] = ssl->major_ver;
1140 add_data[10] = ssl->minor_ver;
1141 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1142 add_data[12] = ssl->out_msglen & 0xFF;
1143
1144 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1145 add_data, 13 );
1146
Paul Bakker68884e32013-01-07 18:20:04 +01001147 /*
1148 * Generate IV
1149 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001150#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001151 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001152 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1153 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001154 if( ret != 0 )
1155 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001156
Paul Bakker68884e32013-01-07 18:20:04 +01001157 memcpy( ssl->out_iv,
1158 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1159 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001160#else
1161 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1162 {
1163 /* Reminder if we ever add an AEAD mode with a different size */
1164 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1165 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1166 }
1167
1168 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1169 ssl->out_ctr, 8 );
1170 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1171#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001172
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001173 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001174 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001175
Paul Bakker68884e32013-01-07 18:20:04 +01001176 /*
1177 * Fix pointer positions and message length with added IV
1178 */
1179 enc_msg = ssl->out_msg;
1180 enc_msglen = ssl->out_msglen;
1181 ssl->out_msglen += ssl->transform_out->ivlen -
1182 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001183
Paul Bakker68884e32013-01-07 18:20:04 +01001184 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1185 "including %d bytes of padding",
1186 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001187
Paul Bakker68884e32013-01-07 18:20:04 +01001188 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1189 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001190
Paul Bakker68884e32013-01-07 18:20:04 +01001191 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001192 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001193 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001194 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1195 ssl->transform_out->iv_enc,
1196 ssl->transform_out->ivlen,
1197 add_data, 13,
1198 enc_msg, enc_msglen,
1199 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001200 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001201 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001202 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001203 return( ret );
1204 }
1205
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001206 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001207 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001208 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001209 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001210 }
1211
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001212 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001213
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001214 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001215 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001217#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001218#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1219 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001220 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001222 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001223 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001224 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001225
Paul Bakker48916f92012-09-16 19:57:18 +00001226 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1227 ssl->transform_out->ivlen;
1228 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 padlen = 0;
1230
1231 for( i = 0; i <= padlen; i++ )
1232 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1233
1234 ssl->out_msglen += padlen + 1;
1235
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001236 enc_msglen = ssl->out_msglen;
1237 enc_msg = ssl->out_msg;
1238
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001239#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001241 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1242 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001244 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001245 {
1246 /*
1247 * Generate IV
1248 */
Paul Bakker48916f92012-09-16 19:57:18 +00001249 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1250 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001251 if( ret != 0 )
1252 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001253
Paul Bakker92be97b2013-01-02 17:30:03 +01001254 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001255 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001256
1257 /*
1258 * Fix pointer positions and message length with added IV
1259 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001260 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001261 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001262 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001263 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001264#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001265
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001267 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001268 ssl->out_msglen, ssl->transform_out->ivlen,
1269 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001270
1271 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001272 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001274 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001275 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001276 ssl->transform_out->ivlen,
1277 enc_msg, enc_msglen,
1278 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001279 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001280 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001281 return( ret );
1282 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001283
Paul Bakkercca5b812013-08-31 17:40:26 +02001284 if( enc_msglen != olen )
1285 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001286 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001287 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001288 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001289
1290#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001291 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1292 {
1293 /*
1294 * Save IV in SSL3 and TLS1
1295 */
1296 memcpy( ssl->transform_out->iv_enc,
1297 ssl->transform_out->cipher_ctx_enc.iv,
1298 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001300#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001301 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001302 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001303#endif /* POLARSSL_CIPHER_MODE_CBC &&
1304 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001305 {
1306 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001307 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001308 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
Paul Bakkerca4ab492012-04-18 14:23:57 +00001310 for( i = 8; i > 0; i-- )
1311 if( ++ssl->out_ctr[i - 1] != 0 )
1312 break;
1313
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001314 /* The loops goes to its end iff the counter is wrapping */
1315 if( i == 0 )
1316 {
1317 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1318 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1319 }
1320
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1322
1323 return( 0 );
1324}
1325
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001326#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001327
Paul Bakker5121ce52009-01-03 21:22:43 +00001328static int ssl_decrypt_buf( ssl_context *ssl )
1329{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001330 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001331 const cipher_mode_t mode = cipher_get_cipher_mode(
1332 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001333#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1334 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1335 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1336 size_t padlen = 0, correct = 1;
1337#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
1339 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1340
Paul Bakker48916f92012-09-16 19:57:18 +00001341 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 {
1343 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001344 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001345 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
1347
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001348#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001349 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001350 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001351 int ret;
1352 size_t olen = 0;
1353
Paul Bakker68884e32013-01-07 18:20:04 +01001354 padlen = 0;
1355
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001356 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001357 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001358 ssl->transform_in->ivlen,
1359 ssl->in_msg, ssl->in_msglen,
1360 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001361 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001362 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001363 return( ret );
1364 }
1365
1366 if( ssl->in_msglen != olen )
1367 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001368 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001369 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001370 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 }
Paul Bakker68884e32013-01-07 18:20:04 +01001372 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001373#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001374#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1375 if( mode == POLARSSL_MODE_GCM ||
1376 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001377 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001378 int ret;
1379 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001380 unsigned char *dec_msg;
1381 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001382 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001383 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1384 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001385 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1386 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001387
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001388 if( ssl->in_msglen < explicit_iv_len + taglen )
1389 {
1390 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1391 "+ taglen (%d)", ssl->in_msglen,
1392 explicit_iv_len, taglen ) );
1393 return( POLARSSL_ERR_SSL_INVALID_MAC );
1394 }
1395 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1396
Paul Bakker68884e32013-01-07 18:20:04 +01001397 dec_msg = ssl->in_msg;
1398 dec_msg_result = ssl->in_msg;
1399 ssl->in_msglen = dec_msglen;
1400
1401 memcpy( add_data, ssl->in_ctr, 8 );
1402 add_data[8] = ssl->in_msgtype;
1403 add_data[9] = ssl->major_ver;
1404 add_data[10] = ssl->minor_ver;
1405 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1406 add_data[12] = ssl->in_msglen & 0xFF;
1407
1408 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1409 add_data, 13 );
1410
1411 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1412 ssl->in_iv,
1413 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1414
1415 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1416 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001417 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001418
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001419 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001420 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001421 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001422 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1423 ssl->transform_in->iv_dec,
1424 ssl->transform_in->ivlen,
1425 add_data, 13,
1426 dec_msg, dec_msglen,
1427 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001428 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001429 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001430 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1431
1432 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1433 return( POLARSSL_ERR_SSL_INVALID_MAC );
1434
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001435 return( ret );
1436 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001437
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001438 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001439 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001440 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001441 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001442 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001443 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001445#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001446#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1447 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001448 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 {
Paul Bakker45829992013-01-03 14:52:21 +01001450 /*
1451 * Decrypt and check the padding
1452 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001453 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001454 unsigned char *dec_msg;
1455 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001456 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001457 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001458 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001459
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 /*
Paul Bakker45829992013-01-03 14:52:21 +01001461 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 */
Paul Bakker48916f92012-09-16 19:57:18 +00001463 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 {
1465 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001466 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001467 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 }
1469
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001470#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001471 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1472 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001473#endif
Paul Bakker45829992013-01-03 14:52:21 +01001474
1475 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1476 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1477 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001478 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1479 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1480 ssl->transform_in->ivlen,
1481 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001482 return( POLARSSL_ERR_SSL_INVALID_MAC );
1483 }
1484
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001485 dec_msglen = ssl->in_msglen;
1486 dec_msg = ssl->in_msg;
1487 dec_msg_result = ssl->in_msg;
1488
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001489#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001490 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001491 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001492 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001493 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001494 {
Paul Bakker48916f92012-09-16 19:57:18 +00001495 dec_msglen -= ssl->transform_in->ivlen;
1496 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001497
Paul Bakker48916f92012-09-16 19:57:18 +00001498 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001499 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001500 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001501#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001502
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001503 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001504 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001505 ssl->transform_in->ivlen,
1506 dec_msg, dec_msglen,
1507 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001508 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001509 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001510 return( ret );
1511 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001512
Paul Bakkercca5b812013-08-31 17:40:26 +02001513 if( dec_msglen != olen )
1514 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001515 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001516 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001517 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001518
1519#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001520 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1521 {
1522 /*
1523 * Save IV in SSL3 and TLS1
1524 */
1525 memcpy( ssl->transform_in->iv_dec,
1526 ssl->transform_in->cipher_ctx_dec.iv,
1527 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001529#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
1531 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001532
1533 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1534 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001535#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001536 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1537 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001538#endif
Paul Bakker45829992013-01-03 14:52:21 +01001539 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001540 correct = 0;
1541 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001542
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001543#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1545 {
Paul Bakker48916f92012-09-16 19:57:18 +00001546 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001548#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001549 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1550 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001551 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001552#endif
Paul Bakker45829992013-01-03 14:52:21 +01001553 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 }
1555 }
1556 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001557#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001558#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1559 defined(POLARSSL_SSL_PROTO_TLS1_2)
1560 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 {
1562 /*
Paul Bakker45829992013-01-03 14:52:21 +01001563 * TLSv1+: always check the padding up to the first failure
1564 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001566 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001567 size_t padding_idx = ssl->in_msglen - padlen - 1;
1568
Paul Bakker956c9e02013-12-19 14:42:28 +01001569 /*
1570 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001571 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001572 *
Paul Bakker61885c72014-04-25 12:59:03 +02001573 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1574 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001575 *
1576 * In both cases we reset padding_idx to a safe value (0) to
1577 * prevent out-of-buffer reads.
1578 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001579 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001580 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1581 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001582
1583 padding_idx *= correct;
1584
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001585 for( i = 1; i <= 256; i++ )
1586 {
1587 real_count &= ( i <= padlen );
1588 pad_count += real_count *
1589 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1590 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001591
1592 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001593
Paul Bakkerd66f0702013-01-31 16:57:45 +01001594#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001595 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001596 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001597#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001598 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001599 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001600 else
1601#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1602 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001603 {
1604 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001605 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001606 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001608 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001609#endif /* POLARSSL_CIPHER_MODE_CBC &&
1610 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001611 {
1612 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001613 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
1616 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1617 ssl->in_msg, ssl->in_msglen );
1618
1619 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001620 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001622#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1623 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1624 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001625 if( mode != POLARSSL_MODE_GCM &&
1626 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001627 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001628 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1629
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001630 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001631
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001632 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1633 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001634
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001635 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001637#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001638 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1639 {
1640 ssl_mac( &ssl->transform_in->md_ctx_dec,
1641 ssl->transform_in->mac_dec,
1642 ssl->in_msg, ssl->in_msglen,
1643 ssl->in_ctr, ssl->in_msgtype );
1644 }
1645 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001646#endif /* POLARSSL_SSL_PROTO_SSL3 */
1647#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001648 defined(POLARSSL_SSL_PROTO_TLS1_2)
1649 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1650 {
1651 /*
1652 * Process MAC and always update for padlen afterwards to make
1653 * total time independent of padlen
1654 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001655 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001656 *
1657 * Known timing attacks:
1658 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1659 *
1660 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1661 * correctly. (We round down instead of up, so -56 is the correct
1662 * value for our calculations instead of -55)
1663 */
1664 size_t j, extra_run = 0;
1665 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1666 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001667
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001669
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001670 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1671 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1672 ssl->in_msglen );
1673 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1674 ssl->in_msg + ssl->in_msglen );
1675 for( j = 0; j < extra_run; j++ )
1676 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001677
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1679 }
1680 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001681#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 POLARSSL_SSL_PROTO_TLS1_2 */
1683 {
1684 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001685 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001686 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001688 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1689 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1690 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001692 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001693 ssl->transform_in->maclen ) != 0 )
1694 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001695#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001696 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001697#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001698 correct = 0;
1699 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001700
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001701 /*
1702 * Finally check the correct flag
1703 */
1704 if( correct == 0 )
1705 return( POLARSSL_ERR_SSL_INVALID_MAC );
1706 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001707#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001708
1709 if( ssl->in_msglen == 0 )
1710 {
1711 ssl->nb_zero++;
1712
1713 /*
1714 * Three or more empty messages may be a DoS attack
1715 * (excessive CPU consumption).
1716 */
1717 if( ssl->nb_zero > 3 )
1718 {
1719 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1720 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001721 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001722 }
1723 }
1724 else
1725 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001726
Paul Bakker23986e52011-04-24 08:57:21 +00001727 for( i = 8; i > 0; i-- )
1728 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001729 break;
1730
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001731 /* The loops goes to its end iff the counter is wrapping */
1732 if( i == 0 )
1733 {
1734 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1735 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1736 }
1737
Paul Bakker5121ce52009-01-03 21:22:43 +00001738 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1739
1740 return( 0 );
1741}
1742
Paul Bakker2770fbd2012-07-03 13:30:23 +00001743#if defined(POLARSSL_ZLIB_SUPPORT)
1744/*
1745 * Compression/decompression functions
1746 */
1747static int ssl_compress_buf( ssl_context *ssl )
1748{
1749 int ret;
1750 unsigned char *msg_post = ssl->out_msg;
1751 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001752 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001753
1754 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1755
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001756 if( len_pre == 0 )
1757 return( 0 );
1758
Paul Bakker2770fbd2012-07-03 13:30:23 +00001759 memcpy( msg_pre, ssl->out_msg, len_pre );
1760
1761 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1762 ssl->out_msglen ) );
1763
1764 SSL_DEBUG_BUF( 4, "before compression: output payload",
1765 ssl->out_msg, ssl->out_msglen );
1766
Paul Bakker48916f92012-09-16 19:57:18 +00001767 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1768 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1769 ssl->transform_out->ctx_deflate.next_out = msg_post;
1770 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001771
Paul Bakker48916f92012-09-16 19:57:18 +00001772 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001773 if( ret != Z_OK )
1774 {
1775 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1776 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1777 }
1778
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001779 ssl->out_msglen = SSL_BUFFER_LEN -
1780 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001781
Paul Bakker2770fbd2012-07-03 13:30:23 +00001782 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1783 ssl->out_msglen ) );
1784
1785 SSL_DEBUG_BUF( 4, "after compression: output payload",
1786 ssl->out_msg, ssl->out_msglen );
1787
1788 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1789
1790 return( 0 );
1791}
1792
1793static int ssl_decompress_buf( ssl_context *ssl )
1794{
1795 int ret;
1796 unsigned char *msg_post = ssl->in_msg;
1797 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001798 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799
1800 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1801
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001802 if( len_pre == 0 )
1803 return( 0 );
1804
Paul Bakker2770fbd2012-07-03 13:30:23 +00001805 memcpy( msg_pre, ssl->in_msg, len_pre );
1806
1807 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1808 ssl->in_msglen ) );
1809
1810 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1811 ssl->in_msg, ssl->in_msglen );
1812
Paul Bakker48916f92012-09-16 19:57:18 +00001813 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1814 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1815 ssl->transform_in->ctx_inflate.next_out = msg_post;
1816 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001817
Paul Bakker48916f92012-09-16 19:57:18 +00001818 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001819 if( ret != Z_OK )
1820 {
1821 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1822 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1823 }
1824
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001825 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1826 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001827
Paul Bakker2770fbd2012-07-03 13:30:23 +00001828 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1829 ssl->in_msglen ) );
1830
1831 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1832 ssl->in_msg, ssl->in_msglen );
1833
1834 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1835
1836 return( 0 );
1837}
1838#endif /* POLARSSL_ZLIB_SUPPORT */
1839
Paul Bakker5121ce52009-01-03 21:22:43 +00001840/*
1841 * Fill the input message buffer
1842 */
Paul Bakker23986e52011-04-24 08:57:21 +00001843int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001844{
Paul Bakker23986e52011-04-24 08:57:21 +00001845 int ret;
1846 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
1848 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1849
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001850 if( nb_want > SSL_BUFFER_LEN - 8 )
1851 {
1852 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1853 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1854 }
1855
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 while( ssl->in_left < nb_want )
1857 {
1858 len = nb_want - ssl->in_left;
1859 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1860
1861 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1862 ssl->in_left, nb_want ) );
1863 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1864
Paul Bakker831a7552011-05-18 13:32:51 +00001865 if( ret == 0 )
1866 return( POLARSSL_ERR_SSL_CONN_EOF );
1867
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 if( ret < 0 )
1869 return( ret );
1870
1871 ssl->in_left += ret;
1872 }
1873
1874 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1875
1876 return( 0 );
1877}
1878
1879/*
1880 * Flush any data not yet written
1881 */
1882int ssl_flush_output( ssl_context *ssl )
1883{
1884 int ret;
1885 unsigned char *buf;
1886
1887 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1888
1889 while( ssl->out_left > 0 )
1890 {
1891 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1892 5 + ssl->out_msglen, ssl->out_left ) );
1893
Paul Bakker5bd42292012-12-19 14:40:42 +01001894 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001896
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1898
1899 if( ret <= 0 )
1900 return( ret );
1901
1902 ssl->out_left -= ret;
1903 }
1904
1905 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1906
1907 return( 0 );
1908}
1909
1910/*
1911 * Record layer functions
1912 */
1913int ssl_write_record( ssl_context *ssl )
1914{
Paul Bakker05ef8352012-05-08 09:17:57 +00001915 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001916 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
1918 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1919
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1921 {
1922 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1923 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1924 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1925
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001926 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1927 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 }
1929
Paul Bakker2770fbd2012-07-03 13:30:23 +00001930#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001931 if( ssl->transform_out != NULL &&
1932 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001933 {
1934 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1935 {
1936 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1937 return( ret );
1938 }
1939
1940 len = ssl->out_msglen;
1941 }
1942#endif /*POLARSSL_ZLIB_SUPPORT */
1943
Paul Bakker05ef8352012-05-08 09:17:57 +00001944#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001945 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001946 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001947 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001948
Paul Bakker05ef8352012-05-08 09:17:57 +00001949 ret = ssl_hw_record_write( ssl );
1950 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1951 {
1952 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001953 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001954 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001955
1956 if( ret == 0 )
1957 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001958 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001959#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001960 if( !done )
1961 {
1962 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1963 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1964 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1966 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001967
Paul Bakker48916f92012-09-16 19:57:18 +00001968 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001969 {
1970 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1971 {
1972 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1973 return( ret );
1974 }
1975
1976 len = ssl->out_msglen;
1977 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1978 ssl->out_hdr[4] = (unsigned char)( len );
1979 }
1980
1981 ssl->out_left = 5 + ssl->out_msglen;
1982
1983 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1984 "version = [%d:%d], msglen = %d",
1985 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1986 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1987
1988 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001989 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 }
1991
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1993 {
1994 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1995 return( ret );
1996 }
1997
1998 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1999
2000 return( 0 );
2001}
2002
2003int ssl_read_record( ssl_context *ssl )
2004{
Paul Bakker05ef8352012-05-08 09:17:57 +00002005 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002006
2007 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2008
2009 if( ssl->in_hslen != 0 &&
2010 ssl->in_hslen < ssl->in_msglen )
2011 {
2012 /*
2013 * Get next Handshake message in the current record
2014 */
2015 ssl->in_msglen -= ssl->in_hslen;
2016
Paul Bakker8934a982011-08-05 11:11:53 +00002017 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002018 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
2020 ssl->in_hslen = 4;
2021 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2022
2023 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2024 " %d, type = %d, hslen = %d",
2025 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2026
2027 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2028 {
2029 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002030 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 }
2032
2033 if( ssl->in_msglen < ssl->in_hslen )
2034 {
2035 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002036 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 }
2038
Paul Bakker4224bc02014-04-08 14:36:50 +02002039 if( ssl->state != SSL_HANDSHAKE_OVER )
2040 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002041
2042 return( 0 );
2043 }
2044
2045 ssl->in_hslen = 0;
2046
2047 /*
2048 * Read the record header and validate it
2049 */
2050 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2051 {
2052 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2053 return( ret );
2054 }
2055
2056 ssl->in_msgtype = ssl->in_hdr[0];
2057 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2058
2059 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2060 "version = [%d:%d], msglen = %d",
2061 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2062 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2063
2064 if( ssl->in_hdr[1] != ssl->major_ver )
2065 {
2066 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002067 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
2069
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002070 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 {
2072 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002073 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 }
2075
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002076 /* Sanity check (outer boundaries) */
2077 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2078 {
2079 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2080 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2081 }
2082
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002084 * Make sure the message length is acceptable for the current transform
2085 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 */
Paul Bakker48916f92012-09-16 19:57:18 +00002087 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002089 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 {
2091 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002092 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 }
2094 }
2095 else
2096 {
Paul Bakker48916f92012-09-16 19:57:18 +00002097 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 {
2099 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002100 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 }
2102
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002103#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002105 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 {
2107 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002108 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002111
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002112#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2113 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 /*
2115 * TLS encrypted messages can have up to 256 bytes of padding
2116 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002117 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002118 ssl->in_msglen > ssl->transform_in->minlen +
2119 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 {
2121 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002122 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002124#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 }
2126
2127 /*
2128 * Read and optionally decrypt the message contents
2129 */
2130 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2131 {
2132 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2133 return( ret );
2134 }
2135
2136 SSL_DEBUG_BUF( 4, "input record from network",
2137 ssl->in_hdr, 5 + ssl->in_msglen );
2138
Paul Bakker05ef8352012-05-08 09:17:57 +00002139#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002140 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002141 {
2142 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2143
2144 ret = ssl_hw_record_read( ssl );
2145 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2146 {
2147 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002148 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002149 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002150
2151 if( ret == 0 )
2152 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002153 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002154#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002155 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 {
2157 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2158 {
Paul Bakker40865c82013-01-31 17:13:13 +01002159#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2160 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2161 {
2162 ssl_send_alert_message( ssl,
2163 SSL_ALERT_LEVEL_FATAL,
2164 SSL_ALERT_MSG_BAD_RECORD_MAC );
2165 }
2166#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2168 return( ret );
2169 }
2170
2171 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2172 ssl->in_msg, ssl->in_msglen );
2173
2174 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2175 {
2176 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002177 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 }
2179 }
2180
Paul Bakker2770fbd2012-07-03 13:30:23 +00002181#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002182 if( ssl->transform_in != NULL &&
2183 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002184 {
2185 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2186 {
2187 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2188 return( ret );
2189 }
2190
2191 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2192 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2193 }
2194#endif /* POLARSSL_ZLIB_SUPPORT */
2195
Paul Bakker0a925182012-04-16 06:46:41 +00002196 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2197 ssl->in_msgtype != SSL_MSG_ALERT &&
2198 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2199 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2200 {
2201 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2202
Paul Bakker48916f92012-09-16 19:57:18 +00002203 if( ( ret = ssl_send_alert_message( ssl,
2204 SSL_ALERT_LEVEL_FATAL,
2205 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002206 {
2207 return( ret );
2208 }
2209
2210 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2211 }
2212
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2214 {
2215 ssl->in_hslen = 4;
2216 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2217
2218 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2219 " %d, type = %d, hslen = %d",
2220 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2221
2222 /*
2223 * Additional checks to validate the handshake header
2224 */
2225 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2226 {
2227 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002228 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 }
2230
2231 if( ssl->in_msglen < ssl->in_hslen )
2232 {
2233 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002234 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
2236
Paul Bakker48916f92012-09-16 19:57:18 +00002237 if( ssl->state != SSL_HANDSHAKE_OVER )
2238 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
2240
2241 if( ssl->in_msgtype == SSL_MSG_ALERT )
2242 {
2243 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2244 ssl->in_msg[0], ssl->in_msg[1] ) );
2245
2246 /*
2247 * Ignore non-fatal alerts, except close_notify
2248 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002249 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002251 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2252 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002253 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 }
2255
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002256 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2257 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 {
2259 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002260 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262 }
2263
2264 ssl->in_left = 0;
2265
2266 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2267
2268 return( 0 );
2269}
2270
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002271int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2272{
2273 int ret;
2274
2275 if( ( ret = ssl_send_alert_message( ssl,
2276 SSL_ALERT_LEVEL_FATAL,
2277 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2278 {
2279 return( ret );
2280 }
2281
2282 return( 0 );
2283}
2284
Paul Bakker0a925182012-04-16 06:46:41 +00002285int ssl_send_alert_message( ssl_context *ssl,
2286 unsigned char level,
2287 unsigned char message )
2288{
2289 int ret;
2290
2291 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2292
2293 ssl->out_msgtype = SSL_MSG_ALERT;
2294 ssl->out_msglen = 2;
2295 ssl->out_msg[0] = level;
2296 ssl->out_msg[1] = message;
2297
2298 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2299 {
2300 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2301 return( ret );
2302 }
2303
2304 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2305
2306 return( 0 );
2307}
2308
Paul Bakker5121ce52009-01-03 21:22:43 +00002309/*
2310 * Handshake functions
2311 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002312#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2313 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2314 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2315 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2316 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2317 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2318 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002319int ssl_write_certificate( ssl_context *ssl )
2320{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002321 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002322
2323 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2324
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002325 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002326 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2327 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002328 {
2329 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2330 ssl->state++;
2331 return( 0 );
2332 }
2333
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002334 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2335 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002336}
2337
2338int ssl_parse_certificate( ssl_context *ssl )
2339{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002340 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2341
2342 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2343
2344 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002345 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2346 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002347 {
2348 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2349 ssl->state++;
2350 return( 0 );
2351 }
2352
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002353 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2354 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002355}
2356#else
2357int ssl_write_certificate( ssl_context *ssl )
2358{
2359 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2360 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002361 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002362 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2363
2364 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2365
2366 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002367 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2368 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002369 {
2370 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2371 ssl->state++;
2372 return( 0 );
2373 }
2374
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002375#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 if( ssl->endpoint == SSL_IS_CLIENT )
2377 {
2378 if( ssl->client_auth == 0 )
2379 {
2380 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2381 ssl->state++;
2382 return( 0 );
2383 }
2384
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002385#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 /*
2387 * If using SSLv3 and got no cert, send an Alert message
2388 * (otherwise an empty Certificate message will be sent).
2389 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002390 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2392 {
2393 ssl->out_msglen = 2;
2394 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002395 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2396 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
2398 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2399 goto write_msg;
2400 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002401#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002402 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002403#endif /* POLARSSL_SSL_CLI_C */
2404#if defined(POLARSSL_SSL_SRV_C)
2405 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002407 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 {
2409 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002410 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 }
2412 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002413#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002415 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002416
2417 /*
2418 * 0 . 0 handshake type
2419 * 1 . 3 handshake length
2420 * 4 . 6 length of all certs
2421 * 7 . 9 length of cert. 1
2422 * 10 . n-1 peer certificate
2423 * n . n+2 length of cert. 2
2424 * n+3 . ... upper level cert, etc.
2425 */
2426 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002427 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
Paul Bakker29087132010-03-21 21:03:34 +00002429 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 {
2431 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002432 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 {
2434 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2435 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002436 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 }
2438
2439 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2440 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2441 ssl->out_msg[i + 2] = (unsigned char)( n );
2442
2443 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2444 i += n; crt = crt->next;
2445 }
2446
2447 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2448 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2449 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2450
2451 ssl->out_msglen = i;
2452 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2453 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2454
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002455#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002456write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002457#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
2459 ssl->state++;
2460
2461 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2462 {
2463 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2464 return( ret );
2465 }
2466
2467 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2468
Paul Bakkered27a042013-04-18 22:46:23 +02002469 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470}
2471
2472int ssl_parse_certificate( ssl_context *ssl )
2473{
Paul Bakkered27a042013-04-18 22:46:23 +02002474 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002475 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002476 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002477
2478 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2479
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002480 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002481 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2482 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002483 {
2484 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2485 ssl->state++;
2486 return( 0 );
2487 }
2488
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002489#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002491 ( ssl->authmode == SSL_VERIFY_NONE ||
2492 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002494 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2496 ssl->state++;
2497 return( 0 );
2498 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002499#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
2501 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2502 {
2503 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2504 return( ret );
2505 }
2506
2507 ssl->state++;
2508
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002509#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002510#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 /*
2512 * Check if the client sent an empty certificate
2513 */
2514 if( ssl->endpoint == SSL_IS_SERVER &&
2515 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2516 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002517 if( ssl->in_msglen == 2 &&
2518 ssl->in_msgtype == SSL_MSG_ALERT &&
2519 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2520 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 {
2522 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2523
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002524 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2526 return( 0 );
2527 else
Paul Bakker40e46942009-01-03 21:51:57 +00002528 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 }
2530 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002531#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2534 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 if( ssl->endpoint == SSL_IS_SERVER &&
2536 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2537 {
2538 if( ssl->in_hslen == 7 &&
2539 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2540 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2541 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2542 {
2543 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2544
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002545 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002547 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 else
2549 return( 0 );
2550 }
2551 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002552#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2553 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002554#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
2556 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2557 {
2558 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002559 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 }
2561
2562 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2563 {
2564 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002565 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
2567
2568 /*
2569 * Same message structure as in ssl_write_certificate()
2570 */
2571 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2572
2573 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2574 {
2575 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002576 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 }
2578
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002579 /* In case we tried to reuse a session but it failed */
2580 if( ssl->session_negotiate->peer_cert != NULL )
2581 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002582 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002583 polarssl_free( ssl->session_negotiate->peer_cert );
2584 }
2585
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002586 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2587 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 {
2589 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002590 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002591 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 }
2593
Paul Bakkerb6b09562013-09-18 14:17:41 +02002594 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
2596 i = 7;
2597
2598 while( i < ssl->in_hslen )
2599 {
2600 if( ssl->in_msg[i] != 0 )
2601 {
2602 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002603 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 }
2605
2606 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2607 | (unsigned int) ssl->in_msg[i + 2];
2608 i += 3;
2609
2610 if( n < 128 || i + n > ssl->in_hslen )
2611 {
2612 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002613 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 }
2615
Paul Bakkerddf26b42013-09-18 13:46:23 +02002616 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2617 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 if( ret != 0 )
2619 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002620 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 return( ret );
2622 }
2623
2624 i += n;
2625 }
2626
Paul Bakker48916f92012-09-16 19:57:18 +00002627 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002629 /*
2630 * On client, make sure the server cert doesn't change during renego to
2631 * avoid "triple handshake" attack: https://secure-resumption.com/
2632 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002633#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002634 if( ssl->endpoint == SSL_IS_CLIENT &&
2635 ssl->renegotiation == SSL_RENEGOTIATION )
2636 {
2637 if( ssl->session->peer_cert == NULL )
2638 {
2639 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2640 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2641 }
2642
2643 if( ssl->session->peer_cert->raw.len !=
2644 ssl->session_negotiate->peer_cert->raw.len ||
2645 memcmp( ssl->session->peer_cert->raw.p,
2646 ssl->session_negotiate->peer_cert->raw.p,
2647 ssl->session->peer_cert->raw.len ) != 0 )
2648 {
2649 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2650 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2651 }
2652 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002653#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002654
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 if( ssl->authmode != SSL_VERIFY_NONE )
2656 {
2657 if( ssl->ca_chain == NULL )
2658 {
2659 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002660 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 }
2662
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002663 /*
2664 * Main check: verify certificate
2665 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002666 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2667 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2668 &ssl->session_negotiate->verify_result,
2669 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
2671 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002672 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002674 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002675
2676 /*
2677 * Secondary checks: always done, but change 'ret' only if it was 0
2678 */
2679
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002680#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002681 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002682 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002683
2684 /* If certificate uses an EC key, make sure the curve is OK */
2685 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2686 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2687 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002688 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002689 if( ret == 0 )
2690 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002691 }
2692 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002693#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002695 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2696 ciphersuite_info,
2697 ! ssl->endpoint ) != 0 )
2698 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002699 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002700 if( ret == 0 )
2701 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2702 }
2703
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2705 ret = 0;
2706 }
2707
2708 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2709
2710 return( ret );
2711}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002712#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2713 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2714 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2715 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2716 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2717 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2718 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
2720int ssl_write_change_cipher_spec( ssl_context *ssl )
2721{
2722 int ret;
2723
2724 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2725
2726 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2727 ssl->out_msglen = 1;
2728 ssl->out_msg[0] = 1;
2729
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 ssl->state++;
2731
2732 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2733 {
2734 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2735 return( ret );
2736 }
2737
2738 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2739
2740 return( 0 );
2741}
2742
2743int ssl_parse_change_cipher_spec( ssl_context *ssl )
2744{
2745 int ret;
2746
2747 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2748
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2750 {
2751 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2752 return( ret );
2753 }
2754
2755 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2756 {
2757 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002758 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 }
2760
2761 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2762 {
2763 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002764 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 }
2766
2767 ssl->state++;
2768
2769 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2770
2771 return( 0 );
2772}
2773
Paul Bakker41c83d32013-03-20 14:39:14 +01002774void ssl_optimize_checksum( ssl_context *ssl,
2775 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002776{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002777 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002778
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002779#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2780 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002781 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002782 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002783 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002784#endif
2785#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2786#if defined(POLARSSL_SHA512_C)
2787 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2788 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2789 else
2790#endif
2791#if defined(POLARSSL_SHA256_C)
2792 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002793 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794 else
2795#endif
2796#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002797 {
2798 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002799 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002800 }
Paul Bakker380da532012-04-18 16:10:25 +00002801}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002802
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002803static void ssl_update_checksum_start( ssl_context *ssl,
2804 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002805{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002806#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2807 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002808 md5_update( &ssl->handshake->fin_md5 , buf, len );
2809 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002810#endif
2811#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2812#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002813 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002814#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002815#if defined(POLARSSL_SHA512_C)
2816 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002817#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002818#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002819}
2820
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002821#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2822 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002823static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2824 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002825{
Paul Bakker48916f92012-09-16 19:57:18 +00002826 md5_update( &ssl->handshake->fin_md5 , buf, len );
2827 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002828}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002829#endif
Paul Bakker380da532012-04-18 16:10:25 +00002830
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002831#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2832#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002833static void ssl_update_checksum_sha256( ssl_context *ssl,
2834 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002835{
Paul Bakker9e36f042013-06-30 14:34:05 +02002836 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002837}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002838#endif
Paul Bakker380da532012-04-18 16:10:25 +00002839
Paul Bakker9e36f042013-06-30 14:34:05 +02002840#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002841static void ssl_update_checksum_sha384( ssl_context *ssl,
2842 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002843{
Paul Bakker9e36f042013-06-30 14:34:05 +02002844 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002845}
Paul Bakker769075d2012-11-24 11:26:46 +01002846#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002847#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002848
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002849#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002850static void ssl_calc_finished_ssl(
2851 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002852{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002853 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002854 md5_context md5;
2855 sha1_context sha1;
2856
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 unsigned char padbuf[48];
2858 unsigned char md5sum[16];
2859 unsigned char sha1sum[20];
2860
Paul Bakker48916f92012-09-16 19:57:18 +00002861 ssl_session *session = ssl->session_negotiate;
2862 if( !session )
2863 session = ssl->session;
2864
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2866
Paul Bakker48916f92012-09-16 19:57:18 +00002867 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2868 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002869
2870 /*
2871 * SSLv3:
2872 * hash =
2873 * MD5( master + pad2 +
2874 * MD5( handshake + sender + master + pad1 ) )
2875 * + SHA1( master + pad2 +
2876 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 */
2878
Paul Bakker90995b52013-06-24 19:20:35 +02002879#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002881 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002882#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Paul Bakker90995b52013-06-24 19:20:35 +02002884#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002886 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002887#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002888
Paul Bakker3c2122f2013-06-24 19:03:14 +02002889 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2890 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakker1ef83d62012-04-11 12:09:53 +00002892 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
Paul Bakker3c2122f2013-06-24 19:03:14 +02002894 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002895 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896 md5_update( &md5, padbuf, 48 );
2897 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
Paul Bakker3c2122f2013-06-24 19:03:14 +02002899 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002900 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002901 sha1_update( &sha1, padbuf, 40 );
2902 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002903
Paul Bakker1ef83d62012-04-11 12:09:53 +00002904 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Paul Bakker1ef83d62012-04-11 12:09:53 +00002906 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002907 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002908 md5_update( &md5, padbuf, 48 );
2909 md5_update( &md5, md5sum, 16 );
2910 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Paul Bakker1ef83d62012-04-11 12:09:53 +00002912 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002913 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002914 sha1_update( &sha1, padbuf , 40 );
2915 sha1_update( &sha1, sha1sum, 20 );
2916 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002917
Paul Bakker1ef83d62012-04-11 12:09:53 +00002918 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002919
Paul Bakker5b4af392014-06-26 12:09:34 +02002920 md5_free( &md5 );
2921 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Paul Bakker34617722014-06-13 17:20:13 +02002923 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2924 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2925 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
2927 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2928}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002929#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002931#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002932static void ssl_calc_finished_tls(
2933 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002934{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002935 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002936 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002937 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002938 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002939 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Paul Bakker48916f92012-09-16 19:57:18 +00002941 ssl_session *session = ssl->session_negotiate;
2942 if( !session )
2943 session = ssl->session;
2944
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
Paul Bakker48916f92012-09-16 19:57:18 +00002947 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2948 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Paul Bakker1ef83d62012-04-11 12:09:53 +00002950 /*
2951 * TLSv1:
2952 * hash = PRF( master, finished_label,
2953 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2954 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Paul Bakker90995b52013-06-24 19:20:35 +02002956#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002957 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2958 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002959#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960
Paul Bakker90995b52013-06-24 19:20:35 +02002961#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2963 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002964#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002965
2966 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002967 ? "client finished"
2968 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969
2970 md5_finish( &md5, padbuf );
2971 sha1_finish( &sha1, padbuf + 16 );
2972
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002973 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002974 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002975
2976 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2977
Paul Bakker5b4af392014-06-26 12:09:34 +02002978 md5_free( &md5 );
2979 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002980
Paul Bakker34617722014-06-13 17:20:13 +02002981 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982
2983 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2984}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002985#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002987#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2988#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002989static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002990 ssl_context *ssl, unsigned char *buf, int from )
2991{
2992 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002993 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002994 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995 unsigned char padbuf[32];
2996
Paul Bakker48916f92012-09-16 19:57:18 +00002997 ssl_session *session = ssl->session_negotiate;
2998 if( !session )
2999 session = ssl->session;
3000
Paul Bakker380da532012-04-18 16:10:25 +00003001 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003002
Paul Bakker9e36f042013-06-30 14:34:05 +02003003 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003004
3005 /*
3006 * TLSv1.2:
3007 * hash = PRF( master, finished_label,
3008 * Hash( handshake ) )[0.11]
3009 */
3010
Paul Bakker9e36f042013-06-30 14:34:05 +02003011#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003012 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003013 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003014#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003015
3016 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003017 ? "client finished"
3018 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003019
Paul Bakker9e36f042013-06-30 14:34:05 +02003020 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003022 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003023 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003024
3025 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3026
Paul Bakker5b4af392014-06-26 12:09:34 +02003027 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003028
Paul Bakker34617722014-06-13 17:20:13 +02003029 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003030
3031 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3032}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003033#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003034
Paul Bakker9e36f042013-06-30 14:34:05 +02003035#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003036static void ssl_calc_finished_tls_sha384(
3037 ssl_context *ssl, unsigned char *buf, int from )
3038{
3039 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003040 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003041 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003042 unsigned char padbuf[48];
3043
Paul Bakker48916f92012-09-16 19:57:18 +00003044 ssl_session *session = ssl->session_negotiate;
3045 if( !session )
3046 session = ssl->session;
3047
Paul Bakker380da532012-04-18 16:10:25 +00003048 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003049
Paul Bakker9e36f042013-06-30 14:34:05 +02003050 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003051
3052 /*
3053 * TLSv1.2:
3054 * hash = PRF( master, finished_label,
3055 * Hash( handshake ) )[0.11]
3056 */
3057
Paul Bakker9e36f042013-06-30 14:34:05 +02003058#if !defined(POLARSSL_SHA512_ALT)
3059 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3060 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003061#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003062
3063 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003064 ? "client finished"
3065 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003066
Paul Bakker9e36f042013-06-30 14:34:05 +02003067 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003068
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003069 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003070 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003071
3072 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3073
Paul Bakker5b4af392014-06-26 12:09:34 +02003074 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003075
Paul Bakker34617722014-06-13 17:20:13 +02003076 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003077
3078 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3079}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003080#endif /* POLARSSL_SHA512_C */
3081#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003082
Paul Bakker48916f92012-09-16 19:57:18 +00003083void ssl_handshake_wrapup( ssl_context *ssl )
3084{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003085 int resume = ssl->handshake->resume;
3086
Paul Bakker48916f92012-09-16 19:57:18 +00003087 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3088
3089 /*
3090 * Free our handshake params
3091 */
3092 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003093 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003094 ssl->handshake = NULL;
3095
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003096 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003097 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003098 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003099 ssl->renego_records_seen = 0;
3100 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003101
Paul Bakker48916f92012-09-16 19:57:18 +00003102 /*
3103 * Switch in our now active transform context
3104 */
3105 if( ssl->transform )
3106 {
3107 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003108 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003109 }
3110 ssl->transform = ssl->transform_negotiate;
3111 ssl->transform_negotiate = NULL;
3112
Paul Bakker0a597072012-09-25 21:55:46 +00003113 if( ssl->session )
3114 {
3115 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003116 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003117 }
3118 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003119 ssl->session_negotiate = NULL;
3120
Paul Bakker0a597072012-09-25 21:55:46 +00003121 /*
3122 * Add cache entry
3123 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003124 if( ssl->f_set_cache != NULL &&
3125 ssl->session->length != 0 &&
3126 resume == 0 )
3127 {
Paul Bakker0a597072012-09-25 21:55:46 +00003128 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3129 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003130 }
Paul Bakker0a597072012-09-25 21:55:46 +00003131
Paul Bakker48916f92012-09-16 19:57:18 +00003132 ssl->state++;
3133
3134 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3135}
3136
Paul Bakker1ef83d62012-04-11 12:09:53 +00003137int ssl_write_finished( ssl_context *ssl )
3138{
3139 int ret, hash_len;
3140
3141 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3142
Paul Bakker92be97b2013-01-02 17:30:03 +01003143 /*
3144 * Set the out_msg pointer to the correct location based on IV length
3145 */
3146 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3147 {
3148 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3149 ssl->transform_negotiate->fixed_ivlen;
3150 }
3151 else
3152 ssl->out_msg = ssl->out_iv;
3153
Paul Bakker48916f92012-09-16 19:57:18 +00003154 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155
3156 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003157 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3158
Paul Bakker48916f92012-09-16 19:57:18 +00003159 ssl->verify_data_len = hash_len;
3160 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3161
Paul Bakker5121ce52009-01-03 21:22:43 +00003162 ssl->out_msglen = 4 + hash_len;
3163 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3164 ssl->out_msg[0] = SSL_HS_FINISHED;
3165
3166 /*
3167 * In case of session resuming, invert the client and server
3168 * ChangeCipherSpec messages order.
3169 */
Paul Bakker0a597072012-09-25 21:55:46 +00003170 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003171 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003172#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003173 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003174 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003175#endif
3176#if defined(POLARSSL_SSL_SRV_C)
3177 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003179#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003180 }
3181 else
3182 ssl->state++;
3183
Paul Bakker48916f92012-09-16 19:57:18 +00003184 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003185 * Switch to our negotiated transform and session parameters for outbound
3186 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003187 */
3188 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3189 ssl->transform_out = ssl->transform_negotiate;
3190 ssl->session_out = ssl->session_negotiate;
3191 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003192
Paul Bakker07eb38b2012-12-19 14:42:06 +01003193#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003194 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003195 {
3196 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3197 {
3198 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3199 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3200 }
3201 }
3202#endif
3203
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3205 {
3206 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3207 return( ret );
3208 }
3209
3210 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3211
3212 return( 0 );
3213}
3214
3215int ssl_parse_finished( ssl_context *ssl )
3216{
Paul Bakker23986e52011-04-24 08:57:21 +00003217 int ret;
3218 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 unsigned char buf[36];
3220
3221 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3222
Paul Bakker48916f92012-09-16 19:57:18 +00003223 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
Paul Bakker48916f92012-09-16 19:57:18 +00003225 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003226 * Switch to our negotiated transform and session parameters for inbound
3227 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003228 */
3229 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3230 ssl->transform_in = ssl->transform_negotiate;
3231 ssl->session_in = ssl->session_negotiate;
3232 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Paul Bakker92be97b2013-01-02 17:30:03 +01003234 /*
3235 * Set the in_msg pointer to the correct location based on IV length
3236 */
3237 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3238 {
3239 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3240 ssl->transform_negotiate->fixed_ivlen;
3241 }
3242 else
3243 ssl->in_msg = ssl->in_iv;
3244
Paul Bakker07eb38b2012-12-19 14:42:06 +01003245#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003246 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003247 {
3248 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3249 {
3250 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3251 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3252 }
3253 }
3254#endif
3255
Paul Bakker5121ce52009-01-03 21:22:43 +00003256 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3257 {
3258 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3259 return( ret );
3260 }
3261
3262 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3263 {
3264 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003265 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 }
3267
Paul Bakker1ef83d62012-04-11 12:09:53 +00003268 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003269 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3270
3271 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3272 ssl->in_hslen != 4 + hash_len )
3273 {
3274 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003275 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 }
3277
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003278 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 {
3280 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003281 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 }
3283
Paul Bakker48916f92012-09-16 19:57:18 +00003284 ssl->verify_data_len = hash_len;
3285 memcpy( ssl->peer_verify_data, buf, hash_len );
3286
Paul Bakker0a597072012-09-25 21:55:46 +00003287 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003289#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 if( ssl->endpoint == SSL_IS_CLIENT )
3291 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003292#endif
3293#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003295 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 }
3298 else
3299 ssl->state++;
3300
3301 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3302
3303 return( 0 );
3304}
3305
Paul Bakker968afaa2014-07-09 11:09:24 +02003306static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003307{
3308 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3309
3310#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3311 defined(POLARSSL_SSL_PROTO_TLS1_1)
3312 md5_init( &handshake->fin_md5 );
3313 sha1_init( &handshake->fin_sha1 );
3314 md5_starts( &handshake->fin_md5 );
3315 sha1_starts( &handshake->fin_sha1 );
3316#endif
3317#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3318#if defined(POLARSSL_SHA256_C)
3319 sha256_init( &handshake->fin_sha256 );
3320 sha256_starts( &handshake->fin_sha256, 0 );
3321#endif
3322#if defined(POLARSSL_SHA512_C)
3323 sha512_init( &handshake->fin_sha512 );
3324 sha512_starts( &handshake->fin_sha512, 1 );
3325#endif
3326#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3327
3328 handshake->update_checksum = ssl_update_checksum_start;
3329 handshake->sig_alg = SSL_HASH_SHA1;
3330
3331#if defined(POLARSSL_DHM_C)
3332 dhm_init( &handshake->dhm_ctx );
3333#endif
3334#if defined(POLARSSL_ECDH_C)
3335 ecdh_init( &handshake->ecdh_ctx );
3336#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003337}
3338
3339static void ssl_transform_init( ssl_transform *transform )
3340{
3341 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003342
3343 cipher_init( &transform->cipher_ctx_enc );
3344 cipher_init( &transform->cipher_ctx_dec );
3345
3346 md_init( &transform->md_ctx_enc );
3347 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003348}
3349
3350void ssl_session_init( ssl_session *session )
3351{
3352 memset( session, 0, sizeof(ssl_session) );
3353}
3354
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003355static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003356{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003357 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003358 if( ssl->transform_negotiate )
3359 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003360 if( ssl->session_negotiate )
3361 ssl_session_free( ssl->session_negotiate );
3362 if( ssl->handshake )
3363 ssl_handshake_free( ssl->handshake );
3364
3365 /*
3366 * Either the pointers are now NULL or cleared properly and can be freed.
3367 * Now allocate missing structures.
3368 */
3369 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003370 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003371 ssl->transform_negotiate = (ssl_transform *) polarssl_malloc(
3372 sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003373 }
Paul Bakker48916f92012-09-16 19:57:18 +00003374
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003375 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003376 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003377 ssl->session_negotiate = (ssl_session *) polarssl_malloc(
3378 sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003379 }
Paul Bakker48916f92012-09-16 19:57:18 +00003380
Paul Bakker82788fb2014-10-20 13:59:19 +02003381 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003382 {
3383 ssl->handshake = (ssl_handshake_params *)
3384 polarssl_malloc( sizeof(ssl_handshake_params) );
3385 }
Paul Bakker48916f92012-09-16 19:57:18 +00003386
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003387 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003388 if( ssl->handshake == NULL ||
3389 ssl->transform_negotiate == NULL ||
3390 ssl->session_negotiate == NULL )
3391 {
3392 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003393
3394 polarssl_free( ssl->handshake );
3395 polarssl_free( ssl->transform_negotiate );
3396 polarssl_free( ssl->session_negotiate );
3397
3398 ssl->handshake = NULL;
3399 ssl->transform_negotiate = NULL;
3400 ssl->session_negotiate = NULL;
3401
Paul Bakker48916f92012-09-16 19:57:18 +00003402 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3403 }
3404
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003405 /* Initialize structures */
3406 ssl_session_init( ssl->session_negotiate );
3407 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003408 ssl_handshake_params_init( ssl->handshake );
3409
3410#if defined(POLARSSL_X509_CRT_PARSE_C)
3411 ssl->handshake->key_cert = ssl->key_cert;
3412#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003413
Paul Bakker48916f92012-09-16 19:57:18 +00003414 return( 0 );
3415}
3416
Paul Bakker5121ce52009-01-03 21:22:43 +00003417/*
3418 * Initialize an SSL context
3419 */
3420int ssl_init( ssl_context *ssl )
3421{
Paul Bakker48916f92012-09-16 19:57:18 +00003422 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003423 int len = SSL_BUFFER_LEN;
3424
3425 memset( ssl, 0, sizeof( ssl_context ) );
3426
Paul Bakker62f2dee2012-09-28 07:31:51 +00003427 /*
3428 * Sane defaults
3429 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003430 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3431 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3432 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3433 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003434
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003435 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003436
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003437 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3438
Paul Bakker62f2dee2012-09-28 07:31:51 +00003439#if defined(POLARSSL_DHM_C)
3440 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3441 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3442 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3443 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3444 {
3445 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3446 return( ret );
3447 }
3448#endif
3449
3450 /*
3451 * Prepare base structures
3452 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003453 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003455 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003456 ssl->in_msg = ssl->in_ctr + 13;
3457
3458 if( ssl->in_ctr == NULL )
3459 {
3460 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003461 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 }
3463
Paul Bakker6e339b52013-07-03 13:37:05 +02003464 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003466 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003467 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003468
3469 if( ssl->out_ctr == NULL )
3470 {
3471 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003472 polarssl_free( ssl->in_ctr );
3473 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003474 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 }
3476
3477 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3478 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3479
Paul Bakker606b4ba2013-08-14 16:52:14 +02003480#if defined(POLARSSL_SSL_SESSION_TICKETS)
3481 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3482#endif
3483
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003484#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003485 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003486#endif
3487
Paul Bakker48916f92012-09-16 19:57:18 +00003488 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3489 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003490
3491 return( 0 );
3492}
3493
3494/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003495 * Reset an initialized and used SSL context for re-use while retaining
3496 * all application-set variables, function pointers and data.
3497 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003498int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003499{
Paul Bakker48916f92012-09-16 19:57:18 +00003500 int ret;
3501
Paul Bakker7eb013f2011-10-06 12:37:39 +00003502 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003503 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3504 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3505
3506 ssl->verify_data_len = 0;
3507 memset( ssl->own_verify_data, 0, 36 );
3508 memset( ssl->peer_verify_data, 0, 36 );
3509
Paul Bakker7eb013f2011-10-06 12:37:39 +00003510 ssl->in_offt = NULL;
3511
Paul Bakker92be97b2013-01-02 17:30:03 +01003512 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003513 ssl->in_msgtype = 0;
3514 ssl->in_msglen = 0;
3515 ssl->in_left = 0;
3516
3517 ssl->in_hslen = 0;
3518 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003519 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003520
Paul Bakker92be97b2013-01-02 17:30:03 +01003521 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003522 ssl->out_msgtype = 0;
3523 ssl->out_msglen = 0;
3524 ssl->out_left = 0;
3525
Paul Bakker48916f92012-09-16 19:57:18 +00003526 ssl->transform_in = NULL;
3527 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003528
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003529 ssl->renego_records_seen = 0;
3530
Paul Bakker7eb013f2011-10-06 12:37:39 +00003531 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3532 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003533
3534#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003535 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003536 {
3537 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003538 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003539 {
3540 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3541 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3542 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003543 }
3544#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003545
Paul Bakker48916f92012-09-16 19:57:18 +00003546 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003547 {
Paul Bakker48916f92012-09-16 19:57:18 +00003548 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003549 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003550 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003551 }
Paul Bakker48916f92012-09-16 19:57:18 +00003552
Paul Bakkerc0463502013-02-14 11:19:38 +01003553 if( ssl->session )
3554 {
3555 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003556 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003557 ssl->session = NULL;
3558 }
3559
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003560#if defined(POLARSSL_SSL_ALPN)
3561 ssl->alpn_chosen = NULL;
3562#endif
3563
Paul Bakker48916f92012-09-16 19:57:18 +00003564 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3565 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003566
3567 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003568}
3569
Paul Bakkera503a632013-08-14 13:48:06 +02003570#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003571static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3572{
3573 aes_free( &tkeys->enc );
3574 aes_free( &tkeys->dec );
3575
3576 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3577}
3578
Paul Bakker7eb013f2011-10-06 12:37:39 +00003579/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003580 * Allocate and initialize ticket keys
3581 */
3582static int ssl_ticket_keys_init( ssl_context *ssl )
3583{
3584 int ret;
3585 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003586 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003587
3588 if( ssl->ticket_keys != NULL )
3589 return( 0 );
3590
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003591 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3592 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003593 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3594
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003595 aes_init( &tkeys->enc );
3596 aes_init( &tkeys->dec );
3597
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003598 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003599 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003600 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003601 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003602 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003603 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003604
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003605 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3606 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3607 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3608 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003609 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003610 polarssl_free( tkeys );
3611 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003612 }
3613
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003614 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003615 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003616 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003617 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003618 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003619 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003620
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003621 ssl->ticket_keys = tkeys;
3622
3623 return( 0 );
3624}
Paul Bakkera503a632013-08-14 13:48:06 +02003625#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003626
3627/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 * SSL set accessors
3629 */
3630void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3631{
3632 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003633
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003634#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3635 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003636 if( endpoint == SSL_IS_CLIENT )
3637 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003638#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003639}
3640
3641void ssl_set_authmode( ssl_context *ssl, int authmode )
3642{
3643 ssl->authmode = authmode;
3644}
3645
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003646#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003647void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003648 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003649 void *p_vrfy )
3650{
3651 ssl->f_vrfy = f_vrfy;
3652 ssl->p_vrfy = p_vrfy;
3653}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003654#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003655
Paul Bakker5121ce52009-01-03 21:22:43 +00003656void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003657 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 void *p_rng )
3659{
3660 ssl->f_rng = f_rng;
3661 ssl->p_rng = p_rng;
3662}
3663
3664void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003665 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 void *p_dbg )
3667{
3668 ssl->f_dbg = f_dbg;
3669 ssl->p_dbg = p_dbg;
3670}
3671
3672void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003673 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003674 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003675{
3676 ssl->f_recv = f_recv;
3677 ssl->f_send = f_send;
3678 ssl->p_recv = p_recv;
3679 ssl->p_send = p_send;
3680}
3681
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003682#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003683void ssl_set_session_cache( ssl_context *ssl,
3684 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3685 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003686{
Paul Bakker0a597072012-09-25 21:55:46 +00003687 ssl->f_get_cache = f_get_cache;
3688 ssl->p_get_cache = p_get_cache;
3689 ssl->f_set_cache = f_set_cache;
3690 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003691}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003692#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003693
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003694#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003695int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003696{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003697 int ret;
3698
3699 if( ssl == NULL ||
3700 session == NULL ||
3701 ssl->session_negotiate == NULL ||
3702 ssl->endpoint != SSL_IS_CLIENT )
3703 {
3704 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3705 }
3706
3707 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3708 return( ret );
3709
Paul Bakker0a597072012-09-25 21:55:46 +00003710 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003711
3712 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003713}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003714#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
Paul Bakkerb68cad62012-08-23 08:34:18 +00003716void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003717{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003718 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3719 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3720 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3721 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3722}
3723
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003724void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3725 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003726 int major, int minor )
3727{
3728 if( major != SSL_MAJOR_VERSION_3 )
3729 return;
3730
3731 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3732 return;
3733
3734 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003735}
3736
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003737#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003738/* Add a new (empty) key_cert entry an return a pointer to it */
3739static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3740{
3741 ssl_key_cert *key_cert, *last;
3742
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003743 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3744 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003745 return( NULL );
3746
3747 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3748
3749 /* Append the new key_cert to the (possibly empty) current list */
3750 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003751 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003752 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003753 if( ssl->handshake != NULL )
3754 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003755 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003756 else
3757 {
3758 last = ssl->key_cert;
3759 while( last->next != NULL )
3760 last = last->next;
3761 last->next = key_cert;
3762 }
3763
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003764 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003765}
3766
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003767void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003768 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003769{
3770 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003771 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003772 ssl->peer_cn = peer_cn;
3773}
3774
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003775int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003776 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003777{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003778 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3779
3780 if( key_cert == NULL )
3781 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3782
3783 key_cert->cert = own_cert;
3784 key_cert->key = pk_key;
3785
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003786 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003787}
3788
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003789#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003790int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003791 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003792{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003793 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003794 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003795
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003796 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003797 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3798
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003799 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3800 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003801 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003802
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003803 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003804
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003805 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003806 if( ret != 0 )
3807 return( ret );
3808
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003809 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003810 return( ret );
3811
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003812 key_cert->cert = own_cert;
3813 key_cert->key_own_alloc = 1;
3814
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003815 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003816}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003817#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003818
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003819int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003820 void *rsa_key,
3821 rsa_decrypt_func rsa_decrypt,
3822 rsa_sign_func rsa_sign,
3823 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003824{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003825 int ret;
3826 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003827
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003828 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003829 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3830
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003831 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3832 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003833 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003834
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003835 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003836
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003837 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3838 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3839 return( ret );
3840
3841 key_cert->cert = own_cert;
3842 key_cert->key_own_alloc = 1;
3843
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003844 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker43b7e352011-01-18 15:27:19 +00003845}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003846#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003847
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003848#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003849int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3850 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003851{
Paul Bakker6db455e2013-09-18 17:29:31 +02003852 if( psk == NULL || psk_identity == NULL )
3853 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3854
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003855 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003856 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3857
Paul Bakker6db455e2013-09-18 17:29:31 +02003858 if( ssl->psk != NULL )
3859 {
3860 polarssl_free( ssl->psk );
3861 polarssl_free( ssl->psk_identity );
3862 }
3863
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003864 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003865 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003866
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003867 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003868 ssl->psk_identity = (unsigned char *)
3869 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003870
3871 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3872 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3873
3874 memcpy( ssl->psk, psk, ssl->psk_len );
3875 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003876
3877 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003878}
3879
3880void ssl_set_psk_cb( ssl_context *ssl,
3881 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3882 size_t),
3883 void *p_psk )
3884{
3885 ssl->f_psk = f_psk;
3886 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003887}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003888#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003889
Paul Bakker48916f92012-09-16 19:57:18 +00003890#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003891int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003892{
3893 int ret;
3894
Paul Bakker48916f92012-09-16 19:57:18 +00003895 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003896 {
3897 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3898 return( ret );
3899 }
3900
Paul Bakker48916f92012-09-16 19:57:18 +00003901 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003902 {
3903 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3904 return( ret );
3905 }
3906
3907 return( 0 );
3908}
3909
Paul Bakker1b57b062011-01-06 15:48:19 +00003910int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3911{
3912 int ret;
3913
Paul Bakker66d5d072014-06-17 16:39:18 +02003914 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003915 {
3916 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3917 return( ret );
3918 }
3919
Paul Bakker66d5d072014-06-17 16:39:18 +02003920 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003921 {
3922 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3923 return( ret );
3924 }
3925
3926 return( 0 );
3927}
Paul Bakker48916f92012-09-16 19:57:18 +00003928#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003929
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003930#if defined(POLARSSL_SSL_SET_CURVES)
3931/*
3932 * Set the allowed elliptic curves
3933 */
3934void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3935{
3936 ssl->curve_list = curve_list;
3937}
3938#endif
3939
Paul Bakker0be444a2013-08-27 21:55:01 +02003940#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003941int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003942{
3943 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003944 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003945
3946 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003947
3948 if( ssl->hostname_len + 1 == 0 )
3949 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3950
Paul Bakker6e339b52013-07-03 13:37:05 +02003951 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003952
Paul Bakkerb15b8512012-01-13 13:44:06 +00003953 if( ssl->hostname == NULL )
3954 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3955
Paul Bakker3c2122f2013-06-24 19:03:14 +02003956 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003957 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003958
Paul Bakker40ea7de2009-05-03 10:18:48 +00003959 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003960
3961 return( 0 );
3962}
3963
Paul Bakker5701cdc2012-09-27 21:49:42 +00003964void ssl_set_sni( ssl_context *ssl,
3965 int (*f_sni)(void *, ssl_context *,
3966 const unsigned char *, size_t),
3967 void *p_sni )
3968{
3969 ssl->f_sni = f_sni;
3970 ssl->p_sni = p_sni;
3971}
Paul Bakker0be444a2013-08-27 21:55:01 +02003972#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003973
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003974#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003975int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003976{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003977 size_t cur_len, tot_len;
3978 const char **p;
3979
3980 /*
3981 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3982 * truncated". Check lengths now rather than later.
3983 */
3984 tot_len = 0;
3985 for( p = protos; *p != NULL; p++ )
3986 {
3987 cur_len = strlen( *p );
3988 tot_len += cur_len;
3989
3990 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3991 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3992 }
3993
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003994 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003995
3996 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003997}
3998
3999const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4000{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004001 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004002}
4003#endif /* POLARSSL_SSL_ALPN */
4004
Paul Bakker490ecc82011-10-06 13:04:09 +00004005void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4006{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004007 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4008 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4009 {
4010 ssl->max_major_ver = major;
4011 ssl->max_minor_ver = minor;
4012 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004013}
4014
Paul Bakker1d29fb52012-09-28 13:28:45 +00004015void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4016{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004017 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4018 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4019 {
4020 ssl->min_major_ver = major;
4021 ssl->min_minor_ver = minor;
4022 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004023}
4024
Paul Bakker05decb22013-08-15 13:33:48 +02004025#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004026int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4027{
Paul Bakker77e257e2013-12-16 15:29:52 +01004028 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004029 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004030 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004031 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004032 }
4033
4034 ssl->mfl_code = mfl_code;
4035
4036 return( 0 );
4037}
Paul Bakker05decb22013-08-15 13:33:48 +02004038#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004039
Paul Bakker1f2bc622013-08-15 13:45:55 +02004040#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004041int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004042{
4043 if( ssl->endpoint != SSL_IS_CLIENT )
4044 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4045
Paul Bakker8c1ede62013-07-19 14:14:37 +02004046 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004047
4048 return( 0 );
4049}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004050#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004051
Paul Bakker48916f92012-09-16 19:57:18 +00004052void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4053{
4054 ssl->disable_renegotiation = renegotiation;
4055}
4056
4057void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4058{
4059 ssl->allow_legacy_renegotiation = allow_legacy;
4060}
4061
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004062void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4063{
4064 ssl->renego_max_records = max_records;
4065}
4066
Paul Bakkera503a632013-08-14 13:48:06 +02004067#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004068int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4069{
4070 ssl->session_tickets = use_tickets;
4071
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004072#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004073 if( ssl->endpoint == SSL_IS_CLIENT )
4074 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004075#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004076
4077 if( ssl->f_rng == NULL )
4078 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4079
4080 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004081}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004082
4083void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4084{
4085 ssl->ticket_lifetime = lifetime;
4086}
Paul Bakkera503a632013-08-14 13:48:06 +02004087#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004088
Paul Bakker5121ce52009-01-03 21:22:43 +00004089/*
4090 * SSL get accessors
4091 */
Paul Bakker23986e52011-04-24 08:57:21 +00004092size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004093{
4094 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4095}
4096
Paul Bakkerff60ee62010-03-16 21:09:09 +00004097int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004098{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004099 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004100}
4101
Paul Bakkere3166ce2011-01-27 17:40:50 +00004102const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004103{
Paul Bakker926c8e42013-03-06 10:23:34 +01004104 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004105 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004106
Paul Bakkere3166ce2011-01-27 17:40:50 +00004107 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004108}
4109
Paul Bakker43ca69c2011-01-15 17:35:19 +00004110const char *ssl_get_version( const ssl_context *ssl )
4111{
4112 switch( ssl->minor_ver )
4113 {
4114 case SSL_MINOR_VERSION_0:
4115 return( "SSLv3.0" );
4116
4117 case SSL_MINOR_VERSION_1:
4118 return( "TLSv1.0" );
4119
4120 case SSL_MINOR_VERSION_2:
4121 return( "TLSv1.1" );
4122
Paul Bakker1ef83d62012-04-11 12:09:53 +00004123 case SSL_MINOR_VERSION_3:
4124 return( "TLSv1.2" );
4125
Paul Bakker43ca69c2011-01-15 17:35:19 +00004126 default:
4127 break;
4128 }
4129 return( "unknown" );
4130}
4131
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004132#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004133const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004134{
4135 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004136 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004137
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004138 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004139}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004140#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004141
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004142#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004143int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4144{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004145 if( ssl == NULL ||
4146 dst == NULL ||
4147 ssl->session == NULL ||
4148 ssl->endpoint != SSL_IS_CLIENT )
4149 {
4150 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4151 }
4152
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004153 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004154}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004155#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004156
Paul Bakker5121ce52009-01-03 21:22:43 +00004157/*
Paul Bakker1961b702013-01-25 14:49:24 +01004158 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004159 */
Paul Bakker1961b702013-01-25 14:49:24 +01004160int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004161{
Paul Bakker40e46942009-01-03 21:51:57 +00004162 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004163
Paul Bakker40e46942009-01-03 21:51:57 +00004164#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004165 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004166 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004167#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004168#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004169 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004170 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004171#endif
4172
Paul Bakker1961b702013-01-25 14:49:24 +01004173 return( ret );
4174}
4175
4176/*
4177 * Perform the SSL handshake
4178 */
4179int ssl_handshake( ssl_context *ssl )
4180{
4181 int ret = 0;
4182
4183 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4184
4185 while( ssl->state != SSL_HANDSHAKE_OVER )
4186 {
4187 ret = ssl_handshake_step( ssl );
4188
4189 if( ret != 0 )
4190 break;
4191 }
4192
Paul Bakker5121ce52009-01-03 21:22:43 +00004193 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4194
4195 return( ret );
4196}
4197
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004198#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004199/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004200 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004201 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004202static int ssl_write_hello_request( ssl_context *ssl )
4203{
4204 int ret;
4205
4206 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4207
4208 ssl->out_msglen = 4;
4209 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4210 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4211
4212 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4213 {
4214 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4215 return( ret );
4216 }
4217
4218 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4219
4220 return( 0 );
4221}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004222#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004223
4224/*
4225 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004226 * - any side: calling ssl_renegotiate(),
4227 * - client: receiving a HelloRequest during ssl_read(),
4228 * - server: receiving any handshake message on server during ssl_read() after
4229 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004230 * If the handshake doesn't complete due to waiting for I/O, it will continue
4231 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004232 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004233static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004234{
4235 int ret;
4236
4237 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4238
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004239 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4240 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004241
4242 ssl->state = SSL_HELLO_REQUEST;
4243 ssl->renegotiation = SSL_RENEGOTIATION;
4244
Paul Bakker48916f92012-09-16 19:57:18 +00004245 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4246 {
4247 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4248 return( ret );
4249 }
4250
4251 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4252
4253 return( 0 );
4254}
4255
4256/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004257 * Renegotiate current connection on client,
4258 * or request renegotiation on server
4259 */
4260int ssl_renegotiate( ssl_context *ssl )
4261{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004262 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004263
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004264#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004265 /* On server, just send the request */
4266 if( ssl->endpoint == SSL_IS_SERVER )
4267 {
4268 if( ssl->state != SSL_HANDSHAKE_OVER )
4269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4270
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004271 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4272
4273 /* Did we already try/start sending HelloRequest? */
4274 if( ssl->out_left != 0 )
4275 return( ssl_flush_output( ssl ) );
4276
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004277 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004278 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004279#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004280
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004281#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004282 /*
4283 * On client, either start the renegotiation process or,
4284 * if already in progress, continue the handshake
4285 */
4286 if( ssl->renegotiation != SSL_RENEGOTIATION )
4287 {
4288 if( ssl->state != SSL_HANDSHAKE_OVER )
4289 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4290
4291 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4292 {
4293 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4294 return( ret );
4295 }
4296 }
4297 else
4298 {
4299 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4300 {
4301 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4302 return( ret );
4303 }
4304 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004305#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004306
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004307 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004308}
4309
4310/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 * Receive application data decrypted from the SSL layer
4312 */
Paul Bakker23986e52011-04-24 08:57:21 +00004313int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004314{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004315 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004316 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004317
4318 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4319
4320 if( ssl->state != SSL_HANDSHAKE_OVER )
4321 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004322 ret = ssl_handshake( ssl );
4323 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4324 {
4325 record_read = 1;
4326 }
4327 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004328 {
4329 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4330 return( ret );
4331 }
4332 }
4333
4334 if( ssl->in_offt == NULL )
4335 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004336 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004337 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004338 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4339 {
4340 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4341 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004342
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004343 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4344 return( ret );
4345 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004346 }
4347
4348 if( ssl->in_msglen == 0 &&
4349 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4350 {
4351 /*
4352 * OpenSSL sends empty messages to randomize the IV
4353 */
4354 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4355 {
Paul Bakker831a7552011-05-18 13:32:51 +00004356 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4357 return( 0 );
4358
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4360 return( ret );
4361 }
4362 }
4363
Paul Bakker48916f92012-09-16 19:57:18 +00004364 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4365 {
4366 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4367
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004368#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004369 if( ssl->endpoint == SSL_IS_CLIENT &&
4370 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4371 ssl->in_hslen != 4 ) )
4372 {
4373 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4374 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4375 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004376#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004377
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004378 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4379 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004380 ssl->allow_legacy_renegotiation ==
4381 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004382 {
4383 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4384
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004385#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004386 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004387 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004388 /*
4389 * SSLv3 does not have a "no_renegotiation" alert
4390 */
4391 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4392 return( ret );
4393 }
4394 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004395#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004396#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4397 defined(POLARSSL_SSL_PROTO_TLS1_2)
4398 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004399 {
4400 if( ( ret = ssl_send_alert_message( ssl,
4401 SSL_ALERT_LEVEL_WARNING,
4402 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4403 {
4404 return( ret );
4405 }
Paul Bakker48916f92012-09-16 19:57:18 +00004406 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004407 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004408#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4409 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004410 {
4411 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004412 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004413 }
Paul Bakker48916f92012-09-16 19:57:18 +00004414 }
4415 else
4416 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004417 ret = ssl_start_renegotiation( ssl );
4418 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4419 {
4420 record_read = 1;
4421 }
4422 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004423 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004424 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004425 return( ret );
4426 }
Paul Bakker48916f92012-09-16 19:57:18 +00004427 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004428
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004429 /* If a non-handshake record was read during renego, fallthrough,
4430 * else tell the user they should call ssl_read() again */
4431 if( ! record_read )
4432 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004433 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004434 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4435 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004436 ssl->renego_records_seen++;
4437
4438 if( ssl->renego_max_records >= 0 &&
4439 ssl->renego_records_seen > ssl->renego_max_records )
4440 {
4441 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4442 "but not honored by client" ) );
4443 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4444 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004445 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004446
4447 /* Fatal and closure alerts handled by ssl_read_record() */
4448 if( ssl->in_msgtype == SSL_MSG_ALERT )
4449 {
4450 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4451 return( POLARSSL_ERR_NET_WANT_READ );
4452 }
4453
4454 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004455 {
4456 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004457 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004458 }
4459
4460 ssl->in_offt = ssl->in_msg;
4461 }
4462
4463 n = ( len < ssl->in_msglen )
4464 ? len : ssl->in_msglen;
4465
4466 memcpy( buf, ssl->in_offt, n );
4467 ssl->in_msglen -= n;
4468
4469 if( ssl->in_msglen == 0 )
4470 /* all bytes consumed */
4471 ssl->in_offt = NULL;
4472 else
4473 /* more data available */
4474 ssl->in_offt += n;
4475
4476 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4477
Paul Bakker23986e52011-04-24 08:57:21 +00004478 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004479}
4480
4481/*
4482 * Send application data to be encrypted by the SSL layer
4483 */
Paul Bakker23986e52011-04-24 08:57:21 +00004484int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004485{
Paul Bakker23986e52011-04-24 08:57:21 +00004486 int ret;
4487 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004488 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004489
4490 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4491
4492 if( ssl->state != SSL_HANDSHAKE_OVER )
4493 {
4494 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4495 {
4496 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4497 return( ret );
4498 }
4499 }
4500
Paul Bakker05decb22013-08-15 13:33:48 +02004501#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004502 /*
4503 * Assume mfl_code is correct since it was checked when set
4504 */
4505 max_len = mfl_code_to_length[ssl->mfl_code];
4506
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004507 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004508 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004509 */
4510 if( ssl->session_out != NULL &&
4511 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4512 {
4513 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4514 }
Paul Bakker05decb22013-08-15 13:33:48 +02004515#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004516
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004517 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004518
Paul Bakker5121ce52009-01-03 21:22:43 +00004519 if( ssl->out_left != 0 )
4520 {
4521 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4522 {
4523 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4524 return( ret );
4525 }
4526 }
Paul Bakker887bd502011-06-08 13:10:54 +00004527 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004528 {
Paul Bakker887bd502011-06-08 13:10:54 +00004529 ssl->out_msglen = n;
4530 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4531 memcpy( ssl->out_msg, buf, n );
4532
4533 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4534 {
4535 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4536 return( ret );
4537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004538 }
4539
4540 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4541
Paul Bakker23986e52011-04-24 08:57:21 +00004542 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004543}
4544
4545/*
4546 * Notify the peer that the connection is being closed
4547 */
4548int ssl_close_notify( ssl_context *ssl )
4549{
4550 int ret;
4551
4552 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4553
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004554 if( ssl->out_left != 0 )
4555 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004556
4557 if( ssl->state == SSL_HANDSHAKE_OVER )
4558 {
Paul Bakker48916f92012-09-16 19:57:18 +00004559 if( ( ret = ssl_send_alert_message( ssl,
4560 SSL_ALERT_LEVEL_WARNING,
4561 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004562 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004563 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004564 return( ret );
4565 }
4566 }
4567
4568 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4569
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004570 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004571}
4572
Paul Bakker48916f92012-09-16 19:57:18 +00004573void ssl_transform_free( ssl_transform *transform )
4574{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004575 if( transform == NULL )
4576 return;
4577
Paul Bakker48916f92012-09-16 19:57:18 +00004578#if defined(POLARSSL_ZLIB_SUPPORT)
4579 deflateEnd( &transform->ctx_deflate );
4580 inflateEnd( &transform->ctx_inflate );
4581#endif
4582
Paul Bakker84bbeb52014-07-01 14:53:22 +02004583 cipher_free( &transform->cipher_ctx_enc );
4584 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004585
Paul Bakker84bbeb52014-07-01 14:53:22 +02004586 md_free( &transform->md_ctx_enc );
4587 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004588
Paul Bakker34617722014-06-13 17:20:13 +02004589 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004590}
4591
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004592#if defined(POLARSSL_X509_CRT_PARSE_C)
4593static void ssl_key_cert_free( ssl_key_cert *key_cert )
4594{
4595 ssl_key_cert *cur = key_cert, *next;
4596
4597 while( cur != NULL )
4598 {
4599 next = cur->next;
4600
4601 if( cur->key_own_alloc )
4602 {
4603 pk_free( cur->key );
4604 polarssl_free( cur->key );
4605 }
4606 polarssl_free( cur );
4607
4608 cur = next;
4609 }
4610}
4611#endif /* POLARSSL_X509_CRT_PARSE_C */
4612
Paul Bakker48916f92012-09-16 19:57:18 +00004613void ssl_handshake_free( ssl_handshake_params *handshake )
4614{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004615 if( handshake == NULL )
4616 return;
4617
Paul Bakker48916f92012-09-16 19:57:18 +00004618#if defined(POLARSSL_DHM_C)
4619 dhm_free( &handshake->dhm_ctx );
4620#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004621#if defined(POLARSSL_ECDH_C)
4622 ecdh_free( &handshake->ecdh_ctx );
4623#endif
4624
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004625#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004626 /* explicit void pointer cast for buggy MS compiler */
4627 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004628#endif
4629
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004630#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4631 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4632 /*
4633 * Free only the linked list wrapper, not the keys themselves
4634 * since the belong to the SNI callback
4635 */
4636 if( handshake->sni_key_cert != NULL )
4637 {
4638 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4639
4640 while( cur != NULL )
4641 {
4642 next = cur->next;
4643 polarssl_free( cur );
4644 cur = next;
4645 }
4646 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004647#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004648
Paul Bakker34617722014-06-13 17:20:13 +02004649 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004650}
4651
4652void ssl_session_free( ssl_session *session )
4653{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004654 if( session == NULL )
4655 return;
4656
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004657#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004658 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004659 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004660 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004661 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004662 }
Paul Bakkered27a042013-04-18 22:46:23 +02004663#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004664
Paul Bakkera503a632013-08-14 13:48:06 +02004665#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004666 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004667#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004668
Paul Bakker34617722014-06-13 17:20:13 +02004669 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004670}
4671
Paul Bakker5121ce52009-01-03 21:22:43 +00004672/*
4673 * Free an SSL context
4674 */
4675void ssl_free( ssl_context *ssl )
4676{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004677 if( ssl == NULL )
4678 return;
4679
Paul Bakker5121ce52009-01-03 21:22:43 +00004680 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4681
Paul Bakker5121ce52009-01-03 21:22:43 +00004682 if( ssl->out_ctr != NULL )
4683 {
Paul Bakker34617722014-06-13 17:20:13 +02004684 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004685 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004686 }
4687
4688 if( ssl->in_ctr != NULL )
4689 {
Paul Bakker34617722014-06-13 17:20:13 +02004690 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004691 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004692 }
4693
Paul Bakker16770332013-10-11 09:59:44 +02004694#if defined(POLARSSL_ZLIB_SUPPORT)
4695 if( ssl->compress_buf != NULL )
4696 {
Paul Bakker34617722014-06-13 17:20:13 +02004697 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004698 polarssl_free( ssl->compress_buf );
4699 }
4700#endif
4701
Paul Bakker40e46942009-01-03 21:51:57 +00004702#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004703 mpi_free( &ssl->dhm_P );
4704 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004705#endif
4706
Paul Bakker48916f92012-09-16 19:57:18 +00004707 if( ssl->transform )
4708 {
4709 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004710 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004711 }
4712
4713 if( ssl->handshake )
4714 {
4715 ssl_handshake_free( ssl->handshake );
4716 ssl_transform_free( ssl->transform_negotiate );
4717 ssl_session_free( ssl->session_negotiate );
4718
Paul Bakker6e339b52013-07-03 13:37:05 +02004719 polarssl_free( ssl->handshake );
4720 polarssl_free( ssl->transform_negotiate );
4721 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004722 }
4723
Paul Bakkerc0463502013-02-14 11:19:38 +01004724 if( ssl->session )
4725 {
4726 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004727 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004728 }
4729
Paul Bakkera503a632013-08-14 13:48:06 +02004730#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004731 if( ssl->ticket_keys )
4732 {
4733 ssl_ticket_keys_free( ssl->ticket_keys );
4734 polarssl_free( ssl->ticket_keys );
4735 }
Paul Bakkera503a632013-08-14 13:48:06 +02004736#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004737
Paul Bakker0be444a2013-08-27 21:55:01 +02004738#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004739 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004740 {
Paul Bakker34617722014-06-13 17:20:13 +02004741 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004742 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004743 ssl->hostname_len = 0;
4744 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004745#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004746
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004747#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004748 if( ssl->psk != NULL )
4749 {
Paul Bakker34617722014-06-13 17:20:13 +02004750 polarssl_zeroize( ssl->psk, ssl->psk_len );
4751 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004752 polarssl_free( ssl->psk );
4753 polarssl_free( ssl->psk_identity );
4754 ssl->psk_len = 0;
4755 ssl->psk_identity_len = 0;
4756 }
4757#endif
4758
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004759#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004760 ssl_key_cert_free( ssl->key_cert );
4761#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004762
Paul Bakker05ef8352012-05-08 09:17:57 +00004763#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4764 if( ssl_hw_record_finish != NULL )
4765 {
4766 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4767 ssl_hw_record_finish( ssl );
4768 }
4769#endif
4770
Paul Bakker5121ce52009-01-03 21:22:43 +00004771 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004772
Paul Bakker86f04f42013-02-14 11:20:09 +01004773 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004774 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004775}
4776
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004777#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004778/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004779 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004780 */
4781unsigned char ssl_sig_from_pk( pk_context *pk )
4782{
4783#if defined(POLARSSL_RSA_C)
4784 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4785 return( SSL_SIG_RSA );
4786#endif
4787#if defined(POLARSSL_ECDSA_C)
4788 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4789 return( SSL_SIG_ECDSA );
4790#endif
4791 return( SSL_SIG_ANON );
4792}
4793
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004794pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4795{
4796 switch( sig )
4797 {
4798#if defined(POLARSSL_RSA_C)
4799 case SSL_SIG_RSA:
4800 return( POLARSSL_PK_RSA );
4801#endif
4802#if defined(POLARSSL_ECDSA_C)
4803 case SSL_SIG_ECDSA:
4804 return( POLARSSL_PK_ECDSA );
4805#endif
4806 default:
4807 return( POLARSSL_PK_NONE );
4808 }
4809}
Paul Bakker9af723c2014-05-01 13:03:14 +02004810#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004811
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004812/*
4813 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4814 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004815md_type_t ssl_md_alg_from_hash( unsigned char hash )
4816{
4817 switch( hash )
4818 {
4819#if defined(POLARSSL_MD5_C)
4820 case SSL_HASH_MD5:
4821 return( POLARSSL_MD_MD5 );
4822#endif
4823#if defined(POLARSSL_SHA1_C)
4824 case SSL_HASH_SHA1:
4825 return( POLARSSL_MD_SHA1 );
4826#endif
4827#if defined(POLARSSL_SHA256_C)
4828 case SSL_HASH_SHA224:
4829 return( POLARSSL_MD_SHA224 );
4830 case SSL_HASH_SHA256:
4831 return( POLARSSL_MD_SHA256 );
4832#endif
4833#if defined(POLARSSL_SHA512_C)
4834 case SSL_HASH_SHA384:
4835 return( POLARSSL_MD_SHA384 );
4836 case SSL_HASH_SHA512:
4837 return( POLARSSL_MD_SHA512 );
4838#endif
4839 default:
4840 return( POLARSSL_MD_NONE );
4841 }
4842}
4843
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004844#if defined(POLARSSL_SSL_SET_CURVES)
4845/*
4846 * Check is a curve proposed by the peer is in our list.
4847 * Return 1 if we're willing to use it, 0 otherwise.
4848 */
4849int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4850{
4851 const ecp_group_id *gid;
4852
4853 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4854 if( *gid == grp_id )
4855 return( 1 );
4856
4857 return( 0 );
4858}
Paul Bakker9af723c2014-05-01 13:03:14 +02004859#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004860
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004861#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004862int ssl_check_cert_usage( const x509_crt *cert,
4863 const ssl_ciphersuite_t *ciphersuite,
4864 int cert_endpoint )
4865{
4866#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4867 int usage = 0;
4868#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004869#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4870 const char *ext_oid;
4871 size_t ext_len;
4872#endif
4873
4874#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4875 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4876 ((void) cert);
4877 ((void) cert_endpoint);
4878#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004879
4880#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4881 if( cert_endpoint == SSL_IS_SERVER )
4882 {
4883 /* Server part of the key exchange */
4884 switch( ciphersuite->key_exchange )
4885 {
4886 case POLARSSL_KEY_EXCHANGE_RSA:
4887 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4888 usage = KU_KEY_ENCIPHERMENT;
4889 break;
4890
4891 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4892 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4893 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4894 usage = KU_DIGITAL_SIGNATURE;
4895 break;
4896
4897 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4898 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4899 usage = KU_KEY_AGREEMENT;
4900 break;
4901
4902 /* Don't use default: we want warnings when adding new values */
4903 case POLARSSL_KEY_EXCHANGE_NONE:
4904 case POLARSSL_KEY_EXCHANGE_PSK:
4905 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4906 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4907 usage = 0;
4908 }
4909 }
4910 else
4911 {
4912 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4913 usage = KU_DIGITAL_SIGNATURE;
4914 }
4915
4916 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4917 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004918#else
4919 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004920#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4921
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004922#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4923 if( cert_endpoint == SSL_IS_SERVER )
4924 {
4925 ext_oid = OID_SERVER_AUTH;
4926 ext_len = OID_SIZE( OID_SERVER_AUTH );
4927 }
4928 else
4929 {
4930 ext_oid = OID_CLIENT_AUTH;
4931 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4932 }
4933
4934 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4935 return( -1 );
4936#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4937
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004938 return( 0 );
4939}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004940#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004941
4942#endif /* POLARSSL_SSL_TLS_C */