blob: e3b0a17757ca4d5ffdd2d1cbc0a2223b4dc29a0a [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é-Gonnard2e5ee322014-05-14 13:09:22 +0200514 if( cipher_info->mode == POLARSSL_MODE_GCM ||
515 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 {
Paul Bakker68884e32013-01-07 18:20:04 +0100517 transform->keylen = cipher_info->key_length;
518 transform->keylen /= 8;
519 transform->minlen = 1;
520 transform->ivlen = 12;
521 transform->fixed_ivlen = 4;
522 transform->maclen = 0;
523 }
524 else
525 {
526 if( md_info->type != POLARSSL_MD_NONE )
527 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200528 int ret;
529
Paul Bakker61d113b2013-07-04 11:51:43 +0200530 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
531 {
532 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
533 return( ret );
534 }
535
536 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
537 {
538 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
539 return( ret );
540 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker68884e32013-01-07 18:20:04 +0100542 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200543
Paul Bakker1f2bc622013-08-15 13:45:55 +0200544#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200545 /*
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;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200552#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100553 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
Paul Bakker68884e32013-01-07 18:20:04 +0100555 transform->keylen = cipher_info->key_length;
556 transform->keylen /= 8;
557 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
Paul Bakker68884e32013-01-07 18:20:04 +0100559 transform->minlen = transform->keylen;
560 if( transform->minlen < transform->maclen )
561 {
562 if( cipher_info->mode == POLARSSL_MODE_STREAM )
563 transform->minlen = transform->maclen;
564 else
565 transform->minlen += transform->keylen;
566 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 }
568
569 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000570 transform->keylen, transform->minlen, transform->ivlen,
571 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
573 /*
574 * Finally setup the cipher contexts, IVs and MAC secrets.
575 */
576 if( ssl->endpoint == SSL_IS_CLIENT )
577 {
Paul Bakker48916f92012-09-16 19:57:18 +0000578 key1 = keyblk + transform->maclen * 2;
579 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Paul Bakker68884e32013-01-07 18:20:04 +0100581 mac_enc = keyblk;
582 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000584 /*
585 * This is not used in TLS v1.1.
586 */
Paul Bakker48916f92012-09-16 19:57:18 +0000587 iv_copy_len = ( transform->fixed_ivlen ) ?
588 transform->fixed_ivlen : transform->ivlen;
589 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
590 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000591 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 }
593 else
594 {
Paul Bakker48916f92012-09-16 19:57:18 +0000595 key1 = keyblk + transform->maclen * 2 + transform->keylen;
596 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
Paul Bakker68884e32013-01-07 18:20:04 +0100598 mac_enc = keyblk + transform->maclen;
599 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000601 /*
602 * This is not used in TLS v1.1.
603 */
Paul Bakker48916f92012-09-16 19:57:18 +0000604 iv_copy_len = ( transform->fixed_ivlen ) ?
605 transform->fixed_ivlen : transform->ivlen;
606 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
607 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000608 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 }
610
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200611#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100612 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
613 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100614 if( transform->maclen > sizeof transform->mac_enc )
615 {
616 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200617 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100618 }
619
Paul Bakker68884e32013-01-07 18:20:04 +0100620 memcpy( transform->mac_enc, mac_enc, transform->maclen );
621 memcpy( transform->mac_dec, mac_dec, transform->maclen );
622 }
623 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200624#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200625#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
626 defined(POLARSSL_SSL_PROTO_TLS1_2)
627 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100628 {
629 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
630 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
631 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200632 else
633#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200634 {
635 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200636 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200637 }
Paul Bakker68884e32013-01-07 18:20:04 +0100638
Paul Bakker05ef8352012-05-08 09:17:57 +0000639#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200640 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000641 {
642 int ret = 0;
643
644 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
645
Paul Bakker07eb38b2012-12-19 14:42:06 +0100646 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
647 transform->iv_enc, transform->iv_dec,
648 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100649 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100650 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000651 {
652 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200653 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000654 }
655 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200656#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000657
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200658 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
659 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200661 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
662 return( ret );
663 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200664
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200665 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
666 cipher_info ) ) != 0 )
667 {
668 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
669 return( ret );
670 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200671
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200672 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
673 cipher_info->key_length,
674 POLARSSL_ENCRYPT ) ) != 0 )
675 {
676 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
677 return( ret );
678 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200679
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200680 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
681 cipher_info->key_length,
682 POLARSSL_DECRYPT ) ) != 0 )
683 {
684 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
685 return( ret );
686 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200687
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200688#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200689 if( cipher_info->mode == POLARSSL_MODE_CBC )
690 {
691 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
692 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200693 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200694 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
695 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200696 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200697
698 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
699 POLARSSL_PADDING_NONE ) ) != 0 )
700 {
701 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
702 return( ret );
703 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200705#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
Paul Bakker34617722014-06-13 17:20:13 +0200707 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
Paul Bakker2770fbd2012-07-03 13:30:23 +0000709#if defined(POLARSSL_ZLIB_SUPPORT)
710 // Initialize compression
711 //
Paul Bakker48916f92012-09-16 19:57:18 +0000712 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000713 {
Paul Bakker16770332013-10-11 09:59:44 +0200714 if( ssl->compress_buf == NULL )
715 {
716 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
717 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
718 if( ssl->compress_buf == NULL )
719 {
720 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
721 SSL_BUFFER_LEN ) );
722 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
723 }
724 }
725
Paul Bakker2770fbd2012-07-03 13:30:23 +0000726 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
727
Paul Bakker48916f92012-09-16 19:57:18 +0000728 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
729 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000730
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200731 if( deflateInit( &transform->ctx_deflate,
732 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000733 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000734 {
735 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
736 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
737 }
738 }
739#endif /* POLARSSL_ZLIB_SUPPORT */
740
Paul Bakker5121ce52009-01-03 21:22:43 +0000741 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
742
743 return( 0 );
744}
745
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200746#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000747void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000748{
749 md5_context md5;
750 sha1_context sha1;
751 unsigned char pad_1[48];
752 unsigned char pad_2[48];
753
Paul Bakker380da532012-04-18 16:10:25 +0000754 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Paul Bakker48916f92012-09-16 19:57:18 +0000756 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
757 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Paul Bakker380da532012-04-18 16:10:25 +0000759 memset( pad_1, 0x36, 48 );
760 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
Paul Bakker48916f92012-09-16 19:57:18 +0000762 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000763 md5_update( &md5, pad_1, 48 );
764 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Paul Bakker380da532012-04-18 16:10:25 +0000766 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000767 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000768 md5_update( &md5, pad_2, 48 );
769 md5_update( &md5, hash, 16 );
770 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
Paul Bakker48916f92012-09-16 19:57:18 +0000772 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000773 sha1_update( &sha1, pad_1, 40 );
774 sha1_finish( &sha1, hash + 16 );
775
776 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000777 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000778 sha1_update( &sha1, pad_2, 40 );
779 sha1_update( &sha1, hash + 16, 20 );
780 sha1_finish( &sha1, hash + 16 );
781
782 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
783 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
784
785 return;
786}
Paul Bakker9af723c2014-05-01 13:03:14 +0200787#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000788
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200789#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000790void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
791{
792 md5_context md5;
793 sha1_context sha1;
794
795 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
796
Paul Bakker48916f92012-09-16 19:57:18 +0000797 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
798 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000799
Paul Bakker48916f92012-09-16 19:57:18 +0000800 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000801 sha1_finish( &sha1, hash + 16 );
802
803 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
804 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
805
806 return;
807}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200808#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000809
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200810#if defined(POLARSSL_SSL_PROTO_TLS1_2)
811#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000812void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
813{
Paul Bakker9e36f042013-06-30 14:34:05 +0200814 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000815
816 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
817
Paul Bakker9e36f042013-06-30 14:34:05 +0200818 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
819 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000820
821 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
822 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
823
824 return;
825}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200826#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000827
Paul Bakker9e36f042013-06-30 14:34:05 +0200828#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000829void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
830{
Paul Bakker9e36f042013-06-30 14:34:05 +0200831 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000832
833 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
834
Paul Bakker9e36f042013-06-30 14:34:05 +0200835 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
836 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000837
Paul Bakkerca4ab492012-04-18 14:23:57 +0000838 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
840
841 return;
842}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200843#endif /* POLARSSL_SHA512_C */
844#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200846#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200847int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
848{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200849 unsigned char *p = ssl->handshake->premaster;
850 unsigned char *end = p + sizeof( ssl->handshake->premaster );
851
852 /*
853 * PMS = struct {
854 * opaque other_secret<0..2^16-1>;
855 * opaque psk<0..2^16-1>;
856 * };
857 * with "other_secret" depending on the particular key exchange
858 */
859#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
860 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
861 {
862 if( end - p < 2 + (int) ssl->psk_len )
863 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
864
865 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
866 *(p++) = (unsigned char)( ssl->psk_len );
867 p += ssl->psk_len;
868 }
869 else
870#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200871#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
872 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
873 {
874 /*
875 * other_secret already set by the ClientKeyExchange message,
876 * and is 48 bytes long
877 */
878 *p++ = 0;
879 *p++ = 48;
880 p += 48;
881 }
882 else
883#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200884#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
885 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
886 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200887 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200888 size_t len = ssl->handshake->dhm_ctx.len;
889
890 if( end - p < 2 + (int) len )
891 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
892
893 *(p++) = (unsigned char)( len >> 8 );
894 *(p++) = (unsigned char)( len );
895 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
896 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
897 {
898 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
899 return( ret );
900 }
901 p += len;
902
903 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
904 }
905 else
906#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
907#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
908 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_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 zlen;
912
913 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200914 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200915 ssl->f_rng, ssl->p_rng ) ) != 0 )
916 {
917 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
918 return( ret );
919 }
920
921 *(p++) = (unsigned char)( zlen >> 8 );
922 *(p++) = (unsigned char)( zlen );
923 p += zlen;
924
925 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
926 }
927 else
928#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
929 {
930 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200931 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932 }
933
934 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100935 if( end - p < 2 + (int) ssl->psk_len )
936 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
937
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200938 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
939 *(p++) = (unsigned char)( ssl->psk_len );
940 memcpy( p, ssl->psk, ssl->psk_len );
941 p += ssl->psk_len;
942
943 ssl->handshake->pmslen = p - ssl->handshake->premaster;
944
945 return( 0 );
946}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200947#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200948
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200949#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000950/*
951 * SSLv3.0 MAC functions
952 */
Paul Bakker68884e32013-01-07 18:20:04 +0100953static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
954 unsigned char *buf, size_t len,
955 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000956{
957 unsigned char header[11];
958 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100959 int padlen = 0;
960 int md_size = md_get_size( md_ctx->md_info );
961 int md_type = md_get_type( md_ctx->md_info );
962
963 if( md_type == POLARSSL_MD_MD5 )
964 padlen = 48;
965 else if( md_type == POLARSSL_MD_SHA1 )
966 padlen = 40;
967 else if( md_type == POLARSSL_MD_SHA256 )
968 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100969 else if( md_type == POLARSSL_MD_SHA384 )
970 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
972 memcpy( header, ctr, 8 );
973 header[ 8] = (unsigned char) type;
974 header[ 9] = (unsigned char)( len >> 8 );
975 header[10] = (unsigned char)( len );
976
Paul Bakker68884e32013-01-07 18:20:04 +0100977 memset( padding, 0x36, padlen );
978 md_starts( md_ctx );
979 md_update( md_ctx, secret, md_size );
980 md_update( md_ctx, padding, padlen );
981 md_update( md_ctx, header, 11 );
982 md_update( md_ctx, buf, len );
983 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
Paul Bakker68884e32013-01-07 18:20:04 +0100985 memset( padding, 0x5C, padlen );
986 md_starts( md_ctx );
987 md_update( md_ctx, secret, md_size );
988 md_update( md_ctx, padding, padlen );
989 md_update( md_ctx, buf + len, md_size );
990 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000991}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200992#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000993
Paul Bakker5121ce52009-01-03 21:22:43 +0000994/*
995 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200996 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000997static int ssl_encrypt_buf( ssl_context *ssl )
998{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200999 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001000 const cipher_mode_t mode = cipher_get_cipher_mode(
1001 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
1003 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1004
1005 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001006 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001007 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001008#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1009 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1010 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001011 if( mode != POLARSSL_MODE_GCM &&
1012 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001014#if defined(POLARSSL_SSL_PROTO_SSL3)
1015 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1016 {
1017 ssl_mac( &ssl->transform_out->md_ctx_enc,
1018 ssl->transform_out->mac_enc,
1019 ssl->out_msg, ssl->out_msglen,
1020 ssl->out_ctr, ssl->out_msgtype );
1021 }
1022 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001023#endif
1024#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001025 defined(POLARSSL_SSL_PROTO_TLS1_2)
1026 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1027 {
1028 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1029 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1030 ssl->out_msg, ssl->out_msglen );
1031 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1032 ssl->out_msg + ssl->out_msglen );
1033 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1034 }
1035 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001036#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001037 {
1038 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001039 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001040 }
1041
1042 SSL_DEBUG_BUF( 4, "computed mac",
1043 ssl->out_msg + ssl->out_msglen,
1044 ssl->transform_out->maclen );
1045
1046 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001047 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001048#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001050 /*
1051 * Encrypt
1052 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001053#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001054 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001056 int ret;
1057 size_t olen = 0;
1058
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1060 "including %d bytes of padding",
1061 ssl->out_msglen, 0 ) );
1062
1063 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1064 ssl->out_msg, ssl->out_msglen );
1065
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001066 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001067 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001068 ssl->transform_out->ivlen,
1069 ssl->out_msg, ssl->out_msglen,
1070 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001071 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001072 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001073 return( ret );
1074 }
1075
1076 if( ssl->out_msglen != olen )
1077 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001078 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001079 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001080 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 }
Paul Bakker68884e32013-01-07 18:20:04 +01001082 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001083#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001084#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1085 if( mode == POLARSSL_MODE_GCM ||
1086 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001087 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001088 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001089 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001090 unsigned char *enc_msg;
1091 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001092 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1093 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001094
Paul Bakkerca4ab492012-04-18 14:23:57 +00001095 memcpy( add_data, ssl->out_ctr, 8 );
1096 add_data[8] = ssl->out_msgtype;
1097 add_data[9] = ssl->major_ver;
1098 add_data[10] = ssl->minor_ver;
1099 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1100 add_data[12] = ssl->out_msglen & 0xFF;
1101
1102 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1103 add_data, 13 );
1104
Paul Bakker68884e32013-01-07 18:20:04 +01001105 /*
1106 * Generate IV
1107 */
1108 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001109 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1110 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001111 if( ret != 0 )
1112 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001113
Paul Bakker68884e32013-01-07 18:20:04 +01001114 memcpy( ssl->out_iv,
1115 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1116 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001117
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001118 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001119 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001120
Paul Bakker68884e32013-01-07 18:20:04 +01001121 /*
1122 * Fix pointer positions and message length with added IV
1123 */
1124 enc_msg = ssl->out_msg;
1125 enc_msglen = ssl->out_msglen;
1126 ssl->out_msglen += ssl->transform_out->ivlen -
1127 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001128
Paul Bakker68884e32013-01-07 18:20:04 +01001129 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1130 "including %d bytes of padding",
1131 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001132
Paul Bakker68884e32013-01-07 18:20:04 +01001133 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1134 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001135
Paul Bakker68884e32013-01-07 18:20:04 +01001136 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001137 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001138 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001139 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1140 ssl->transform_out->iv_enc,
1141 ssl->transform_out->ivlen,
1142 add_data, 13,
1143 enc_msg, enc_msglen,
1144 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001145 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001146 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001147 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001148 return( ret );
1149 }
1150
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001151 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001152 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001153 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001154 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001155 }
1156
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001157 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001158
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001159 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001160 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001162#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001163#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1164 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001165 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001167 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001168 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001169 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001170
Paul Bakker48916f92012-09-16 19:57:18 +00001171 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1172 ssl->transform_out->ivlen;
1173 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 padlen = 0;
1175
1176 for( i = 0; i <= padlen; i++ )
1177 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1178
1179 ssl->out_msglen += padlen + 1;
1180
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001181 enc_msglen = ssl->out_msglen;
1182 enc_msg = ssl->out_msg;
1183
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001184#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001185 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001186 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1187 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001188 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001189 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001190 {
1191 /*
1192 * Generate IV
1193 */
Paul Bakker48916f92012-09-16 19:57:18 +00001194 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1195 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001196 if( ret != 0 )
1197 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001198
Paul Bakker92be97b2013-01-02 17:30:03 +01001199 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001200 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001201
1202 /*
1203 * Fix pointer positions and message length with added IV
1204 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001205 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001206 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001207 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001208 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001209#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001210
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001212 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001213 ssl->out_msglen, ssl->transform_out->ivlen,
1214 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
1216 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001217 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001219 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001220 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001221 ssl->transform_out->ivlen,
1222 enc_msg, enc_msglen,
1223 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001224 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001225 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001226 return( ret );
1227 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001228
Paul Bakkercca5b812013-08-31 17:40:26 +02001229 if( enc_msglen != olen )
1230 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001231 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001232 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001233 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001234
1235#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001236 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1237 {
1238 /*
1239 * Save IV in SSL3 and TLS1
1240 */
1241 memcpy( ssl->transform_out->iv_enc,
1242 ssl->transform_out->cipher_ctx_enc.iv,
1243 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001245#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001247 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001248#endif /* POLARSSL_CIPHER_MODE_CBC &&
1249 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001250 {
1251 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001252 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001253 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Paul Bakkerca4ab492012-04-18 14:23:57 +00001255 for( i = 8; i > 0; i-- )
1256 if( ++ssl->out_ctr[i - 1] != 0 )
1257 break;
1258
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001259 /* The loops goes to its end iff the counter is wrapping */
1260 if( i == 0 )
1261 {
1262 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1263 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1264 }
1265
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1267
1268 return( 0 );
1269}
1270
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001271#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001272
Paul Bakker5121ce52009-01-03 21:22:43 +00001273static int ssl_decrypt_buf( ssl_context *ssl )
1274{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001275 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001276 const cipher_mode_t mode = cipher_get_cipher_mode(
1277 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001278#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1279 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1280 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1281 size_t padlen = 0, correct = 1;
1282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
1284 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1285
Paul Bakker48916f92012-09-16 19:57:18 +00001286 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 {
1288 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001289 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001290 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 }
1292
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001293#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001294 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001295 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001296 int ret;
1297 size_t olen = 0;
1298
Paul Bakker68884e32013-01-07 18:20:04 +01001299 padlen = 0;
1300
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001301 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001302 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001303 ssl->transform_in->ivlen,
1304 ssl->in_msg, ssl->in_msglen,
1305 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001306 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001307 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001308 return( ret );
1309 }
1310
1311 if( ssl->in_msglen != olen )
1312 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001313 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001314 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 }
Paul Bakker68884e32013-01-07 18:20:04 +01001317 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001318#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001319#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1320 if( mode == POLARSSL_MODE_GCM ||
1321 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001322 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001323 int ret;
1324 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001325 unsigned char *dec_msg;
1326 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001327 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001328 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1329 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001330 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1331 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001332
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001333 if( ssl->in_msglen < explicit_iv_len + taglen )
1334 {
1335 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1336 "+ taglen (%d)", ssl->in_msglen,
1337 explicit_iv_len, taglen ) );
1338 return( POLARSSL_ERR_SSL_INVALID_MAC );
1339 }
1340 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1341
Paul Bakker68884e32013-01-07 18:20:04 +01001342 dec_msg = ssl->in_msg;
1343 dec_msg_result = ssl->in_msg;
1344 ssl->in_msglen = dec_msglen;
1345
1346 memcpy( add_data, ssl->in_ctr, 8 );
1347 add_data[8] = ssl->in_msgtype;
1348 add_data[9] = ssl->major_ver;
1349 add_data[10] = ssl->minor_ver;
1350 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1351 add_data[12] = ssl->in_msglen & 0xFF;
1352
1353 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1354 add_data, 13 );
1355
1356 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1357 ssl->in_iv,
1358 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1359
1360 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1361 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001362 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001363
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001364 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001365 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001366 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001367 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1368 ssl->transform_in->iv_dec,
1369 ssl->transform_in->ivlen,
1370 add_data, 13,
1371 dec_msg, dec_msglen,
1372 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001373 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001374 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001375 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1376
1377 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1378 return( POLARSSL_ERR_SSL_INVALID_MAC );
1379
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001380 return( ret );
1381 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001382
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001383 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001384 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001385 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001386 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001387 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001390#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001391#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1392 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001393 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 {
Paul Bakker45829992013-01-03 14:52:21 +01001395 /*
1396 * Decrypt and check the padding
1397 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001398 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001399 unsigned char *dec_msg;
1400 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001401 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001402 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001403 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001404
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 /*
Paul Bakker45829992013-01-03 14:52:21 +01001406 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 */
Paul Bakker48916f92012-09-16 19:57:18 +00001408 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 {
1410 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001411 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001412 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 }
1414
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001415#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001416 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1417 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001418#endif
Paul Bakker45829992013-01-03 14:52:21 +01001419
1420 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1421 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1422 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001423 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1424 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1425 ssl->transform_in->ivlen,
1426 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001427 return( POLARSSL_ERR_SSL_INVALID_MAC );
1428 }
1429
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001430 dec_msglen = ssl->in_msglen;
1431 dec_msg = ssl->in_msg;
1432 dec_msg_result = ssl->in_msg;
1433
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001434#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001435 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001436 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001437 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001438 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001439 {
Paul Bakker48916f92012-09-16 19:57:18 +00001440 dec_msglen -= ssl->transform_in->ivlen;
1441 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001442
Paul Bakker48916f92012-09-16 19:57:18 +00001443 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001444 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001445 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001446#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001447
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001448 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001449 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001450 ssl->transform_in->ivlen,
1451 dec_msg, dec_msglen,
1452 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001453 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001454 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001455 return( ret );
1456 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001457
Paul Bakkercca5b812013-08-31 17:40:26 +02001458 if( dec_msglen != olen )
1459 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001460 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001461 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001462 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001463
1464#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001465 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1466 {
1467 /*
1468 * Save IV in SSL3 and TLS1
1469 */
1470 memcpy( ssl->transform_in->iv_dec,
1471 ssl->transform_in->cipher_ctx_dec.iv,
1472 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001474#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001475
1476 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001477
1478 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1479 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001480#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001481 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1482 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001483#endif
Paul Bakker45829992013-01-03 14:52:21 +01001484 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001485 correct = 0;
1486 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001487
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001488#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1490 {
Paul Bakker48916f92012-09-16 19:57:18 +00001491 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001493#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001494 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1495 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001496 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001497#endif
Paul Bakker45829992013-01-03 14:52:21 +01001498 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 }
1500 }
1501 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001502#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001503#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1504 defined(POLARSSL_SSL_PROTO_TLS1_2)
1505 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 {
1507 /*
Paul Bakker45829992013-01-03 14:52:21 +01001508 * TLSv1+: always check the padding up to the first failure
1509 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001511 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001512 size_t padding_idx = ssl->in_msglen - padlen - 1;
1513
Paul Bakker956c9e02013-12-19 14:42:28 +01001514 /*
1515 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001516 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001517 *
Paul Bakker61885c72014-04-25 12:59:03 +02001518 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1519 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001520 *
1521 * In both cases we reset padding_idx to a safe value (0) to
1522 * prevent out-of-buffer reads.
1523 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001524 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001525 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1526 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001527
1528 padding_idx *= correct;
1529
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001530 for( i = 1; i <= 256; i++ )
1531 {
1532 real_count &= ( i <= padlen );
1533 pad_count += real_count *
1534 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1535 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001536
1537 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001538
Paul Bakkerd66f0702013-01-31 16:57:45 +01001539#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001540 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001541 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001542#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001543 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001545 else
1546#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1547 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001548 {
1549 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001550 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001553 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001554#endif /* POLARSSL_CIPHER_MODE_CBC &&
1555 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001556 {
1557 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001558 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001560
1561 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1562 ssl->in_msg, ssl->in_msglen );
1563
1564 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001565 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001567#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1568 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1569 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001570 if( mode != POLARSSL_MODE_GCM &&
1571 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001572 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001573 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1574
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001575 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001576
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001577 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1578 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001579
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001580 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001582#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001583 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1584 {
1585 ssl_mac( &ssl->transform_in->md_ctx_dec,
1586 ssl->transform_in->mac_dec,
1587 ssl->in_msg, ssl->in_msglen,
1588 ssl->in_ctr, ssl->in_msgtype );
1589 }
1590 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001591#endif /* POLARSSL_SSL_PROTO_SSL3 */
1592#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001593 defined(POLARSSL_SSL_PROTO_TLS1_2)
1594 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1595 {
1596 /*
1597 * Process MAC and always update for padlen afterwards to make
1598 * total time independent of padlen
1599 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001600 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001601 *
1602 * Known timing attacks:
1603 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1604 *
1605 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1606 * correctly. (We round down instead of up, so -56 is the correct
1607 * value for our calculations instead of -55)
1608 */
1609 size_t j, extra_run = 0;
1610 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1611 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001612
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001613 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001614
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001615 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1616 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1617 ssl->in_msglen );
1618 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1619 ssl->in_msg + ssl->in_msglen );
1620 for( j = 0; j < extra_run; j++ )
1621 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001622
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001623 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1624 }
1625 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001626#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001627 POLARSSL_SSL_PROTO_TLS1_2 */
1628 {
1629 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001630 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001631 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001632
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001633 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1634 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1635 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001637 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001638 ssl->transform_in->maclen ) != 0 )
1639 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001640#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001641 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001642#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001643 correct = 0;
1644 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001645
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001646 /*
1647 * Finally check the correct flag
1648 */
1649 if( correct == 0 )
1650 return( POLARSSL_ERR_SSL_INVALID_MAC );
1651 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001652#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
1654 if( ssl->in_msglen == 0 )
1655 {
1656 ssl->nb_zero++;
1657
1658 /*
1659 * Three or more empty messages may be a DoS attack
1660 * (excessive CPU consumption).
1661 */
1662 if( ssl->nb_zero > 3 )
1663 {
1664 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1665 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001666 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 }
1668 }
1669 else
1670 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001671
Paul Bakker23986e52011-04-24 08:57:21 +00001672 for( i = 8; i > 0; i-- )
1673 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 break;
1675
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001676 /* The loops goes to its end iff the counter is wrapping */
1677 if( i == 0 )
1678 {
1679 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1680 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1681 }
1682
Paul Bakker5121ce52009-01-03 21:22:43 +00001683 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1684
1685 return( 0 );
1686}
1687
Paul Bakker2770fbd2012-07-03 13:30:23 +00001688#if defined(POLARSSL_ZLIB_SUPPORT)
1689/*
1690 * Compression/decompression functions
1691 */
1692static int ssl_compress_buf( ssl_context *ssl )
1693{
1694 int ret;
1695 unsigned char *msg_post = ssl->out_msg;
1696 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001697 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001698
1699 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1700
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001701 if( len_pre == 0 )
1702 return( 0 );
1703
Paul Bakker2770fbd2012-07-03 13:30:23 +00001704 memcpy( msg_pre, ssl->out_msg, len_pre );
1705
1706 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1707 ssl->out_msglen ) );
1708
1709 SSL_DEBUG_BUF( 4, "before compression: output payload",
1710 ssl->out_msg, ssl->out_msglen );
1711
Paul Bakker48916f92012-09-16 19:57:18 +00001712 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1713 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1714 ssl->transform_out->ctx_deflate.next_out = msg_post;
1715 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001716
Paul Bakker48916f92012-09-16 19:57:18 +00001717 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001718 if( ret != Z_OK )
1719 {
1720 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1721 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1722 }
1723
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001724 ssl->out_msglen = SSL_BUFFER_LEN -
1725 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001726
Paul Bakker2770fbd2012-07-03 13:30:23 +00001727 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1728 ssl->out_msglen ) );
1729
1730 SSL_DEBUG_BUF( 4, "after compression: output payload",
1731 ssl->out_msg, ssl->out_msglen );
1732
1733 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1734
1735 return( 0 );
1736}
1737
1738static int ssl_decompress_buf( ssl_context *ssl )
1739{
1740 int ret;
1741 unsigned char *msg_post = ssl->in_msg;
1742 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001743 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001744
1745 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1746
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001747 if( len_pre == 0 )
1748 return( 0 );
1749
Paul Bakker2770fbd2012-07-03 13:30:23 +00001750 memcpy( msg_pre, ssl->in_msg, len_pre );
1751
1752 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1753 ssl->in_msglen ) );
1754
1755 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1756 ssl->in_msg, ssl->in_msglen );
1757
Paul Bakker48916f92012-09-16 19:57:18 +00001758 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1759 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1760 ssl->transform_in->ctx_inflate.next_out = msg_post;
1761 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001762
Paul Bakker48916f92012-09-16 19:57:18 +00001763 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764 if( ret != Z_OK )
1765 {
1766 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1767 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1768 }
1769
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001770 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1771 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001772
Paul Bakker2770fbd2012-07-03 13:30:23 +00001773 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1774 ssl->in_msglen ) );
1775
1776 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1777 ssl->in_msg, ssl->in_msglen );
1778
1779 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1780
1781 return( 0 );
1782}
1783#endif /* POLARSSL_ZLIB_SUPPORT */
1784
Paul Bakker5121ce52009-01-03 21:22:43 +00001785/*
1786 * Fill the input message buffer
1787 */
Paul Bakker23986e52011-04-24 08:57:21 +00001788int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001789{
Paul Bakker23986e52011-04-24 08:57:21 +00001790 int ret;
1791 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
1793 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1794
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001795 if( nb_want > SSL_BUFFER_LEN - 8 )
1796 {
1797 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1798 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1799 }
1800
Paul Bakker5121ce52009-01-03 21:22:43 +00001801 while( ssl->in_left < nb_want )
1802 {
1803 len = nb_want - ssl->in_left;
1804 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1805
1806 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1807 ssl->in_left, nb_want ) );
1808 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1809
Paul Bakker831a7552011-05-18 13:32:51 +00001810 if( ret == 0 )
1811 return( POLARSSL_ERR_SSL_CONN_EOF );
1812
Paul Bakker5121ce52009-01-03 21:22:43 +00001813 if( ret < 0 )
1814 return( ret );
1815
1816 ssl->in_left += ret;
1817 }
1818
1819 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1820
1821 return( 0 );
1822}
1823
1824/*
1825 * Flush any data not yet written
1826 */
1827int ssl_flush_output( ssl_context *ssl )
1828{
1829 int ret;
1830 unsigned char *buf;
1831
1832 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1833
1834 while( ssl->out_left > 0 )
1835 {
1836 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1837 5 + ssl->out_msglen, ssl->out_left ) );
1838
Paul Bakker5bd42292012-12-19 14:40:42 +01001839 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001840 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001841
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1843
1844 if( ret <= 0 )
1845 return( ret );
1846
1847 ssl->out_left -= ret;
1848 }
1849
1850 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1851
1852 return( 0 );
1853}
1854
1855/*
1856 * Record layer functions
1857 */
1858int ssl_write_record( ssl_context *ssl )
1859{
Paul Bakker05ef8352012-05-08 09:17:57 +00001860 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001861 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
1863 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1864
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1866 {
1867 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1868 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1869 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1870
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001871 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1872 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001873 }
1874
Paul Bakker2770fbd2012-07-03 13:30:23 +00001875#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001876 if( ssl->transform_out != NULL &&
1877 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001878 {
1879 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1880 {
1881 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1882 return( ret );
1883 }
1884
1885 len = ssl->out_msglen;
1886 }
1887#endif /*POLARSSL_ZLIB_SUPPORT */
1888
Paul Bakker05ef8352012-05-08 09:17:57 +00001889#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001890 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001892 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001893
Paul Bakker05ef8352012-05-08 09:17:57 +00001894 ret = ssl_hw_record_write( ssl );
1895 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1896 {
1897 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001898 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001899 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001900
1901 if( ret == 0 )
1902 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001903 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001904#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001905 if( !done )
1906 {
1907 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1908 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1909 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1911 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001912
Paul Bakker48916f92012-09-16 19:57:18 +00001913 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001914 {
1915 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1916 {
1917 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1918 return( ret );
1919 }
1920
1921 len = ssl->out_msglen;
1922 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1923 ssl->out_hdr[4] = (unsigned char)( len );
1924 }
1925
1926 ssl->out_left = 5 + ssl->out_msglen;
1927
1928 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1929 "version = [%d:%d], msglen = %d",
1930 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1931 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1932
1933 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001934 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 }
1936
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1938 {
1939 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1940 return( ret );
1941 }
1942
1943 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1944
1945 return( 0 );
1946}
1947
1948int ssl_read_record( ssl_context *ssl )
1949{
Paul Bakker05ef8352012-05-08 09:17:57 +00001950 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
1952 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1953
Paul Bakker68884e32013-01-07 18:20:04 +01001954 SSL_DEBUG_BUF( 4, "input record from network",
1955 ssl->in_hdr, 5 + ssl->in_msglen );
1956
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 if( ssl->in_hslen != 0 &&
1958 ssl->in_hslen < ssl->in_msglen )
1959 {
1960 /*
1961 * Get next Handshake message in the current record
1962 */
1963 ssl->in_msglen -= ssl->in_hslen;
1964
Paul Bakker8934a982011-08-05 11:11:53 +00001965 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001966 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001967
1968 ssl->in_hslen = 4;
1969 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1970
1971 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1972 " %d, type = %d, hslen = %d",
1973 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1974
1975 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1976 {
1977 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001978 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 }
1980
1981 if( ssl->in_msglen < ssl->in_hslen )
1982 {
1983 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001984 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 }
1986
Paul Bakker4224bc02014-04-08 14:36:50 +02001987 if( ssl->state != SSL_HANDSHAKE_OVER )
1988 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
1990 return( 0 );
1991 }
1992
1993 ssl->in_hslen = 0;
1994
1995 /*
1996 * Read the record header and validate it
1997 */
1998 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1999 {
2000 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2001 return( ret );
2002 }
2003
2004 ssl->in_msgtype = ssl->in_hdr[0];
2005 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2006
2007 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2008 "version = [%d:%d], msglen = %d",
2009 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2010 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2011
2012 if( ssl->in_hdr[1] != ssl->major_ver )
2013 {
2014 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002015 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002016 }
2017
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002018 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 {
2020 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002021 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 }
2023
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002024 /* Sanity check (outer boundaries) */
2025 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2026 {
2027 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2028 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2029 }
2030
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002032 * Make sure the message length is acceptable for the current transform
2033 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 */
Paul Bakker48916f92012-09-16 19:57:18 +00002035 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002037 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 {
2039 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002040 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 }
2042 }
2043 else
2044 {
Paul Bakker48916f92012-09-16 19:57:18 +00002045 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 {
2047 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002048 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 }
2050
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002051#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002053 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 {
2055 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002056 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002059
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002060#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2061 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 /*
2063 * TLS encrypted messages can have up to 256 bytes of padding
2064 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002065 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002066 ssl->in_msglen > ssl->transform_in->minlen +
2067 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 {
2069 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002070 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002072#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 }
2074
2075 /*
2076 * Read and optionally decrypt the message contents
2077 */
2078 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2079 {
2080 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2081 return( ret );
2082 }
2083
2084 SSL_DEBUG_BUF( 4, "input record from network",
2085 ssl->in_hdr, 5 + ssl->in_msglen );
2086
Paul Bakker05ef8352012-05-08 09:17:57 +00002087#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002088 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002089 {
2090 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2091
2092 ret = ssl_hw_record_read( ssl );
2093 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2094 {
2095 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002096 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002097 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002098
2099 if( ret == 0 )
2100 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002101 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002102#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002103 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 {
2105 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2106 {
Paul Bakker40865c82013-01-31 17:13:13 +01002107#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2108 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2109 {
2110 ssl_send_alert_message( ssl,
2111 SSL_ALERT_LEVEL_FATAL,
2112 SSL_ALERT_MSG_BAD_RECORD_MAC );
2113 }
2114#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2116 return( ret );
2117 }
2118
2119 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2120 ssl->in_msg, ssl->in_msglen );
2121
2122 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2123 {
2124 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002125 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
2127 }
2128
Paul Bakker2770fbd2012-07-03 13:30:23 +00002129#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002130 if( ssl->transform_in != NULL &&
2131 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002132 {
2133 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2134 {
2135 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2136 return( ret );
2137 }
2138
2139 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2140 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2141 }
2142#endif /* POLARSSL_ZLIB_SUPPORT */
2143
Paul Bakker0a925182012-04-16 06:46:41 +00002144 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2145 ssl->in_msgtype != SSL_MSG_ALERT &&
2146 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2147 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2148 {
2149 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2150
Paul Bakker48916f92012-09-16 19:57:18 +00002151 if( ( ret = ssl_send_alert_message( ssl,
2152 SSL_ALERT_LEVEL_FATAL,
2153 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002154 {
2155 return( ret );
2156 }
2157
2158 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2159 }
2160
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2162 {
2163 ssl->in_hslen = 4;
2164 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2165
2166 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2167 " %d, type = %d, hslen = %d",
2168 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2169
2170 /*
2171 * Additional checks to validate the handshake header
2172 */
2173 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2174 {
2175 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002176 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177 }
2178
2179 if( ssl->in_msglen < ssl->in_hslen )
2180 {
2181 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002182 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 }
2184
Paul Bakker48916f92012-09-16 19:57:18 +00002185 if( ssl->state != SSL_HANDSHAKE_OVER )
2186 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 }
2188
2189 if( ssl->in_msgtype == SSL_MSG_ALERT )
2190 {
2191 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2192 ssl->in_msg[0], ssl->in_msg[1] ) );
2193
2194 /*
2195 * Ignore non-fatal alerts, except close_notify
2196 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002197 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002199 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2200 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002201 /**
2202 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2203 * error identifier.
2204 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002205 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
2207
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002208 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2209 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 {
2211 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002212 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 }
2214 }
2215
2216 ssl->in_left = 0;
2217
2218 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2219
2220 return( 0 );
2221}
2222
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002223int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2224{
2225 int ret;
2226
2227 if( ( ret = ssl_send_alert_message( ssl,
2228 SSL_ALERT_LEVEL_FATAL,
2229 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2230 {
2231 return( ret );
2232 }
2233
2234 return( 0 );
2235}
2236
Paul Bakker0a925182012-04-16 06:46:41 +00002237int ssl_send_alert_message( ssl_context *ssl,
2238 unsigned char level,
2239 unsigned char message )
2240{
2241 int ret;
2242
2243 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2244
2245 ssl->out_msgtype = SSL_MSG_ALERT;
2246 ssl->out_msglen = 2;
2247 ssl->out_msg[0] = level;
2248 ssl->out_msg[1] = message;
2249
2250 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2251 {
2252 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2253 return( ret );
2254 }
2255
2256 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2257
2258 return( 0 );
2259}
2260
Paul Bakker5121ce52009-01-03 21:22:43 +00002261/*
2262 * Handshake functions
2263 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002264#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2265 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2266 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2267 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2268 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2269 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2270 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002271int ssl_write_certificate( ssl_context *ssl )
2272{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002273 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
2275 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2276
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002277 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002278 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2279 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002280 {
2281 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2282 ssl->state++;
2283 return( 0 );
2284 }
2285
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002286 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2287 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002288}
2289
2290int ssl_parse_certificate( ssl_context *ssl )
2291{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002292 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2293
2294 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2295
2296 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002297 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2298 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002299 {
2300 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2301 ssl->state++;
2302 return( 0 );
2303 }
2304
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002305 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2306 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307}
2308#else
2309int ssl_write_certificate( ssl_context *ssl )
2310{
2311 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2312 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002313 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002314 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2315
2316 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2317
2318 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002319 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2320 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002321 {
2322 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2323 ssl->state++;
2324 return( 0 );
2325 }
2326
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 if( ssl->endpoint == SSL_IS_CLIENT )
2328 {
2329 if( ssl->client_auth == 0 )
2330 {
2331 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2332 ssl->state++;
2333 return( 0 );
2334 }
2335
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002336#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002337 /*
2338 * If using SSLv3 and got no cert, send an Alert message
2339 * (otherwise an empty Certificate message will be sent).
2340 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002341 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2343 {
2344 ssl->out_msglen = 2;
2345 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002346 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2347 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002348
2349 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2350 goto write_msg;
2351 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002352#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002353 }
2354 else /* SSL_IS_SERVER */
2355 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002356 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 {
2358 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002359 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002360 }
2361 }
2362
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002363 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002364
2365 /*
2366 * 0 . 0 handshake type
2367 * 1 . 3 handshake length
2368 * 4 . 6 length of all certs
2369 * 7 . 9 length of cert. 1
2370 * 10 . n-1 peer certificate
2371 * n . n+2 length of cert. 2
2372 * n+3 . ... upper level cert, etc.
2373 */
2374 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002375 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002376
Paul Bakker29087132010-03-21 21:03:34 +00002377 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 {
2379 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002380 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 {
2382 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2383 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002384 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 }
2386
2387 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2388 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2389 ssl->out_msg[i + 2] = (unsigned char)( n );
2390
2391 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2392 i += n; crt = crt->next;
2393 }
2394
2395 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2396 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2397 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2398
2399 ssl->out_msglen = i;
2400 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2401 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2402
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002403#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002404write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002405#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
2407 ssl->state++;
2408
2409 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2410 {
2411 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2412 return( ret );
2413 }
2414
2415 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2416
Paul Bakkered27a042013-04-18 22:46:23 +02002417 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418}
2419
2420int ssl_parse_certificate( ssl_context *ssl )
2421{
Paul Bakkered27a042013-04-18 22:46:23 +02002422 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002423 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002424 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
2426 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2427
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002428 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002429 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2430 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002431 {
2432 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2433 ssl->state++;
2434 return( 0 );
2435 }
2436
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002438 ( ssl->authmode == SSL_VERIFY_NONE ||
2439 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002441 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2443 ssl->state++;
2444 return( 0 );
2445 }
2446
2447 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2448 {
2449 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2450 return( ret );
2451 }
2452
2453 ssl->state++;
2454
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002455#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 /*
2457 * Check if the client sent an empty certificate
2458 */
2459 if( ssl->endpoint == SSL_IS_SERVER &&
2460 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2461 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002462 if( ssl->in_msglen == 2 &&
2463 ssl->in_msgtype == SSL_MSG_ALERT &&
2464 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2465 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 {
2467 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2468
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002469 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2471 return( 0 );
2472 else
Paul Bakker40e46942009-01-03 21:51:57 +00002473 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 }
2475 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002476#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002477
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002478#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2479 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 if( ssl->endpoint == SSL_IS_SERVER &&
2481 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2482 {
2483 if( ssl->in_hslen == 7 &&
2484 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2485 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2486 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2487 {
2488 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2489
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002490 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002492 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 else
2494 return( 0 );
2495 }
2496 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002497#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2498 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
2500 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2501 {
2502 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002503 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 }
2505
2506 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2507 {
2508 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002509 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 }
2511
2512 /*
2513 * Same message structure as in ssl_write_certificate()
2514 */
2515 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2516
2517 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2518 {
2519 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002520 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 }
2522
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002523 /* In case we tried to reuse a session but it failed */
2524 if( ssl->session_negotiate->peer_cert != NULL )
2525 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002526 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002527 polarssl_free( ssl->session_negotiate->peer_cert );
2528 }
2529
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002530 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2531 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 {
2533 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002534 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002535 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 }
2537
Paul Bakkerb6b09562013-09-18 14:17:41 +02002538 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
2540 i = 7;
2541
2542 while( i < ssl->in_hslen )
2543 {
2544 if( ssl->in_msg[i] != 0 )
2545 {
2546 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002547 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 }
2549
2550 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2551 | (unsigned int) ssl->in_msg[i + 2];
2552 i += 3;
2553
2554 if( n < 128 || i + n > ssl->in_hslen )
2555 {
2556 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002557 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 }
2559
Paul Bakkerddf26b42013-09-18 13:46:23 +02002560 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2561 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 if( ret != 0 )
2563 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002564 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 return( ret );
2566 }
2567
2568 i += n;
2569 }
2570
Paul Bakker48916f92012-09-16 19:57:18 +00002571 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002573 /*
2574 * On client, make sure the server cert doesn't change during renego to
2575 * avoid "triple handshake" attack: https://secure-resumption.com/
2576 */
2577 if( ssl->endpoint == SSL_IS_CLIENT &&
2578 ssl->renegotiation == SSL_RENEGOTIATION )
2579 {
2580 if( ssl->session->peer_cert == NULL )
2581 {
2582 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2583 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2584 }
2585
2586 if( ssl->session->peer_cert->raw.len !=
2587 ssl->session_negotiate->peer_cert->raw.len ||
2588 memcmp( ssl->session->peer_cert->raw.p,
2589 ssl->session_negotiate->peer_cert->raw.p,
2590 ssl->session->peer_cert->raw.len ) != 0 )
2591 {
2592 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2593 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2594 }
2595 }
2596
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 if( ssl->authmode != SSL_VERIFY_NONE )
2598 {
2599 if( ssl->ca_chain == NULL )
2600 {
2601 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002602 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 }
2604
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002605 /*
2606 * Main check: verify certificate
2607 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002608 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2609 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2610 &ssl->session_negotiate->verify_result,
2611 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
2613 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002614 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002616 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002617
2618 /*
2619 * Secondary checks: always done, but change 'ret' only if it was 0
2620 */
2621
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002622#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002623 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002624 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002625
2626 /* If certificate uses an EC key, make sure the curve is OK */
2627 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2628 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2629 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002630 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002631 if( ret == 0 )
2632 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002633 }
2634 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002635#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002637 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2638 ciphersuite_info,
2639 ! ssl->endpoint ) != 0 )
2640 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002641 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002642 if( ret == 0 )
2643 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2644 }
2645
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2647 ret = 0;
2648 }
2649
2650 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2651
2652 return( ret );
2653}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002654#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2655 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2656 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2657 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2658 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2659 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2660 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
2662int ssl_write_change_cipher_spec( ssl_context *ssl )
2663{
2664 int ret;
2665
2666 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2667
2668 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2669 ssl->out_msglen = 1;
2670 ssl->out_msg[0] = 1;
2671
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 ssl->state++;
2673
2674 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2675 {
2676 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2677 return( ret );
2678 }
2679
2680 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2681
2682 return( 0 );
2683}
2684
2685int ssl_parse_change_cipher_spec( ssl_context *ssl )
2686{
2687 int ret;
2688
2689 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2690
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2692 {
2693 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2694 return( ret );
2695 }
2696
2697 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2698 {
2699 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002700 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 }
2702
2703 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2704 {
2705 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002706 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 }
2708
2709 ssl->state++;
2710
2711 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2712
2713 return( 0 );
2714}
2715
Paul Bakker41c83d32013-03-20 14:39:14 +01002716void ssl_optimize_checksum( ssl_context *ssl,
2717 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002718{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002719 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002720
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002721#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2722 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002723 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002724 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002725 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002726#endif
2727#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2728#if defined(POLARSSL_SHA512_C)
2729 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2730 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2731 else
2732#endif
2733#if defined(POLARSSL_SHA256_C)
2734 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002735 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002736 else
2737#endif
2738#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002739 {
2740 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002741 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002742 }
Paul Bakker380da532012-04-18 16:10:25 +00002743}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002744
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002745static void ssl_update_checksum_start( ssl_context *ssl,
2746 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002747{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002748#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2749 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002750 md5_update( &ssl->handshake->fin_md5 , buf, len );
2751 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002752#endif
2753#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2754#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002755 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002756#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002757#if defined(POLARSSL_SHA512_C)
2758 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002759#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002760#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002761}
2762
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002763#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2764 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002765static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2766 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002767{
Paul Bakker48916f92012-09-16 19:57:18 +00002768 md5_update( &ssl->handshake->fin_md5 , buf, len );
2769 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002770}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002771#endif
Paul Bakker380da532012-04-18 16:10:25 +00002772
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002773#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2774#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002775static void ssl_update_checksum_sha256( ssl_context *ssl,
2776 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002777{
Paul Bakker9e36f042013-06-30 14:34:05 +02002778 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002779}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002780#endif
Paul Bakker380da532012-04-18 16:10:25 +00002781
Paul Bakker9e36f042013-06-30 14:34:05 +02002782#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002783static void ssl_update_checksum_sha384( ssl_context *ssl,
2784 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002785{
Paul Bakker9e36f042013-06-30 14:34:05 +02002786 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002787}
Paul Bakker769075d2012-11-24 11:26:46 +01002788#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002789#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002790
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002791#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002792static void ssl_calc_finished_ssl(
2793 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002794{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002795 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002796 md5_context md5;
2797 sha1_context sha1;
2798
Paul Bakker5121ce52009-01-03 21:22:43 +00002799 unsigned char padbuf[48];
2800 unsigned char md5sum[16];
2801 unsigned char sha1sum[20];
2802
Paul Bakker48916f92012-09-16 19:57:18 +00002803 ssl_session *session = ssl->session_negotiate;
2804 if( !session )
2805 session = ssl->session;
2806
Paul Bakker1ef83d62012-04-11 12:09:53 +00002807 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2808
Paul Bakker48916f92012-09-16 19:57:18 +00002809 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2810 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
2812 /*
2813 * SSLv3:
2814 * hash =
2815 * MD5( master + pad2 +
2816 * MD5( handshake + sender + master + pad1 ) )
2817 * + SHA1( master + pad2 +
2818 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 */
2820
Paul Bakker90995b52013-06-24 19:20:35 +02002821#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002823 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002824#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Paul Bakker90995b52013-06-24 19:20:35 +02002826#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002828 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002829#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Paul Bakker3c2122f2013-06-24 19:03:14 +02002831 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2832 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Paul Bakker1ef83d62012-04-11 12:09:53 +00002834 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Paul Bakker3c2122f2013-06-24 19:03:14 +02002836 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002837 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002838 md5_update( &md5, padbuf, 48 );
2839 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Paul Bakker3c2122f2013-06-24 19:03:14 +02002841 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002842 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002843 sha1_update( &sha1, padbuf, 40 );
2844 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Paul Bakker1ef83d62012-04-11 12:09:53 +00002846 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Paul Bakker1ef83d62012-04-11 12:09:53 +00002848 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002849 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002850 md5_update( &md5, padbuf, 48 );
2851 md5_update( &md5, md5sum, 16 );
2852 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Paul Bakker1ef83d62012-04-11 12:09:53 +00002854 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002855 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002856 sha1_update( &sha1, padbuf , 40 );
2857 sha1_update( &sha1, sha1sum, 20 );
2858 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Paul Bakker1ef83d62012-04-11 12:09:53 +00002860 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Paul Bakker34617722014-06-13 17:20:13 +02002862 polarssl_zeroize( &md5, sizeof( md5_context ) );
2863 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Paul Bakker34617722014-06-13 17:20:13 +02002865 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2866 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2867 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
2869 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2870}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002871#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002873#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002874static void ssl_calc_finished_tls(
2875 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002876{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002877 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002878 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002881 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Paul Bakker48916f92012-09-16 19:57:18 +00002883 ssl_session *session = ssl->session_negotiate;
2884 if( !session )
2885 session = ssl->session;
2886
Paul Bakker1ef83d62012-04-11 12:09:53 +00002887 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002888
Paul Bakker48916f92012-09-16 19:57:18 +00002889 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2890 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakker1ef83d62012-04-11 12:09:53 +00002892 /*
2893 * TLSv1:
2894 * hash = PRF( master, finished_label,
2895 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2896 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Paul Bakker90995b52013-06-24 19:20:35 +02002898#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002899 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2900 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002901#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002902
Paul Bakker90995b52013-06-24 19:20:35 +02002903#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002904 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2905 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002906#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002907
2908 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002909 ? "client finished"
2910 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911
2912 md5_finish( &md5, padbuf );
2913 sha1_finish( &sha1, padbuf + 16 );
2914
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002915 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002916 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002917
2918 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2919
Paul Bakker34617722014-06-13 17:20:13 +02002920 polarssl_zeroize( &md5, sizeof( md5_context ) );
2921 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002922
Paul Bakker34617722014-06-13 17:20:13 +02002923 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002924
2925 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2926}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002927#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002928
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002929#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2930#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002931static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002932 ssl_context *ssl, unsigned char *buf, int from )
2933{
2934 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002935 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002936 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002937 unsigned char padbuf[32];
2938
Paul Bakker48916f92012-09-16 19:57:18 +00002939 ssl_session *session = ssl->session_negotiate;
2940 if( !session )
2941 session = ssl->session;
2942
Paul Bakker380da532012-04-18 16:10:25 +00002943 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002944
Paul Bakker9e36f042013-06-30 14:34:05 +02002945 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002946
2947 /*
2948 * TLSv1.2:
2949 * hash = PRF( master, finished_label,
2950 * Hash( handshake ) )[0.11]
2951 */
2952
Paul Bakker9e36f042013-06-30 14:34:05 +02002953#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002955 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002956#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002957
2958 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002959 ? "client finished"
2960 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002961
Paul Bakker9e36f042013-06-30 14:34:05 +02002962 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002964 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002965 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002966
2967 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2968
Paul Bakker34617722014-06-13 17:20:13 +02002969 polarssl_zeroize( &sha256, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002970
Paul Bakker34617722014-06-13 17:20:13 +02002971 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002972
2973 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2974}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002975#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976
Paul Bakker9e36f042013-06-30 14:34:05 +02002977#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002978static void ssl_calc_finished_tls_sha384(
2979 ssl_context *ssl, unsigned char *buf, int from )
2980{
2981 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002982 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002983 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002984 unsigned char padbuf[48];
2985
Paul Bakker48916f92012-09-16 19:57:18 +00002986 ssl_session *session = ssl->session_negotiate;
2987 if( !session )
2988 session = ssl->session;
2989
Paul Bakker380da532012-04-18 16:10:25 +00002990 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002991
Paul Bakker9e36f042013-06-30 14:34:05 +02002992 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002993
2994 /*
2995 * TLSv1.2:
2996 * hash = PRF( master, finished_label,
2997 * Hash( handshake ) )[0.11]
2998 */
2999
Paul Bakker9e36f042013-06-30 14:34:05 +02003000#if !defined(POLARSSL_SHA512_ALT)
3001 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3002 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003003#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003004
3005 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003006 ? "client finished"
3007 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003008
Paul Bakker9e36f042013-06-30 14:34:05 +02003009 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003010
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003011 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003012 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003013
3014 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3015
Paul Bakker34617722014-06-13 17:20:13 +02003016 polarssl_zeroize( &sha512, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003017
Paul Bakker34617722014-06-13 17:20:13 +02003018 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003019
3020 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3021}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003022#endif /* POLARSSL_SHA512_C */
3023#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003024
Paul Bakker48916f92012-09-16 19:57:18 +00003025void ssl_handshake_wrapup( ssl_context *ssl )
3026{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003027 int resume = ssl->handshake->resume;
3028
Paul Bakker48916f92012-09-16 19:57:18 +00003029 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3030
3031 /*
3032 * Free our handshake params
3033 */
3034 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003035 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003036 ssl->handshake = NULL;
3037
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003038 if( ssl->renegotiation == SSL_RENEGOTIATION )
3039 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3040
Paul Bakker48916f92012-09-16 19:57:18 +00003041 /*
3042 * Switch in our now active transform context
3043 */
3044 if( ssl->transform )
3045 {
3046 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003047 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003048 }
3049 ssl->transform = ssl->transform_negotiate;
3050 ssl->transform_negotiate = NULL;
3051
Paul Bakker0a597072012-09-25 21:55:46 +00003052 if( ssl->session )
3053 {
3054 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003055 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003056 }
3057 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003058 ssl->session_negotiate = NULL;
3059
Paul Bakker0a597072012-09-25 21:55:46 +00003060 /*
3061 * Add cache entry
3062 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003063 if( ssl->f_set_cache != NULL &&
3064 ssl->session->length != 0 &&
3065 resume == 0 )
3066 {
Paul Bakker0a597072012-09-25 21:55:46 +00003067 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3068 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003069 }
Paul Bakker0a597072012-09-25 21:55:46 +00003070
Paul Bakker48916f92012-09-16 19:57:18 +00003071 ssl->state++;
3072
3073 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3074}
3075
Paul Bakker1ef83d62012-04-11 12:09:53 +00003076int ssl_write_finished( ssl_context *ssl )
3077{
3078 int ret, hash_len;
3079
3080 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3081
Paul Bakker92be97b2013-01-02 17:30:03 +01003082 /*
3083 * Set the out_msg pointer to the correct location based on IV length
3084 */
3085 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3086 {
3087 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3088 ssl->transform_negotiate->fixed_ivlen;
3089 }
3090 else
3091 ssl->out_msg = ssl->out_iv;
3092
Paul Bakker48916f92012-09-16 19:57:18 +00003093 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003094
3095 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3097
Paul Bakker48916f92012-09-16 19:57:18 +00003098 ssl->verify_data_len = hash_len;
3099 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3100
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 ssl->out_msglen = 4 + hash_len;
3102 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3103 ssl->out_msg[0] = SSL_HS_FINISHED;
3104
3105 /*
3106 * In case of session resuming, invert the client and server
3107 * ChangeCipherSpec messages order.
3108 */
Paul Bakker0a597072012-09-25 21:55:46 +00003109 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003110 {
3111 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003112 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 else
3114 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3115 }
3116 else
3117 ssl->state++;
3118
Paul Bakker48916f92012-09-16 19:57:18 +00003119 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003120 * Switch to our negotiated transform and session parameters for outbound
3121 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003122 */
3123 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3124 ssl->transform_out = ssl->transform_negotiate;
3125 ssl->session_out = ssl->session_negotiate;
3126 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003127
Paul Bakker07eb38b2012-12-19 14:42:06 +01003128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003129 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003130 {
3131 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3132 {
3133 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3134 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3135 }
3136 }
3137#endif
3138
Paul Bakker5121ce52009-01-03 21:22:43 +00003139 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3140 {
3141 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3142 return( ret );
3143 }
3144
3145 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3146
3147 return( 0 );
3148}
3149
3150int ssl_parse_finished( ssl_context *ssl )
3151{
Paul Bakker23986e52011-04-24 08:57:21 +00003152 int ret;
3153 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003154 unsigned char buf[36];
3155
3156 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3157
Paul Bakker48916f92012-09-16 19:57:18 +00003158 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
Paul Bakker48916f92012-09-16 19:57:18 +00003160 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003161 * Switch to our negotiated transform and session parameters for inbound
3162 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003163 */
3164 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3165 ssl->transform_in = ssl->transform_negotiate;
3166 ssl->session_in = ssl->session_negotiate;
3167 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003168
Paul Bakker92be97b2013-01-02 17:30:03 +01003169 /*
3170 * Set the in_msg pointer to the correct location based on IV length
3171 */
3172 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3173 {
3174 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3175 ssl->transform_negotiate->fixed_ivlen;
3176 }
3177 else
3178 ssl->in_msg = ssl->in_iv;
3179
Paul Bakker07eb38b2012-12-19 14:42:06 +01003180#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003181 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003182 {
3183 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3184 {
3185 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3186 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3187 }
3188 }
3189#endif
3190
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3192 {
3193 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3194 return( ret );
3195 }
3196
3197 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3198 {
3199 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003200 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 }
3202
Paul Bakker1ef83d62012-04-11 12:09:53 +00003203 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3205
3206 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3207 ssl->in_hslen != 4 + hash_len )
3208 {
3209 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003210 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 }
3212
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003213 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 {
3215 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003216 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 }
3218
Paul Bakker48916f92012-09-16 19:57:18 +00003219 ssl->verify_data_len = hash_len;
3220 memcpy( ssl->peer_verify_data, buf, hash_len );
3221
Paul Bakker0a597072012-09-25 21:55:46 +00003222 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 {
3224 if( ssl->endpoint == SSL_IS_CLIENT )
3225 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3226
3227 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003228 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 }
3230 else
3231 ssl->state++;
3232
3233 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3234
3235 return( 0 );
3236}
3237
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003238static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003239{
3240 if( ssl->transform_negotiate )
3241 ssl_transform_free( ssl->transform_negotiate );
3242 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003243 {
3244 ssl->transform_negotiate =
3245 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003246
3247 if( ssl->transform_negotiate != NULL )
3248 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003249 }
Paul Bakker48916f92012-09-16 19:57:18 +00003250
3251 if( ssl->session_negotiate )
3252 ssl_session_free( ssl->session_negotiate );
3253 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003254 {
3255 ssl->session_negotiate =
3256 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003257
3258 if( ssl->session_negotiate != NULL )
3259 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003260 }
Paul Bakker48916f92012-09-16 19:57:18 +00003261
3262 if( ssl->handshake )
3263 ssl_handshake_free( ssl->handshake );
3264 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003265 {
3266 ssl->handshake = (ssl_handshake_params *)
3267 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003268
3269 if( ssl->handshake != NULL )
3270 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003271 }
Paul Bakker48916f92012-09-16 19:57:18 +00003272
3273 if( ssl->handshake == NULL ||
3274 ssl->transform_negotiate == NULL ||
3275 ssl->session_negotiate == NULL )
3276 {
3277 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3278 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3279 }
3280
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003281#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3282 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003283 md5_starts( &ssl->handshake->fin_md5 );
3284 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003285#endif
3286#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3287#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003288 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003289#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003290#if defined(POLARSSL_SHA512_C)
3291 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003292#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003293#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003294
3295 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003296 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003297
Paul Bakker61d113b2013-07-04 11:51:43 +02003298#if defined(POLARSSL_ECDH_C)
3299 ecdh_init( &ssl->handshake->ecdh_ctx );
3300#endif
3301
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003302#if defined(POLARSSL_X509_CRT_PARSE_C)
3303 ssl->handshake->key_cert = ssl->key_cert;
3304#endif
3305
Paul Bakker48916f92012-09-16 19:57:18 +00003306 return( 0 );
3307}
3308
Paul Bakker5121ce52009-01-03 21:22:43 +00003309/*
3310 * Initialize an SSL context
3311 */
3312int ssl_init( ssl_context *ssl )
3313{
Paul Bakker48916f92012-09-16 19:57:18 +00003314 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003315 int len = SSL_BUFFER_LEN;
3316
3317 memset( ssl, 0, sizeof( ssl_context ) );
3318
Paul Bakker62f2dee2012-09-28 07:31:51 +00003319 /*
3320 * Sane defaults
3321 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003322 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3323 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3324 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3325 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003326
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003327 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003328
Paul Bakker62f2dee2012-09-28 07:31:51 +00003329#if defined(POLARSSL_DHM_C)
3330 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3331 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3332 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3333 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3334 {
3335 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3336 return( ret );
3337 }
3338#endif
3339
3340 /*
3341 * Prepare base structures
3342 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003343 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003345 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 ssl->in_msg = ssl->in_ctr + 13;
3347
3348 if( ssl->in_ctr == NULL )
3349 {
3350 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003351 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 }
3353
Paul Bakker6e339b52013-07-03 13:37:05 +02003354 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003356 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003357 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003358
3359 if( ssl->out_ctr == NULL )
3360 {
3361 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003362 polarssl_free( ssl->in_ctr );
3363 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003364 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003365 }
3366
3367 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3368 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3369
Paul Bakker606b4ba2013-08-14 16:52:14 +02003370#if defined(POLARSSL_SSL_SESSION_TICKETS)
3371 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3372#endif
3373
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003374#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003375 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003376#endif
3377
Paul Bakker48916f92012-09-16 19:57:18 +00003378 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3379 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003380
3381 return( 0 );
3382}
3383
3384/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003385 * Reset an initialized and used SSL context for re-use while retaining
3386 * all application-set variables, function pointers and data.
3387 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003388int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003389{
Paul Bakker48916f92012-09-16 19:57:18 +00003390 int ret;
3391
Paul Bakker7eb013f2011-10-06 12:37:39 +00003392 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003393 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3394 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3395
3396 ssl->verify_data_len = 0;
3397 memset( ssl->own_verify_data, 0, 36 );
3398 memset( ssl->peer_verify_data, 0, 36 );
3399
Paul Bakker7eb013f2011-10-06 12:37:39 +00003400 ssl->in_offt = NULL;
3401
Paul Bakker92be97b2013-01-02 17:30:03 +01003402 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003403 ssl->in_msgtype = 0;
3404 ssl->in_msglen = 0;
3405 ssl->in_left = 0;
3406
3407 ssl->in_hslen = 0;
3408 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003409 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003410
Paul Bakker92be97b2013-01-02 17:30:03 +01003411 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003412 ssl->out_msgtype = 0;
3413 ssl->out_msglen = 0;
3414 ssl->out_left = 0;
3415
Paul Bakker48916f92012-09-16 19:57:18 +00003416 ssl->transform_in = NULL;
3417 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003418
3419 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3420 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003421
3422#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003423 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003424 {
3425 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003426 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003427 {
3428 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3429 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3430 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003431 }
3432#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003433
Paul Bakker48916f92012-09-16 19:57:18 +00003434 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003435 {
Paul Bakker48916f92012-09-16 19:57:18 +00003436 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003437 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003438 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003439 }
Paul Bakker48916f92012-09-16 19:57:18 +00003440
Paul Bakkerc0463502013-02-14 11:19:38 +01003441 if( ssl->session )
3442 {
3443 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003444 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003445 ssl->session = NULL;
3446 }
3447
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003448#if defined(POLARSSL_SSL_ALPN)
3449 ssl->alpn_chosen = NULL;
3450#endif
3451
Paul Bakker48916f92012-09-16 19:57:18 +00003452 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3453 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003454
3455 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003456}
3457
Paul Bakkera503a632013-08-14 13:48:06 +02003458#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003459/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003460 * Allocate and initialize ticket keys
3461 */
3462static int ssl_ticket_keys_init( ssl_context *ssl )
3463{
3464 int ret;
3465 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003466 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003467
3468 if( ssl->ticket_keys != NULL )
3469 return( 0 );
3470
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003471 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3472 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003473 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3474
3475 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003476 {
3477 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003478 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003479 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003480
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003481 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3482 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3483 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3484 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003485 polarssl_free( tkeys );
3486 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003487 }
3488
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003489 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003490 {
3491 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003492 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003493 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003494
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003495 ssl->ticket_keys = tkeys;
3496
3497 return( 0 );
3498}
Paul Bakkera503a632013-08-14 13:48:06 +02003499#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003500
3501/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 * SSL set accessors
3503 */
3504void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3505{
3506 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003507
Paul Bakker606b4ba2013-08-14 16:52:14 +02003508#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003509 if( endpoint == SSL_IS_CLIENT )
3510 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003511#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003512}
3513
3514void ssl_set_authmode( ssl_context *ssl, int authmode )
3515{
3516 ssl->authmode = authmode;
3517}
3518
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003519#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003520void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003521 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003522 void *p_vrfy )
3523{
3524 ssl->f_vrfy = f_vrfy;
3525 ssl->p_vrfy = p_vrfy;
3526}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003527#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003528
Paul Bakker5121ce52009-01-03 21:22:43 +00003529void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003530 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003531 void *p_rng )
3532{
3533 ssl->f_rng = f_rng;
3534 ssl->p_rng = p_rng;
3535}
3536
3537void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003538 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003539 void *p_dbg )
3540{
3541 ssl->f_dbg = f_dbg;
3542 ssl->p_dbg = p_dbg;
3543}
3544
3545void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003546 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003547 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003548{
3549 ssl->f_recv = f_recv;
3550 ssl->f_send = f_send;
3551 ssl->p_recv = p_recv;
3552 ssl->p_send = p_send;
3553}
3554
Paul Bakker0a597072012-09-25 21:55:46 +00003555void ssl_set_session_cache( ssl_context *ssl,
3556 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3557 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003558{
Paul Bakker0a597072012-09-25 21:55:46 +00003559 ssl->f_get_cache = f_get_cache;
3560 ssl->p_get_cache = p_get_cache;
3561 ssl->f_set_cache = f_set_cache;
3562 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003563}
3564
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003565int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003566{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003567 int ret;
3568
3569 if( ssl == NULL ||
3570 session == NULL ||
3571 ssl->session_negotiate == NULL ||
3572 ssl->endpoint != SSL_IS_CLIENT )
3573 {
3574 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3575 }
3576
3577 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3578 return( ret );
3579
Paul Bakker0a597072012-09-25 21:55:46 +00003580 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003581
3582 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003583}
3584
Paul Bakkerb68cad62012-08-23 08:34:18 +00003585void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003586{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003587 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3588 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3589 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3590 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3591}
3592
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003593void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3594 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003595 int major, int minor )
3596{
3597 if( major != SSL_MAJOR_VERSION_3 )
3598 return;
3599
3600 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3601 return;
3602
3603 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003604}
3605
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003606#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003607/* Add a new (empty) key_cert entry an return a pointer to it */
3608static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3609{
3610 ssl_key_cert *key_cert, *last;
3611
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003612 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3613 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003614 return( NULL );
3615
3616 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3617
3618 /* Append the new key_cert to the (possibly empty) current list */
3619 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003620 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003621 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003622 if( ssl->handshake != NULL )
3623 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003624 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003625 else
3626 {
3627 last = ssl->key_cert;
3628 while( last->next != NULL )
3629 last = last->next;
3630 last->next = key_cert;
3631 }
3632
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003633 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003634}
3635
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003636void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003637 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003638{
3639 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003640 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 ssl->peer_cn = peer_cn;
3642}
3643
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003644int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003645 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003646{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003647 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3648
3649 if( key_cert == NULL )
3650 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3651
3652 key_cert->cert = own_cert;
3653 key_cert->key = pk_key;
3654
3655 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003656}
3657
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003658#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003659int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003660 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003661{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003662 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003663 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003664
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003665 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003666 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3667
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003668 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3669 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003670 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003671
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003672 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003673
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003674 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003675 if( ret != 0 )
3676 return( ret );
3677
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003678 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003679 return( ret );
3680
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003681 key_cert->cert = own_cert;
3682 key_cert->key_own_alloc = 1;
3683
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003684 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003685}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003686#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003688int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003689 void *rsa_key,
3690 rsa_decrypt_func rsa_decrypt,
3691 rsa_sign_func rsa_sign,
3692 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003693{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003694 int ret;
3695 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003696
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003697 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003698 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3699
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003700 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3701 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003702 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003703
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003704 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003705
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003706 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3707 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3708 return( ret );
3709
3710 key_cert->cert = own_cert;
3711 key_cert->key_own_alloc = 1;
3712
3713 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003714}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003715#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003716
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003717#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003718int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3719 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003720{
Paul Bakker6db455e2013-09-18 17:29:31 +02003721 if( psk == NULL || psk_identity == NULL )
3722 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3723
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003724 /*
3725 * The length will be check later anyway, but in case it is obviously
3726 * too large, better abort now. The PMS is as follows:
3727 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3728 */
3729 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3730 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3731
Paul Bakker6db455e2013-09-18 17:29:31 +02003732 if( ssl->psk != NULL )
3733 {
3734 polarssl_free( ssl->psk );
3735 polarssl_free( ssl->psk_identity );
3736 }
3737
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003738 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003739 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003740
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003741 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003742 ssl->psk_identity = (unsigned char *)
3743 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003744
3745 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3746 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3747
3748 memcpy( ssl->psk, psk, ssl->psk_len );
3749 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003750
3751 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003752}
3753
3754void ssl_set_psk_cb( ssl_context *ssl,
3755 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3756 size_t),
3757 void *p_psk )
3758{
3759 ssl->f_psk = f_psk;
3760 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003761}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003762#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003763
Paul Bakker48916f92012-09-16 19:57:18 +00003764#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003765int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003766{
3767 int ret;
3768
Paul Bakker48916f92012-09-16 19:57:18 +00003769 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 {
3771 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3772 return( ret );
3773 }
3774
Paul Bakker48916f92012-09-16 19:57:18 +00003775 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 {
3777 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3778 return( ret );
3779 }
3780
3781 return( 0 );
3782}
3783
Paul Bakker1b57b062011-01-06 15:48:19 +00003784int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3785{
3786 int ret;
3787
Paul Bakker66d5d072014-06-17 16:39:18 +02003788 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003789 {
3790 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3791 return( ret );
3792 }
3793
Paul Bakker66d5d072014-06-17 16:39:18 +02003794 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003795 {
3796 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3797 return( ret );
3798 }
3799
3800 return( 0 );
3801}
Paul Bakker48916f92012-09-16 19:57:18 +00003802#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003803
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003804#if defined(POLARSSL_SSL_SET_CURVES)
3805/*
3806 * Set the allowed elliptic curves
3807 */
3808void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3809{
3810 ssl->curve_list = curve_list;
3811}
3812#endif
3813
Paul Bakker0be444a2013-08-27 21:55:01 +02003814#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003815int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003816{
3817 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003818 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003819
3820 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003821
3822 if( ssl->hostname_len + 1 == 0 )
3823 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3824
Paul Bakker6e339b52013-07-03 13:37:05 +02003825 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003826
Paul Bakkerb15b8512012-01-13 13:44:06 +00003827 if( ssl->hostname == NULL )
3828 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3829
Paul Bakker3c2122f2013-06-24 19:03:14 +02003830 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003831 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003832
Paul Bakker40ea7de2009-05-03 10:18:48 +00003833 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003834
3835 return( 0 );
3836}
3837
Paul Bakker5701cdc2012-09-27 21:49:42 +00003838void ssl_set_sni( ssl_context *ssl,
3839 int (*f_sni)(void *, ssl_context *,
3840 const unsigned char *, size_t),
3841 void *p_sni )
3842{
3843 ssl->f_sni = f_sni;
3844 ssl->p_sni = p_sni;
3845}
Paul Bakker0be444a2013-08-27 21:55:01 +02003846#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003847
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003848#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003849int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003850{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003851 size_t cur_len, tot_len;
3852 const char **p;
3853
3854 /*
3855 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3856 * truncated". Check lengths now rather than later.
3857 */
3858 tot_len = 0;
3859 for( p = protos; *p != NULL; p++ )
3860 {
3861 cur_len = strlen( *p );
3862 tot_len += cur_len;
3863
3864 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3865 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3866 }
3867
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003868 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003869
3870 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003871}
3872
3873const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3874{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003875 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003876}
3877#endif /* POLARSSL_SSL_ALPN */
3878
Paul Bakker490ecc82011-10-06 13:04:09 +00003879void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3880{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003881 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3882 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3883 {
3884 ssl->max_major_ver = major;
3885 ssl->max_minor_ver = minor;
3886 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003887}
3888
Paul Bakker1d29fb52012-09-28 13:28:45 +00003889void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3890{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003891 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3892 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3893 {
3894 ssl->min_major_ver = major;
3895 ssl->min_minor_ver = minor;
3896 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003897}
3898
Paul Bakker05decb22013-08-15 13:33:48 +02003899#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003900int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3901{
Paul Bakker77e257e2013-12-16 15:29:52 +01003902 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003903 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003904 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003905 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003906 }
3907
3908 ssl->mfl_code = mfl_code;
3909
3910 return( 0 );
3911}
Paul Bakker05decb22013-08-15 13:33:48 +02003912#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003913
Paul Bakker1f2bc622013-08-15 13:45:55 +02003914#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003915int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003916{
3917 if( ssl->endpoint != SSL_IS_CLIENT )
3918 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3919
Paul Bakker8c1ede62013-07-19 14:14:37 +02003920 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003921
3922 return( 0 );
3923}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003924#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003925
Paul Bakker48916f92012-09-16 19:57:18 +00003926void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3927{
3928 ssl->disable_renegotiation = renegotiation;
3929}
3930
3931void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3932{
3933 ssl->allow_legacy_renegotiation = allow_legacy;
3934}
3935
Paul Bakkera503a632013-08-14 13:48:06 +02003936#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003937int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3938{
3939 ssl->session_tickets = use_tickets;
3940
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003941 if( ssl->endpoint == SSL_IS_CLIENT )
3942 return( 0 );
3943
3944 if( ssl->f_rng == NULL )
3945 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3946
3947 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003948}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003949
3950void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3951{
3952 ssl->ticket_lifetime = lifetime;
3953}
Paul Bakkera503a632013-08-14 13:48:06 +02003954#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003955
Paul Bakker5121ce52009-01-03 21:22:43 +00003956/*
3957 * SSL get accessors
3958 */
Paul Bakker23986e52011-04-24 08:57:21 +00003959size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003960{
3961 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3962}
3963
Paul Bakkerff60ee62010-03-16 21:09:09 +00003964int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003965{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003966 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003967}
3968
Paul Bakkere3166ce2011-01-27 17:40:50 +00003969const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003970{
Paul Bakker926c8e42013-03-06 10:23:34 +01003971 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003972 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003973
Paul Bakkere3166ce2011-01-27 17:40:50 +00003974 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003975}
3976
Paul Bakker43ca69c2011-01-15 17:35:19 +00003977const char *ssl_get_version( const ssl_context *ssl )
3978{
3979 switch( ssl->minor_ver )
3980 {
3981 case SSL_MINOR_VERSION_0:
3982 return( "SSLv3.0" );
3983
3984 case SSL_MINOR_VERSION_1:
3985 return( "TLSv1.0" );
3986
3987 case SSL_MINOR_VERSION_2:
3988 return( "TLSv1.1" );
3989
Paul Bakker1ef83d62012-04-11 12:09:53 +00003990 case SSL_MINOR_VERSION_3:
3991 return( "TLSv1.2" );
3992
Paul Bakker43ca69c2011-01-15 17:35:19 +00003993 default:
3994 break;
3995 }
3996 return( "unknown" );
3997}
3998
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003999#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004000const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004001{
4002 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004003 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004004
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004005 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004006}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004007#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004008
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004009int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4010{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004011 if( ssl == NULL ||
4012 dst == NULL ||
4013 ssl->session == NULL ||
4014 ssl->endpoint != SSL_IS_CLIENT )
4015 {
4016 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4017 }
4018
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004019 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004020}
4021
Paul Bakker5121ce52009-01-03 21:22:43 +00004022/*
Paul Bakker1961b702013-01-25 14:49:24 +01004023 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004024 */
Paul Bakker1961b702013-01-25 14:49:24 +01004025int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004026{
Paul Bakker40e46942009-01-03 21:51:57 +00004027 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004028
Paul Bakker40e46942009-01-03 21:51:57 +00004029#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004030 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004031 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004032#endif
4033
Paul Bakker40e46942009-01-03 21:51:57 +00004034#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004035 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004036 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004037#endif
4038
Paul Bakker1961b702013-01-25 14:49:24 +01004039 return( ret );
4040}
4041
4042/*
4043 * Perform the SSL handshake
4044 */
4045int ssl_handshake( ssl_context *ssl )
4046{
4047 int ret = 0;
4048
4049 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4050
4051 while( ssl->state != SSL_HANDSHAKE_OVER )
4052 {
4053 ret = ssl_handshake_step( ssl );
4054
4055 if( ret != 0 )
4056 break;
4057 }
4058
Paul Bakker5121ce52009-01-03 21:22:43 +00004059 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4060
4061 return( ret );
4062}
4063
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004064#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004065/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004066 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004067 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004068static int ssl_write_hello_request( ssl_context *ssl )
4069{
4070 int ret;
4071
4072 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4073
4074 ssl->out_msglen = 4;
4075 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4076 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4077
4078 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4079 {
4080 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4081 return( ret );
4082 }
4083
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004084 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4085
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004086 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4087
4088 return( 0 );
4089}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004090#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004091
4092/*
4093 * Actually renegotiate current connection, triggered by either:
4094 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004095 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004096 * - receiving any handshake message on server during ssl_read() after the
4097 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004098 * If the handshake doesn't complete due to waiting for I/O, it will continue
4099 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004100 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004101static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004102{
4103 int ret;
4104
4105 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4106
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004107 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4108 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004109
4110 ssl->state = SSL_HELLO_REQUEST;
4111 ssl->renegotiation = SSL_RENEGOTIATION;
4112
Paul Bakker48916f92012-09-16 19:57:18 +00004113 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4114 {
4115 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4116 return( ret );
4117 }
4118
4119 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4120
4121 return( 0 );
4122}
4123
4124/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004125 * Renegotiate current connection on client,
4126 * or request renegotiation on server
4127 */
4128int ssl_renegotiate( ssl_context *ssl )
4129{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004130 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004131
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004132#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004133 /* On server, just send the request */
4134 if( ssl->endpoint == SSL_IS_SERVER )
4135 {
4136 if( ssl->state != SSL_HANDSHAKE_OVER )
4137 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4138
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004139 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004140 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004141#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004142
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004143#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004144 /*
4145 * On client, either start the renegotiation process or,
4146 * if already in progress, continue the handshake
4147 */
4148 if( ssl->renegotiation != SSL_RENEGOTIATION )
4149 {
4150 if( ssl->state != SSL_HANDSHAKE_OVER )
4151 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4152
4153 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4154 {
4155 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4156 return( ret );
4157 }
4158 }
4159 else
4160 {
4161 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4162 {
4163 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4164 return( ret );
4165 }
4166 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004167#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004168
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004169 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004170}
4171
4172/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004173 * Receive application data decrypted from the SSL layer
4174 */
Paul Bakker23986e52011-04-24 08:57:21 +00004175int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004176{
Paul Bakker23986e52011-04-24 08:57:21 +00004177 int ret;
4178 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004179
4180 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4181
4182 if( ssl->state != SSL_HANDSHAKE_OVER )
4183 {
4184 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4185 {
4186 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4187 return( ret );
4188 }
4189 }
4190
4191 if( ssl->in_offt == NULL )
4192 {
4193 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4194 {
Paul Bakker831a7552011-05-18 13:32:51 +00004195 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4196 return( 0 );
4197
Paul Bakker5121ce52009-01-03 21:22:43 +00004198 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4199 return( ret );
4200 }
4201
4202 if( ssl->in_msglen == 0 &&
4203 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4204 {
4205 /*
4206 * OpenSSL sends empty messages to randomize the IV
4207 */
4208 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4209 {
Paul Bakker831a7552011-05-18 13:32:51 +00004210 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4211 return( 0 );
4212
Paul Bakker5121ce52009-01-03 21:22:43 +00004213 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4214 return( ret );
4215 }
4216 }
4217
Paul Bakker48916f92012-09-16 19:57:18 +00004218 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4219 {
4220 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4221
4222 if( ssl->endpoint == SSL_IS_CLIENT &&
4223 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4224 ssl->in_hslen != 4 ) )
4225 {
4226 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4227 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4228 }
4229
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004230 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4231 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004232 ssl->allow_legacy_renegotiation ==
4233 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004234 {
4235 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4236
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004237#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004238 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004239 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004240 /*
4241 * SSLv3 does not have a "no_renegotiation" alert
4242 */
4243 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4244 return( ret );
4245 }
4246 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004247#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004248#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4249 defined(POLARSSL_SSL_PROTO_TLS1_2)
4250 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004251 {
4252 if( ( ret = ssl_send_alert_message( ssl,
4253 SSL_ALERT_LEVEL_WARNING,
4254 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4255 {
4256 return( ret );
4257 }
Paul Bakker48916f92012-09-16 19:57:18 +00004258 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004259 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004260#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4261 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004262 {
4263 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004264 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004265 }
Paul Bakker48916f92012-09-16 19:57:18 +00004266 }
4267 else
4268 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004269 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004270 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004271 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004272 return( ret );
4273 }
4274
4275 return( POLARSSL_ERR_NET_WANT_READ );
4276 }
4277 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004278 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4279 {
4280 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4281 "but not honored by client" ) );
4282 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4283 }
Paul Bakker48916f92012-09-16 19:57:18 +00004284 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004285 {
4286 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004287 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004288 }
4289
4290 ssl->in_offt = ssl->in_msg;
4291 }
4292
4293 n = ( len < ssl->in_msglen )
4294 ? len : ssl->in_msglen;
4295
4296 memcpy( buf, ssl->in_offt, n );
4297 ssl->in_msglen -= n;
4298
4299 if( ssl->in_msglen == 0 )
4300 /* all bytes consumed */
4301 ssl->in_offt = NULL;
4302 else
4303 /* more data available */
4304 ssl->in_offt += n;
4305
4306 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4307
Paul Bakker23986e52011-04-24 08:57:21 +00004308 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004309}
4310
4311/*
4312 * Send application data to be encrypted by the SSL layer
4313 */
Paul Bakker23986e52011-04-24 08:57:21 +00004314int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004315{
Paul Bakker23986e52011-04-24 08:57:21 +00004316 int ret;
4317 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004318 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004319
4320 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4321
4322 if( ssl->state != SSL_HANDSHAKE_OVER )
4323 {
4324 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4325 {
4326 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4327 return( ret );
4328 }
4329 }
4330
Paul Bakker05decb22013-08-15 13:33:48 +02004331#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004332 /*
4333 * Assume mfl_code is correct since it was checked when set
4334 */
4335 max_len = mfl_code_to_length[ssl->mfl_code];
4336
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004337 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004338 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004339 */
4340 if( ssl->session_out != NULL &&
4341 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4342 {
4343 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4344 }
Paul Bakker05decb22013-08-15 13:33:48 +02004345#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004346
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004347 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004348
Paul Bakker5121ce52009-01-03 21:22:43 +00004349 if( ssl->out_left != 0 )
4350 {
4351 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4352 {
4353 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4354 return( ret );
4355 }
4356 }
Paul Bakker887bd502011-06-08 13:10:54 +00004357 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004358 {
Paul Bakker887bd502011-06-08 13:10:54 +00004359 ssl->out_msglen = n;
4360 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4361 memcpy( ssl->out_msg, buf, n );
4362
4363 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4364 {
4365 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4366 return( ret );
4367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 }
4369
4370 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4371
Paul Bakker23986e52011-04-24 08:57:21 +00004372 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004373}
4374
4375/*
4376 * Notify the peer that the connection is being closed
4377 */
4378int ssl_close_notify( ssl_context *ssl )
4379{
4380 int ret;
4381
4382 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4383
4384 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4385 {
4386 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4387 return( ret );
4388 }
4389
4390 if( ssl->state == SSL_HANDSHAKE_OVER )
4391 {
Paul Bakker48916f92012-09-16 19:57:18 +00004392 if( ( ret = ssl_send_alert_message( ssl,
4393 SSL_ALERT_LEVEL_WARNING,
4394 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004395 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004396 return( ret );
4397 }
4398 }
4399
4400 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4401
4402 return( ret );
4403}
4404
Paul Bakker48916f92012-09-16 19:57:18 +00004405void ssl_transform_free( ssl_transform *transform )
4406{
4407#if defined(POLARSSL_ZLIB_SUPPORT)
4408 deflateEnd( &transform->ctx_deflate );
4409 inflateEnd( &transform->ctx_inflate );
4410#endif
4411
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004412 cipher_free_ctx( &transform->cipher_ctx_enc );
4413 cipher_free_ctx( &transform->cipher_ctx_dec );
4414
Paul Bakker61d113b2013-07-04 11:51:43 +02004415 md_free_ctx( &transform->md_ctx_enc );
4416 md_free_ctx( &transform->md_ctx_dec );
4417
Paul Bakker34617722014-06-13 17:20:13 +02004418 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004419}
4420
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004421#if defined(POLARSSL_X509_CRT_PARSE_C)
4422static void ssl_key_cert_free( ssl_key_cert *key_cert )
4423{
4424 ssl_key_cert *cur = key_cert, *next;
4425
4426 while( cur != NULL )
4427 {
4428 next = cur->next;
4429
4430 if( cur->key_own_alloc )
4431 {
4432 pk_free( cur->key );
4433 polarssl_free( cur->key );
4434 }
4435 polarssl_free( cur );
4436
4437 cur = next;
4438 }
4439}
4440#endif /* POLARSSL_X509_CRT_PARSE_C */
4441
Paul Bakker48916f92012-09-16 19:57:18 +00004442void ssl_handshake_free( ssl_handshake_params *handshake )
4443{
4444#if defined(POLARSSL_DHM_C)
4445 dhm_free( &handshake->dhm_ctx );
4446#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004447#if defined(POLARSSL_ECDH_C)
4448 ecdh_free( &handshake->ecdh_ctx );
4449#endif
4450
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004451#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004452 /* explicit void pointer cast for buggy MS compiler */
4453 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004454#endif
4455
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004456#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4457 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4458 /*
4459 * Free only the linked list wrapper, not the keys themselves
4460 * since the belong to the SNI callback
4461 */
4462 if( handshake->sni_key_cert != NULL )
4463 {
4464 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4465
4466 while( cur != NULL )
4467 {
4468 next = cur->next;
4469 polarssl_free( cur );
4470 cur = next;
4471 }
4472 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004473#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004474
Paul Bakker34617722014-06-13 17:20:13 +02004475 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004476}
4477
4478void ssl_session_free( ssl_session *session )
4479{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004480#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004481 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004482 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004483 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004484 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004485 }
Paul Bakkered27a042013-04-18 22:46:23 +02004486#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004487
Paul Bakkera503a632013-08-14 13:48:06 +02004488#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004489 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004490#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004491
Paul Bakker34617722014-06-13 17:20:13 +02004492 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004493}
4494
Paul Bakker5121ce52009-01-03 21:22:43 +00004495/*
4496 * Free an SSL context
4497 */
4498void ssl_free( ssl_context *ssl )
4499{
4500 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4501
Paul Bakker5121ce52009-01-03 21:22:43 +00004502 if( ssl->out_ctr != NULL )
4503 {
Paul Bakker34617722014-06-13 17:20:13 +02004504 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004505 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004506 }
4507
4508 if( ssl->in_ctr != NULL )
4509 {
Paul Bakker34617722014-06-13 17:20:13 +02004510 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004511 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004512 }
4513
Paul Bakker16770332013-10-11 09:59:44 +02004514#if defined(POLARSSL_ZLIB_SUPPORT)
4515 if( ssl->compress_buf != NULL )
4516 {
Paul Bakker34617722014-06-13 17:20:13 +02004517 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004518 polarssl_free( ssl->compress_buf );
4519 }
4520#endif
4521
Paul Bakker40e46942009-01-03 21:51:57 +00004522#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004523 mpi_free( &ssl->dhm_P );
4524 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004525#endif
4526
Paul Bakker48916f92012-09-16 19:57:18 +00004527 if( ssl->transform )
4528 {
4529 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004530 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004531 }
4532
4533 if( ssl->handshake )
4534 {
4535 ssl_handshake_free( ssl->handshake );
4536 ssl_transform_free( ssl->transform_negotiate );
4537 ssl_session_free( ssl->session_negotiate );
4538
Paul Bakker6e339b52013-07-03 13:37:05 +02004539 polarssl_free( ssl->handshake );
4540 polarssl_free( ssl->transform_negotiate );
4541 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004542 }
4543
Paul Bakkerc0463502013-02-14 11:19:38 +01004544 if( ssl->session )
4545 {
4546 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004547 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004548 }
4549
Paul Bakkera503a632013-08-14 13:48:06 +02004550#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004551 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004552#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004553
Paul Bakker0be444a2013-08-27 21:55:01 +02004554#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004555 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004556 {
Paul Bakker34617722014-06-13 17:20:13 +02004557 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004558 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004559 ssl->hostname_len = 0;
4560 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004561#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004562
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004563#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004564 if( ssl->psk != NULL )
4565 {
Paul Bakker34617722014-06-13 17:20:13 +02004566 polarssl_zeroize( ssl->psk, ssl->psk_len );
4567 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004568 polarssl_free( ssl->psk );
4569 polarssl_free( ssl->psk_identity );
4570 ssl->psk_len = 0;
4571 ssl->psk_identity_len = 0;
4572 }
4573#endif
4574
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004575#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004576 ssl_key_cert_free( ssl->key_cert );
4577#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004578
Paul Bakker05ef8352012-05-08 09:17:57 +00004579#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4580 if( ssl_hw_record_finish != NULL )
4581 {
4582 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4583 ssl_hw_record_finish( ssl );
4584 }
4585#endif
4586
Paul Bakker5121ce52009-01-03 21:22:43 +00004587 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004588
Paul Bakker86f04f42013-02-14 11:20:09 +01004589 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004590 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004591}
4592
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004593#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004594/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004595 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004596 */
4597unsigned char ssl_sig_from_pk( pk_context *pk )
4598{
4599#if defined(POLARSSL_RSA_C)
4600 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4601 return( SSL_SIG_RSA );
4602#endif
4603#if defined(POLARSSL_ECDSA_C)
4604 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4605 return( SSL_SIG_ECDSA );
4606#endif
4607 return( SSL_SIG_ANON );
4608}
4609
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004610pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4611{
4612 switch( sig )
4613 {
4614#if defined(POLARSSL_RSA_C)
4615 case SSL_SIG_RSA:
4616 return( POLARSSL_PK_RSA );
4617#endif
4618#if defined(POLARSSL_ECDSA_C)
4619 case SSL_SIG_ECDSA:
4620 return( POLARSSL_PK_ECDSA );
4621#endif
4622 default:
4623 return( POLARSSL_PK_NONE );
4624 }
4625}
Paul Bakker9af723c2014-05-01 13:03:14 +02004626#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004627
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004628/*
4629 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4630 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004631md_type_t ssl_md_alg_from_hash( unsigned char hash )
4632{
4633 switch( hash )
4634 {
4635#if defined(POLARSSL_MD5_C)
4636 case SSL_HASH_MD5:
4637 return( POLARSSL_MD_MD5 );
4638#endif
4639#if defined(POLARSSL_SHA1_C)
4640 case SSL_HASH_SHA1:
4641 return( POLARSSL_MD_SHA1 );
4642#endif
4643#if defined(POLARSSL_SHA256_C)
4644 case SSL_HASH_SHA224:
4645 return( POLARSSL_MD_SHA224 );
4646 case SSL_HASH_SHA256:
4647 return( POLARSSL_MD_SHA256 );
4648#endif
4649#if defined(POLARSSL_SHA512_C)
4650 case SSL_HASH_SHA384:
4651 return( POLARSSL_MD_SHA384 );
4652 case SSL_HASH_SHA512:
4653 return( POLARSSL_MD_SHA512 );
4654#endif
4655 default:
4656 return( POLARSSL_MD_NONE );
4657 }
4658}
4659
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004660#if defined(POLARSSL_SSL_SET_CURVES)
4661/*
4662 * Check is a curve proposed by the peer is in our list.
4663 * Return 1 if we're willing to use it, 0 otherwise.
4664 */
4665int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4666{
4667 const ecp_group_id *gid;
4668
4669 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4670 if( *gid == grp_id )
4671 return( 1 );
4672
4673 return( 0 );
4674}
Paul Bakker9af723c2014-05-01 13:03:14 +02004675#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004676
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004677#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004678int ssl_check_cert_usage( const x509_crt *cert,
4679 const ssl_ciphersuite_t *ciphersuite,
4680 int cert_endpoint )
4681{
4682#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4683 int usage = 0;
4684#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004685#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4686 const char *ext_oid;
4687 size_t ext_len;
4688#endif
4689
4690#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4691 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4692 ((void) cert);
4693 ((void) cert_endpoint);
4694#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004695
4696#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4697 if( cert_endpoint == SSL_IS_SERVER )
4698 {
4699 /* Server part of the key exchange */
4700 switch( ciphersuite->key_exchange )
4701 {
4702 case POLARSSL_KEY_EXCHANGE_RSA:
4703 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4704 usage = KU_KEY_ENCIPHERMENT;
4705 break;
4706
4707 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4708 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4709 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4710 usage = KU_DIGITAL_SIGNATURE;
4711 break;
4712
4713 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4714 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4715 usage = KU_KEY_AGREEMENT;
4716 break;
4717
4718 /* Don't use default: we want warnings when adding new values */
4719 case POLARSSL_KEY_EXCHANGE_NONE:
4720 case POLARSSL_KEY_EXCHANGE_PSK:
4721 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4722 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4723 usage = 0;
4724 }
4725 }
4726 else
4727 {
4728 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4729 usage = KU_DIGITAL_SIGNATURE;
4730 }
4731
4732 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4733 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004734#else
4735 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004736#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4737
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004738#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4739 if( cert_endpoint == SSL_IS_SERVER )
4740 {
4741 ext_oid = OID_SERVER_AUTH;
4742 ext_len = OID_SIZE( OID_SERVER_AUTH );
4743 }
4744 else
4745 {
4746 ext_oid = OID_CLIENT_AUTH;
4747 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4748 }
4749
4750 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4751 return( -1 );
4752#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4753
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004754 return( 0 );
4755}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004756#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004757
4758#endif /* POLARSSL_SSL_TLS_C */