blob: 3de7f7c93159cc70eaa3573c7f30e8664c21af3e [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
159 /*
160 * SSLv3:
161 * block =
162 * MD5( secret + SHA1( 'A' + secret + random ) ) +
163 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
164 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
165 * ...
166 */
167 for( i = 0; i < dlen / 16; i++ )
168 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200169 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000170
171 sha1_starts( &sha1 );
172 sha1_update( &sha1, padding, 1 + i );
173 sha1_update( &sha1, secret, slen );
174 sha1_update( &sha1, random, rlen );
175 sha1_finish( &sha1, sha1sum );
176
177 md5_starts( &md5 );
178 md5_update( &md5, secret, slen );
179 md5_update( &md5, sha1sum, 20 );
180 md5_finish( &md5, dstbuf + i * 16 );
181 }
182
Paul Bakker34617722014-06-13 17:20:13 +0200183 polarssl_zeroize( &md5, sizeof( md5 ) );
184 polarssl_zeroize( &sha1, sizeof( sha1 ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000185
Paul Bakker34617722014-06-13 17:20:13 +0200186 polarssl_zeroize( padding, sizeof( padding ) );
187 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
189 return( 0 );
190}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200191#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000192
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200193#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200194static int tls1_prf( const unsigned char *secret, size_t slen,
195 const char *label,
196 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000197 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
Paul Bakker23986e52011-04-24 08:57:21 +0000199 size_t nb, hs;
200 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200201 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 unsigned char tmp[128];
203 unsigned char h_i[20];
204
205 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000206 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 hs = ( slen + 1 ) / 2;
209 S1 = secret;
210 S2 = secret + slen - hs;
211
212 nb = strlen( label );
213 memcpy( tmp + 20, label, nb );
214 memcpy( tmp + 20 + nb, random, rlen );
215 nb += rlen;
216
217 /*
218 * First compute P_md5(secret,label+random)[0..dlen]
219 */
220 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
221
222 for( i = 0; i < dlen; i += 16 )
223 {
224 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
225 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
226
227 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
228
229 for( j = 0; j < k; j++ )
230 dstbuf[i + j] = h_i[j];
231 }
232
233 /*
234 * XOR out with P_sha1(secret,label+random)[0..dlen]
235 */
236 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
237
238 for( i = 0; i < dlen; i += 20 )
239 {
240 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
241 sha1_hmac( S2, hs, tmp, 20, tmp );
242
243 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
244
245 for( j = 0; j < k; j++ )
246 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
247 }
248
Paul Bakker34617722014-06-13 17:20:13 +0200249 polarssl_zeroize( tmp, sizeof( tmp ) );
250 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252 return( 0 );
253}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200254#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200256#if defined(POLARSSL_SSL_PROTO_TLS1_2)
257#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200258static int tls_prf_sha256( const unsigned char *secret, size_t slen,
259 const char *label,
260 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000261 unsigned char *dstbuf, size_t dlen )
262{
263 size_t nb;
264 size_t i, j, k;
265 unsigned char tmp[128];
266 unsigned char h_i[32];
267
268 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
270
271 nb = strlen( label );
272 memcpy( tmp + 32, label, nb );
273 memcpy( tmp + 32 + nb, random, rlen );
274 nb += rlen;
275
276 /*
277 * Compute P_<hash>(secret, label + random)[0..dlen]
278 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200279 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000280
281 for( i = 0; i < dlen; i += 32 )
282 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200283 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
284 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000285
286 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
287
288 for( j = 0; j < k; j++ )
289 dstbuf[i + j] = h_i[j];
290 }
291
Paul Bakker34617722014-06-13 17:20:13 +0200292 polarssl_zeroize( tmp, sizeof( tmp ) );
293 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000294
295 return( 0 );
296}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200297#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000298
Paul Bakker9e36f042013-06-30 14:34:05 +0200299#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200300static int tls_prf_sha384( const unsigned char *secret, size_t slen,
301 const char *label,
302 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000303 unsigned char *dstbuf, size_t dlen )
304{
305 size_t nb;
306 size_t i, j, k;
307 unsigned char tmp[128];
308 unsigned char h_i[48];
309
310 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
311 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
312
313 nb = strlen( label );
314 memcpy( tmp + 48, label, nb );
315 memcpy( tmp + 48 + nb, random, rlen );
316 nb += rlen;
317
318 /*
319 * Compute P_<hash>(secret, label + random)[0..dlen]
320 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200321 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000322
323 for( i = 0; i < dlen; i += 48 )
324 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200325 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
326 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000327
328 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
329
330 for( j = 0; j < k; j++ )
331 dstbuf[i + j] = h_i[j];
332 }
333
Paul Bakker34617722014-06-13 17:20:13 +0200334 polarssl_zeroize( tmp, sizeof( tmp ) );
335 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336
337 return( 0 );
338}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200339#endif /* POLARSSL_SHA512_C */
340#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000341
Paul Bakker66d5d072014-06-17 16:39:18 +0200342static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343
344#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
345 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200346static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
Paul Bakker380da532012-04-18 16:10:25 +0000348
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200349#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200350static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
351static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif
353
354#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200355static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
356static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200357#endif
358
359#if defined(POLARSSL_SSL_PROTO_TLS1_2)
360#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200361static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
362static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
363static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100365
Paul Bakker9e36f042013-06-30 14:34:05 +0200366#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200367static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
368static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
369static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100370#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200371#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000372
Paul Bakker5121ce52009-01-03 21:22:43 +0000373int ssl_derive_keys( ssl_context *ssl )
374{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200375 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char keyblk[256];
378 unsigned char *key1;
379 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100380 unsigned char *mac_enc;
381 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200382 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 const cipher_info_t *cipher_info;
384 const md_info_t *md_info;
385
Paul Bakker48916f92012-09-16 19:57:18 +0000386 ssl_session *session = ssl->session_negotiate;
387 ssl_transform *transform = ssl->transform_negotiate;
388 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
391
Paul Bakker68884e32013-01-07 18:20:04 +0100392 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
393 if( cipher_info == NULL )
394 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200395 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100396 transform->ciphersuite_info->cipher ) );
397 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
398 }
399
400 md_info = md_info_from_type( transform->ciphersuite_info->mac );
401 if( md_info == NULL )
402 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200403 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100404 transform->ciphersuite_info->mac ) );
405 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
406 }
407
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000409 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000410 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200411#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000412 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413 {
Paul Bakker48916f92012-09-16 19:57:18 +0000414 handshake->tls_prf = ssl3_prf;
415 handshake->calc_verify = ssl_calc_verify_ssl;
416 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200418 else
419#endif
420#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
421 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000422 {
Paul Bakker48916f92012-09-16 19:57:18 +0000423 handshake->tls_prf = tls1_prf;
424 handshake->calc_verify = ssl_calc_verify_tls;
425 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000426 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200427 else
428#endif
429#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200430#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
432 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000433 {
Paul Bakker48916f92012-09-16 19:57:18 +0000434 handshake->tls_prf = tls_prf_sha384;
435 handshake->calc_verify = ssl_calc_verify_tls_sha384;
436 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000438 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif
440#if defined(POLARSSL_SHA256_C)
441 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000442 {
Paul Bakker48916f92012-09-16 19:57:18 +0000443 handshake->tls_prf = tls_prf_sha256;
444 handshake->calc_verify = ssl_calc_verify_tls_sha256;
445 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000446 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447 else
448#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200449#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200450 {
451 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200452 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200453 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000454
455 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 * SSLv3:
457 * master =
458 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
459 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200461 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200462 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 * master = PRF( premaster, "master secret", randbytes )[0..47]
464 */
Paul Bakker0a597072012-09-25 21:55:46 +0000465 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 {
Paul Bakker48916f92012-09-16 19:57:18 +0000467 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
468 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
Paul Bakker48916f92012-09-16 19:57:18 +0000470 handshake->tls_prf( handshake->premaster, handshake->pmslen,
471 "master secret",
472 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Paul Bakker34617722014-06-13 17:20:13 +0200474 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 }
476 else
477 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
478
479 /*
480 * Swap the client and server random values.
481 */
Paul Bakker48916f92012-09-16 19:57:18 +0000482 memcpy( tmp, handshake->randbytes, 64 );
483 memcpy( handshake->randbytes, tmp + 32, 32 );
484 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200485 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487 /*
488 * SSLv3:
489 * key block =
490 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
491 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
492 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
493 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
494 * ...
495 *
496 * TLSv1:
497 * key block = PRF( master, "key expansion", randbytes )
498 */
Paul Bakker48916f92012-09-16 19:57:18 +0000499 handshake->tls_prf( session->master, 48, "key expansion",
500 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
Paul Bakker48916f92012-09-16 19:57:18 +0000502 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
503 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
504 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
505 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
507
Paul Bakker34617722014-06-13 17:20:13 +0200508 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 /*
511 * Determine the appropriate key, IV and MAC length.
512 */
Paul Bakker68884e32013-01-07 18:20:04 +0100513
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200514 transform->keylen = cipher_info->key_length / 8;
515
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200516 if( cipher_info->mode == POLARSSL_MODE_GCM ||
517 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200519 transform->maclen = 0;
520
Paul Bakker68884e32013-01-07 18:20:04 +0100521 transform->ivlen = 12;
522 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200523
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200524 /* Minimum length is expicit IV + tag */
525 transform->minlen = transform->ivlen - transform->fixed_ivlen
526 + ( transform->ciphersuite_info->flags &
527 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100528 }
529 else
530 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200531 int ret;
532
533 /* Initialize HMAC contexts */
534 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
535 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100536 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200537 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
538 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100539 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200541 /* Get MAC length */
542 transform->maclen = md_get_size( md_info );
543
544#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
545 /*
546 * If HMAC is to be truncated, we shall keep the leftmost bytes,
547 * (rfc 6066 page 13 or rfc 2104 section 4),
548 * so we only need to adjust the length here.
549 */
550 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
551 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
552#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
553
554 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100555 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200557 /* Minimum length */
558 if( cipher_info->mode == POLARSSL_MODE_STREAM )
559 transform->minlen = transform->maclen;
560 else
Paul Bakker68884e32013-01-07 18:20:04 +0100561 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200562 /*
563 * GenericBlockCipher:
564 * first multiple of blocklen greater than maclen
565 * + IV except for SSL3 and TLS 1.0
566 */
567 transform->minlen = transform->maclen
568 + cipher_info->block_size
569 - transform->maclen % cipher_info->block_size;
570
571#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
572 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
573 ssl->minor_ver == SSL_MINOR_VERSION_1 )
574 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100575 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200576#endif
577#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
578 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
579 ssl->minor_ver == SSL_MINOR_VERSION_3 )
580 {
581 transform->minlen += transform->ivlen;
582 }
583 else
584#endif
585 {
586 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
587 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
588 }
Paul Bakker68884e32013-01-07 18:20:04 +0100589 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 }
591
592 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000593 transform->keylen, transform->minlen, transform->ivlen,
594 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
596 /*
597 * Finally setup the cipher contexts, IVs and MAC secrets.
598 */
599 if( ssl->endpoint == SSL_IS_CLIENT )
600 {
Paul Bakker48916f92012-09-16 19:57:18 +0000601 key1 = keyblk + transform->maclen * 2;
602 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
Paul Bakker68884e32013-01-07 18:20:04 +0100604 mac_enc = keyblk;
605 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000607 /*
608 * This is not used in TLS v1.1.
609 */
Paul Bakker48916f92012-09-16 19:57:18 +0000610 iv_copy_len = ( transform->fixed_ivlen ) ?
611 transform->fixed_ivlen : transform->ivlen;
612 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
613 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000614 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 }
616 else
617 {
Paul Bakker48916f92012-09-16 19:57:18 +0000618 key1 = keyblk + transform->maclen * 2 + transform->keylen;
619 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Paul Bakker68884e32013-01-07 18:20:04 +0100621 mac_enc = keyblk + transform->maclen;
622 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000624 /*
625 * This is not used in TLS v1.1.
626 */
Paul Bakker48916f92012-09-16 19:57:18 +0000627 iv_copy_len = ( transform->fixed_ivlen ) ?
628 transform->fixed_ivlen : transform->ivlen;
629 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
630 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000631 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 }
633
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200634#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100635 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
636 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100637 if( transform->maclen > sizeof transform->mac_enc )
638 {
639 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200640 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100641 }
642
Paul Bakker68884e32013-01-07 18:20:04 +0100643 memcpy( transform->mac_enc, mac_enc, transform->maclen );
644 memcpy( transform->mac_dec, mac_dec, transform->maclen );
645 }
646 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200647#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200648#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
649 defined(POLARSSL_SSL_PROTO_TLS1_2)
650 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100651 {
652 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
653 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
654 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200655 else
656#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200657 {
658 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200659 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200660 }
Paul Bakker68884e32013-01-07 18:20:04 +0100661
Paul Bakker05ef8352012-05-08 09:17:57 +0000662#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200663 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000664 {
665 int ret = 0;
666
667 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
668
Paul Bakker07eb38b2012-12-19 14:42:06 +0100669 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
670 transform->iv_enc, transform->iv_dec,
671 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100672 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100673 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000674 {
675 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200676 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000677 }
678 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200679#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000680
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200681 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
682 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200684 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
685 return( ret );
686 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200687
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200688 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
689 cipher_info ) ) != 0 )
690 {
691 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
692 return( ret );
693 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200694
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200695 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
696 cipher_info->key_length,
697 POLARSSL_ENCRYPT ) ) != 0 )
698 {
699 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
700 return( ret );
701 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200702
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200703 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
704 cipher_info->key_length,
705 POLARSSL_DECRYPT ) ) != 0 )
706 {
707 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
708 return( ret );
709 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200710
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200711#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200712 if( cipher_info->mode == POLARSSL_MODE_CBC )
713 {
714 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
715 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200716 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200717 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
718 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200719 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200720
721 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
722 POLARSSL_PADDING_NONE ) ) != 0 )
723 {
724 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
725 return( ret );
726 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200728#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Paul Bakker34617722014-06-13 17:20:13 +0200730 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Paul Bakker2770fbd2012-07-03 13:30:23 +0000732#if defined(POLARSSL_ZLIB_SUPPORT)
733 // Initialize compression
734 //
Paul Bakker48916f92012-09-16 19:57:18 +0000735 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000736 {
Paul Bakker16770332013-10-11 09:59:44 +0200737 if( ssl->compress_buf == NULL )
738 {
739 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
740 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
741 if( ssl->compress_buf == NULL )
742 {
743 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
744 SSL_BUFFER_LEN ) );
745 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
746 }
747 }
748
Paul Bakker2770fbd2012-07-03 13:30:23 +0000749 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
750
Paul Bakker48916f92012-09-16 19:57:18 +0000751 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
752 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000753
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200754 if( deflateInit( &transform->ctx_deflate,
755 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000756 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000757 {
758 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
759 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
760 }
761 }
762#endif /* POLARSSL_ZLIB_SUPPORT */
763
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
765
766 return( 0 );
767}
768
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200769#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000770void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000771{
772 md5_context md5;
773 sha1_context sha1;
774 unsigned char pad_1[48];
775 unsigned char pad_2[48];
776
Paul Bakker380da532012-04-18 16:10:25 +0000777 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Paul Bakker48916f92012-09-16 19:57:18 +0000779 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
780 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
Paul Bakker380da532012-04-18 16:10:25 +0000782 memset( pad_1, 0x36, 48 );
783 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Paul Bakker48916f92012-09-16 19:57:18 +0000785 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000786 md5_update( &md5, pad_1, 48 );
787 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker380da532012-04-18 16:10:25 +0000789 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000790 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000791 md5_update( &md5, pad_2, 48 );
792 md5_update( &md5, hash, 16 );
793 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Paul Bakker48916f92012-09-16 19:57:18 +0000795 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000796 sha1_update( &sha1, pad_1, 40 );
797 sha1_finish( &sha1, hash + 16 );
798
799 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000800 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000801 sha1_update( &sha1, pad_2, 40 );
802 sha1_update( &sha1, hash + 16, 20 );
803 sha1_finish( &sha1, hash + 16 );
804
805 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
806 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
807
808 return;
809}
Paul Bakker9af723c2014-05-01 13:03:14 +0200810#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000811
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200812#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000813void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
814{
815 md5_context md5;
816 sha1_context sha1;
817
818 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
819
Paul Bakker48916f92012-09-16 19:57:18 +0000820 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
821 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000822
Paul Bakker48916f92012-09-16 19:57:18 +0000823 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000824 sha1_finish( &sha1, hash + 16 );
825
826 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
827 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
828
829 return;
830}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200831#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000832
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200833#if defined(POLARSSL_SSL_PROTO_TLS1_2)
834#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000835void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
836{
Paul Bakker9e36f042013-06-30 14:34:05 +0200837 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000838
839 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
840
Paul Bakker9e36f042013-06-30 14:34:05 +0200841 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
842 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000843
844 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
845 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
846
847 return;
848}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200849#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000850
Paul Bakker9e36f042013-06-30 14:34:05 +0200851#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000852void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
853{
Paul Bakker9e36f042013-06-30 14:34:05 +0200854 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000855
856 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
857
Paul Bakker9e36f042013-06-30 14:34:05 +0200858 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
859 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakkerca4ab492012-04-18 14:23:57 +0000861 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
863
864 return;
865}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200866#endif /* POLARSSL_SHA512_C */
867#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000868
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200869#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200870int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
871{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200872 unsigned char *p = ssl->handshake->premaster;
873 unsigned char *end = p + sizeof( ssl->handshake->premaster );
874
875 /*
876 * PMS = struct {
877 * opaque other_secret<0..2^16-1>;
878 * opaque psk<0..2^16-1>;
879 * };
880 * with "other_secret" depending on the particular key exchange
881 */
882#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
883 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
884 {
885 if( end - p < 2 + (int) ssl->psk_len )
886 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
887
888 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
889 *(p++) = (unsigned char)( ssl->psk_len );
890 p += ssl->psk_len;
891 }
892 else
893#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200894#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
895 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
896 {
897 /*
898 * other_secret already set by the ClientKeyExchange message,
899 * and is 48 bytes long
900 */
901 *p++ = 0;
902 *p++ = 48;
903 p += 48;
904 }
905 else
906#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200907#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
908 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
909 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200910 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200911 size_t len = ssl->handshake->dhm_ctx.len;
912
913 if( end - p < 2 + (int) len )
914 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
915
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200916 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200917 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200918 p + 2, &len,
919 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200920 {
921 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
922 return( ret );
923 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200924 *(p++) = (unsigned char)( len >> 8 );
925 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200926 p += len;
927
928 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
929 }
930 else
931#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
932#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
933 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
934 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200935 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200936 size_t zlen;
937
938 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200939 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200940 ssl->f_rng, ssl->p_rng ) ) != 0 )
941 {
942 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
943 return( ret );
944 }
945
946 *(p++) = (unsigned char)( zlen >> 8 );
947 *(p++) = (unsigned char)( zlen );
948 p += zlen;
949
950 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
951 }
952 else
953#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
954 {
955 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200956 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200957 }
958
959 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100960 if( end - p < 2 + (int) ssl->psk_len )
961 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
962
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200963 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
964 *(p++) = (unsigned char)( ssl->psk_len );
965 memcpy( p, ssl->psk, ssl->psk_len );
966 p += ssl->psk_len;
967
968 ssl->handshake->pmslen = p - ssl->handshake->premaster;
969
970 return( 0 );
971}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200972#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200973
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200974#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000975/*
976 * SSLv3.0 MAC functions
977 */
Paul Bakker68884e32013-01-07 18:20:04 +0100978static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
979 unsigned char *buf, size_t len,
980 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000981{
982 unsigned char header[11];
983 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100984 int padlen = 0;
985 int md_size = md_get_size( md_ctx->md_info );
986 int md_type = md_get_type( md_ctx->md_info );
987
988 if( md_type == POLARSSL_MD_MD5 )
989 padlen = 48;
990 else if( md_type == POLARSSL_MD_SHA1 )
991 padlen = 40;
992 else if( md_type == POLARSSL_MD_SHA256 )
993 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100994 else if( md_type == POLARSSL_MD_SHA384 )
995 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000996
997 memcpy( header, ctr, 8 );
998 header[ 8] = (unsigned char) type;
999 header[ 9] = (unsigned char)( len >> 8 );
1000 header[10] = (unsigned char)( len );
1001
Paul Bakker68884e32013-01-07 18:20:04 +01001002 memset( padding, 0x36, padlen );
1003 md_starts( md_ctx );
1004 md_update( md_ctx, secret, md_size );
1005 md_update( md_ctx, padding, padlen );
1006 md_update( md_ctx, header, 11 );
1007 md_update( md_ctx, buf, len );
1008 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009
Paul Bakker68884e32013-01-07 18:20:04 +01001010 memset( padding, 0x5C, padlen );
1011 md_starts( md_ctx );
1012 md_update( md_ctx, secret, md_size );
1013 md_update( md_ctx, padding, padlen );
1014 md_update( md_ctx, buf + len, md_size );
1015 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001016}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001017#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001018
Paul Bakker5121ce52009-01-03 21:22:43 +00001019/*
1020 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001021 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001022static int ssl_encrypt_buf( ssl_context *ssl )
1023{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001024 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001025 const cipher_mode_t mode = cipher_get_cipher_mode(
1026 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
1028 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1029
1030 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001031 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001032 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001033#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1034 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1035 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001036 if( mode != POLARSSL_MODE_GCM &&
1037 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001039#if defined(POLARSSL_SSL_PROTO_SSL3)
1040 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1041 {
1042 ssl_mac( &ssl->transform_out->md_ctx_enc,
1043 ssl->transform_out->mac_enc,
1044 ssl->out_msg, ssl->out_msglen,
1045 ssl->out_ctr, ssl->out_msgtype );
1046 }
1047 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001048#endif
1049#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001050 defined(POLARSSL_SSL_PROTO_TLS1_2)
1051 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1052 {
1053 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1054 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1055 ssl->out_msg, ssl->out_msglen );
1056 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1057 ssl->out_msg + ssl->out_msglen );
1058 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1059 }
1060 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001061#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001062 {
1063 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001064 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001065 }
1066
1067 SSL_DEBUG_BUF( 4, "computed mac",
1068 ssl->out_msg + ssl->out_msglen,
1069 ssl->transform_out->maclen );
1070
1071 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001072 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001073#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001075 /*
1076 * Encrypt
1077 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001078#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001079 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001081 int ret;
1082 size_t olen = 0;
1083
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1085 "including %d bytes of padding",
1086 ssl->out_msglen, 0 ) );
1087
1088 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1089 ssl->out_msg, ssl->out_msglen );
1090
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001091 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001092 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001093 ssl->transform_out->ivlen,
1094 ssl->out_msg, ssl->out_msglen,
1095 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001096 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001097 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001098 return( ret );
1099 }
1100
1101 if( ssl->out_msglen != olen )
1102 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001103 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001104 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001105 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001106 }
Paul Bakker68884e32013-01-07 18:20:04 +01001107 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001108#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001109#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1110 if( mode == POLARSSL_MODE_GCM ||
1111 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001112 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001113 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001114 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001115 unsigned char *enc_msg;
1116 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001117 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1118 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001119
Paul Bakkerca4ab492012-04-18 14:23:57 +00001120 memcpy( add_data, ssl->out_ctr, 8 );
1121 add_data[8] = ssl->out_msgtype;
1122 add_data[9] = ssl->major_ver;
1123 add_data[10] = ssl->minor_ver;
1124 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1125 add_data[12] = ssl->out_msglen & 0xFF;
1126
1127 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1128 add_data, 13 );
1129
Paul Bakker68884e32013-01-07 18:20:04 +01001130 /*
1131 * Generate IV
1132 */
1133 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001134 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1135 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001136 if( ret != 0 )
1137 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001138
Paul Bakker68884e32013-01-07 18:20:04 +01001139 memcpy( ssl->out_iv,
1140 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1141 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001142
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001143 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001144 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001145
Paul Bakker68884e32013-01-07 18:20:04 +01001146 /*
1147 * Fix pointer positions and message length with added IV
1148 */
1149 enc_msg = ssl->out_msg;
1150 enc_msglen = ssl->out_msglen;
1151 ssl->out_msglen += ssl->transform_out->ivlen -
1152 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001153
Paul Bakker68884e32013-01-07 18:20:04 +01001154 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1155 "including %d bytes of padding",
1156 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001157
Paul Bakker68884e32013-01-07 18:20:04 +01001158 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1159 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001160
Paul Bakker68884e32013-01-07 18:20:04 +01001161 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001162 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001163 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001164 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1165 ssl->transform_out->iv_enc,
1166 ssl->transform_out->ivlen,
1167 add_data, 13,
1168 enc_msg, enc_msglen,
1169 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001170 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001171 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001172 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001173 return( ret );
1174 }
1175
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001176 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001177 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001178 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001179 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001180 }
1181
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001182 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001183
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001184 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001187#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001188#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1189 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001190 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001192 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001193 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001194 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001195
Paul Bakker48916f92012-09-16 19:57:18 +00001196 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1197 ssl->transform_out->ivlen;
1198 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 padlen = 0;
1200
1201 for( i = 0; i <= padlen; i++ )
1202 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1203
1204 ssl->out_msglen += padlen + 1;
1205
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001206 enc_msglen = ssl->out_msglen;
1207 enc_msg = ssl->out_msg;
1208
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001209#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001210 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001211 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1212 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001214 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001215 {
1216 /*
1217 * Generate IV
1218 */
Paul Bakker48916f92012-09-16 19:57:18 +00001219 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1220 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001221 if( ret != 0 )
1222 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001223
Paul Bakker92be97b2013-01-02 17:30:03 +01001224 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001225 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001226
1227 /*
1228 * Fix pointer positions and message length with added IV
1229 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001230 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001231 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001232 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001234#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001237 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001238 ssl->out_msglen, ssl->transform_out->ivlen,
1239 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
1241 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001242 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001244 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001245 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001246 ssl->transform_out->ivlen,
1247 enc_msg, enc_msglen,
1248 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001249 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001250 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001251 return( ret );
1252 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001253
Paul Bakkercca5b812013-08-31 17:40:26 +02001254 if( enc_msglen != olen )
1255 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001256 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001257 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001258 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001259
1260#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001261 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1262 {
1263 /*
1264 * Save IV in SSL3 and TLS1
1265 */
1266 memcpy( ssl->transform_out->iv_enc,
1267 ssl->transform_out->cipher_ctx_enc.iv,
1268 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001270#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001272 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001273#endif /* POLARSSL_CIPHER_MODE_CBC &&
1274 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001275 {
1276 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001277 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001278 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Paul Bakkerca4ab492012-04-18 14:23:57 +00001280 for( i = 8; i > 0; i-- )
1281 if( ++ssl->out_ctr[i - 1] != 0 )
1282 break;
1283
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001284 /* The loops goes to its end iff the counter is wrapping */
1285 if( i == 0 )
1286 {
1287 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1288 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1289 }
1290
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1292
1293 return( 0 );
1294}
1295
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001296#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001297
Paul Bakker5121ce52009-01-03 21:22:43 +00001298static int ssl_decrypt_buf( ssl_context *ssl )
1299{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001300 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001301 const cipher_mode_t mode = cipher_get_cipher_mode(
1302 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001303#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1304 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1305 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1306 size_t padlen = 0, correct = 1;
1307#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
1309 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1310
Paul Bakker48916f92012-09-16 19:57:18 +00001311 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 {
1313 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001314 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001315 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 }
1317
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001318#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001319 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001320 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001321 int ret;
1322 size_t olen = 0;
1323
Paul Bakker68884e32013-01-07 18:20:04 +01001324 padlen = 0;
1325
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001326 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001327 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001328 ssl->transform_in->ivlen,
1329 ssl->in_msg, ssl->in_msglen,
1330 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001331 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001332 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001333 return( ret );
1334 }
1335
1336 if( ssl->in_msglen != olen )
1337 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001338 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001339 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001340 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 }
Paul Bakker68884e32013-01-07 18:20:04 +01001342 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001343#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001344#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1345 if( mode == POLARSSL_MODE_GCM ||
1346 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001347 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001348 int ret;
1349 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001350 unsigned char *dec_msg;
1351 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001352 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001353 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1354 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001355 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1356 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001357
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001358 if( ssl->in_msglen < explicit_iv_len + taglen )
1359 {
1360 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1361 "+ taglen (%d)", ssl->in_msglen,
1362 explicit_iv_len, taglen ) );
1363 return( POLARSSL_ERR_SSL_INVALID_MAC );
1364 }
1365 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1366
Paul Bakker68884e32013-01-07 18:20:04 +01001367 dec_msg = ssl->in_msg;
1368 dec_msg_result = ssl->in_msg;
1369 ssl->in_msglen = dec_msglen;
1370
1371 memcpy( add_data, ssl->in_ctr, 8 );
1372 add_data[8] = ssl->in_msgtype;
1373 add_data[9] = ssl->major_ver;
1374 add_data[10] = ssl->minor_ver;
1375 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1376 add_data[12] = ssl->in_msglen & 0xFF;
1377
1378 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1379 add_data, 13 );
1380
1381 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1382 ssl->in_iv,
1383 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1384
1385 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1386 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001387 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001388
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001389 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001390 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001391 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001392 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1393 ssl->transform_in->iv_dec,
1394 ssl->transform_in->ivlen,
1395 add_data, 13,
1396 dec_msg, dec_msglen,
1397 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001398 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001399 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001400 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1401
1402 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1403 return( POLARSSL_ERR_SSL_INVALID_MAC );
1404
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001405 return( ret );
1406 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001407
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001408 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001409 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001410 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001411 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001412 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001413 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001415#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001416#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1417 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001418 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 {
Paul Bakker45829992013-01-03 14:52:21 +01001420 /*
1421 * Decrypt and check the padding
1422 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001423 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001424 unsigned char *dec_msg;
1425 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001426 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001427 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001428 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001429
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 /*
Paul Bakker45829992013-01-03 14:52:21 +01001431 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 */
Paul Bakker48916f92012-09-16 19:57:18 +00001433 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 {
1435 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001436 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001437 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 }
1439
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001440#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001441 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1442 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001443#endif
Paul Bakker45829992013-01-03 14:52:21 +01001444
1445 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1446 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1447 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001448 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1449 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1450 ssl->transform_in->ivlen,
1451 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001452 return( POLARSSL_ERR_SSL_INVALID_MAC );
1453 }
1454
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001455 dec_msglen = ssl->in_msglen;
1456 dec_msg = ssl->in_msg;
1457 dec_msg_result = ssl->in_msg;
1458
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001459#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001460 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001461 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001462 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001463 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001464 {
Paul Bakker48916f92012-09-16 19:57:18 +00001465 dec_msglen -= ssl->transform_in->ivlen;
1466 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001467
Paul Bakker48916f92012-09-16 19:57:18 +00001468 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001469 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001470 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001471#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001472
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001473 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001474 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001475 ssl->transform_in->ivlen,
1476 dec_msg, dec_msglen,
1477 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001478 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001479 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001480 return( ret );
1481 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001482
Paul Bakkercca5b812013-08-31 17:40:26 +02001483 if( dec_msglen != olen )
1484 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001485 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001486 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001487 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001488
1489#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001490 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1491 {
1492 /*
1493 * Save IV in SSL3 and TLS1
1494 */
1495 memcpy( ssl->transform_in->iv_dec,
1496 ssl->transform_in->cipher_ctx_dec.iv,
1497 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001499#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001500
1501 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001502
1503 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1504 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001505#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001506 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1507 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001508#endif
Paul Bakker45829992013-01-03 14:52:21 +01001509 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001510 correct = 0;
1511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001512
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001513#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1515 {
Paul Bakker48916f92012-09-16 19:57:18 +00001516 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001517 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001518#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001519 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1520 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001521 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001522#endif
Paul Bakker45829992013-01-03 14:52:21 +01001523 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 }
1525 }
1526 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001527#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001528#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1529 defined(POLARSSL_SSL_PROTO_TLS1_2)
1530 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 {
1532 /*
Paul Bakker45829992013-01-03 14:52:21 +01001533 * TLSv1+: always check the padding up to the first failure
1534 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001536 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001537 size_t padding_idx = ssl->in_msglen - padlen - 1;
1538
Paul Bakker956c9e02013-12-19 14:42:28 +01001539 /*
1540 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001541 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001542 *
Paul Bakker61885c72014-04-25 12:59:03 +02001543 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1544 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001545 *
1546 * In both cases we reset padding_idx to a safe value (0) to
1547 * prevent out-of-buffer reads.
1548 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001549 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001550 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1551 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001552
1553 padding_idx *= correct;
1554
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001555 for( i = 1; i <= 256; i++ )
1556 {
1557 real_count &= ( i <= padlen );
1558 pad_count += real_count *
1559 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1560 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001561
1562 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001563
Paul Bakkerd66f0702013-01-31 16:57:45 +01001564#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001565 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001566 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001567#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001568 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001570 else
1571#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1572 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001573 {
1574 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001575 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001576 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001577 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001578 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001579#endif /* POLARSSL_CIPHER_MODE_CBC &&
1580 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001581 {
1582 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001583 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001584 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
1586 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1587 ssl->in_msg, ssl->in_msglen );
1588
1589 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001590 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001592#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1593 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1594 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001595 if( mode != POLARSSL_MODE_GCM &&
1596 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001597 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001598 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1599
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001600 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001602 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1603 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001605 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001606
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001607#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001608 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1609 {
1610 ssl_mac( &ssl->transform_in->md_ctx_dec,
1611 ssl->transform_in->mac_dec,
1612 ssl->in_msg, ssl->in_msglen,
1613 ssl->in_ctr, ssl->in_msgtype );
1614 }
1615 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001616#endif /* POLARSSL_SSL_PROTO_SSL3 */
1617#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001618 defined(POLARSSL_SSL_PROTO_TLS1_2)
1619 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1620 {
1621 /*
1622 * Process MAC and always update for padlen afterwards to make
1623 * total time independent of padlen
1624 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001625 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001626 *
1627 * Known timing attacks:
1628 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1629 *
1630 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1631 * correctly. (We round down instead of up, so -56 is the correct
1632 * value for our calculations instead of -55)
1633 */
1634 size_t j, extra_run = 0;
1635 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1636 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001637
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001638 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001639
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001640 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1641 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1642 ssl->in_msglen );
1643 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1644 ssl->in_msg + ssl->in_msglen );
1645 for( j = 0; j < extra_run; j++ )
1646 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001647
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001648 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1649 }
1650 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001651#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001652 POLARSSL_SSL_PROTO_TLS1_2 */
1653 {
1654 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001655 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001658 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1659 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1660 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001661
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001662 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663 ssl->transform_in->maclen ) != 0 )
1664 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001665#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001666 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001667#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 correct = 0;
1669 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001670
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001671 /*
1672 * Finally check the correct flag
1673 */
1674 if( correct == 0 )
1675 return( POLARSSL_ERR_SSL_INVALID_MAC );
1676 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001677#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
1679 if( ssl->in_msglen == 0 )
1680 {
1681 ssl->nb_zero++;
1682
1683 /*
1684 * Three or more empty messages may be a DoS attack
1685 * (excessive CPU consumption).
1686 */
1687 if( ssl->nb_zero > 3 )
1688 {
1689 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1690 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001691 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001692 }
1693 }
1694 else
1695 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001696
Paul Bakker23986e52011-04-24 08:57:21 +00001697 for( i = 8; i > 0; i-- )
1698 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 break;
1700
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001701 /* The loops goes to its end iff the counter is wrapping */
1702 if( i == 0 )
1703 {
1704 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1705 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1706 }
1707
Paul Bakker5121ce52009-01-03 21:22:43 +00001708 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1709
1710 return( 0 );
1711}
1712
Paul Bakker2770fbd2012-07-03 13:30:23 +00001713#if defined(POLARSSL_ZLIB_SUPPORT)
1714/*
1715 * Compression/decompression functions
1716 */
1717static int ssl_compress_buf( ssl_context *ssl )
1718{
1719 int ret;
1720 unsigned char *msg_post = ssl->out_msg;
1721 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001722 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001723
1724 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1725
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001726 if( len_pre == 0 )
1727 return( 0 );
1728
Paul Bakker2770fbd2012-07-03 13:30:23 +00001729 memcpy( msg_pre, ssl->out_msg, len_pre );
1730
1731 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1732 ssl->out_msglen ) );
1733
1734 SSL_DEBUG_BUF( 4, "before compression: output payload",
1735 ssl->out_msg, ssl->out_msglen );
1736
Paul Bakker48916f92012-09-16 19:57:18 +00001737 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1738 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1739 ssl->transform_out->ctx_deflate.next_out = msg_post;
1740 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001741
Paul Bakker48916f92012-09-16 19:57:18 +00001742 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001743 if( ret != Z_OK )
1744 {
1745 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1746 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1747 }
1748
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001749 ssl->out_msglen = SSL_BUFFER_LEN -
1750 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001751
Paul Bakker2770fbd2012-07-03 13:30:23 +00001752 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1753 ssl->out_msglen ) );
1754
1755 SSL_DEBUG_BUF( 4, "after compression: output payload",
1756 ssl->out_msg, ssl->out_msglen );
1757
1758 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1759
1760 return( 0 );
1761}
1762
1763static int ssl_decompress_buf( ssl_context *ssl )
1764{
1765 int ret;
1766 unsigned char *msg_post = ssl->in_msg;
1767 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001768 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001769
1770 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1771
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001772 if( len_pre == 0 )
1773 return( 0 );
1774
Paul Bakker2770fbd2012-07-03 13:30:23 +00001775 memcpy( msg_pre, ssl->in_msg, len_pre );
1776
1777 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1778 ssl->in_msglen ) );
1779
1780 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1781 ssl->in_msg, ssl->in_msglen );
1782
Paul Bakker48916f92012-09-16 19:57:18 +00001783 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1784 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1785 ssl->transform_in->ctx_inflate.next_out = msg_post;
1786 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001787
Paul Bakker48916f92012-09-16 19:57:18 +00001788 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001789 if( ret != Z_OK )
1790 {
1791 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1792 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1793 }
1794
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001795 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1796 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001797
Paul Bakker2770fbd2012-07-03 13:30:23 +00001798 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1799 ssl->in_msglen ) );
1800
1801 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1802 ssl->in_msg, ssl->in_msglen );
1803
1804 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1805
1806 return( 0 );
1807}
1808#endif /* POLARSSL_ZLIB_SUPPORT */
1809
Paul Bakker5121ce52009-01-03 21:22:43 +00001810/*
1811 * Fill the input message buffer
1812 */
Paul Bakker23986e52011-04-24 08:57:21 +00001813int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001814{
Paul Bakker23986e52011-04-24 08:57:21 +00001815 int ret;
1816 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001817
1818 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1819
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001820 if( nb_want > SSL_BUFFER_LEN - 8 )
1821 {
1822 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1823 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1824 }
1825
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 while( ssl->in_left < nb_want )
1827 {
1828 len = nb_want - ssl->in_left;
1829 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1830
1831 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1832 ssl->in_left, nb_want ) );
1833 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1834
Paul Bakker831a7552011-05-18 13:32:51 +00001835 if( ret == 0 )
1836 return( POLARSSL_ERR_SSL_CONN_EOF );
1837
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 if( ret < 0 )
1839 return( ret );
1840
1841 ssl->in_left += ret;
1842 }
1843
1844 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1845
1846 return( 0 );
1847}
1848
1849/*
1850 * Flush any data not yet written
1851 */
1852int ssl_flush_output( ssl_context *ssl )
1853{
1854 int ret;
1855 unsigned char *buf;
1856
1857 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1858
1859 while( ssl->out_left > 0 )
1860 {
1861 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1862 5 + ssl->out_msglen, ssl->out_left ) );
1863
Paul Bakker5bd42292012-12-19 14:40:42 +01001864 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001866
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1868
1869 if( ret <= 0 )
1870 return( ret );
1871
1872 ssl->out_left -= ret;
1873 }
1874
1875 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1876
1877 return( 0 );
1878}
1879
1880/*
1881 * Record layer functions
1882 */
1883int ssl_write_record( ssl_context *ssl )
1884{
Paul Bakker05ef8352012-05-08 09:17:57 +00001885 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001886 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
1888 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1889
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1891 {
1892 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1893 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1894 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1895
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001896 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1897 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 }
1899
Paul Bakker2770fbd2012-07-03 13:30:23 +00001900#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001901 if( ssl->transform_out != NULL &&
1902 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001903 {
1904 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1905 {
1906 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1907 return( ret );
1908 }
1909
1910 len = ssl->out_msglen;
1911 }
1912#endif /*POLARSSL_ZLIB_SUPPORT */
1913
Paul Bakker05ef8352012-05-08 09:17:57 +00001914#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001915 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001917 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
Paul Bakker05ef8352012-05-08 09:17:57 +00001919 ret = ssl_hw_record_write( ssl );
1920 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1921 {
1922 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001923 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001924 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001925
1926 if( ret == 0 )
1927 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001928 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001929#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001930 if( !done )
1931 {
1932 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1933 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1934 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1936 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001937
Paul Bakker48916f92012-09-16 19:57:18 +00001938 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001939 {
1940 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1941 {
1942 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1943 return( ret );
1944 }
1945
1946 len = ssl->out_msglen;
1947 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1948 ssl->out_hdr[4] = (unsigned char)( len );
1949 }
1950
1951 ssl->out_left = 5 + ssl->out_msglen;
1952
1953 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1954 "version = [%d:%d], msglen = %d",
1955 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1956 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1957
1958 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001959 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 }
1961
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1963 {
1964 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1965 return( ret );
1966 }
1967
1968 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1969
1970 return( 0 );
1971}
1972
1973int ssl_read_record( ssl_context *ssl )
1974{
Paul Bakker05ef8352012-05-08 09:17:57 +00001975 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
1977 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1978
1979 if( ssl->in_hslen != 0 &&
1980 ssl->in_hslen < ssl->in_msglen )
1981 {
1982 /*
1983 * Get next Handshake message in the current record
1984 */
1985 ssl->in_msglen -= ssl->in_hslen;
1986
Paul Bakker8934a982011-08-05 11:11:53 +00001987 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001988 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
1990 ssl->in_hslen = 4;
1991 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1992
1993 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1994 " %d, type = %d, hslen = %d",
1995 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1996
1997 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1998 {
1999 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002000 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 }
2002
2003 if( ssl->in_msglen < ssl->in_hslen )
2004 {
2005 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002006 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 }
2008
Paul Bakker4224bc02014-04-08 14:36:50 +02002009 if( ssl->state != SSL_HANDSHAKE_OVER )
2010 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002011
2012 return( 0 );
2013 }
2014
2015 ssl->in_hslen = 0;
2016
2017 /*
2018 * Read the record header and validate it
2019 */
2020 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2021 {
2022 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2023 return( ret );
2024 }
2025
2026 ssl->in_msgtype = ssl->in_hdr[0];
2027 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2028
2029 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2030 "version = [%d:%d], msglen = %d",
2031 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2032 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2033
2034 if( ssl->in_hdr[1] != ssl->major_ver )
2035 {
2036 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002037 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 }
2039
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002040 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 {
2042 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002043 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 }
2045
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002046 /* Sanity check (outer boundaries) */
2047 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2048 {
2049 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2050 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2051 }
2052
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002054 * Make sure the message length is acceptable for the current transform
2055 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 */
Paul Bakker48916f92012-09-16 19:57:18 +00002057 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002059 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 {
2061 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002062 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 }
2064 }
2065 else
2066 {
Paul Bakker48916f92012-09-16 19:57:18 +00002067 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 {
2069 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002070 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 }
2072
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002073#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002075 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 {
2077 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002078 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002080#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002082#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2083 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 /*
2085 * TLS encrypted messages can have up to 256 bytes of padding
2086 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002087 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002088 ssl->in_msglen > ssl->transform_in->minlen +
2089 SSL_MAX_CONTENT_LEN + 256 )
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 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002094#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 }
2096
2097 /*
2098 * Read and optionally decrypt the message contents
2099 */
2100 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2101 {
2102 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2103 return( ret );
2104 }
2105
2106 SSL_DEBUG_BUF( 4, "input record from network",
2107 ssl->in_hdr, 5 + ssl->in_msglen );
2108
Paul Bakker05ef8352012-05-08 09:17:57 +00002109#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002110 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002111 {
2112 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2113
2114 ret = ssl_hw_record_read( ssl );
2115 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2116 {
2117 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002118 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002119 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002120
2121 if( ret == 0 )
2122 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002123 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002124#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002125 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 {
2127 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2128 {
Paul Bakker40865c82013-01-31 17:13:13 +01002129#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2130 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2131 {
2132 ssl_send_alert_message( ssl,
2133 SSL_ALERT_LEVEL_FATAL,
2134 SSL_ALERT_MSG_BAD_RECORD_MAC );
2135 }
2136#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2138 return( ret );
2139 }
2140
2141 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2142 ssl->in_msg, ssl->in_msglen );
2143
2144 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2145 {
2146 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002147 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 }
2149 }
2150
Paul Bakker2770fbd2012-07-03 13:30:23 +00002151#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002152 if( ssl->transform_in != NULL &&
2153 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002154 {
2155 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2156 {
2157 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2158 return( ret );
2159 }
2160
2161 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2162 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2163 }
2164#endif /* POLARSSL_ZLIB_SUPPORT */
2165
Paul Bakker0a925182012-04-16 06:46:41 +00002166 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2167 ssl->in_msgtype != SSL_MSG_ALERT &&
2168 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2169 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2170 {
2171 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2172
Paul Bakker48916f92012-09-16 19:57:18 +00002173 if( ( ret = ssl_send_alert_message( ssl,
2174 SSL_ALERT_LEVEL_FATAL,
2175 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002176 {
2177 return( ret );
2178 }
2179
2180 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2181 }
2182
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2184 {
2185 ssl->in_hslen = 4;
2186 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2187
2188 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2189 " %d, type = %d, hslen = %d",
2190 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2191
2192 /*
2193 * Additional checks to validate the handshake header
2194 */
2195 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2196 {
2197 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002198 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 }
2200
2201 if( ssl->in_msglen < ssl->in_hslen )
2202 {
2203 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002204 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002205 }
2206
Paul Bakker48916f92012-09-16 19:57:18 +00002207 if( ssl->state != SSL_HANDSHAKE_OVER )
2208 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 }
2210
2211 if( ssl->in_msgtype == SSL_MSG_ALERT )
2212 {
2213 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2214 ssl->in_msg[0], ssl->in_msg[1] ) );
2215
2216 /*
2217 * Ignore non-fatal alerts, except close_notify
2218 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002219 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002221 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2222 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002223 /**
2224 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2225 * error identifier.
2226 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002227 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002228 }
2229
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002230 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2231 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 {
2233 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002234 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
2236 }
2237
2238 ssl->in_left = 0;
2239
2240 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2241
2242 return( 0 );
2243}
2244
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002245int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2246{
2247 int ret;
2248
2249 if( ( ret = ssl_send_alert_message( ssl,
2250 SSL_ALERT_LEVEL_FATAL,
2251 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2252 {
2253 return( ret );
2254 }
2255
2256 return( 0 );
2257}
2258
Paul Bakker0a925182012-04-16 06:46:41 +00002259int ssl_send_alert_message( ssl_context *ssl,
2260 unsigned char level,
2261 unsigned char message )
2262{
2263 int ret;
2264
2265 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2266
2267 ssl->out_msgtype = SSL_MSG_ALERT;
2268 ssl->out_msglen = 2;
2269 ssl->out_msg[0] = level;
2270 ssl->out_msg[1] = message;
2271
2272 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2273 {
2274 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2275 return( ret );
2276 }
2277
2278 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2279
2280 return( 0 );
2281}
2282
Paul Bakker5121ce52009-01-03 21:22:43 +00002283/*
2284 * Handshake functions
2285 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002286#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2287 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2288 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2289 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2290 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2291 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2292 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002293int ssl_write_certificate( ssl_context *ssl )
2294{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002295 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
2297 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2298
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002299 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002300 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2301 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002302 {
2303 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2304 ssl->state++;
2305 return( 0 );
2306 }
2307
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002308 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2309 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002310}
2311
2312int ssl_parse_certificate( ssl_context *ssl )
2313{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002314 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2315
2316 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2317
2318 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002319 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2320 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002321 {
2322 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2323 ssl->state++;
2324 return( 0 );
2325 }
2326
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002327 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2328 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002329}
2330#else
2331int ssl_write_certificate( ssl_context *ssl )
2332{
2333 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2334 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002335 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002336 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2337
2338 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2339
2340 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002341 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2342 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002343 {
2344 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2345 ssl->state++;
2346 return( 0 );
2347 }
2348
Paul Bakker5121ce52009-01-03 21:22:43 +00002349 if( ssl->endpoint == SSL_IS_CLIENT )
2350 {
2351 if( ssl->client_auth == 0 )
2352 {
2353 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2354 ssl->state++;
2355 return( 0 );
2356 }
2357
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002358#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002359 /*
2360 * If using SSLv3 and got no cert, send an Alert message
2361 * (otherwise an empty Certificate message will be sent).
2362 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002363 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2365 {
2366 ssl->out_msglen = 2;
2367 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002368 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2369 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002370
2371 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2372 goto write_msg;
2373 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002374#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 }
2376 else /* SSL_IS_SERVER */
2377 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002378 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 {
2380 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002381 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 }
2383 }
2384
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002385 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
2387 /*
2388 * 0 . 0 handshake type
2389 * 1 . 3 handshake length
2390 * 4 . 6 length of all certs
2391 * 7 . 9 length of cert. 1
2392 * 10 . n-1 peer certificate
2393 * n . n+2 length of cert. 2
2394 * n+3 . ... upper level cert, etc.
2395 */
2396 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002397 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Paul Bakker29087132010-03-21 21:03:34 +00002399 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 {
2401 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002402 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 {
2404 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2405 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002406 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 }
2408
2409 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2410 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2411 ssl->out_msg[i + 2] = (unsigned char)( n );
2412
2413 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2414 i += n; crt = crt->next;
2415 }
2416
2417 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2418 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2419 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2420
2421 ssl->out_msglen = i;
2422 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2423 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2424
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002425#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002426write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002427#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
2429 ssl->state++;
2430
2431 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2432 {
2433 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2434 return( ret );
2435 }
2436
2437 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2438
Paul Bakkered27a042013-04-18 22:46:23 +02002439 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440}
2441
2442int ssl_parse_certificate( ssl_context *ssl )
2443{
Paul Bakkered27a042013-04-18 22:46:23 +02002444 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002445 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002446 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002447
2448 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2449
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002450 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002451 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2452 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002453 {
2454 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2455 ssl->state++;
2456 return( 0 );
2457 }
2458
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002460 ( ssl->authmode == SSL_VERIFY_NONE ||
2461 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002463 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2465 ssl->state++;
2466 return( 0 );
2467 }
2468
2469 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2470 {
2471 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2472 return( ret );
2473 }
2474
2475 ssl->state++;
2476
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002477#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 /*
2479 * Check if the client sent an empty certificate
2480 */
2481 if( ssl->endpoint == SSL_IS_SERVER &&
2482 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2483 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002484 if( ssl->in_msglen == 2 &&
2485 ssl->in_msgtype == SSL_MSG_ALERT &&
2486 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2487 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 {
2489 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2490
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002491 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2493 return( 0 );
2494 else
Paul Bakker40e46942009-01-03 21:51:57 +00002495 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 }
2497 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002498#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002500#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2501 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 if( ssl->endpoint == SSL_IS_SERVER &&
2503 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2504 {
2505 if( ssl->in_hslen == 7 &&
2506 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2507 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2508 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2509 {
2510 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2511
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002512 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002514 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 else
2516 return( 0 );
2517 }
2518 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002519#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2520 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
2522 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2523 {
2524 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002525 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 }
2527
2528 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2529 {
2530 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002531 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 }
2533
2534 /*
2535 * Same message structure as in ssl_write_certificate()
2536 */
2537 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2538
2539 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2540 {
2541 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002542 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 }
2544
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002545 /* In case we tried to reuse a session but it failed */
2546 if( ssl->session_negotiate->peer_cert != NULL )
2547 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002548 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002549 polarssl_free( ssl->session_negotiate->peer_cert );
2550 }
2551
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002552 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2553 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 {
2555 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002556 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002557 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 }
2559
Paul Bakkerb6b09562013-09-18 14:17:41 +02002560 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
2562 i = 7;
2563
2564 while( i < ssl->in_hslen )
2565 {
2566 if( ssl->in_msg[i] != 0 )
2567 {
2568 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002569 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 }
2571
2572 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2573 | (unsigned int) ssl->in_msg[i + 2];
2574 i += 3;
2575
2576 if( n < 128 || i + n > ssl->in_hslen )
2577 {
2578 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002579 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 }
2581
Paul Bakkerddf26b42013-09-18 13:46:23 +02002582 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2583 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 if( ret != 0 )
2585 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002586 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 return( ret );
2588 }
2589
2590 i += n;
2591 }
2592
Paul Bakker48916f92012-09-16 19:57:18 +00002593 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002595 /*
2596 * On client, make sure the server cert doesn't change during renego to
2597 * avoid "triple handshake" attack: https://secure-resumption.com/
2598 */
2599 if( ssl->endpoint == SSL_IS_CLIENT &&
2600 ssl->renegotiation == SSL_RENEGOTIATION )
2601 {
2602 if( ssl->session->peer_cert == NULL )
2603 {
2604 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2605 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2606 }
2607
2608 if( ssl->session->peer_cert->raw.len !=
2609 ssl->session_negotiate->peer_cert->raw.len ||
2610 memcmp( ssl->session->peer_cert->raw.p,
2611 ssl->session_negotiate->peer_cert->raw.p,
2612 ssl->session->peer_cert->raw.len ) != 0 )
2613 {
2614 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2615 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2616 }
2617 }
2618
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 if( ssl->authmode != SSL_VERIFY_NONE )
2620 {
2621 if( ssl->ca_chain == NULL )
2622 {
2623 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002624 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
2626
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002627 /*
2628 * Main check: verify certificate
2629 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002630 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2631 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2632 &ssl->session_negotiate->verify_result,
2633 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
2635 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002636 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002638 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002639
2640 /*
2641 * Secondary checks: always done, but change 'ret' only if it was 0
2642 */
2643
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002644#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002645 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002646 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002647
2648 /* If certificate uses an EC key, make sure the curve is OK */
2649 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2650 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2651 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002652 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002653 if( ret == 0 )
2654 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002655 }
2656 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002657#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002659 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2660 ciphersuite_info,
2661 ! ssl->endpoint ) != 0 )
2662 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002663 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002664 if( ret == 0 )
2665 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2666 }
2667
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2669 ret = 0;
2670 }
2671
2672 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2673
2674 return( ret );
2675}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002676#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2677 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2678 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2679 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2680 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2681 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2682 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
2684int ssl_write_change_cipher_spec( ssl_context *ssl )
2685{
2686 int ret;
2687
2688 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2689
2690 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2691 ssl->out_msglen = 1;
2692 ssl->out_msg[0] = 1;
2693
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 ssl->state++;
2695
2696 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2697 {
2698 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2699 return( ret );
2700 }
2701
2702 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2703
2704 return( 0 );
2705}
2706
2707int ssl_parse_change_cipher_spec( ssl_context *ssl )
2708{
2709 int ret;
2710
2711 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2712
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2714 {
2715 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2716 return( ret );
2717 }
2718
2719 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2720 {
2721 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002722 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 }
2724
2725 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2726 {
2727 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002728 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 }
2730
2731 ssl->state++;
2732
2733 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2734
2735 return( 0 );
2736}
2737
Paul Bakker41c83d32013-03-20 14:39:14 +01002738void ssl_optimize_checksum( ssl_context *ssl,
2739 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002740{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002741 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002742
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002743#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2744 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002745 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002746 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002747 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002748#endif
2749#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2750#if defined(POLARSSL_SHA512_C)
2751 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2752 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2753 else
2754#endif
2755#if defined(POLARSSL_SHA256_C)
2756 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002757 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002758 else
2759#endif
2760#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002761 {
2762 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002763 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002764 }
Paul Bakker380da532012-04-18 16:10:25 +00002765}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002766
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002767static void ssl_update_checksum_start( ssl_context *ssl,
2768 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002769{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002770#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2771 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002772 md5_update( &ssl->handshake->fin_md5 , buf, len );
2773 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002774#endif
2775#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2776#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002777 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002778#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002779#if defined(POLARSSL_SHA512_C)
2780 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002781#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002782#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002783}
2784
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002785#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2786 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002787static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2788 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002789{
Paul Bakker48916f92012-09-16 19:57:18 +00002790 md5_update( &ssl->handshake->fin_md5 , buf, len );
2791 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002792}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002793#endif
Paul Bakker380da532012-04-18 16:10:25 +00002794
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002795#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2796#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002797static void ssl_update_checksum_sha256( ssl_context *ssl,
2798 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002799{
Paul Bakker9e36f042013-06-30 14:34:05 +02002800 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002801}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002802#endif
Paul Bakker380da532012-04-18 16:10:25 +00002803
Paul Bakker9e36f042013-06-30 14:34:05 +02002804#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002805static void ssl_update_checksum_sha384( ssl_context *ssl,
2806 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002807{
Paul Bakker9e36f042013-06-30 14:34:05 +02002808 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002809}
Paul Bakker769075d2012-11-24 11:26:46 +01002810#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002811#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002812
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002813#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002814static void ssl_calc_finished_ssl(
2815 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002816{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002817 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002818 md5_context md5;
2819 sha1_context sha1;
2820
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 unsigned char padbuf[48];
2822 unsigned char md5sum[16];
2823 unsigned char sha1sum[20];
2824
Paul Bakker48916f92012-09-16 19:57:18 +00002825 ssl_session *session = ssl->session_negotiate;
2826 if( !session )
2827 session = ssl->session;
2828
Paul Bakker1ef83d62012-04-11 12:09:53 +00002829 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2830
Paul Bakker48916f92012-09-16 19:57:18 +00002831 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2832 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
2834 /*
2835 * SSLv3:
2836 * hash =
2837 * MD5( master + pad2 +
2838 * MD5( handshake + sender + master + pad1 ) )
2839 * + SHA1( master + pad2 +
2840 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 */
2842
Paul Bakker90995b52013-06-24 19:20:35 +02002843#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002844 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002845 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002846#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Paul Bakker90995b52013-06-24 19:20:35 +02002848#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002849 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002850 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002851#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
Paul Bakker3c2122f2013-06-24 19:03:14 +02002853 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2854 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Paul Bakker1ef83d62012-04-11 12:09:53 +00002856 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002857
Paul Bakker3c2122f2013-06-24 19:03:14 +02002858 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002859 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002860 md5_update( &md5, padbuf, 48 );
2861 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Paul Bakker3c2122f2013-06-24 19:03:14 +02002863 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002864 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865 sha1_update( &sha1, padbuf, 40 );
2866 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Paul Bakker1ef83d62012-04-11 12:09:53 +00002868 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002869
Paul Bakker1ef83d62012-04-11 12:09:53 +00002870 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002871 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002872 md5_update( &md5, padbuf, 48 );
2873 md5_update( &md5, md5sum, 16 );
2874 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875
Paul Bakker1ef83d62012-04-11 12:09:53 +00002876 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002877 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002878 sha1_update( &sha1, padbuf , 40 );
2879 sha1_update( &sha1, sha1sum, 20 );
2880 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Paul Bakker1ef83d62012-04-11 12:09:53 +00002882 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Paul Bakker34617722014-06-13 17:20:13 +02002884 polarssl_zeroize( &md5, sizeof( md5_context ) );
2885 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
Paul Bakker34617722014-06-13 17:20:13 +02002887 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2888 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2889 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002890
2891 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2892}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002893#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002895#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896static void ssl_calc_finished_tls(
2897 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002898{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002899 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002900 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002901 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002902 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002903 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002904
Paul Bakker48916f92012-09-16 19:57:18 +00002905 ssl_session *session = ssl->session_negotiate;
2906 if( !session )
2907 session = ssl->session;
2908
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
Paul Bakker48916f92012-09-16 19:57:18 +00002911 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2912 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
Paul Bakker1ef83d62012-04-11 12:09:53 +00002914 /*
2915 * TLSv1:
2916 * hash = PRF( master, finished_label,
2917 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2918 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002919
Paul Bakker90995b52013-06-24 19:20:35 +02002920#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002921 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2922 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002923#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002924
Paul Bakker90995b52013-06-24 19:20:35 +02002925#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002926 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2927 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002928#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002929
2930 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002931 ? "client finished"
2932 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002933
2934 md5_finish( &md5, padbuf );
2935 sha1_finish( &sha1, padbuf + 16 );
2936
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002937 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002938 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002939
2940 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2941
Paul Bakker34617722014-06-13 17:20:13 +02002942 polarssl_zeroize( &md5, sizeof( md5_context ) );
2943 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002944
Paul Bakker34617722014-06-13 17:20:13 +02002945 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002946
2947 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2948}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002949#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002950
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002951#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2952#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002953static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954 ssl_context *ssl, unsigned char *buf, int from )
2955{
2956 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002957 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002958 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002959 unsigned char padbuf[32];
2960
Paul Bakker48916f92012-09-16 19:57:18 +00002961 ssl_session *session = ssl->session_negotiate;
2962 if( !session )
2963 session = ssl->session;
2964
Paul Bakker380da532012-04-18 16:10:25 +00002965 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002966
Paul Bakker9e36f042013-06-30 14:34:05 +02002967 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968
2969 /*
2970 * TLSv1.2:
2971 * hash = PRF( master, finished_label,
2972 * Hash( handshake ) )[0.11]
2973 */
2974
Paul Bakker9e36f042013-06-30 14:34:05 +02002975#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002977 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002978#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979
2980 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002981 ? "client finished"
2982 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002983
Paul Bakker9e36f042013-06-30 14:34:05 +02002984 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002985
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002986 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002987 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002988
2989 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2990
Paul Bakker34617722014-06-13 17:20:13 +02002991 polarssl_zeroize( &sha256, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002992
Paul Bakker34617722014-06-13 17:20:13 +02002993 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994
2995 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2996}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002997#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002998
Paul Bakker9e36f042013-06-30 14:34:05 +02002999#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000static void ssl_calc_finished_tls_sha384(
3001 ssl_context *ssl, unsigned char *buf, int from )
3002{
3003 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003004 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003005 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003006 unsigned char padbuf[48];
3007
Paul Bakker48916f92012-09-16 19:57:18 +00003008 ssl_session *session = ssl->session_negotiate;
3009 if( !session )
3010 session = ssl->session;
3011
Paul Bakker380da532012-04-18 16:10:25 +00003012 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003013
Paul Bakker9e36f042013-06-30 14:34:05 +02003014 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003015
3016 /*
3017 * TLSv1.2:
3018 * hash = PRF( master, finished_label,
3019 * Hash( handshake ) )[0.11]
3020 */
3021
Paul Bakker9e36f042013-06-30 14:34:05 +02003022#if !defined(POLARSSL_SHA512_ALT)
3023 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3024 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003025#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003026
3027 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003028 ? "client finished"
3029 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003030
Paul Bakker9e36f042013-06-30 14:34:05 +02003031 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003032
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003033 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003034 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003035
3036 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3037
Paul Bakker34617722014-06-13 17:20:13 +02003038 polarssl_zeroize( &sha512, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003039
Paul Bakker34617722014-06-13 17:20:13 +02003040 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003041
3042 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3043}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003044#endif /* POLARSSL_SHA512_C */
3045#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003046
Paul Bakker48916f92012-09-16 19:57:18 +00003047void ssl_handshake_wrapup( ssl_context *ssl )
3048{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003049 int resume = ssl->handshake->resume;
3050
Paul Bakker48916f92012-09-16 19:57:18 +00003051 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3052
3053 /*
3054 * Free our handshake params
3055 */
3056 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003057 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003058 ssl->handshake = NULL;
3059
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003060 if( ssl->renegotiation == SSL_RENEGOTIATION )
3061 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3062
Paul Bakker48916f92012-09-16 19:57:18 +00003063 /*
3064 * Switch in our now active transform context
3065 */
3066 if( ssl->transform )
3067 {
3068 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003069 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003070 }
3071 ssl->transform = ssl->transform_negotiate;
3072 ssl->transform_negotiate = NULL;
3073
Paul Bakker0a597072012-09-25 21:55:46 +00003074 if( ssl->session )
3075 {
3076 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003077 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003078 }
3079 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003080 ssl->session_negotiate = NULL;
3081
Paul Bakker0a597072012-09-25 21:55:46 +00003082 /*
3083 * Add cache entry
3084 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003085 if( ssl->f_set_cache != NULL &&
3086 ssl->session->length != 0 &&
3087 resume == 0 )
3088 {
Paul Bakker0a597072012-09-25 21:55:46 +00003089 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3090 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003091 }
Paul Bakker0a597072012-09-25 21:55:46 +00003092
Paul Bakker48916f92012-09-16 19:57:18 +00003093 ssl->state++;
3094
3095 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3096}
3097
Paul Bakker1ef83d62012-04-11 12:09:53 +00003098int ssl_write_finished( ssl_context *ssl )
3099{
3100 int ret, hash_len;
3101
3102 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3103
Paul Bakker92be97b2013-01-02 17:30:03 +01003104 /*
3105 * Set the out_msg pointer to the correct location based on IV length
3106 */
3107 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3108 {
3109 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3110 ssl->transform_negotiate->fixed_ivlen;
3111 }
3112 else
3113 ssl->out_msg = ssl->out_iv;
3114
Paul Bakker48916f92012-09-16 19:57:18 +00003115 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003116
3117 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003118 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3119
Paul Bakker48916f92012-09-16 19:57:18 +00003120 ssl->verify_data_len = hash_len;
3121 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3122
Paul Bakker5121ce52009-01-03 21:22:43 +00003123 ssl->out_msglen = 4 + hash_len;
3124 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3125 ssl->out_msg[0] = SSL_HS_FINISHED;
3126
3127 /*
3128 * In case of session resuming, invert the client and server
3129 * ChangeCipherSpec messages order.
3130 */
Paul Bakker0a597072012-09-25 21:55:46 +00003131 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 {
3133 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003134 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003135 else
3136 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3137 }
3138 else
3139 ssl->state++;
3140
Paul Bakker48916f92012-09-16 19:57:18 +00003141 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003142 * Switch to our negotiated transform and session parameters for outbound
3143 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003144 */
3145 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3146 ssl->transform_out = ssl->transform_negotiate;
3147 ssl->session_out = ssl->session_negotiate;
3148 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003149
Paul Bakker07eb38b2012-12-19 14:42:06 +01003150#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003151 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003152 {
3153 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3154 {
3155 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3156 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3157 }
3158 }
3159#endif
3160
Paul Bakker5121ce52009-01-03 21:22:43 +00003161 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3162 {
3163 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3164 return( ret );
3165 }
3166
3167 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3168
3169 return( 0 );
3170}
3171
3172int ssl_parse_finished( ssl_context *ssl )
3173{
Paul Bakker23986e52011-04-24 08:57:21 +00003174 int ret;
3175 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 unsigned char buf[36];
3177
3178 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3179
Paul Bakker48916f92012-09-16 19:57:18 +00003180 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Paul Bakker48916f92012-09-16 19:57:18 +00003182 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003183 * Switch to our negotiated transform and session parameters for inbound
3184 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003185 */
3186 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3187 ssl->transform_in = ssl->transform_negotiate;
3188 ssl->session_in = ssl->session_negotiate;
3189 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003190
Paul Bakker92be97b2013-01-02 17:30:03 +01003191 /*
3192 * Set the in_msg pointer to the correct location based on IV length
3193 */
3194 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3195 {
3196 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3197 ssl->transform_negotiate->fixed_ivlen;
3198 }
3199 else
3200 ssl->in_msg = ssl->in_iv;
3201
Paul Bakker07eb38b2012-12-19 14:42:06 +01003202#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003203 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003204 {
3205 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3206 {
3207 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3208 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3209 }
3210 }
3211#endif
3212
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3214 {
3215 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3216 return( ret );
3217 }
3218
3219 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3220 {
3221 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003222 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 }
3224
Paul Bakker1ef83d62012-04-11 12:09:53 +00003225 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3227
3228 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3229 ssl->in_hslen != 4 + hash_len )
3230 {
3231 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003232 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 }
3234
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003235 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 {
3237 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003238 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 }
3240
Paul Bakker48916f92012-09-16 19:57:18 +00003241 ssl->verify_data_len = hash_len;
3242 memcpy( ssl->peer_verify_data, buf, hash_len );
3243
Paul Bakker0a597072012-09-25 21:55:46 +00003244 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 {
3246 if( ssl->endpoint == SSL_IS_CLIENT )
3247 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3248
3249 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003250 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 }
3252 else
3253 ssl->state++;
3254
3255 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3256
3257 return( 0 );
3258}
3259
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003260static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003261{
3262 if( ssl->transform_negotiate )
3263 ssl_transform_free( ssl->transform_negotiate );
3264 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003265 {
3266 ssl->transform_negotiate =
3267 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003268
3269 if( ssl->transform_negotiate != NULL )
3270 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003271 }
Paul Bakker48916f92012-09-16 19:57:18 +00003272
3273 if( ssl->session_negotiate )
3274 ssl_session_free( ssl->session_negotiate );
3275 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003276 {
3277 ssl->session_negotiate =
3278 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003279
3280 if( ssl->session_negotiate != NULL )
3281 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003282 }
Paul Bakker48916f92012-09-16 19:57:18 +00003283
3284 if( ssl->handshake )
3285 ssl_handshake_free( ssl->handshake );
3286 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003287 {
3288 ssl->handshake = (ssl_handshake_params *)
3289 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003290
3291 if( ssl->handshake != NULL )
3292 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003293 }
Paul Bakker48916f92012-09-16 19:57:18 +00003294
3295 if( ssl->handshake == NULL ||
3296 ssl->transform_negotiate == NULL ||
3297 ssl->session_negotiate == NULL )
3298 {
3299 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3300 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3301 }
3302
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003303#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3304 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003305 md5_starts( &ssl->handshake->fin_md5 );
3306 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003307#endif
3308#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3309#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003310 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003311#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003312#if defined(POLARSSL_SHA512_C)
3313 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003314#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003315#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003316
3317 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003318 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003319
Paul Bakker61d113b2013-07-04 11:51:43 +02003320#if defined(POLARSSL_ECDH_C)
3321 ecdh_init( &ssl->handshake->ecdh_ctx );
3322#endif
3323
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003324#if defined(POLARSSL_X509_CRT_PARSE_C)
3325 ssl->handshake->key_cert = ssl->key_cert;
3326#endif
3327
Paul Bakker48916f92012-09-16 19:57:18 +00003328 return( 0 );
3329}
3330
Paul Bakker5121ce52009-01-03 21:22:43 +00003331/*
3332 * Initialize an SSL context
3333 */
3334int ssl_init( ssl_context *ssl )
3335{
Paul Bakker48916f92012-09-16 19:57:18 +00003336 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 int len = SSL_BUFFER_LEN;
3338
3339 memset( ssl, 0, sizeof( ssl_context ) );
3340
Paul Bakker62f2dee2012-09-28 07:31:51 +00003341 /*
3342 * Sane defaults
3343 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003344 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3345 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3346 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3347 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003348
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003349 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003350
Paul Bakker62f2dee2012-09-28 07:31:51 +00003351#if defined(POLARSSL_DHM_C)
3352 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3353 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3354 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3355 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3356 {
3357 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3358 return( ret );
3359 }
3360#endif
3361
3362 /*
3363 * Prepare base structures
3364 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003365 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003367 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003368 ssl->in_msg = ssl->in_ctr + 13;
3369
3370 if( ssl->in_ctr == NULL )
3371 {
3372 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003373 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 }
3375
Paul Bakker6e339b52013-07-03 13:37:05 +02003376 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003378 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003379 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003380
3381 if( ssl->out_ctr == NULL )
3382 {
3383 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003384 polarssl_free( ssl->in_ctr );
3385 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003386 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003387 }
3388
3389 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3390 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3391
Paul Bakker606b4ba2013-08-14 16:52:14 +02003392#if defined(POLARSSL_SSL_SESSION_TICKETS)
3393 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3394#endif
3395
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003396#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003397 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003398#endif
3399
Paul Bakker48916f92012-09-16 19:57:18 +00003400 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3401 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003402
3403 return( 0 );
3404}
3405
3406/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003407 * Reset an initialized and used SSL context for re-use while retaining
3408 * all application-set variables, function pointers and data.
3409 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003410int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003411{
Paul Bakker48916f92012-09-16 19:57:18 +00003412 int ret;
3413
Paul Bakker7eb013f2011-10-06 12:37:39 +00003414 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003415 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3416 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3417
3418 ssl->verify_data_len = 0;
3419 memset( ssl->own_verify_data, 0, 36 );
3420 memset( ssl->peer_verify_data, 0, 36 );
3421
Paul Bakker7eb013f2011-10-06 12:37:39 +00003422 ssl->in_offt = NULL;
3423
Paul Bakker92be97b2013-01-02 17:30:03 +01003424 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003425 ssl->in_msgtype = 0;
3426 ssl->in_msglen = 0;
3427 ssl->in_left = 0;
3428
3429 ssl->in_hslen = 0;
3430 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003431 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003432
Paul Bakker92be97b2013-01-02 17:30:03 +01003433 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003434 ssl->out_msgtype = 0;
3435 ssl->out_msglen = 0;
3436 ssl->out_left = 0;
3437
Paul Bakker48916f92012-09-16 19:57:18 +00003438 ssl->transform_in = NULL;
3439 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003440
3441 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3442 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003443
3444#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003445 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003446 {
3447 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003448 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003449 {
3450 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3451 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3452 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003453 }
3454#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003455
Paul Bakker48916f92012-09-16 19:57:18 +00003456 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003457 {
Paul Bakker48916f92012-09-16 19:57:18 +00003458 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003459 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003460 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003461 }
Paul Bakker48916f92012-09-16 19:57:18 +00003462
Paul Bakkerc0463502013-02-14 11:19:38 +01003463 if( ssl->session )
3464 {
3465 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003466 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003467 ssl->session = NULL;
3468 }
3469
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003470#if defined(POLARSSL_SSL_ALPN)
3471 ssl->alpn_chosen = NULL;
3472#endif
3473
Paul Bakker48916f92012-09-16 19:57:18 +00003474 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3475 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003476
3477 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003478}
3479
Paul Bakkera503a632013-08-14 13:48:06 +02003480#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003481/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003482 * Allocate and initialize ticket keys
3483 */
3484static int ssl_ticket_keys_init( ssl_context *ssl )
3485{
3486 int ret;
3487 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003488 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003489
3490 if( ssl->ticket_keys != NULL )
3491 return( 0 );
3492
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003493 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3494 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003495 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3496
3497 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003498 {
3499 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003500 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003501 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003502
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003503 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3504 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3505 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3506 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003507 polarssl_free( tkeys );
3508 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003509 }
3510
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003511 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003512 {
3513 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003514 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003515 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003516
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003517 ssl->ticket_keys = tkeys;
3518
3519 return( 0 );
3520}
Paul Bakkera503a632013-08-14 13:48:06 +02003521#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003522
3523/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003524 * SSL set accessors
3525 */
3526void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3527{
3528 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003529
Paul Bakker606b4ba2013-08-14 16:52:14 +02003530#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003531 if( endpoint == SSL_IS_CLIENT )
3532 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003533#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003534}
3535
3536void ssl_set_authmode( ssl_context *ssl, int authmode )
3537{
3538 ssl->authmode = authmode;
3539}
3540
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003541#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003542void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003543 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003544 void *p_vrfy )
3545{
3546 ssl->f_vrfy = f_vrfy;
3547 ssl->p_vrfy = p_vrfy;
3548}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003549#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003550
Paul Bakker5121ce52009-01-03 21:22:43 +00003551void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003552 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003553 void *p_rng )
3554{
3555 ssl->f_rng = f_rng;
3556 ssl->p_rng = p_rng;
3557}
3558
3559void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003560 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 void *p_dbg )
3562{
3563 ssl->f_dbg = f_dbg;
3564 ssl->p_dbg = p_dbg;
3565}
3566
3567void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003568 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003569 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003570{
3571 ssl->f_recv = f_recv;
3572 ssl->f_send = f_send;
3573 ssl->p_recv = p_recv;
3574 ssl->p_send = p_send;
3575}
3576
Paul Bakker0a597072012-09-25 21:55:46 +00003577void ssl_set_session_cache( ssl_context *ssl,
3578 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3579 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003580{
Paul Bakker0a597072012-09-25 21:55:46 +00003581 ssl->f_get_cache = f_get_cache;
3582 ssl->p_get_cache = p_get_cache;
3583 ssl->f_set_cache = f_set_cache;
3584 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003585}
3586
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003587int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003588{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003589 int ret;
3590
3591 if( ssl == NULL ||
3592 session == NULL ||
3593 ssl->session_negotiate == NULL ||
3594 ssl->endpoint != SSL_IS_CLIENT )
3595 {
3596 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3597 }
3598
3599 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3600 return( ret );
3601
Paul Bakker0a597072012-09-25 21:55:46 +00003602 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003603
3604 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003605}
3606
Paul Bakkerb68cad62012-08-23 08:34:18 +00003607void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003608{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003609 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3610 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3611 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3612 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3613}
3614
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003615void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3616 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003617 int major, int minor )
3618{
3619 if( major != SSL_MAJOR_VERSION_3 )
3620 return;
3621
3622 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3623 return;
3624
3625 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003626}
3627
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003628#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003629/* Add a new (empty) key_cert entry an return a pointer to it */
3630static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3631{
3632 ssl_key_cert *key_cert, *last;
3633
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003634 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3635 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003636 return( NULL );
3637
3638 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3639
3640 /* Append the new key_cert to the (possibly empty) current list */
3641 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003642 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003643 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003644 if( ssl->handshake != NULL )
3645 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003646 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003647 else
3648 {
3649 last = ssl->key_cert;
3650 while( last->next != NULL )
3651 last = last->next;
3652 last->next = key_cert;
3653 }
3654
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003655 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003656}
3657
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003658void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003659 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003660{
3661 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003662 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003663 ssl->peer_cn = peer_cn;
3664}
3665
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003666int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003667 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003668{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003669 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3670
3671 if( key_cert == NULL )
3672 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3673
3674 key_cert->cert = own_cert;
3675 key_cert->key = pk_key;
3676
3677 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003678}
3679
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003680#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003681int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003682 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003683{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003684 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003685 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003686
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003687 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003688 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3689
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003690 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3691 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003692 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003693
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003694 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003695
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003696 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003697 if( ret != 0 )
3698 return( ret );
3699
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003700 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003701 return( ret );
3702
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003703 key_cert->cert = own_cert;
3704 key_cert->key_own_alloc = 1;
3705
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003706 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003707}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003708#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003709
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003710int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003711 void *rsa_key,
3712 rsa_decrypt_func rsa_decrypt,
3713 rsa_sign_func rsa_sign,
3714 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003715{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003716 int ret;
3717 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003718
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003719 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003720 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3721
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003722 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3723 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003724 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003725
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003726 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003727
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003728 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3729 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3730 return( ret );
3731
3732 key_cert->cert = own_cert;
3733 key_cert->key_own_alloc = 1;
3734
3735 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003736}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003737#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003738
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003739#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003740int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3741 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003742{
Paul Bakker6db455e2013-09-18 17:29:31 +02003743 if( psk == NULL || psk_identity == NULL )
3744 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3745
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003746 /*
3747 * The length will be check later anyway, but in case it is obviously
3748 * too large, better abort now. The PMS is as follows:
3749 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3750 */
3751 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3752 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3753
Paul Bakker6db455e2013-09-18 17:29:31 +02003754 if( ssl->psk != NULL )
3755 {
3756 polarssl_free( ssl->psk );
3757 polarssl_free( ssl->psk_identity );
3758 }
3759
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003760 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003761 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003762
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003763 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003764 ssl->psk_identity = (unsigned char *)
3765 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003766
3767 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3768 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3769
3770 memcpy( ssl->psk, psk, ssl->psk_len );
3771 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003772
3773 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003774}
3775
3776void ssl_set_psk_cb( ssl_context *ssl,
3777 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3778 size_t),
3779 void *p_psk )
3780{
3781 ssl->f_psk = f_psk;
3782 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003783}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003784#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003785
Paul Bakker48916f92012-09-16 19:57:18 +00003786#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003787int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003788{
3789 int ret;
3790
Paul Bakker48916f92012-09-16 19:57:18 +00003791 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003792 {
3793 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3794 return( ret );
3795 }
3796
Paul Bakker48916f92012-09-16 19:57:18 +00003797 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003798 {
3799 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3800 return( ret );
3801 }
3802
3803 return( 0 );
3804}
3805
Paul Bakker1b57b062011-01-06 15:48:19 +00003806int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3807{
3808 int ret;
3809
Paul Bakker66d5d072014-06-17 16:39:18 +02003810 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003811 {
3812 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3813 return( ret );
3814 }
3815
Paul Bakker66d5d072014-06-17 16:39:18 +02003816 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003817 {
3818 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3819 return( ret );
3820 }
3821
3822 return( 0 );
3823}
Paul Bakker48916f92012-09-16 19:57:18 +00003824#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003825
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003826#if defined(POLARSSL_SSL_SET_CURVES)
3827/*
3828 * Set the allowed elliptic curves
3829 */
3830void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3831{
3832 ssl->curve_list = curve_list;
3833}
3834#endif
3835
Paul Bakker0be444a2013-08-27 21:55:01 +02003836#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003837int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003838{
3839 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003840 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003841
3842 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003843
3844 if( ssl->hostname_len + 1 == 0 )
3845 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3846
Paul Bakker6e339b52013-07-03 13:37:05 +02003847 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003848
Paul Bakkerb15b8512012-01-13 13:44:06 +00003849 if( ssl->hostname == NULL )
3850 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3851
Paul Bakker3c2122f2013-06-24 19:03:14 +02003852 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003853 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003854
Paul Bakker40ea7de2009-05-03 10:18:48 +00003855 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003856
3857 return( 0 );
3858}
3859
Paul Bakker5701cdc2012-09-27 21:49:42 +00003860void ssl_set_sni( ssl_context *ssl,
3861 int (*f_sni)(void *, ssl_context *,
3862 const unsigned char *, size_t),
3863 void *p_sni )
3864{
3865 ssl->f_sni = f_sni;
3866 ssl->p_sni = p_sni;
3867}
Paul Bakker0be444a2013-08-27 21:55:01 +02003868#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003869
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003870#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003871int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003872{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003873 size_t cur_len, tot_len;
3874 const char **p;
3875
3876 /*
3877 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3878 * truncated". Check lengths now rather than later.
3879 */
3880 tot_len = 0;
3881 for( p = protos; *p != NULL; p++ )
3882 {
3883 cur_len = strlen( *p );
3884 tot_len += cur_len;
3885
3886 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3887 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3888 }
3889
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003890 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003891
3892 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003893}
3894
3895const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3896{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003897 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003898}
3899#endif /* POLARSSL_SSL_ALPN */
3900
Paul Bakker490ecc82011-10-06 13:04:09 +00003901void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3902{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003903 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3904 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3905 {
3906 ssl->max_major_ver = major;
3907 ssl->max_minor_ver = minor;
3908 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003909}
3910
Paul Bakker1d29fb52012-09-28 13:28:45 +00003911void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3912{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003913 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3914 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3915 {
3916 ssl->min_major_ver = major;
3917 ssl->min_minor_ver = minor;
3918 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003919}
3920
Paul Bakker05decb22013-08-15 13:33:48 +02003921#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003922int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3923{
Paul Bakker77e257e2013-12-16 15:29:52 +01003924 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003925 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003926 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003927 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003928 }
3929
3930 ssl->mfl_code = mfl_code;
3931
3932 return( 0 );
3933}
Paul Bakker05decb22013-08-15 13:33:48 +02003934#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003935
Paul Bakker1f2bc622013-08-15 13:45:55 +02003936#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003937int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003938{
3939 if( ssl->endpoint != SSL_IS_CLIENT )
3940 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3941
Paul Bakker8c1ede62013-07-19 14:14:37 +02003942 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003943
3944 return( 0 );
3945}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003946#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003947
Paul Bakker48916f92012-09-16 19:57:18 +00003948void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3949{
3950 ssl->disable_renegotiation = renegotiation;
3951}
3952
3953void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3954{
3955 ssl->allow_legacy_renegotiation = allow_legacy;
3956}
3957
Paul Bakkera503a632013-08-14 13:48:06 +02003958#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003959int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3960{
3961 ssl->session_tickets = use_tickets;
3962
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003963 if( ssl->endpoint == SSL_IS_CLIENT )
3964 return( 0 );
3965
3966 if( ssl->f_rng == NULL )
3967 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3968
3969 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003970}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003971
3972void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3973{
3974 ssl->ticket_lifetime = lifetime;
3975}
Paul Bakkera503a632013-08-14 13:48:06 +02003976#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003977
Paul Bakker5121ce52009-01-03 21:22:43 +00003978/*
3979 * SSL get accessors
3980 */
Paul Bakker23986e52011-04-24 08:57:21 +00003981size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003982{
3983 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3984}
3985
Paul Bakkerff60ee62010-03-16 21:09:09 +00003986int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003987{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003988 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003989}
3990
Paul Bakkere3166ce2011-01-27 17:40:50 +00003991const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003992{
Paul Bakker926c8e42013-03-06 10:23:34 +01003993 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003994 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003995
Paul Bakkere3166ce2011-01-27 17:40:50 +00003996 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003997}
3998
Paul Bakker43ca69c2011-01-15 17:35:19 +00003999const char *ssl_get_version( const ssl_context *ssl )
4000{
4001 switch( ssl->minor_ver )
4002 {
4003 case SSL_MINOR_VERSION_0:
4004 return( "SSLv3.0" );
4005
4006 case SSL_MINOR_VERSION_1:
4007 return( "TLSv1.0" );
4008
4009 case SSL_MINOR_VERSION_2:
4010 return( "TLSv1.1" );
4011
Paul Bakker1ef83d62012-04-11 12:09:53 +00004012 case SSL_MINOR_VERSION_3:
4013 return( "TLSv1.2" );
4014
Paul Bakker43ca69c2011-01-15 17:35:19 +00004015 default:
4016 break;
4017 }
4018 return( "unknown" );
4019}
4020
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004021#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004022const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004023{
4024 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004025 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004026
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004027 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004028}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004029#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004030
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004031int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4032{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004033 if( ssl == NULL ||
4034 dst == NULL ||
4035 ssl->session == NULL ||
4036 ssl->endpoint != SSL_IS_CLIENT )
4037 {
4038 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4039 }
4040
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004041 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004042}
4043
Paul Bakker5121ce52009-01-03 21:22:43 +00004044/*
Paul Bakker1961b702013-01-25 14:49:24 +01004045 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004046 */
Paul Bakker1961b702013-01-25 14:49:24 +01004047int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004048{
Paul Bakker40e46942009-01-03 21:51:57 +00004049 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004050
Paul Bakker40e46942009-01-03 21:51:57 +00004051#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004052 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004053 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004054#endif
4055
Paul Bakker40e46942009-01-03 21:51:57 +00004056#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004057 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004058 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004059#endif
4060
Paul Bakker1961b702013-01-25 14:49:24 +01004061 return( ret );
4062}
4063
4064/*
4065 * Perform the SSL handshake
4066 */
4067int ssl_handshake( ssl_context *ssl )
4068{
4069 int ret = 0;
4070
4071 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4072
4073 while( ssl->state != SSL_HANDSHAKE_OVER )
4074 {
4075 ret = ssl_handshake_step( ssl );
4076
4077 if( ret != 0 )
4078 break;
4079 }
4080
Paul Bakker5121ce52009-01-03 21:22:43 +00004081 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4082
4083 return( ret );
4084}
4085
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004086#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004087/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004088 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004089 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004090static int ssl_write_hello_request( ssl_context *ssl )
4091{
4092 int ret;
4093
4094 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4095
4096 ssl->out_msglen = 4;
4097 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4098 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4099
4100 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4101 {
4102 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4103 return( ret );
4104 }
4105
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004106 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4107
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004108 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4109
4110 return( 0 );
4111}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004112#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004113
4114/*
4115 * Actually renegotiate current connection, triggered by either:
4116 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004117 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004118 * - receiving any handshake message on server during ssl_read() after the
4119 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004120 * If the handshake doesn't complete due to waiting for I/O, it will continue
4121 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004122 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004123static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004124{
4125 int ret;
4126
4127 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4128
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004129 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4130 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004131
4132 ssl->state = SSL_HELLO_REQUEST;
4133 ssl->renegotiation = SSL_RENEGOTIATION;
4134
Paul Bakker48916f92012-09-16 19:57:18 +00004135 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4136 {
4137 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4138 return( ret );
4139 }
4140
4141 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4142
4143 return( 0 );
4144}
4145
4146/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004147 * Renegotiate current connection on client,
4148 * or request renegotiation on server
4149 */
4150int ssl_renegotiate( ssl_context *ssl )
4151{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004152 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004153
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004154#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004155 /* On server, just send the request */
4156 if( ssl->endpoint == SSL_IS_SERVER )
4157 {
4158 if( ssl->state != SSL_HANDSHAKE_OVER )
4159 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4160
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004161 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004162 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004163#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004164
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004165#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004166 /*
4167 * On client, either start the renegotiation process or,
4168 * if already in progress, continue the handshake
4169 */
4170 if( ssl->renegotiation != SSL_RENEGOTIATION )
4171 {
4172 if( ssl->state != SSL_HANDSHAKE_OVER )
4173 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4174
4175 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4176 {
4177 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4178 return( ret );
4179 }
4180 }
4181 else
4182 {
4183 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4184 {
4185 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4186 return( ret );
4187 }
4188 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004189#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004190
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004191 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004192}
4193
4194/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004195 * Receive application data decrypted from the SSL layer
4196 */
Paul Bakker23986e52011-04-24 08:57:21 +00004197int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004198{
Paul Bakker23986e52011-04-24 08:57:21 +00004199 int ret;
4200 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004201
4202 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4203
4204 if( ssl->state != SSL_HANDSHAKE_OVER )
4205 {
4206 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4207 {
4208 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4209 return( ret );
4210 }
4211 }
4212
4213 if( ssl->in_offt == NULL )
4214 {
4215 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4216 {
Paul Bakker831a7552011-05-18 13:32:51 +00004217 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4218 return( 0 );
4219
Paul Bakker5121ce52009-01-03 21:22:43 +00004220 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4221 return( ret );
4222 }
4223
4224 if( ssl->in_msglen == 0 &&
4225 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4226 {
4227 /*
4228 * OpenSSL sends empty messages to randomize the IV
4229 */
4230 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4231 {
Paul Bakker831a7552011-05-18 13:32:51 +00004232 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4233 return( 0 );
4234
Paul Bakker5121ce52009-01-03 21:22:43 +00004235 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4236 return( ret );
4237 }
4238 }
4239
Paul Bakker48916f92012-09-16 19:57:18 +00004240 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4241 {
4242 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4243
4244 if( ssl->endpoint == SSL_IS_CLIENT &&
4245 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4246 ssl->in_hslen != 4 ) )
4247 {
4248 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4249 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4250 }
4251
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004252 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4253 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004254 ssl->allow_legacy_renegotiation ==
4255 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004256 {
4257 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4258
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004259#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004260 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004261 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004262 /*
4263 * SSLv3 does not have a "no_renegotiation" alert
4264 */
4265 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4266 return( ret );
4267 }
4268 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004269#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004270#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4271 defined(POLARSSL_SSL_PROTO_TLS1_2)
4272 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004273 {
4274 if( ( ret = ssl_send_alert_message( ssl,
4275 SSL_ALERT_LEVEL_WARNING,
4276 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4277 {
4278 return( ret );
4279 }
Paul Bakker48916f92012-09-16 19:57:18 +00004280 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004281 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004282#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4283 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004284 {
4285 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004286 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004287 }
Paul Bakker48916f92012-09-16 19:57:18 +00004288 }
4289 else
4290 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004291 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004292 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004293 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004294 return( ret );
4295 }
4296
4297 return( POLARSSL_ERR_NET_WANT_READ );
4298 }
4299 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004300 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4301 {
4302 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4303 "but not honored by client" ) );
4304 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4305 }
Paul Bakker48916f92012-09-16 19:57:18 +00004306 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004307 {
4308 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004309 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004310 }
4311
4312 ssl->in_offt = ssl->in_msg;
4313 }
4314
4315 n = ( len < ssl->in_msglen )
4316 ? len : ssl->in_msglen;
4317
4318 memcpy( buf, ssl->in_offt, n );
4319 ssl->in_msglen -= n;
4320
4321 if( ssl->in_msglen == 0 )
4322 /* all bytes consumed */
4323 ssl->in_offt = NULL;
4324 else
4325 /* more data available */
4326 ssl->in_offt += n;
4327
4328 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4329
Paul Bakker23986e52011-04-24 08:57:21 +00004330 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004331}
4332
4333/*
4334 * Send application data to be encrypted by the SSL layer
4335 */
Paul Bakker23986e52011-04-24 08:57:21 +00004336int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004337{
Paul Bakker23986e52011-04-24 08:57:21 +00004338 int ret;
4339 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004340 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004341
4342 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4343
4344 if( ssl->state != SSL_HANDSHAKE_OVER )
4345 {
4346 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4347 {
4348 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4349 return( ret );
4350 }
4351 }
4352
Paul Bakker05decb22013-08-15 13:33:48 +02004353#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004354 /*
4355 * Assume mfl_code is correct since it was checked when set
4356 */
4357 max_len = mfl_code_to_length[ssl->mfl_code];
4358
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004359 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004360 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004361 */
4362 if( ssl->session_out != NULL &&
4363 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4364 {
4365 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4366 }
Paul Bakker05decb22013-08-15 13:33:48 +02004367#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004368
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004369 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004370
Paul Bakker5121ce52009-01-03 21:22:43 +00004371 if( ssl->out_left != 0 )
4372 {
4373 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4374 {
4375 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4376 return( ret );
4377 }
4378 }
Paul Bakker887bd502011-06-08 13:10:54 +00004379 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004380 {
Paul Bakker887bd502011-06-08 13:10:54 +00004381 ssl->out_msglen = n;
4382 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4383 memcpy( ssl->out_msg, buf, n );
4384
4385 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4386 {
4387 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4388 return( ret );
4389 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004390 }
4391
4392 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4393
Paul Bakker23986e52011-04-24 08:57:21 +00004394 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004395}
4396
4397/*
4398 * Notify the peer that the connection is being closed
4399 */
4400int ssl_close_notify( ssl_context *ssl )
4401{
4402 int ret;
4403
4404 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4405
4406 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4407 {
4408 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4409 return( ret );
4410 }
4411
4412 if( ssl->state == SSL_HANDSHAKE_OVER )
4413 {
Paul Bakker48916f92012-09-16 19:57:18 +00004414 if( ( ret = ssl_send_alert_message( ssl,
4415 SSL_ALERT_LEVEL_WARNING,
4416 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004417 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004418 return( ret );
4419 }
4420 }
4421
4422 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4423
4424 return( ret );
4425}
4426
Paul Bakker48916f92012-09-16 19:57:18 +00004427void ssl_transform_free( ssl_transform *transform )
4428{
4429#if defined(POLARSSL_ZLIB_SUPPORT)
4430 deflateEnd( &transform->ctx_deflate );
4431 inflateEnd( &transform->ctx_inflate );
4432#endif
4433
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004434 cipher_free_ctx( &transform->cipher_ctx_enc );
4435 cipher_free_ctx( &transform->cipher_ctx_dec );
4436
Paul Bakker61d113b2013-07-04 11:51:43 +02004437 md_free_ctx( &transform->md_ctx_enc );
4438 md_free_ctx( &transform->md_ctx_dec );
4439
Paul Bakker34617722014-06-13 17:20:13 +02004440 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004441}
4442
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004443#if defined(POLARSSL_X509_CRT_PARSE_C)
4444static void ssl_key_cert_free( ssl_key_cert *key_cert )
4445{
4446 ssl_key_cert *cur = key_cert, *next;
4447
4448 while( cur != NULL )
4449 {
4450 next = cur->next;
4451
4452 if( cur->key_own_alloc )
4453 {
4454 pk_free( cur->key );
4455 polarssl_free( cur->key );
4456 }
4457 polarssl_free( cur );
4458
4459 cur = next;
4460 }
4461}
4462#endif /* POLARSSL_X509_CRT_PARSE_C */
4463
Paul Bakker48916f92012-09-16 19:57:18 +00004464void ssl_handshake_free( ssl_handshake_params *handshake )
4465{
4466#if defined(POLARSSL_DHM_C)
4467 dhm_free( &handshake->dhm_ctx );
4468#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004469#if defined(POLARSSL_ECDH_C)
4470 ecdh_free( &handshake->ecdh_ctx );
4471#endif
4472
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004473#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004474 /* explicit void pointer cast for buggy MS compiler */
4475 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004476#endif
4477
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004478#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4479 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4480 /*
4481 * Free only the linked list wrapper, not the keys themselves
4482 * since the belong to the SNI callback
4483 */
4484 if( handshake->sni_key_cert != NULL )
4485 {
4486 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4487
4488 while( cur != NULL )
4489 {
4490 next = cur->next;
4491 polarssl_free( cur );
4492 cur = next;
4493 }
4494 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004495#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004496
Paul Bakker34617722014-06-13 17:20:13 +02004497 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004498}
4499
4500void ssl_session_free( ssl_session *session )
4501{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004502#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004503 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004504 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004505 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004506 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004507 }
Paul Bakkered27a042013-04-18 22:46:23 +02004508#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004509
Paul Bakkera503a632013-08-14 13:48:06 +02004510#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004511 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004512#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004513
Paul Bakker34617722014-06-13 17:20:13 +02004514 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004515}
4516
Paul Bakker5121ce52009-01-03 21:22:43 +00004517/*
4518 * Free an SSL context
4519 */
4520void ssl_free( ssl_context *ssl )
4521{
4522 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4523
Paul Bakker5121ce52009-01-03 21:22:43 +00004524 if( ssl->out_ctr != NULL )
4525 {
Paul Bakker34617722014-06-13 17:20:13 +02004526 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004527 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004528 }
4529
4530 if( ssl->in_ctr != NULL )
4531 {
Paul Bakker34617722014-06-13 17:20:13 +02004532 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004533 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004534 }
4535
Paul Bakker16770332013-10-11 09:59:44 +02004536#if defined(POLARSSL_ZLIB_SUPPORT)
4537 if( ssl->compress_buf != NULL )
4538 {
Paul Bakker34617722014-06-13 17:20:13 +02004539 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004540 polarssl_free( ssl->compress_buf );
4541 }
4542#endif
4543
Paul Bakker40e46942009-01-03 21:51:57 +00004544#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004545 mpi_free( &ssl->dhm_P );
4546 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004547#endif
4548
Paul Bakker48916f92012-09-16 19:57:18 +00004549 if( ssl->transform )
4550 {
4551 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004552 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004553 }
4554
4555 if( ssl->handshake )
4556 {
4557 ssl_handshake_free( ssl->handshake );
4558 ssl_transform_free( ssl->transform_negotiate );
4559 ssl_session_free( ssl->session_negotiate );
4560
Paul Bakker6e339b52013-07-03 13:37:05 +02004561 polarssl_free( ssl->handshake );
4562 polarssl_free( ssl->transform_negotiate );
4563 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004564 }
4565
Paul Bakkerc0463502013-02-14 11:19:38 +01004566 if( ssl->session )
4567 {
4568 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004569 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004570 }
4571
Paul Bakkera503a632013-08-14 13:48:06 +02004572#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004573 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004574#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004575
Paul Bakker0be444a2013-08-27 21:55:01 +02004576#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004577 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004578 {
Paul Bakker34617722014-06-13 17:20:13 +02004579 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004580 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004581 ssl->hostname_len = 0;
4582 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004583#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004584
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004585#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004586 if( ssl->psk != NULL )
4587 {
Paul Bakker34617722014-06-13 17:20:13 +02004588 polarssl_zeroize( ssl->psk, ssl->psk_len );
4589 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004590 polarssl_free( ssl->psk );
4591 polarssl_free( ssl->psk_identity );
4592 ssl->psk_len = 0;
4593 ssl->psk_identity_len = 0;
4594 }
4595#endif
4596
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004597#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004598 ssl_key_cert_free( ssl->key_cert );
4599#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004600
Paul Bakker05ef8352012-05-08 09:17:57 +00004601#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4602 if( ssl_hw_record_finish != NULL )
4603 {
4604 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4605 ssl_hw_record_finish( ssl );
4606 }
4607#endif
4608
Paul Bakker5121ce52009-01-03 21:22:43 +00004609 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004610
Paul Bakker86f04f42013-02-14 11:20:09 +01004611 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004612 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004613}
4614
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004615#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004616/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004617 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004618 */
4619unsigned char ssl_sig_from_pk( pk_context *pk )
4620{
4621#if defined(POLARSSL_RSA_C)
4622 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4623 return( SSL_SIG_RSA );
4624#endif
4625#if defined(POLARSSL_ECDSA_C)
4626 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4627 return( SSL_SIG_ECDSA );
4628#endif
4629 return( SSL_SIG_ANON );
4630}
4631
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004632pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4633{
4634 switch( sig )
4635 {
4636#if defined(POLARSSL_RSA_C)
4637 case SSL_SIG_RSA:
4638 return( POLARSSL_PK_RSA );
4639#endif
4640#if defined(POLARSSL_ECDSA_C)
4641 case SSL_SIG_ECDSA:
4642 return( POLARSSL_PK_ECDSA );
4643#endif
4644 default:
4645 return( POLARSSL_PK_NONE );
4646 }
4647}
Paul Bakker9af723c2014-05-01 13:03:14 +02004648#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004649
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004650/*
4651 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4652 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004653md_type_t ssl_md_alg_from_hash( unsigned char hash )
4654{
4655 switch( hash )
4656 {
4657#if defined(POLARSSL_MD5_C)
4658 case SSL_HASH_MD5:
4659 return( POLARSSL_MD_MD5 );
4660#endif
4661#if defined(POLARSSL_SHA1_C)
4662 case SSL_HASH_SHA1:
4663 return( POLARSSL_MD_SHA1 );
4664#endif
4665#if defined(POLARSSL_SHA256_C)
4666 case SSL_HASH_SHA224:
4667 return( POLARSSL_MD_SHA224 );
4668 case SSL_HASH_SHA256:
4669 return( POLARSSL_MD_SHA256 );
4670#endif
4671#if defined(POLARSSL_SHA512_C)
4672 case SSL_HASH_SHA384:
4673 return( POLARSSL_MD_SHA384 );
4674 case SSL_HASH_SHA512:
4675 return( POLARSSL_MD_SHA512 );
4676#endif
4677 default:
4678 return( POLARSSL_MD_NONE );
4679 }
4680}
4681
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004682#if defined(POLARSSL_SSL_SET_CURVES)
4683/*
4684 * Check is a curve proposed by the peer is in our list.
4685 * Return 1 if we're willing to use it, 0 otherwise.
4686 */
4687int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4688{
4689 const ecp_group_id *gid;
4690
4691 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4692 if( *gid == grp_id )
4693 return( 1 );
4694
4695 return( 0 );
4696}
Paul Bakker9af723c2014-05-01 13:03:14 +02004697#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004698
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004699#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004700int ssl_check_cert_usage( const x509_crt *cert,
4701 const ssl_ciphersuite_t *ciphersuite,
4702 int cert_endpoint )
4703{
4704#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4705 int usage = 0;
4706#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004707#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4708 const char *ext_oid;
4709 size_t ext_len;
4710#endif
4711
4712#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4713 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4714 ((void) cert);
4715 ((void) cert_endpoint);
4716#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004717
4718#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4719 if( cert_endpoint == SSL_IS_SERVER )
4720 {
4721 /* Server part of the key exchange */
4722 switch( ciphersuite->key_exchange )
4723 {
4724 case POLARSSL_KEY_EXCHANGE_RSA:
4725 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4726 usage = KU_KEY_ENCIPHERMENT;
4727 break;
4728
4729 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4730 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4731 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4732 usage = KU_DIGITAL_SIGNATURE;
4733 break;
4734
4735 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4736 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4737 usage = KU_KEY_AGREEMENT;
4738 break;
4739
4740 /* Don't use default: we want warnings when adding new values */
4741 case POLARSSL_KEY_EXCHANGE_NONE:
4742 case POLARSSL_KEY_EXCHANGE_PSK:
4743 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4744 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4745 usage = 0;
4746 }
4747 }
4748 else
4749 {
4750 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4751 usage = KU_DIGITAL_SIGNATURE;
4752 }
4753
4754 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4755 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004756#else
4757 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004758#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4759
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004760#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4761 if( cert_endpoint == SSL_IS_SERVER )
4762 {
4763 ext_oid = OID_SERVER_AUTH;
4764 ext_len = OID_SIZE( OID_SERVER_AUTH );
4765 }
4766 else
4767 {
4768 ext_oid = OID_CLIENT_AUTH;
4769 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4770 }
4771
4772 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4773 return( -1 );
4774#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4775
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004776 return( 0 );
4777}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004778#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004779
4780#endif /* POLARSSL_SSL_TLS_C */