blob: b0812848f1ade47b9fe49052048ab6466c8bedeb [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
916 *(p++) = (unsigned char)( len >> 8 );
917 *(p++) = (unsigned char)( len );
918 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
919 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
920 {
921 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
922 return( ret );
923 }
924 p += len;
925
926 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
927 }
928 else
929#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
930#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
931 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
932 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200933 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200934 size_t zlen;
935
936 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200937 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200938 ssl->f_rng, ssl->p_rng ) ) != 0 )
939 {
940 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
941 return( ret );
942 }
943
944 *(p++) = (unsigned char)( zlen >> 8 );
945 *(p++) = (unsigned char)( zlen );
946 p += zlen;
947
948 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
949 }
950 else
951#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
952 {
953 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200954 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200955 }
956
957 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100958 if( end - p < 2 + (int) ssl->psk_len )
959 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
960
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200961 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
962 *(p++) = (unsigned char)( ssl->psk_len );
963 memcpy( p, ssl->psk, ssl->psk_len );
964 p += ssl->psk_len;
965
966 ssl->handshake->pmslen = p - ssl->handshake->premaster;
967
968 return( 0 );
969}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200970#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200971
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200972#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000973/*
974 * SSLv3.0 MAC functions
975 */
Paul Bakker68884e32013-01-07 18:20:04 +0100976static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
977 unsigned char *buf, size_t len,
978 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000979{
980 unsigned char header[11];
981 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100982 int padlen = 0;
983 int md_size = md_get_size( md_ctx->md_info );
984 int md_type = md_get_type( md_ctx->md_info );
985
986 if( md_type == POLARSSL_MD_MD5 )
987 padlen = 48;
988 else if( md_type == POLARSSL_MD_SHA1 )
989 padlen = 40;
990 else if( md_type == POLARSSL_MD_SHA256 )
991 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100992 else if( md_type == POLARSSL_MD_SHA384 )
993 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
995 memcpy( header, ctr, 8 );
996 header[ 8] = (unsigned char) type;
997 header[ 9] = (unsigned char)( len >> 8 );
998 header[10] = (unsigned char)( len );
999
Paul Bakker68884e32013-01-07 18:20:04 +01001000 memset( padding, 0x36, padlen );
1001 md_starts( md_ctx );
1002 md_update( md_ctx, secret, md_size );
1003 md_update( md_ctx, padding, padlen );
1004 md_update( md_ctx, header, 11 );
1005 md_update( md_ctx, buf, len );
1006 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
Paul Bakker68884e32013-01-07 18:20:04 +01001008 memset( padding, 0x5C, padlen );
1009 md_starts( md_ctx );
1010 md_update( md_ctx, secret, md_size );
1011 md_update( md_ctx, padding, padlen );
1012 md_update( md_ctx, buf + len, md_size );
1013 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001014}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001015#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001016
Paul Bakker5121ce52009-01-03 21:22:43 +00001017/*
1018 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001019 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001020static int ssl_encrypt_buf( ssl_context *ssl )
1021{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001022 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001023 const cipher_mode_t mode = cipher_get_cipher_mode(
1024 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
1026 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1027
1028 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001029 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001031#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1032 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1033 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001034 if( mode != POLARSSL_MODE_GCM &&
1035 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001037#if defined(POLARSSL_SSL_PROTO_SSL3)
1038 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1039 {
1040 ssl_mac( &ssl->transform_out->md_ctx_enc,
1041 ssl->transform_out->mac_enc,
1042 ssl->out_msg, ssl->out_msglen,
1043 ssl->out_ctr, ssl->out_msgtype );
1044 }
1045 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001046#endif
1047#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001048 defined(POLARSSL_SSL_PROTO_TLS1_2)
1049 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1050 {
1051 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1052 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1053 ssl->out_msg, ssl->out_msglen );
1054 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1055 ssl->out_msg + ssl->out_msglen );
1056 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1057 }
1058 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001059#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001060 {
1061 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001062 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001063 }
1064
1065 SSL_DEBUG_BUF( 4, "computed mac",
1066 ssl->out_msg + ssl->out_msglen,
1067 ssl->transform_out->maclen );
1068
1069 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001070 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001071#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001073 /*
1074 * Encrypt
1075 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001076#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001077 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001079 int ret;
1080 size_t olen = 0;
1081
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1083 "including %d bytes of padding",
1084 ssl->out_msglen, 0 ) );
1085
1086 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1087 ssl->out_msg, ssl->out_msglen );
1088
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001089 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001090 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001091 ssl->transform_out->ivlen,
1092 ssl->out_msg, ssl->out_msglen,
1093 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001094 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001095 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001096 return( ret );
1097 }
1098
1099 if( ssl->out_msglen != olen )
1100 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001101 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001102 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001103 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 }
Paul Bakker68884e32013-01-07 18:20:04 +01001105 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001106#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001107#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1108 if( mode == POLARSSL_MODE_GCM ||
1109 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001110 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001111 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001112 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001113 unsigned char *enc_msg;
1114 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001115 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1116 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001117
Paul Bakkerca4ab492012-04-18 14:23:57 +00001118 memcpy( add_data, ssl->out_ctr, 8 );
1119 add_data[8] = ssl->out_msgtype;
1120 add_data[9] = ssl->major_ver;
1121 add_data[10] = ssl->minor_ver;
1122 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1123 add_data[12] = ssl->out_msglen & 0xFF;
1124
1125 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1126 add_data, 13 );
1127
Paul Bakker68884e32013-01-07 18:20:04 +01001128 /*
1129 * Generate IV
1130 */
1131 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001132 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1133 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001134 if( ret != 0 )
1135 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001136
Paul Bakker68884e32013-01-07 18:20:04 +01001137 memcpy( ssl->out_iv,
1138 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1139 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001140
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001141 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001142 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001143
Paul Bakker68884e32013-01-07 18:20:04 +01001144 /*
1145 * Fix pointer positions and message length with added IV
1146 */
1147 enc_msg = ssl->out_msg;
1148 enc_msglen = ssl->out_msglen;
1149 ssl->out_msglen += ssl->transform_out->ivlen -
1150 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001151
Paul Bakker68884e32013-01-07 18:20:04 +01001152 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1153 "including %d bytes of padding",
1154 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001155
Paul Bakker68884e32013-01-07 18:20:04 +01001156 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1157 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001158
Paul Bakker68884e32013-01-07 18:20:04 +01001159 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001160 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001161 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001162 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1163 ssl->transform_out->iv_enc,
1164 ssl->transform_out->ivlen,
1165 add_data, 13,
1166 enc_msg, enc_msglen,
1167 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001168 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001169 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001170 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001171 return( ret );
1172 }
1173
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001174 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001175 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001176 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001177 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001178 }
1179
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001180 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001181
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001182 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001185#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001186#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1187 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001188 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001190 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001191 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001192 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001193
Paul Bakker48916f92012-09-16 19:57:18 +00001194 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1195 ssl->transform_out->ivlen;
1196 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 padlen = 0;
1198
1199 for( i = 0; i <= padlen; i++ )
1200 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1201
1202 ssl->out_msglen += padlen + 1;
1203
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001204 enc_msglen = ssl->out_msglen;
1205 enc_msg = ssl->out_msg;
1206
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001207#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001208 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001209 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1210 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001211 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001212 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 {
1214 /*
1215 * Generate IV
1216 */
Paul Bakker48916f92012-09-16 19:57:18 +00001217 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1218 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001219 if( ret != 0 )
1220 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001221
Paul Bakker92be97b2013-01-02 17:30:03 +01001222 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001223 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001224
1225 /*
1226 * Fix pointer positions and message length with added IV
1227 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001228 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001229 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001230 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001231 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001232#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001236 ssl->out_msglen, ssl->transform_out->ivlen,
1237 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
1239 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001240 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001242 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001243 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001244 ssl->transform_out->ivlen,
1245 enc_msg, enc_msglen,
1246 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001247 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001248 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001249 return( ret );
1250 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001251
Paul Bakkercca5b812013-08-31 17:40:26 +02001252 if( enc_msglen != olen )
1253 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001254 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001255 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001256 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001257
1258#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001259 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1260 {
1261 /*
1262 * Save IV in SSL3 and TLS1
1263 */
1264 memcpy( ssl->transform_out->iv_enc,
1265 ssl->transform_out->cipher_ctx_enc.iv,
1266 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001268#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001270 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001271#endif /* POLARSSL_CIPHER_MODE_CBC &&
1272 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001273 {
1274 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001275 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001276 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
Paul Bakkerca4ab492012-04-18 14:23:57 +00001278 for( i = 8; i > 0; i-- )
1279 if( ++ssl->out_ctr[i - 1] != 0 )
1280 break;
1281
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001282 /* The loops goes to its end iff the counter is wrapping */
1283 if( i == 0 )
1284 {
1285 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1286 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1287 }
1288
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1290
1291 return( 0 );
1292}
1293
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001294#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001295
Paul Bakker5121ce52009-01-03 21:22:43 +00001296static int ssl_decrypt_buf( ssl_context *ssl )
1297{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001298 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001299 const cipher_mode_t mode = cipher_get_cipher_mode(
1300 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001301#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1302 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1303 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1304 size_t padlen = 0, correct = 1;
1305#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
1307 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1308
Paul Bakker48916f92012-09-16 19:57:18 +00001309 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 {
1311 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001312 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001313 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 }
1315
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001316#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001317 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001318 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001319 int ret;
1320 size_t olen = 0;
1321
Paul Bakker68884e32013-01-07 18:20:04 +01001322 padlen = 0;
1323
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001324 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001325 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001326 ssl->transform_in->ivlen,
1327 ssl->in_msg, ssl->in_msglen,
1328 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001329 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001330 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001331 return( ret );
1332 }
1333
1334 if( ssl->in_msglen != olen )
1335 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001336 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001337 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
Paul Bakker68884e32013-01-07 18:20:04 +01001340 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001341#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001342#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1343 if( mode == POLARSSL_MODE_GCM ||
1344 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001345 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001346 int ret;
1347 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001348 unsigned char *dec_msg;
1349 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001350 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001351 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1352 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001353 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1354 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001355
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001356 if( ssl->in_msglen < explicit_iv_len + taglen )
1357 {
1358 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1359 "+ taglen (%d)", ssl->in_msglen,
1360 explicit_iv_len, taglen ) );
1361 return( POLARSSL_ERR_SSL_INVALID_MAC );
1362 }
1363 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1364
Paul Bakker68884e32013-01-07 18:20:04 +01001365 dec_msg = ssl->in_msg;
1366 dec_msg_result = ssl->in_msg;
1367 ssl->in_msglen = dec_msglen;
1368
1369 memcpy( add_data, ssl->in_ctr, 8 );
1370 add_data[8] = ssl->in_msgtype;
1371 add_data[9] = ssl->major_ver;
1372 add_data[10] = ssl->minor_ver;
1373 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1374 add_data[12] = ssl->in_msglen & 0xFF;
1375
1376 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1377 add_data, 13 );
1378
1379 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1380 ssl->in_iv,
1381 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1382
1383 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1384 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001385 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001386
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001387 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001388 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001389 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001390 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1391 ssl->transform_in->iv_dec,
1392 ssl->transform_in->ivlen,
1393 add_data, 13,
1394 dec_msg, dec_msglen,
1395 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001396 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001397 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001398 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1399
1400 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1401 return( POLARSSL_ERR_SSL_INVALID_MAC );
1402
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001403 return( ret );
1404 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001405
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001406 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001407 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001408 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001409 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001410 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001413#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001414#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1415 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001416 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 {
Paul Bakker45829992013-01-03 14:52:21 +01001418 /*
1419 * Decrypt and check the padding
1420 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001421 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001422 unsigned char *dec_msg;
1423 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001424 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001425 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001426 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001427
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 /*
Paul Bakker45829992013-01-03 14:52:21 +01001429 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 */
Paul Bakker48916f92012-09-16 19:57:18 +00001431 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 {
1433 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001434 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001435 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 }
1437
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001438#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001439 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1440 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001441#endif
Paul Bakker45829992013-01-03 14:52:21 +01001442
1443 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1444 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1445 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001446 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1447 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1448 ssl->transform_in->ivlen,
1449 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001450 return( POLARSSL_ERR_SSL_INVALID_MAC );
1451 }
1452
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001453 dec_msglen = ssl->in_msglen;
1454 dec_msg = ssl->in_msg;
1455 dec_msg_result = ssl->in_msg;
1456
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001457#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001458 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001459 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001460 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001461 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001462 {
Paul Bakker48916f92012-09-16 19:57:18 +00001463 dec_msglen -= ssl->transform_in->ivlen;
1464 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001465
Paul Bakker48916f92012-09-16 19:57:18 +00001466 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001467 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001468 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001469#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001470
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001471 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001472 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001473 ssl->transform_in->ivlen,
1474 dec_msg, dec_msglen,
1475 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001476 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001477 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001478 return( ret );
1479 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001480
Paul Bakkercca5b812013-08-31 17:40:26 +02001481 if( dec_msglen != olen )
1482 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001483 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001484 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001485 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001486
1487#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001488 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1489 {
1490 /*
1491 * Save IV in SSL3 and TLS1
1492 */
1493 memcpy( ssl->transform_in->iv_dec,
1494 ssl->transform_in->cipher_ctx_dec.iv,
1495 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001498
1499 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001500
1501 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1502 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001503#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001504 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1505 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001506#endif
Paul Bakker45829992013-01-03 14:52:21 +01001507 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001508 correct = 0;
1509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001511#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1513 {
Paul Bakker48916f92012-09-16 19:57:18 +00001514 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001515 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001516#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001517 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1518 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001519 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001520#endif
Paul Bakker45829992013-01-03 14:52:21 +01001521 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 }
1523 }
1524 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001525#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001526#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1527 defined(POLARSSL_SSL_PROTO_TLS1_2)
1528 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001529 {
1530 /*
Paul Bakker45829992013-01-03 14:52:21 +01001531 * TLSv1+: always check the padding up to the first failure
1532 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001534 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001535 size_t padding_idx = ssl->in_msglen - padlen - 1;
1536
Paul Bakker956c9e02013-12-19 14:42:28 +01001537 /*
1538 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001539 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001540 *
Paul Bakker61885c72014-04-25 12:59:03 +02001541 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1542 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001543 *
1544 * In both cases we reset padding_idx to a safe value (0) to
1545 * prevent out-of-buffer reads.
1546 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001547 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001548 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1549 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001550
1551 padding_idx *= correct;
1552
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001553 for( i = 1; i <= 256; i++ )
1554 {
1555 real_count &= ( i <= padlen );
1556 pad_count += real_count *
1557 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1558 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001559
1560 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001561
Paul Bakkerd66f0702013-01-31 16:57:45 +01001562#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001563 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001564 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001565#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001566 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001568 else
1569#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1570 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001571 {
1572 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001573 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001576 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001577#endif /* POLARSSL_CIPHER_MODE_CBC &&
1578 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001579 {
1580 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001581 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583
1584 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1585 ssl->in_msg, ssl->in_msglen );
1586
1587 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001588 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001590#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1591 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1592 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001593 if( mode != POLARSSL_MODE_GCM &&
1594 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001595 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001596 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1597
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001598 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001600 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1601 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001602
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001603 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001605#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001606 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1607 {
1608 ssl_mac( &ssl->transform_in->md_ctx_dec,
1609 ssl->transform_in->mac_dec,
1610 ssl->in_msg, ssl->in_msglen,
1611 ssl->in_ctr, ssl->in_msgtype );
1612 }
1613 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001614#endif /* POLARSSL_SSL_PROTO_SSL3 */
1615#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001616 defined(POLARSSL_SSL_PROTO_TLS1_2)
1617 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1618 {
1619 /*
1620 * Process MAC and always update for padlen afterwards to make
1621 * total time independent of padlen
1622 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001623 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001624 *
1625 * Known timing attacks:
1626 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1627 *
1628 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1629 * correctly. (We round down instead of up, so -56 is the correct
1630 * value for our calculations instead of -55)
1631 */
1632 size_t j, extra_run = 0;
1633 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1634 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001635
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001636 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001637
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001638 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1639 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1640 ssl->in_msglen );
1641 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1642 ssl->in_msg + ssl->in_msglen );
1643 for( j = 0; j < extra_run; j++ )
1644 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001645
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001646 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1647 }
1648 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001649#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001650 POLARSSL_SSL_PROTO_TLS1_2 */
1651 {
1652 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001653 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001656 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1657 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1658 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001660 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001661 ssl->transform_in->maclen ) != 0 )
1662 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001663#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001664 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001665#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001666 correct = 0;
1667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001669 /*
1670 * Finally check the correct flag
1671 */
1672 if( correct == 0 )
1673 return( POLARSSL_ERR_SSL_INVALID_MAC );
1674 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001675#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
1677 if( ssl->in_msglen == 0 )
1678 {
1679 ssl->nb_zero++;
1680
1681 /*
1682 * Three or more empty messages may be a DoS attack
1683 * (excessive CPU consumption).
1684 */
1685 if( ssl->nb_zero > 3 )
1686 {
1687 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1688 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001689 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690 }
1691 }
1692 else
1693 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001694
Paul Bakker23986e52011-04-24 08:57:21 +00001695 for( i = 8; i > 0; i-- )
1696 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 break;
1698
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001699 /* The loops goes to its end iff the counter is wrapping */
1700 if( i == 0 )
1701 {
1702 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1703 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1704 }
1705
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1707
1708 return( 0 );
1709}
1710
Paul Bakker2770fbd2012-07-03 13:30:23 +00001711#if defined(POLARSSL_ZLIB_SUPPORT)
1712/*
1713 * Compression/decompression functions
1714 */
1715static int ssl_compress_buf( ssl_context *ssl )
1716{
1717 int ret;
1718 unsigned char *msg_post = ssl->out_msg;
1719 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001720 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001721
1722 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1723
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001724 if( len_pre == 0 )
1725 return( 0 );
1726
Paul Bakker2770fbd2012-07-03 13:30:23 +00001727 memcpy( msg_pre, ssl->out_msg, len_pre );
1728
1729 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1730 ssl->out_msglen ) );
1731
1732 SSL_DEBUG_BUF( 4, "before compression: output payload",
1733 ssl->out_msg, ssl->out_msglen );
1734
Paul Bakker48916f92012-09-16 19:57:18 +00001735 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1736 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1737 ssl->transform_out->ctx_deflate.next_out = msg_post;
1738 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001739
Paul Bakker48916f92012-09-16 19:57:18 +00001740 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001741 if( ret != Z_OK )
1742 {
1743 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1744 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1745 }
1746
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001747 ssl->out_msglen = SSL_BUFFER_LEN -
1748 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001749
Paul Bakker2770fbd2012-07-03 13:30:23 +00001750 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1751 ssl->out_msglen ) );
1752
1753 SSL_DEBUG_BUF( 4, "after compression: output payload",
1754 ssl->out_msg, ssl->out_msglen );
1755
1756 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1757
1758 return( 0 );
1759}
1760
1761static int ssl_decompress_buf( ssl_context *ssl )
1762{
1763 int ret;
1764 unsigned char *msg_post = ssl->in_msg;
1765 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001766 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001767
1768 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1769
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001770 if( len_pre == 0 )
1771 return( 0 );
1772
Paul Bakker2770fbd2012-07-03 13:30:23 +00001773 memcpy( msg_pre, ssl->in_msg, len_pre );
1774
1775 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1776 ssl->in_msglen ) );
1777
1778 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1779 ssl->in_msg, ssl->in_msglen );
1780
Paul Bakker48916f92012-09-16 19:57:18 +00001781 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1782 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1783 ssl->transform_in->ctx_inflate.next_out = msg_post;
1784 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001785
Paul Bakker48916f92012-09-16 19:57:18 +00001786 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001787 if( ret != Z_OK )
1788 {
1789 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1790 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1791 }
1792
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001793 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1794 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795
Paul Bakker2770fbd2012-07-03 13:30:23 +00001796 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1797 ssl->in_msglen ) );
1798
1799 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1800 ssl->in_msg, ssl->in_msglen );
1801
1802 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1803
1804 return( 0 );
1805}
1806#endif /* POLARSSL_ZLIB_SUPPORT */
1807
Paul Bakker5121ce52009-01-03 21:22:43 +00001808/*
1809 * Fill the input message buffer
1810 */
Paul Bakker23986e52011-04-24 08:57:21 +00001811int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001812{
Paul Bakker23986e52011-04-24 08:57:21 +00001813 int ret;
1814 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001815
1816 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1817
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001818 if( nb_want > SSL_BUFFER_LEN - 8 )
1819 {
1820 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1821 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1822 }
1823
Paul Bakker5121ce52009-01-03 21:22:43 +00001824 while( ssl->in_left < nb_want )
1825 {
1826 len = nb_want - ssl->in_left;
1827 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1828
1829 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1830 ssl->in_left, nb_want ) );
1831 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1832
Paul Bakker831a7552011-05-18 13:32:51 +00001833 if( ret == 0 )
1834 return( POLARSSL_ERR_SSL_CONN_EOF );
1835
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 if( ret < 0 )
1837 return( ret );
1838
1839 ssl->in_left += ret;
1840 }
1841
1842 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1843
1844 return( 0 );
1845}
1846
1847/*
1848 * Flush any data not yet written
1849 */
1850int ssl_flush_output( ssl_context *ssl )
1851{
1852 int ret;
1853 unsigned char *buf;
1854
1855 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1856
1857 while( ssl->out_left > 0 )
1858 {
1859 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1860 5 + ssl->out_msglen, ssl->out_left ) );
1861
Paul Bakker5bd42292012-12-19 14:40:42 +01001862 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001864
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1866
1867 if( ret <= 0 )
1868 return( ret );
1869
1870 ssl->out_left -= ret;
1871 }
1872
1873 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1874
1875 return( 0 );
1876}
1877
1878/*
1879 * Record layer functions
1880 */
1881int ssl_write_record( ssl_context *ssl )
1882{
Paul Bakker05ef8352012-05-08 09:17:57 +00001883 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001884 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
1886 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1887
Paul Bakker5121ce52009-01-03 21:22:43 +00001888 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1889 {
1890 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1891 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1892 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1893
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001894 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1895 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 }
1897
Paul Bakker2770fbd2012-07-03 13:30:23 +00001898#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001899 if( ssl->transform_out != NULL &&
1900 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001901 {
1902 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1903 {
1904 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1905 return( ret );
1906 }
1907
1908 len = ssl->out_msglen;
1909 }
1910#endif /*POLARSSL_ZLIB_SUPPORT */
1911
Paul Bakker05ef8352012-05-08 09:17:57 +00001912#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001913 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001915 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001916
Paul Bakker05ef8352012-05-08 09:17:57 +00001917 ret = ssl_hw_record_write( ssl );
1918 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1919 {
1920 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001921 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001922 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001923
1924 if( ret == 0 )
1925 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001926 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001927#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001928 if( !done )
1929 {
1930 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1931 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1932 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1934 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001935
Paul Bakker48916f92012-09-16 19:57:18 +00001936 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001937 {
1938 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1939 {
1940 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1941 return( ret );
1942 }
1943
1944 len = ssl->out_msglen;
1945 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1946 ssl->out_hdr[4] = (unsigned char)( len );
1947 }
1948
1949 ssl->out_left = 5 + ssl->out_msglen;
1950
1951 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1952 "version = [%d:%d], msglen = %d",
1953 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1954 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1955
1956 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001957 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 }
1959
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1961 {
1962 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1963 return( ret );
1964 }
1965
1966 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1967
1968 return( 0 );
1969}
1970
1971int ssl_read_record( ssl_context *ssl )
1972{
Paul Bakker05ef8352012-05-08 09:17:57 +00001973 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001974
1975 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1976
1977 if( ssl->in_hslen != 0 &&
1978 ssl->in_hslen < ssl->in_msglen )
1979 {
1980 /*
1981 * Get next Handshake message in the current record
1982 */
1983 ssl->in_msglen -= ssl->in_hslen;
1984
Paul Bakker8934a982011-08-05 11:11:53 +00001985 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001986 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
1988 ssl->in_hslen = 4;
1989 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1990
1991 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1992 " %d, type = %d, hslen = %d",
1993 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1994
1995 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1996 {
1997 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001998 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 }
2000
2001 if( ssl->in_msglen < ssl->in_hslen )
2002 {
2003 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002004 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 }
2006
Paul Bakker4224bc02014-04-08 14:36:50 +02002007 if( ssl->state != SSL_HANDSHAKE_OVER )
2008 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
2010 return( 0 );
2011 }
2012
2013 ssl->in_hslen = 0;
2014
2015 /*
2016 * Read the record header and validate it
2017 */
2018 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2019 {
2020 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2021 return( ret );
2022 }
2023
2024 ssl->in_msgtype = ssl->in_hdr[0];
2025 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2026
2027 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2028 "version = [%d:%d], msglen = %d",
2029 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2030 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2031
2032 if( ssl->in_hdr[1] != ssl->major_ver )
2033 {
2034 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002035 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 }
2037
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002038 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 {
2040 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002041 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 }
2043
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002044 /* Sanity check (outer boundaries) */
2045 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2046 {
2047 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2048 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2049 }
2050
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002052 * Make sure the message length is acceptable for the current transform
2053 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 */
Paul Bakker48916f92012-09-16 19:57:18 +00002055 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002057 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 {
2059 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002060 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 }
2062 }
2063 else
2064 {
Paul Bakker48916f92012-09-16 19:57:18 +00002065 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 {
2067 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002068 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 }
2070
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002071#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002073 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 {
2075 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002076 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002078#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002079
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002080#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2081 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 /*
2083 * TLS encrypted messages can have up to 256 bytes of padding
2084 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002085 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002086 ssl->in_msglen > ssl->transform_in->minlen +
2087 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 {
2089 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002090 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002092#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 }
2094
2095 /*
2096 * Read and optionally decrypt the message contents
2097 */
2098 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2099 {
2100 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2101 return( ret );
2102 }
2103
2104 SSL_DEBUG_BUF( 4, "input record from network",
2105 ssl->in_hdr, 5 + ssl->in_msglen );
2106
Paul Bakker05ef8352012-05-08 09:17:57 +00002107#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002108 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002109 {
2110 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2111
2112 ret = ssl_hw_record_read( ssl );
2113 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2114 {
2115 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002116 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002117 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002118
2119 if( ret == 0 )
2120 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002121 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002122#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002123 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 {
2125 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2126 {
Paul Bakker40865c82013-01-31 17:13:13 +01002127#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2128 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2129 {
2130 ssl_send_alert_message( ssl,
2131 SSL_ALERT_LEVEL_FATAL,
2132 SSL_ALERT_MSG_BAD_RECORD_MAC );
2133 }
2134#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2136 return( ret );
2137 }
2138
2139 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2140 ssl->in_msg, ssl->in_msglen );
2141
2142 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2143 {
2144 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002145 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 }
2147 }
2148
Paul Bakker2770fbd2012-07-03 13:30:23 +00002149#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002150 if( ssl->transform_in != NULL &&
2151 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002152 {
2153 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2154 {
2155 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2156 return( ret );
2157 }
2158
2159 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2160 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2161 }
2162#endif /* POLARSSL_ZLIB_SUPPORT */
2163
Paul Bakker0a925182012-04-16 06:46:41 +00002164 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2165 ssl->in_msgtype != SSL_MSG_ALERT &&
2166 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2167 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2168 {
2169 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2170
Paul Bakker48916f92012-09-16 19:57:18 +00002171 if( ( ret = ssl_send_alert_message( ssl,
2172 SSL_ALERT_LEVEL_FATAL,
2173 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002174 {
2175 return( ret );
2176 }
2177
2178 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2179 }
2180
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2182 {
2183 ssl->in_hslen = 4;
2184 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2185
2186 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2187 " %d, type = %d, hslen = %d",
2188 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2189
2190 /*
2191 * Additional checks to validate the handshake header
2192 */
2193 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2194 {
2195 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002196 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 }
2198
2199 if( ssl->in_msglen < ssl->in_hslen )
2200 {
2201 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002202 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 }
2204
Paul Bakker48916f92012-09-16 19:57:18 +00002205 if( ssl->state != SSL_HANDSHAKE_OVER )
2206 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 }
2208
2209 if( ssl->in_msgtype == SSL_MSG_ALERT )
2210 {
2211 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2212 ssl->in_msg[0], ssl->in_msg[1] ) );
2213
2214 /*
2215 * Ignore non-fatal alerts, except close_notify
2216 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002217 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002219 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2220 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002221 /**
2222 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2223 * error identifier.
2224 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002225 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 }
2227
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002228 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2229 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 {
2231 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002232 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 }
2234 }
2235
2236 ssl->in_left = 0;
2237
2238 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2239
2240 return( 0 );
2241}
2242
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002243int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2244{
2245 int ret;
2246
2247 if( ( ret = ssl_send_alert_message( ssl,
2248 SSL_ALERT_LEVEL_FATAL,
2249 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2250 {
2251 return( ret );
2252 }
2253
2254 return( 0 );
2255}
2256
Paul Bakker0a925182012-04-16 06:46:41 +00002257int ssl_send_alert_message( ssl_context *ssl,
2258 unsigned char level,
2259 unsigned char message )
2260{
2261 int ret;
2262
2263 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2264
2265 ssl->out_msgtype = SSL_MSG_ALERT;
2266 ssl->out_msglen = 2;
2267 ssl->out_msg[0] = level;
2268 ssl->out_msg[1] = message;
2269
2270 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2271 {
2272 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2273 return( ret );
2274 }
2275
2276 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2277
2278 return( 0 );
2279}
2280
Paul Bakker5121ce52009-01-03 21:22:43 +00002281/*
2282 * Handshake functions
2283 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002284#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2285 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2286 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2287 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2288 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2289 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2290 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002291int ssl_write_certificate( ssl_context *ssl )
2292{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002293 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
2295 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2296
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002297 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002298 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2299 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002300 {
2301 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2302 ssl->state++;
2303 return( 0 );
2304 }
2305
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002306 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2307 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002308}
2309
2310int ssl_parse_certificate( ssl_context *ssl )
2311{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002312 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2313
2314 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2315
2316 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002317 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2318 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002319 {
2320 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2321 ssl->state++;
2322 return( 0 );
2323 }
2324
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002325 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2326 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002327}
2328#else
2329int ssl_write_certificate( ssl_context *ssl )
2330{
2331 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2332 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002333 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002334 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2335
2336 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2337
2338 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002339 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2340 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002341 {
2342 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2343 ssl->state++;
2344 return( 0 );
2345 }
2346
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 if( ssl->endpoint == SSL_IS_CLIENT )
2348 {
2349 if( ssl->client_auth == 0 )
2350 {
2351 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2352 ssl->state++;
2353 return( 0 );
2354 }
2355
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002356#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 /*
2358 * If using SSLv3 and got no cert, send an Alert message
2359 * (otherwise an empty Certificate message will be sent).
2360 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002361 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002362 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2363 {
2364 ssl->out_msglen = 2;
2365 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002366 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2367 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
2369 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2370 goto write_msg;
2371 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002372#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 }
2374 else /* SSL_IS_SERVER */
2375 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002376 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 {
2378 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002379 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 }
2381 }
2382
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002383 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002384
2385 /*
2386 * 0 . 0 handshake type
2387 * 1 . 3 handshake length
2388 * 4 . 6 length of all certs
2389 * 7 . 9 length of cert. 1
2390 * 10 . n-1 peer certificate
2391 * n . n+2 length of cert. 2
2392 * n+3 . ... upper level cert, etc.
2393 */
2394 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002395 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
Paul Bakker29087132010-03-21 21:03:34 +00002397 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 {
2399 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002400 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 {
2402 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2403 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002404 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 }
2406
2407 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2408 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2409 ssl->out_msg[i + 2] = (unsigned char)( n );
2410
2411 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2412 i += n; crt = crt->next;
2413 }
2414
2415 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2416 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2417 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2418
2419 ssl->out_msglen = i;
2420 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2421 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2422
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002423#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002424write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002425#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
2427 ssl->state++;
2428
2429 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2430 {
2431 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2432 return( ret );
2433 }
2434
2435 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2436
Paul Bakkered27a042013-04-18 22:46:23 +02002437 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438}
2439
2440int ssl_parse_certificate( ssl_context *ssl )
2441{
Paul Bakkered27a042013-04-18 22:46:23 +02002442 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002443 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002444 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
2446 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2447
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002448 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002449 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2450 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002451 {
2452 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2453 ssl->state++;
2454 return( 0 );
2455 }
2456
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002458 ( ssl->authmode == SSL_VERIFY_NONE ||
2459 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002461 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2463 ssl->state++;
2464 return( 0 );
2465 }
2466
2467 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2468 {
2469 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2470 return( ret );
2471 }
2472
2473 ssl->state++;
2474
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002475#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 /*
2477 * Check if the client sent an empty certificate
2478 */
2479 if( ssl->endpoint == SSL_IS_SERVER &&
2480 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2481 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002482 if( ssl->in_msglen == 2 &&
2483 ssl->in_msgtype == SSL_MSG_ALERT &&
2484 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2485 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 {
2487 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2488
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002489 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2491 return( 0 );
2492 else
Paul Bakker40e46942009-01-03 21:51:57 +00002493 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 }
2495 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002496#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002498#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2499 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 if( ssl->endpoint == SSL_IS_SERVER &&
2501 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2502 {
2503 if( ssl->in_hslen == 7 &&
2504 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2505 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2506 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2507 {
2508 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2509
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002510 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002512 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 else
2514 return( 0 );
2515 }
2516 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002517#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2518 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
2520 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2521 {
2522 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002523 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 }
2525
2526 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2527 {
2528 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002529 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 }
2531
2532 /*
2533 * Same message structure as in ssl_write_certificate()
2534 */
2535 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2536
2537 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2538 {
2539 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002540 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 }
2542
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002543 /* In case we tried to reuse a session but it failed */
2544 if( ssl->session_negotiate->peer_cert != NULL )
2545 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002546 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002547 polarssl_free( ssl->session_negotiate->peer_cert );
2548 }
2549
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002550 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2551 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 {
2553 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002554 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002555 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 }
2557
Paul Bakkerb6b09562013-09-18 14:17:41 +02002558 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
2560 i = 7;
2561
2562 while( i < ssl->in_hslen )
2563 {
2564 if( ssl->in_msg[i] != 0 )
2565 {
2566 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002567 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 }
2569
2570 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2571 | (unsigned int) ssl->in_msg[i + 2];
2572 i += 3;
2573
2574 if( n < 128 || i + n > ssl->in_hslen )
2575 {
2576 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002577 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 }
2579
Paul Bakkerddf26b42013-09-18 13:46:23 +02002580 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2581 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 if( ret != 0 )
2583 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002584 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 return( ret );
2586 }
2587
2588 i += n;
2589 }
2590
Paul Bakker48916f92012-09-16 19:57:18 +00002591 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002593 /*
2594 * On client, make sure the server cert doesn't change during renego to
2595 * avoid "triple handshake" attack: https://secure-resumption.com/
2596 */
2597 if( ssl->endpoint == SSL_IS_CLIENT &&
2598 ssl->renegotiation == SSL_RENEGOTIATION )
2599 {
2600 if( ssl->session->peer_cert == NULL )
2601 {
2602 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2603 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2604 }
2605
2606 if( ssl->session->peer_cert->raw.len !=
2607 ssl->session_negotiate->peer_cert->raw.len ||
2608 memcmp( ssl->session->peer_cert->raw.p,
2609 ssl->session_negotiate->peer_cert->raw.p,
2610 ssl->session->peer_cert->raw.len ) != 0 )
2611 {
2612 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2613 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2614 }
2615 }
2616
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 if( ssl->authmode != SSL_VERIFY_NONE )
2618 {
2619 if( ssl->ca_chain == NULL )
2620 {
2621 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002622 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 }
2624
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002625 /*
2626 * Main check: verify certificate
2627 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002628 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2629 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2630 &ssl->session_negotiate->verify_result,
2631 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
2633 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002634 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002636 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002637
2638 /*
2639 * Secondary checks: always done, but change 'ret' only if it was 0
2640 */
2641
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002642#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002643 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002644 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002645
2646 /* If certificate uses an EC key, make sure the curve is OK */
2647 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2648 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2649 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002650 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002651 if( ret == 0 )
2652 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002653 }
2654 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002655#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002657 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2658 ciphersuite_info,
2659 ! ssl->endpoint ) != 0 )
2660 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002661 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002662 if( ret == 0 )
2663 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2664 }
2665
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2667 ret = 0;
2668 }
2669
2670 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2671
2672 return( ret );
2673}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002674#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2675 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2676 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2677 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2678 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2679 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2680 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
2682int ssl_write_change_cipher_spec( ssl_context *ssl )
2683{
2684 int ret;
2685
2686 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2687
2688 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2689 ssl->out_msglen = 1;
2690 ssl->out_msg[0] = 1;
2691
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 ssl->state++;
2693
2694 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2695 {
2696 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2697 return( ret );
2698 }
2699
2700 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2701
2702 return( 0 );
2703}
2704
2705int ssl_parse_change_cipher_spec( ssl_context *ssl )
2706{
2707 int ret;
2708
2709 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2710
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2712 {
2713 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2714 return( ret );
2715 }
2716
2717 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2718 {
2719 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002720 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 }
2722
2723 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2724 {
2725 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002726 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 }
2728
2729 ssl->state++;
2730
2731 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2732
2733 return( 0 );
2734}
2735
Paul Bakker41c83d32013-03-20 14:39:14 +01002736void ssl_optimize_checksum( ssl_context *ssl,
2737 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002738{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002739 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002740
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002741#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2742 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002743 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002744 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002745 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002746#endif
2747#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2748#if defined(POLARSSL_SHA512_C)
2749 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2750 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2751 else
2752#endif
2753#if defined(POLARSSL_SHA256_C)
2754 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002755 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002756 else
2757#endif
2758#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002759 {
2760 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002761 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002762 }
Paul Bakker380da532012-04-18 16:10:25 +00002763}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002764
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002765static void ssl_update_checksum_start( ssl_context *ssl,
2766 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002767{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002768#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2769 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002770 md5_update( &ssl->handshake->fin_md5 , buf, len );
2771 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002772#endif
2773#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2774#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002775 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002776#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002777#if defined(POLARSSL_SHA512_C)
2778 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002779#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002780#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002781}
2782
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002783#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2784 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002785static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2786 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002787{
Paul Bakker48916f92012-09-16 19:57:18 +00002788 md5_update( &ssl->handshake->fin_md5 , buf, len );
2789 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002790}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002791#endif
Paul Bakker380da532012-04-18 16:10:25 +00002792
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002793#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2794#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002795static void ssl_update_checksum_sha256( ssl_context *ssl,
2796 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002797{
Paul Bakker9e36f042013-06-30 14:34:05 +02002798 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002799}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002800#endif
Paul Bakker380da532012-04-18 16:10:25 +00002801
Paul Bakker9e36f042013-06-30 14:34:05 +02002802#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002803static void ssl_update_checksum_sha384( ssl_context *ssl,
2804 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002805{
Paul Bakker9e36f042013-06-30 14:34:05 +02002806 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002807}
Paul Bakker769075d2012-11-24 11:26:46 +01002808#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002809#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002810
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002811#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002812static void ssl_calc_finished_ssl(
2813 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002814{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002815 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002816 md5_context md5;
2817 sha1_context sha1;
2818
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 unsigned char padbuf[48];
2820 unsigned char md5sum[16];
2821 unsigned char sha1sum[20];
2822
Paul Bakker48916f92012-09-16 19:57:18 +00002823 ssl_session *session = ssl->session_negotiate;
2824 if( !session )
2825 session = ssl->session;
2826
Paul Bakker1ef83d62012-04-11 12:09:53 +00002827 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2828
Paul Bakker48916f92012-09-16 19:57:18 +00002829 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2830 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
2832 /*
2833 * SSLv3:
2834 * hash =
2835 * MD5( master + pad2 +
2836 * MD5( handshake + sender + master + pad1 ) )
2837 * + SHA1( master + pad2 +
2838 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002839 */
2840
Paul Bakker90995b52013-06-24 19:20:35 +02002841#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002842 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002843 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002844#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Paul Bakker90995b52013-06-24 19:20:35 +02002846#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002847 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002848 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002849#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
Paul Bakker3c2122f2013-06-24 19:03:14 +02002851 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2852 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Paul Bakker1ef83d62012-04-11 12:09:53 +00002854 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Paul Bakker3c2122f2013-06-24 19:03:14 +02002856 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002857 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002858 md5_update( &md5, padbuf, 48 );
2859 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Paul Bakker3c2122f2013-06-24 19:03:14 +02002861 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002862 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002863 sha1_update( &sha1, padbuf, 40 );
2864 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002865
Paul Bakker1ef83d62012-04-11 12:09:53 +00002866 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Paul Bakker1ef83d62012-04-11 12:09:53 +00002868 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002869 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002870 md5_update( &md5, padbuf, 48 );
2871 md5_update( &md5, md5sum, 16 );
2872 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
Paul Bakker1ef83d62012-04-11 12:09:53 +00002874 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002875 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002876 sha1_update( &sha1, padbuf , 40 );
2877 sha1_update( &sha1, sha1sum, 20 );
2878 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002879
Paul Bakker1ef83d62012-04-11 12:09:53 +00002880 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Paul Bakker34617722014-06-13 17:20:13 +02002882 polarssl_zeroize( &md5, sizeof( md5_context ) );
2883 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Paul Bakker34617722014-06-13 17:20:13 +02002885 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2886 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2887 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002888
2889 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2890}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002891#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002892
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002893#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894static void ssl_calc_finished_tls(
2895 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002896{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002897 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002898 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002899 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002901 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
Paul Bakker48916f92012-09-16 19:57:18 +00002903 ssl_session *session = ssl->session_negotiate;
2904 if( !session )
2905 session = ssl->session;
2906
Paul Bakker1ef83d62012-04-11 12:09:53 +00002907 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
Paul Bakker48916f92012-09-16 19:57:18 +00002909 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2910 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Paul Bakker1ef83d62012-04-11 12:09:53 +00002912 /*
2913 * TLSv1:
2914 * hash = PRF( master, finished_label,
2915 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2916 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002917
Paul Bakker90995b52013-06-24 19:20:35 +02002918#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002919 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2920 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002921#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002922
Paul Bakker90995b52013-06-24 19:20:35 +02002923#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002924 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2925 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002926#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002927
2928 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002929 ? "client finished"
2930 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002931
2932 md5_finish( &md5, padbuf );
2933 sha1_finish( &sha1, padbuf + 16 );
2934
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002935 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002936 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002937
2938 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2939
Paul Bakker34617722014-06-13 17:20:13 +02002940 polarssl_zeroize( &md5, sizeof( md5_context ) );
2941 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002942
Paul Bakker34617722014-06-13 17:20:13 +02002943 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002944
2945 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2946}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002947#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002948
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002949#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2950#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002951static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952 ssl_context *ssl, unsigned char *buf, int from )
2953{
2954 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002955 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002956 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002957 unsigned char padbuf[32];
2958
Paul Bakker48916f92012-09-16 19:57:18 +00002959 ssl_session *session = ssl->session_negotiate;
2960 if( !session )
2961 session = ssl->session;
2962
Paul Bakker380da532012-04-18 16:10:25 +00002963 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964
Paul Bakker9e36f042013-06-30 14:34:05 +02002965 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002966
2967 /*
2968 * TLSv1.2:
2969 * hash = PRF( master, finished_label,
2970 * Hash( handshake ) )[0.11]
2971 */
2972
Paul Bakker9e36f042013-06-30 14:34:05 +02002973#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002974 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002975 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002976#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002977
2978 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002979 ? "client finished"
2980 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002981
Paul Bakker9e36f042013-06-30 14:34:05 +02002982 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002983
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002984 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002985 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986
2987 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2988
Paul Bakker34617722014-06-13 17:20:13 +02002989 polarssl_zeroize( &sha256, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002990
Paul Bakker34617722014-06-13 17:20:13 +02002991 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002992
2993 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2994}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002995#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002996
Paul Bakker9e36f042013-06-30 14:34:05 +02002997#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002998static void ssl_calc_finished_tls_sha384(
2999 ssl_context *ssl, unsigned char *buf, int from )
3000{
3001 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003002 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003003 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003004 unsigned char padbuf[48];
3005
Paul Bakker48916f92012-09-16 19:57:18 +00003006 ssl_session *session = ssl->session_negotiate;
3007 if( !session )
3008 session = ssl->session;
3009
Paul Bakker380da532012-04-18 16:10:25 +00003010 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003011
Paul Bakker9e36f042013-06-30 14:34:05 +02003012 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003013
3014 /*
3015 * TLSv1.2:
3016 * hash = PRF( master, finished_label,
3017 * Hash( handshake ) )[0.11]
3018 */
3019
Paul Bakker9e36f042013-06-30 14:34:05 +02003020#if !defined(POLARSSL_SHA512_ALT)
3021 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3022 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003023#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003024
3025 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003026 ? "client finished"
3027 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003028
Paul Bakker9e36f042013-06-30 14:34:05 +02003029 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003030
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003031 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003032 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003033
3034 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3035
Paul Bakker34617722014-06-13 17:20:13 +02003036 polarssl_zeroize( &sha512, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003037
Paul Bakker34617722014-06-13 17:20:13 +02003038 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003039
3040 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3041}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003042#endif /* POLARSSL_SHA512_C */
3043#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003044
Paul Bakker48916f92012-09-16 19:57:18 +00003045void ssl_handshake_wrapup( ssl_context *ssl )
3046{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003047 int resume = ssl->handshake->resume;
3048
Paul Bakker48916f92012-09-16 19:57:18 +00003049 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3050
3051 /*
3052 * Free our handshake params
3053 */
3054 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003055 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003056 ssl->handshake = NULL;
3057
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003058 if( ssl->renegotiation == SSL_RENEGOTIATION )
3059 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3060
Paul Bakker48916f92012-09-16 19:57:18 +00003061 /*
3062 * Switch in our now active transform context
3063 */
3064 if( ssl->transform )
3065 {
3066 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003067 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003068 }
3069 ssl->transform = ssl->transform_negotiate;
3070 ssl->transform_negotiate = NULL;
3071
Paul Bakker0a597072012-09-25 21:55:46 +00003072 if( ssl->session )
3073 {
3074 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003075 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003076 }
3077 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003078 ssl->session_negotiate = NULL;
3079
Paul Bakker0a597072012-09-25 21:55:46 +00003080 /*
3081 * Add cache entry
3082 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003083 if( ssl->f_set_cache != NULL &&
3084 ssl->session->length != 0 &&
3085 resume == 0 )
3086 {
Paul Bakker0a597072012-09-25 21:55:46 +00003087 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3088 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003089 }
Paul Bakker0a597072012-09-25 21:55:46 +00003090
Paul Bakker48916f92012-09-16 19:57:18 +00003091 ssl->state++;
3092
3093 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3094}
3095
Paul Bakker1ef83d62012-04-11 12:09:53 +00003096int ssl_write_finished( ssl_context *ssl )
3097{
3098 int ret, hash_len;
3099
3100 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3101
Paul Bakker92be97b2013-01-02 17:30:03 +01003102 /*
3103 * Set the out_msg pointer to the correct location based on IV length
3104 */
3105 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3106 {
3107 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3108 ssl->transform_negotiate->fixed_ivlen;
3109 }
3110 else
3111 ssl->out_msg = ssl->out_iv;
3112
Paul Bakker48916f92012-09-16 19:57:18 +00003113 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003114
3115 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003116 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3117
Paul Bakker48916f92012-09-16 19:57:18 +00003118 ssl->verify_data_len = hash_len;
3119 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3120
Paul Bakker5121ce52009-01-03 21:22:43 +00003121 ssl->out_msglen = 4 + hash_len;
3122 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3123 ssl->out_msg[0] = SSL_HS_FINISHED;
3124
3125 /*
3126 * In case of session resuming, invert the client and server
3127 * ChangeCipherSpec messages order.
3128 */
Paul Bakker0a597072012-09-25 21:55:46 +00003129 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 {
3131 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003132 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003133 else
3134 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3135 }
3136 else
3137 ssl->state++;
3138
Paul Bakker48916f92012-09-16 19:57:18 +00003139 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003140 * Switch to our negotiated transform and session parameters for outbound
3141 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003142 */
3143 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3144 ssl->transform_out = ssl->transform_negotiate;
3145 ssl->session_out = ssl->session_negotiate;
3146 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003147
Paul Bakker07eb38b2012-12-19 14:42:06 +01003148#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003149 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003150 {
3151 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3152 {
3153 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3154 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3155 }
3156 }
3157#endif
3158
Paul Bakker5121ce52009-01-03 21:22:43 +00003159 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3160 {
3161 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3162 return( ret );
3163 }
3164
3165 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3166
3167 return( 0 );
3168}
3169
3170int ssl_parse_finished( ssl_context *ssl )
3171{
Paul Bakker23986e52011-04-24 08:57:21 +00003172 int ret;
3173 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 unsigned char buf[36];
3175
3176 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3177
Paul Bakker48916f92012-09-16 19:57:18 +00003178 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003179
Paul Bakker48916f92012-09-16 19:57:18 +00003180 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003181 * Switch to our negotiated transform and session parameters for inbound
3182 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003183 */
3184 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3185 ssl->transform_in = ssl->transform_negotiate;
3186 ssl->session_in = ssl->session_negotiate;
3187 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003188
Paul Bakker92be97b2013-01-02 17:30:03 +01003189 /*
3190 * Set the in_msg pointer to the correct location based on IV length
3191 */
3192 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3193 {
3194 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3195 ssl->transform_negotiate->fixed_ivlen;
3196 }
3197 else
3198 ssl->in_msg = ssl->in_iv;
3199
Paul Bakker07eb38b2012-12-19 14:42:06 +01003200#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003201 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003202 {
3203 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3204 {
3205 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3206 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3207 }
3208 }
3209#endif
3210
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3212 {
3213 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3214 return( ret );
3215 }
3216
3217 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3218 {
3219 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003220 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003221 }
3222
Paul Bakker1ef83d62012-04-11 12:09:53 +00003223 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003224 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3225
3226 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3227 ssl->in_hslen != 4 + hash_len )
3228 {
3229 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003230 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 }
3232
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003233 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003234 {
3235 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003236 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003237 }
3238
Paul Bakker48916f92012-09-16 19:57:18 +00003239 ssl->verify_data_len = hash_len;
3240 memcpy( ssl->peer_verify_data, buf, hash_len );
3241
Paul Bakker0a597072012-09-25 21:55:46 +00003242 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003243 {
3244 if( ssl->endpoint == SSL_IS_CLIENT )
3245 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3246
3247 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003248 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 }
3250 else
3251 ssl->state++;
3252
3253 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3254
3255 return( 0 );
3256}
3257
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003258static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003259{
3260 if( ssl->transform_negotiate )
3261 ssl_transform_free( ssl->transform_negotiate );
3262 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003263 {
3264 ssl->transform_negotiate =
3265 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003266
3267 if( ssl->transform_negotiate != NULL )
3268 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003269 }
Paul Bakker48916f92012-09-16 19:57:18 +00003270
3271 if( ssl->session_negotiate )
3272 ssl_session_free( ssl->session_negotiate );
3273 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003274 {
3275 ssl->session_negotiate =
3276 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003277
3278 if( ssl->session_negotiate != NULL )
3279 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003280 }
Paul Bakker48916f92012-09-16 19:57:18 +00003281
3282 if( ssl->handshake )
3283 ssl_handshake_free( ssl->handshake );
3284 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003285 {
3286 ssl->handshake = (ssl_handshake_params *)
3287 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003288
3289 if( ssl->handshake != NULL )
3290 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003291 }
Paul Bakker48916f92012-09-16 19:57:18 +00003292
3293 if( ssl->handshake == NULL ||
3294 ssl->transform_negotiate == NULL ||
3295 ssl->session_negotiate == NULL )
3296 {
3297 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3298 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3299 }
3300
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003301#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3302 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003303 md5_starts( &ssl->handshake->fin_md5 );
3304 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003305#endif
3306#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3307#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003308 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003309#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003310#if defined(POLARSSL_SHA512_C)
3311 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003312#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003313#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003314
3315 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003316 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003317
Paul Bakker61d113b2013-07-04 11:51:43 +02003318#if defined(POLARSSL_ECDH_C)
3319 ecdh_init( &ssl->handshake->ecdh_ctx );
3320#endif
3321
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003322#if defined(POLARSSL_X509_CRT_PARSE_C)
3323 ssl->handshake->key_cert = ssl->key_cert;
3324#endif
3325
Paul Bakker48916f92012-09-16 19:57:18 +00003326 return( 0 );
3327}
3328
Paul Bakker5121ce52009-01-03 21:22:43 +00003329/*
3330 * Initialize an SSL context
3331 */
3332int ssl_init( ssl_context *ssl )
3333{
Paul Bakker48916f92012-09-16 19:57:18 +00003334 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 int len = SSL_BUFFER_LEN;
3336
3337 memset( ssl, 0, sizeof( ssl_context ) );
3338
Paul Bakker62f2dee2012-09-28 07:31:51 +00003339 /*
3340 * Sane defaults
3341 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003342 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3343 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3344 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3345 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003346
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003347 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003348
Paul Bakker62f2dee2012-09-28 07:31:51 +00003349#if defined(POLARSSL_DHM_C)
3350 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3351 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3352 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3353 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3354 {
3355 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3356 return( ret );
3357 }
3358#endif
3359
3360 /*
3361 * Prepare base structures
3362 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003363 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003365 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 ssl->in_msg = ssl->in_ctr + 13;
3367
3368 if( ssl->in_ctr == NULL )
3369 {
3370 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003371 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003372 }
3373
Paul Bakker6e339b52013-07-03 13:37:05 +02003374 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003375 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003376 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003377 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003378
3379 if( ssl->out_ctr == NULL )
3380 {
3381 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003382 polarssl_free( ssl->in_ctr );
3383 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003384 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003385 }
3386
3387 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3388 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3389
Paul Bakker606b4ba2013-08-14 16:52:14 +02003390#if defined(POLARSSL_SSL_SESSION_TICKETS)
3391 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3392#endif
3393
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003394#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003395 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003396#endif
3397
Paul Bakker48916f92012-09-16 19:57:18 +00003398 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3399 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003400
3401 return( 0 );
3402}
3403
3404/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003405 * Reset an initialized and used SSL context for re-use while retaining
3406 * all application-set variables, function pointers and data.
3407 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003408int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003409{
Paul Bakker48916f92012-09-16 19:57:18 +00003410 int ret;
3411
Paul Bakker7eb013f2011-10-06 12:37:39 +00003412 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003413 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3414 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3415
3416 ssl->verify_data_len = 0;
3417 memset( ssl->own_verify_data, 0, 36 );
3418 memset( ssl->peer_verify_data, 0, 36 );
3419
Paul Bakker7eb013f2011-10-06 12:37:39 +00003420 ssl->in_offt = NULL;
3421
Paul Bakker92be97b2013-01-02 17:30:03 +01003422 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003423 ssl->in_msgtype = 0;
3424 ssl->in_msglen = 0;
3425 ssl->in_left = 0;
3426
3427 ssl->in_hslen = 0;
3428 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003429 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003430
Paul Bakker92be97b2013-01-02 17:30:03 +01003431 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003432 ssl->out_msgtype = 0;
3433 ssl->out_msglen = 0;
3434 ssl->out_left = 0;
3435
Paul Bakker48916f92012-09-16 19:57:18 +00003436 ssl->transform_in = NULL;
3437 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003438
3439 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3440 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003441
3442#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003443 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003444 {
3445 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003446 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003447 {
3448 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3449 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3450 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003451 }
3452#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003453
Paul Bakker48916f92012-09-16 19:57:18 +00003454 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003455 {
Paul Bakker48916f92012-09-16 19:57:18 +00003456 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003457 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003458 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003459 }
Paul Bakker48916f92012-09-16 19:57:18 +00003460
Paul Bakkerc0463502013-02-14 11:19:38 +01003461 if( ssl->session )
3462 {
3463 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003464 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003465 ssl->session = NULL;
3466 }
3467
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003468#if defined(POLARSSL_SSL_ALPN)
3469 ssl->alpn_chosen = NULL;
3470#endif
3471
Paul Bakker48916f92012-09-16 19:57:18 +00003472 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3473 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003474
3475 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003476}
3477
Paul Bakkera503a632013-08-14 13:48:06 +02003478#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003479/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003480 * Allocate and initialize ticket keys
3481 */
3482static int ssl_ticket_keys_init( ssl_context *ssl )
3483{
3484 int ret;
3485 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003486 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003487
3488 if( ssl->ticket_keys != NULL )
3489 return( 0 );
3490
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003491 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3492 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003493 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3494
3495 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003496 {
3497 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003498 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003499 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003500
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003501 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3502 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3503 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3504 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003505 polarssl_free( tkeys );
3506 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003507 }
3508
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003509 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003510 {
3511 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003512 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003513 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003514
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003515 ssl->ticket_keys = tkeys;
3516
3517 return( 0 );
3518}
Paul Bakkera503a632013-08-14 13:48:06 +02003519#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003520
3521/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003522 * SSL set accessors
3523 */
3524void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3525{
3526 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003527
Paul Bakker606b4ba2013-08-14 16:52:14 +02003528#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003529 if( endpoint == SSL_IS_CLIENT )
3530 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003531#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003532}
3533
3534void ssl_set_authmode( ssl_context *ssl, int authmode )
3535{
3536 ssl->authmode = authmode;
3537}
3538
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003539#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003540void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003541 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003542 void *p_vrfy )
3543{
3544 ssl->f_vrfy = f_vrfy;
3545 ssl->p_vrfy = p_vrfy;
3546}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003547#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003548
Paul Bakker5121ce52009-01-03 21:22:43 +00003549void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003550 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 void *p_rng )
3552{
3553 ssl->f_rng = f_rng;
3554 ssl->p_rng = p_rng;
3555}
3556
3557void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003558 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003559 void *p_dbg )
3560{
3561 ssl->f_dbg = f_dbg;
3562 ssl->p_dbg = p_dbg;
3563}
3564
3565void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003566 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003567 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003568{
3569 ssl->f_recv = f_recv;
3570 ssl->f_send = f_send;
3571 ssl->p_recv = p_recv;
3572 ssl->p_send = p_send;
3573}
3574
Paul Bakker0a597072012-09-25 21:55:46 +00003575void ssl_set_session_cache( ssl_context *ssl,
3576 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3577 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003578{
Paul Bakker0a597072012-09-25 21:55:46 +00003579 ssl->f_get_cache = f_get_cache;
3580 ssl->p_get_cache = p_get_cache;
3581 ssl->f_set_cache = f_set_cache;
3582 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003583}
3584
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003585int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003586{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003587 int ret;
3588
3589 if( ssl == NULL ||
3590 session == NULL ||
3591 ssl->session_negotiate == NULL ||
3592 ssl->endpoint != SSL_IS_CLIENT )
3593 {
3594 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3595 }
3596
3597 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3598 return( ret );
3599
Paul Bakker0a597072012-09-25 21:55:46 +00003600 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003601
3602 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003603}
3604
Paul Bakkerb68cad62012-08-23 08:34:18 +00003605void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003606{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003607 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3608 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3609 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3610 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3611}
3612
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003613void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3614 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003615 int major, int minor )
3616{
3617 if( major != SSL_MAJOR_VERSION_3 )
3618 return;
3619
3620 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3621 return;
3622
3623 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003624}
3625
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003626#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003627/* Add a new (empty) key_cert entry an return a pointer to it */
3628static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3629{
3630 ssl_key_cert *key_cert, *last;
3631
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003632 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3633 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003634 return( NULL );
3635
3636 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3637
3638 /* Append the new key_cert to the (possibly empty) current list */
3639 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003640 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003641 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003642 if( ssl->handshake != NULL )
3643 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003644 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003645 else
3646 {
3647 last = ssl->key_cert;
3648 while( last->next != NULL )
3649 last = last->next;
3650 last->next = key_cert;
3651 }
3652
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003653 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003654}
3655
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003656void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003657 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003658{
3659 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003660 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 ssl->peer_cn = peer_cn;
3662}
3663
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003664int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003665 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003666{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003667 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3668
3669 if( key_cert == NULL )
3670 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3671
3672 key_cert->cert = own_cert;
3673 key_cert->key = pk_key;
3674
3675 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003676}
3677
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003678#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003679int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003680 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003681{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003682 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003683 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003684
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003685 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003686 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3687
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003688 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3689 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003690 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003691
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003692 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003693
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003694 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003695 if( ret != 0 )
3696 return( ret );
3697
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003698 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003699 return( ret );
3700
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003701 key_cert->cert = own_cert;
3702 key_cert->key_own_alloc = 1;
3703
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003704 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003705}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003706#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003707
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003708int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003709 void *rsa_key,
3710 rsa_decrypt_func rsa_decrypt,
3711 rsa_sign_func rsa_sign,
3712 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003713{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003714 int ret;
3715 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003716
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003717 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003718 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3719
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003720 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3721 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003722 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003723
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003724 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003725
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003726 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3727 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3728 return( ret );
3729
3730 key_cert->cert = own_cert;
3731 key_cert->key_own_alloc = 1;
3732
3733 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003734}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003735#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003736
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003737#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003738int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3739 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003740{
Paul Bakker6db455e2013-09-18 17:29:31 +02003741 if( psk == NULL || psk_identity == NULL )
3742 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3743
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003744 /*
3745 * The length will be check later anyway, but in case it is obviously
3746 * too large, better abort now. The PMS is as follows:
3747 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3748 */
3749 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3750 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3751
Paul Bakker6db455e2013-09-18 17:29:31 +02003752 if( ssl->psk != NULL )
3753 {
3754 polarssl_free( ssl->psk );
3755 polarssl_free( ssl->psk_identity );
3756 }
3757
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003758 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003759 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003760
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003761 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003762 ssl->psk_identity = (unsigned char *)
3763 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003764
3765 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3766 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3767
3768 memcpy( ssl->psk, psk, ssl->psk_len );
3769 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003770
3771 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003772}
3773
3774void ssl_set_psk_cb( ssl_context *ssl,
3775 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3776 size_t),
3777 void *p_psk )
3778{
3779 ssl->f_psk = f_psk;
3780 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003781}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003782#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003783
Paul Bakker48916f92012-09-16 19:57:18 +00003784#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003785int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003786{
3787 int ret;
3788
Paul Bakker48916f92012-09-16 19:57:18 +00003789 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003790 {
3791 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3792 return( ret );
3793 }
3794
Paul Bakker48916f92012-09-16 19:57:18 +00003795 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003796 {
3797 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3798 return( ret );
3799 }
3800
3801 return( 0 );
3802}
3803
Paul Bakker1b57b062011-01-06 15:48:19 +00003804int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3805{
3806 int ret;
3807
Paul Bakker66d5d072014-06-17 16:39:18 +02003808 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003809 {
3810 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3811 return( ret );
3812 }
3813
Paul Bakker66d5d072014-06-17 16:39:18 +02003814 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003815 {
3816 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3817 return( ret );
3818 }
3819
3820 return( 0 );
3821}
Paul Bakker48916f92012-09-16 19:57:18 +00003822#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003823
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003824#if defined(POLARSSL_SSL_SET_CURVES)
3825/*
3826 * Set the allowed elliptic curves
3827 */
3828void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3829{
3830 ssl->curve_list = curve_list;
3831}
3832#endif
3833
Paul Bakker0be444a2013-08-27 21:55:01 +02003834#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003835int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003836{
3837 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003838 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003839
3840 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003841
3842 if( ssl->hostname_len + 1 == 0 )
3843 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3844
Paul Bakker6e339b52013-07-03 13:37:05 +02003845 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003846
Paul Bakkerb15b8512012-01-13 13:44:06 +00003847 if( ssl->hostname == NULL )
3848 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3849
Paul Bakker3c2122f2013-06-24 19:03:14 +02003850 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003851 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003852
Paul Bakker40ea7de2009-05-03 10:18:48 +00003853 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003854
3855 return( 0 );
3856}
3857
Paul Bakker5701cdc2012-09-27 21:49:42 +00003858void ssl_set_sni( ssl_context *ssl,
3859 int (*f_sni)(void *, ssl_context *,
3860 const unsigned char *, size_t),
3861 void *p_sni )
3862{
3863 ssl->f_sni = f_sni;
3864 ssl->p_sni = p_sni;
3865}
Paul Bakker0be444a2013-08-27 21:55:01 +02003866#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003867
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003868#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003869int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003870{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003871 size_t cur_len, tot_len;
3872 const char **p;
3873
3874 /*
3875 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3876 * truncated". Check lengths now rather than later.
3877 */
3878 tot_len = 0;
3879 for( p = protos; *p != NULL; p++ )
3880 {
3881 cur_len = strlen( *p );
3882 tot_len += cur_len;
3883
3884 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3885 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3886 }
3887
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003888 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003889
3890 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003891}
3892
3893const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3894{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003895 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003896}
3897#endif /* POLARSSL_SSL_ALPN */
3898
Paul Bakker490ecc82011-10-06 13:04:09 +00003899void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3900{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003901 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3902 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3903 {
3904 ssl->max_major_ver = major;
3905 ssl->max_minor_ver = minor;
3906 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003907}
3908
Paul Bakker1d29fb52012-09-28 13:28:45 +00003909void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3910{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003911 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3912 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3913 {
3914 ssl->min_major_ver = major;
3915 ssl->min_minor_ver = minor;
3916 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003917}
3918
Paul Bakker05decb22013-08-15 13:33:48 +02003919#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003920int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3921{
Paul Bakker77e257e2013-12-16 15:29:52 +01003922 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003923 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003924 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003925 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003926 }
3927
3928 ssl->mfl_code = mfl_code;
3929
3930 return( 0 );
3931}
Paul Bakker05decb22013-08-15 13:33:48 +02003932#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003933
Paul Bakker1f2bc622013-08-15 13:45:55 +02003934#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003935int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003936{
3937 if( ssl->endpoint != SSL_IS_CLIENT )
3938 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3939
Paul Bakker8c1ede62013-07-19 14:14:37 +02003940 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003941
3942 return( 0 );
3943}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003944#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003945
Paul Bakker48916f92012-09-16 19:57:18 +00003946void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3947{
3948 ssl->disable_renegotiation = renegotiation;
3949}
3950
3951void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3952{
3953 ssl->allow_legacy_renegotiation = allow_legacy;
3954}
3955
Paul Bakkera503a632013-08-14 13:48:06 +02003956#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003957int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3958{
3959 ssl->session_tickets = use_tickets;
3960
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003961 if( ssl->endpoint == SSL_IS_CLIENT )
3962 return( 0 );
3963
3964 if( ssl->f_rng == NULL )
3965 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3966
3967 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003968}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003969
3970void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3971{
3972 ssl->ticket_lifetime = lifetime;
3973}
Paul Bakkera503a632013-08-14 13:48:06 +02003974#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003975
Paul Bakker5121ce52009-01-03 21:22:43 +00003976/*
3977 * SSL get accessors
3978 */
Paul Bakker23986e52011-04-24 08:57:21 +00003979size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003980{
3981 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3982}
3983
Paul Bakkerff60ee62010-03-16 21:09:09 +00003984int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003985{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003986 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003987}
3988
Paul Bakkere3166ce2011-01-27 17:40:50 +00003989const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003990{
Paul Bakker926c8e42013-03-06 10:23:34 +01003991 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003992 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003993
Paul Bakkere3166ce2011-01-27 17:40:50 +00003994 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003995}
3996
Paul Bakker43ca69c2011-01-15 17:35:19 +00003997const char *ssl_get_version( const ssl_context *ssl )
3998{
3999 switch( ssl->minor_ver )
4000 {
4001 case SSL_MINOR_VERSION_0:
4002 return( "SSLv3.0" );
4003
4004 case SSL_MINOR_VERSION_1:
4005 return( "TLSv1.0" );
4006
4007 case SSL_MINOR_VERSION_2:
4008 return( "TLSv1.1" );
4009
Paul Bakker1ef83d62012-04-11 12:09:53 +00004010 case SSL_MINOR_VERSION_3:
4011 return( "TLSv1.2" );
4012
Paul Bakker43ca69c2011-01-15 17:35:19 +00004013 default:
4014 break;
4015 }
4016 return( "unknown" );
4017}
4018
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004019#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004020const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004021{
4022 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004023 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004024
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004025 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004026}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004027#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004028
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004029int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4030{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004031 if( ssl == NULL ||
4032 dst == NULL ||
4033 ssl->session == NULL ||
4034 ssl->endpoint != SSL_IS_CLIENT )
4035 {
4036 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4037 }
4038
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004039 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004040}
4041
Paul Bakker5121ce52009-01-03 21:22:43 +00004042/*
Paul Bakker1961b702013-01-25 14:49:24 +01004043 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 */
Paul Bakker1961b702013-01-25 14:49:24 +01004045int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004046{
Paul Bakker40e46942009-01-03 21:51:57 +00004047 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004048
Paul Bakker40e46942009-01-03 21:51:57 +00004049#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004051 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004052#endif
4053
Paul Bakker40e46942009-01-03 21:51:57 +00004054#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004055 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004056 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004057#endif
4058
Paul Bakker1961b702013-01-25 14:49:24 +01004059 return( ret );
4060}
4061
4062/*
4063 * Perform the SSL handshake
4064 */
4065int ssl_handshake( ssl_context *ssl )
4066{
4067 int ret = 0;
4068
4069 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4070
4071 while( ssl->state != SSL_HANDSHAKE_OVER )
4072 {
4073 ret = ssl_handshake_step( ssl );
4074
4075 if( ret != 0 )
4076 break;
4077 }
4078
Paul Bakker5121ce52009-01-03 21:22:43 +00004079 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4080
4081 return( ret );
4082}
4083
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004084#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004085/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004086 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004087 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004088static int ssl_write_hello_request( ssl_context *ssl )
4089{
4090 int ret;
4091
4092 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4093
4094 ssl->out_msglen = 4;
4095 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4096 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4097
4098 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4099 {
4100 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4101 return( ret );
4102 }
4103
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004104 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4105
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004106 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4107
4108 return( 0 );
4109}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004110#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004111
4112/*
4113 * Actually renegotiate current connection, triggered by either:
4114 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004115 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004116 * - receiving any handshake message on server during ssl_read() after the
4117 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004118 * If the handshake doesn't complete due to waiting for I/O, it will continue
4119 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004120 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004121static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004122{
4123 int ret;
4124
4125 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4126
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004127 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4128 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004129
4130 ssl->state = SSL_HELLO_REQUEST;
4131 ssl->renegotiation = SSL_RENEGOTIATION;
4132
Paul Bakker48916f92012-09-16 19:57:18 +00004133 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4134 {
4135 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4136 return( ret );
4137 }
4138
4139 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4140
4141 return( 0 );
4142}
4143
4144/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004145 * Renegotiate current connection on client,
4146 * or request renegotiation on server
4147 */
4148int ssl_renegotiate( ssl_context *ssl )
4149{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004150 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004151
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004152#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004153 /* On server, just send the request */
4154 if( ssl->endpoint == SSL_IS_SERVER )
4155 {
4156 if( ssl->state != SSL_HANDSHAKE_OVER )
4157 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4158
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004159 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004160 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004161#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004162
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004163#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004164 /*
4165 * On client, either start the renegotiation process or,
4166 * if already in progress, continue the handshake
4167 */
4168 if( ssl->renegotiation != SSL_RENEGOTIATION )
4169 {
4170 if( ssl->state != SSL_HANDSHAKE_OVER )
4171 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4172
4173 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4174 {
4175 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4176 return( ret );
4177 }
4178 }
4179 else
4180 {
4181 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4182 {
4183 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4184 return( ret );
4185 }
4186 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004187#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004188
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004189 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004190}
4191
4192/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004193 * Receive application data decrypted from the SSL layer
4194 */
Paul Bakker23986e52011-04-24 08:57:21 +00004195int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004196{
Paul Bakker23986e52011-04-24 08:57:21 +00004197 int ret;
4198 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004199
4200 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4201
4202 if( ssl->state != SSL_HANDSHAKE_OVER )
4203 {
4204 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4205 {
4206 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4207 return( ret );
4208 }
4209 }
4210
4211 if( ssl->in_offt == NULL )
4212 {
4213 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4214 {
Paul Bakker831a7552011-05-18 13:32:51 +00004215 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4216 return( 0 );
4217
Paul Bakker5121ce52009-01-03 21:22:43 +00004218 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4219 return( ret );
4220 }
4221
4222 if( ssl->in_msglen == 0 &&
4223 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4224 {
4225 /*
4226 * OpenSSL sends empty messages to randomize the IV
4227 */
4228 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4229 {
Paul Bakker831a7552011-05-18 13:32:51 +00004230 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4231 return( 0 );
4232
Paul Bakker5121ce52009-01-03 21:22:43 +00004233 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4234 return( ret );
4235 }
4236 }
4237
Paul Bakker48916f92012-09-16 19:57:18 +00004238 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4239 {
4240 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4241
4242 if( ssl->endpoint == SSL_IS_CLIENT &&
4243 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4244 ssl->in_hslen != 4 ) )
4245 {
4246 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4247 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4248 }
4249
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004250 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4251 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004252 ssl->allow_legacy_renegotiation ==
4253 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004254 {
4255 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4256
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004257#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004258 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004259 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004260 /*
4261 * SSLv3 does not have a "no_renegotiation" alert
4262 */
4263 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4264 return( ret );
4265 }
4266 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004267#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004268#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4269 defined(POLARSSL_SSL_PROTO_TLS1_2)
4270 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004271 {
4272 if( ( ret = ssl_send_alert_message( ssl,
4273 SSL_ALERT_LEVEL_WARNING,
4274 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4275 {
4276 return( ret );
4277 }
Paul Bakker48916f92012-09-16 19:57:18 +00004278 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004279 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004280#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4281 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004282 {
4283 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004284 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004285 }
Paul Bakker48916f92012-09-16 19:57:18 +00004286 }
4287 else
4288 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004289 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004290 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004291 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004292 return( ret );
4293 }
4294
4295 return( POLARSSL_ERR_NET_WANT_READ );
4296 }
4297 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004298 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4299 {
4300 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4301 "but not honored by client" ) );
4302 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4303 }
Paul Bakker48916f92012-09-16 19:57:18 +00004304 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004305 {
4306 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004307 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 }
4309
4310 ssl->in_offt = ssl->in_msg;
4311 }
4312
4313 n = ( len < ssl->in_msglen )
4314 ? len : ssl->in_msglen;
4315
4316 memcpy( buf, ssl->in_offt, n );
4317 ssl->in_msglen -= n;
4318
4319 if( ssl->in_msglen == 0 )
4320 /* all bytes consumed */
4321 ssl->in_offt = NULL;
4322 else
4323 /* more data available */
4324 ssl->in_offt += n;
4325
4326 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4327
Paul Bakker23986e52011-04-24 08:57:21 +00004328 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004329}
4330
4331/*
4332 * Send application data to be encrypted by the SSL layer
4333 */
Paul Bakker23986e52011-04-24 08:57:21 +00004334int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004335{
Paul Bakker23986e52011-04-24 08:57:21 +00004336 int ret;
4337 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004338 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004339
4340 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4341
4342 if( ssl->state != SSL_HANDSHAKE_OVER )
4343 {
4344 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4345 {
4346 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4347 return( ret );
4348 }
4349 }
4350
Paul Bakker05decb22013-08-15 13:33:48 +02004351#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004352 /*
4353 * Assume mfl_code is correct since it was checked when set
4354 */
4355 max_len = mfl_code_to_length[ssl->mfl_code];
4356
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004357 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004358 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004359 */
4360 if( ssl->session_out != NULL &&
4361 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4362 {
4363 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4364 }
Paul Bakker05decb22013-08-15 13:33:48 +02004365#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004366
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004367 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004368
Paul Bakker5121ce52009-01-03 21:22:43 +00004369 if( ssl->out_left != 0 )
4370 {
4371 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4372 {
4373 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4374 return( ret );
4375 }
4376 }
Paul Bakker887bd502011-06-08 13:10:54 +00004377 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004378 {
Paul Bakker887bd502011-06-08 13:10:54 +00004379 ssl->out_msglen = n;
4380 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4381 memcpy( ssl->out_msg, buf, n );
4382
4383 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4384 {
4385 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4386 return( ret );
4387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004388 }
4389
4390 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4391
Paul Bakker23986e52011-04-24 08:57:21 +00004392 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004393}
4394
4395/*
4396 * Notify the peer that the connection is being closed
4397 */
4398int ssl_close_notify( ssl_context *ssl )
4399{
4400 int ret;
4401
4402 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4403
4404 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4405 {
4406 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4407 return( ret );
4408 }
4409
4410 if( ssl->state == SSL_HANDSHAKE_OVER )
4411 {
Paul Bakker48916f92012-09-16 19:57:18 +00004412 if( ( ret = ssl_send_alert_message( ssl,
4413 SSL_ALERT_LEVEL_WARNING,
4414 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004415 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004416 return( ret );
4417 }
4418 }
4419
4420 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4421
4422 return( ret );
4423}
4424
Paul Bakker48916f92012-09-16 19:57:18 +00004425void ssl_transform_free( ssl_transform *transform )
4426{
4427#if defined(POLARSSL_ZLIB_SUPPORT)
4428 deflateEnd( &transform->ctx_deflate );
4429 inflateEnd( &transform->ctx_inflate );
4430#endif
4431
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004432 cipher_free_ctx( &transform->cipher_ctx_enc );
4433 cipher_free_ctx( &transform->cipher_ctx_dec );
4434
Paul Bakker61d113b2013-07-04 11:51:43 +02004435 md_free_ctx( &transform->md_ctx_enc );
4436 md_free_ctx( &transform->md_ctx_dec );
4437
Paul Bakker34617722014-06-13 17:20:13 +02004438 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004439}
4440
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004441#if defined(POLARSSL_X509_CRT_PARSE_C)
4442static void ssl_key_cert_free( ssl_key_cert *key_cert )
4443{
4444 ssl_key_cert *cur = key_cert, *next;
4445
4446 while( cur != NULL )
4447 {
4448 next = cur->next;
4449
4450 if( cur->key_own_alloc )
4451 {
4452 pk_free( cur->key );
4453 polarssl_free( cur->key );
4454 }
4455 polarssl_free( cur );
4456
4457 cur = next;
4458 }
4459}
4460#endif /* POLARSSL_X509_CRT_PARSE_C */
4461
Paul Bakker48916f92012-09-16 19:57:18 +00004462void ssl_handshake_free( ssl_handshake_params *handshake )
4463{
4464#if defined(POLARSSL_DHM_C)
4465 dhm_free( &handshake->dhm_ctx );
4466#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004467#if defined(POLARSSL_ECDH_C)
4468 ecdh_free( &handshake->ecdh_ctx );
4469#endif
4470
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004471#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004472 /* explicit void pointer cast for buggy MS compiler */
4473 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004474#endif
4475
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004476#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4477 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4478 /*
4479 * Free only the linked list wrapper, not the keys themselves
4480 * since the belong to the SNI callback
4481 */
4482 if( handshake->sni_key_cert != NULL )
4483 {
4484 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4485
4486 while( cur != NULL )
4487 {
4488 next = cur->next;
4489 polarssl_free( cur );
4490 cur = next;
4491 }
4492 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004493#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004494
Paul Bakker34617722014-06-13 17:20:13 +02004495 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004496}
4497
4498void ssl_session_free( ssl_session *session )
4499{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004500#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004501 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004502 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004503 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004504 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004505 }
Paul Bakkered27a042013-04-18 22:46:23 +02004506#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004507
Paul Bakkera503a632013-08-14 13:48:06 +02004508#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004509 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004510#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004511
Paul Bakker34617722014-06-13 17:20:13 +02004512 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004513}
4514
Paul Bakker5121ce52009-01-03 21:22:43 +00004515/*
4516 * Free an SSL context
4517 */
4518void ssl_free( ssl_context *ssl )
4519{
4520 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4521
Paul Bakker5121ce52009-01-03 21:22:43 +00004522 if( ssl->out_ctr != NULL )
4523 {
Paul Bakker34617722014-06-13 17:20:13 +02004524 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004525 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004526 }
4527
4528 if( ssl->in_ctr != NULL )
4529 {
Paul Bakker34617722014-06-13 17:20:13 +02004530 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004531 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004532 }
4533
Paul Bakker16770332013-10-11 09:59:44 +02004534#if defined(POLARSSL_ZLIB_SUPPORT)
4535 if( ssl->compress_buf != NULL )
4536 {
Paul Bakker34617722014-06-13 17:20:13 +02004537 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004538 polarssl_free( ssl->compress_buf );
4539 }
4540#endif
4541
Paul Bakker40e46942009-01-03 21:51:57 +00004542#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004543 mpi_free( &ssl->dhm_P );
4544 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004545#endif
4546
Paul Bakker48916f92012-09-16 19:57:18 +00004547 if( ssl->transform )
4548 {
4549 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004550 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004551 }
4552
4553 if( ssl->handshake )
4554 {
4555 ssl_handshake_free( ssl->handshake );
4556 ssl_transform_free( ssl->transform_negotiate );
4557 ssl_session_free( ssl->session_negotiate );
4558
Paul Bakker6e339b52013-07-03 13:37:05 +02004559 polarssl_free( ssl->handshake );
4560 polarssl_free( ssl->transform_negotiate );
4561 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004562 }
4563
Paul Bakkerc0463502013-02-14 11:19:38 +01004564 if( ssl->session )
4565 {
4566 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004567 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004568 }
4569
Paul Bakkera503a632013-08-14 13:48:06 +02004570#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004571 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004572#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004573
Paul Bakker0be444a2013-08-27 21:55:01 +02004574#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004575 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004576 {
Paul Bakker34617722014-06-13 17:20:13 +02004577 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004578 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004579 ssl->hostname_len = 0;
4580 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004581#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004582
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004583#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004584 if( ssl->psk != NULL )
4585 {
Paul Bakker34617722014-06-13 17:20:13 +02004586 polarssl_zeroize( ssl->psk, ssl->psk_len );
4587 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004588 polarssl_free( ssl->psk );
4589 polarssl_free( ssl->psk_identity );
4590 ssl->psk_len = 0;
4591 ssl->psk_identity_len = 0;
4592 }
4593#endif
4594
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004595#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004596 ssl_key_cert_free( ssl->key_cert );
4597#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004598
Paul Bakker05ef8352012-05-08 09:17:57 +00004599#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4600 if( ssl_hw_record_finish != NULL )
4601 {
4602 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4603 ssl_hw_record_finish( ssl );
4604 }
4605#endif
4606
Paul Bakker5121ce52009-01-03 21:22:43 +00004607 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004608
Paul Bakker86f04f42013-02-14 11:20:09 +01004609 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004610 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004611}
4612
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004613#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004614/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004615 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004616 */
4617unsigned char ssl_sig_from_pk( pk_context *pk )
4618{
4619#if defined(POLARSSL_RSA_C)
4620 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4621 return( SSL_SIG_RSA );
4622#endif
4623#if defined(POLARSSL_ECDSA_C)
4624 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4625 return( SSL_SIG_ECDSA );
4626#endif
4627 return( SSL_SIG_ANON );
4628}
4629
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004630pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4631{
4632 switch( sig )
4633 {
4634#if defined(POLARSSL_RSA_C)
4635 case SSL_SIG_RSA:
4636 return( POLARSSL_PK_RSA );
4637#endif
4638#if defined(POLARSSL_ECDSA_C)
4639 case SSL_SIG_ECDSA:
4640 return( POLARSSL_PK_ECDSA );
4641#endif
4642 default:
4643 return( POLARSSL_PK_NONE );
4644 }
4645}
Paul Bakker9af723c2014-05-01 13:03:14 +02004646#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004647
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004648/*
4649 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4650 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004651md_type_t ssl_md_alg_from_hash( unsigned char hash )
4652{
4653 switch( hash )
4654 {
4655#if defined(POLARSSL_MD5_C)
4656 case SSL_HASH_MD5:
4657 return( POLARSSL_MD_MD5 );
4658#endif
4659#if defined(POLARSSL_SHA1_C)
4660 case SSL_HASH_SHA1:
4661 return( POLARSSL_MD_SHA1 );
4662#endif
4663#if defined(POLARSSL_SHA256_C)
4664 case SSL_HASH_SHA224:
4665 return( POLARSSL_MD_SHA224 );
4666 case SSL_HASH_SHA256:
4667 return( POLARSSL_MD_SHA256 );
4668#endif
4669#if defined(POLARSSL_SHA512_C)
4670 case SSL_HASH_SHA384:
4671 return( POLARSSL_MD_SHA384 );
4672 case SSL_HASH_SHA512:
4673 return( POLARSSL_MD_SHA512 );
4674#endif
4675 default:
4676 return( POLARSSL_MD_NONE );
4677 }
4678}
4679
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004680#if defined(POLARSSL_SSL_SET_CURVES)
4681/*
4682 * Check is a curve proposed by the peer is in our list.
4683 * Return 1 if we're willing to use it, 0 otherwise.
4684 */
4685int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4686{
4687 const ecp_group_id *gid;
4688
4689 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4690 if( *gid == grp_id )
4691 return( 1 );
4692
4693 return( 0 );
4694}
Paul Bakker9af723c2014-05-01 13:03:14 +02004695#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004696
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004697#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004698int ssl_check_cert_usage( const x509_crt *cert,
4699 const ssl_ciphersuite_t *ciphersuite,
4700 int cert_endpoint )
4701{
4702#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4703 int usage = 0;
4704#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004705#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4706 const char *ext_oid;
4707 size_t ext_len;
4708#endif
4709
4710#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4711 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4712 ((void) cert);
4713 ((void) cert_endpoint);
4714#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004715
4716#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4717 if( cert_endpoint == SSL_IS_SERVER )
4718 {
4719 /* Server part of the key exchange */
4720 switch( ciphersuite->key_exchange )
4721 {
4722 case POLARSSL_KEY_EXCHANGE_RSA:
4723 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4724 usage = KU_KEY_ENCIPHERMENT;
4725 break;
4726
4727 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4728 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4729 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4730 usage = KU_DIGITAL_SIGNATURE;
4731 break;
4732
4733 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4734 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4735 usage = KU_KEY_AGREEMENT;
4736 break;
4737
4738 /* Don't use default: we want warnings when adding new values */
4739 case POLARSSL_KEY_EXCHANGE_NONE:
4740 case POLARSSL_KEY_EXCHANGE_PSK:
4741 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4742 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4743 usage = 0;
4744 }
4745 }
4746 else
4747 {
4748 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4749 usage = KU_DIGITAL_SIGNATURE;
4750 }
4751
4752 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4753 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004754#else
4755 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004756#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4757
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004758#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4759 if( cert_endpoint == SSL_IS_SERVER )
4760 {
4761 ext_oid = OID_SERVER_AUTH;
4762 ext_len = OID_SIZE( OID_SERVER_AUTH );
4763 }
4764 else
4765 {
4766 ext_oid = OID_CLIENT_AUTH;
4767 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4768 }
4769
4770 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4771 return( -1 );
4772#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4773
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004774 return( 0 );
4775}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004776#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004777
4778#endif /* POLARSSL_SSL_TLS_C */