blob: edfc575c1a20f0ec46b9315bb42c85b7561d92ad [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
159 /*
160 * SSLv3:
161 * block =
162 * MD5( secret + SHA1( 'A' + secret + random ) ) +
163 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
164 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
165 * ...
166 */
167 for( i = 0; i < dlen / 16; i++ )
168 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200169 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000170
171 sha1_starts( &sha1 );
172 sha1_update( &sha1, padding, 1 + i );
173 sha1_update( &sha1, secret, slen );
174 sha1_update( &sha1, random, rlen );
175 sha1_finish( &sha1, sha1sum );
176
177 md5_starts( &md5 );
178 md5_update( &md5, secret, slen );
179 md5_update( &md5, sha1sum, 20 );
180 md5_finish( &md5, dstbuf + i * 16 );
181 }
182
Paul Bakker34617722014-06-13 17:20:13 +0200183 polarssl_zeroize( &md5, sizeof( md5 ) );
184 polarssl_zeroize( &sha1, sizeof( sha1 ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000185
Paul Bakker34617722014-06-13 17:20:13 +0200186 polarssl_zeroize( padding, sizeof( padding ) );
187 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
189 return( 0 );
190}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200191#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000192
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200193#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200194static int tls1_prf( const unsigned char *secret, size_t slen,
195 const char *label,
196 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000197 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
Paul Bakker23986e52011-04-24 08:57:21 +0000199 size_t nb, hs;
200 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200201 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 unsigned char tmp[128];
203 unsigned char h_i[20];
204
205 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000206 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 hs = ( slen + 1 ) / 2;
209 S1 = secret;
210 S2 = secret + slen - hs;
211
212 nb = strlen( label );
213 memcpy( tmp + 20, label, nb );
214 memcpy( tmp + 20 + nb, random, rlen );
215 nb += rlen;
216
217 /*
218 * First compute P_md5(secret,label+random)[0..dlen]
219 */
220 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
221
222 for( i = 0; i < dlen; i += 16 )
223 {
224 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
225 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
226
227 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
228
229 for( j = 0; j < k; j++ )
230 dstbuf[i + j] = h_i[j];
231 }
232
233 /*
234 * XOR out with P_sha1(secret,label+random)[0..dlen]
235 */
236 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
237
238 for( i = 0; i < dlen; i += 20 )
239 {
240 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
241 sha1_hmac( S2, hs, tmp, 20, tmp );
242
243 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
244
245 for( j = 0; j < k; j++ )
246 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
247 }
248
Paul Bakker34617722014-06-13 17:20:13 +0200249 polarssl_zeroize( tmp, sizeof( tmp ) );
250 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252 return( 0 );
253}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200254#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200256#if defined(POLARSSL_SSL_PROTO_TLS1_2)
257#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200258static int tls_prf_sha256( const unsigned char *secret, size_t slen,
259 const char *label,
260 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000261 unsigned char *dstbuf, size_t dlen )
262{
263 size_t nb;
264 size_t i, j, k;
265 unsigned char tmp[128];
266 unsigned char h_i[32];
267
268 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
270
271 nb = strlen( label );
272 memcpy( tmp + 32, label, nb );
273 memcpy( tmp + 32 + nb, random, rlen );
274 nb += rlen;
275
276 /*
277 * Compute P_<hash>(secret, label + random)[0..dlen]
278 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200279 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000280
281 for( i = 0; i < dlen; i += 32 )
282 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200283 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
284 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000285
286 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
287
288 for( j = 0; j < k; j++ )
289 dstbuf[i + j] = h_i[j];
290 }
291
Paul Bakker34617722014-06-13 17:20:13 +0200292 polarssl_zeroize( tmp, sizeof( tmp ) );
293 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000294
295 return( 0 );
296}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200297#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000298
Paul Bakker9e36f042013-06-30 14:34:05 +0200299#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200300static int tls_prf_sha384( const unsigned char *secret, size_t slen,
301 const char *label,
302 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000303 unsigned char *dstbuf, size_t dlen )
304{
305 size_t nb;
306 size_t i, j, k;
307 unsigned char tmp[128];
308 unsigned char h_i[48];
309
310 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
311 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
312
313 nb = strlen( label );
314 memcpy( tmp + 48, label, nb );
315 memcpy( tmp + 48 + nb, random, rlen );
316 nb += rlen;
317
318 /*
319 * Compute P_<hash>(secret, label + random)[0..dlen]
320 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200321 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000322
323 for( i = 0; i < dlen; i += 48 )
324 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200325 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
326 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000327
328 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
329
330 for( j = 0; j < k; j++ )
331 dstbuf[i + j] = h_i[j];
332 }
333
Paul Bakker34617722014-06-13 17:20:13 +0200334 polarssl_zeroize( tmp, sizeof( tmp ) );
335 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336
337 return( 0 );
338}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200339#endif /* POLARSSL_SHA512_C */
340#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000341
Paul Bakker66d5d072014-06-17 16:39:18 +0200342static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343
344#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
345 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200346static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
Paul Bakker380da532012-04-18 16:10:25 +0000348
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200349#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200350static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
351static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif
353
354#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200355static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
356static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200357#endif
358
359#if defined(POLARSSL_SSL_PROTO_TLS1_2)
360#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200361static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
362static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
363static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100365
Paul Bakker9e36f042013-06-30 14:34:05 +0200366#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200367static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
368static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
369static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100370#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200371#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000372
Paul Bakker5121ce52009-01-03 21:22:43 +0000373int ssl_derive_keys( ssl_context *ssl )
374{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200375 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char keyblk[256];
378 unsigned char *key1;
379 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100380 unsigned char *mac_enc;
381 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200382 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 const cipher_info_t *cipher_info;
384 const md_info_t *md_info;
385
Paul Bakker48916f92012-09-16 19:57:18 +0000386 ssl_session *session = ssl->session_negotiate;
387 ssl_transform *transform = ssl->transform_negotiate;
388 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
391
Paul Bakker68884e32013-01-07 18:20:04 +0100392 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
393 if( cipher_info == NULL )
394 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200395 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100396 transform->ciphersuite_info->cipher ) );
397 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
398 }
399
400 md_info = md_info_from_type( transform->ciphersuite_info->mac );
401 if( md_info == NULL )
402 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200403 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100404 transform->ciphersuite_info->mac ) );
405 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
406 }
407
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000409 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000410 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200411#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000412 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413 {
Paul Bakker48916f92012-09-16 19:57:18 +0000414 handshake->tls_prf = ssl3_prf;
415 handshake->calc_verify = ssl_calc_verify_ssl;
416 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200418 else
419#endif
420#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
421 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000422 {
Paul Bakker48916f92012-09-16 19:57:18 +0000423 handshake->tls_prf = tls1_prf;
424 handshake->calc_verify = ssl_calc_verify_tls;
425 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000426 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200427 else
428#endif
429#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200430#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
432 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000433 {
Paul Bakker48916f92012-09-16 19:57:18 +0000434 handshake->tls_prf = tls_prf_sha384;
435 handshake->calc_verify = ssl_calc_verify_tls_sha384;
436 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000438 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif
440#if defined(POLARSSL_SHA256_C)
441 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000442 {
Paul Bakker48916f92012-09-16 19:57:18 +0000443 handshake->tls_prf = tls_prf_sha256;
444 handshake->calc_verify = ssl_calc_verify_tls_sha256;
445 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000446 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447 else
448#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200449#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200450 {
451 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200452 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200453 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000454
455 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 * SSLv3:
457 * master =
458 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
459 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200461 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200462 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 * master = PRF( premaster, "master secret", randbytes )[0..47]
464 */
Paul Bakker0a597072012-09-25 21:55:46 +0000465 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 {
Paul Bakker48916f92012-09-16 19:57:18 +0000467 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
468 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
Paul Bakker48916f92012-09-16 19:57:18 +0000470 handshake->tls_prf( handshake->premaster, handshake->pmslen,
471 "master secret",
472 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Paul Bakker34617722014-06-13 17:20:13 +0200474 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 }
476 else
477 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
478
479 /*
480 * Swap the client and server random values.
481 */
Paul Bakker48916f92012-09-16 19:57:18 +0000482 memcpy( tmp, handshake->randbytes, 64 );
483 memcpy( handshake->randbytes, tmp + 32, 32 );
484 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200485 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487 /*
488 * SSLv3:
489 * key block =
490 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
491 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
492 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
493 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
494 * ...
495 *
496 * TLSv1:
497 * key block = PRF( master, "key expansion", randbytes )
498 */
Paul Bakker48916f92012-09-16 19:57:18 +0000499 handshake->tls_prf( session->master, 48, "key expansion",
500 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
Paul Bakker48916f92012-09-16 19:57:18 +0000502 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
503 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
504 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
505 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
507
Paul Bakker34617722014-06-13 17:20:13 +0200508 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 /*
511 * Determine the appropriate key, IV and MAC length.
512 */
Paul Bakker68884e32013-01-07 18:20:04 +0100513
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200514 transform->keylen = cipher_info->key_length / 8;
515
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200516 if( cipher_info->mode == POLARSSL_MODE_GCM ||
517 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200519 transform->maclen = 0;
520
Paul Bakker68884e32013-01-07 18:20:04 +0100521 transform->ivlen = 12;
522 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200523
524 transform->minlen = 1; // FIXME
Paul Bakker68884e32013-01-07 18:20:04 +0100525 }
526 else
527 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200528 int ret;
529
530 /* Initialize HMAC contexts */
531 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
532 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100533 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200534 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
535 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100536 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200538 /* Get MAC length */
539 transform->maclen = md_get_size( md_info );
540
541#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
542 /*
543 * If HMAC is to be truncated, we shall keep the leftmost bytes,
544 * (rfc 6066 page 13 or rfc 2104 section 4),
545 * so we only need to adjust the length here.
546 */
547 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
548 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
549#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
550
551 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100552 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 /* Minimum length: FIXME */
Paul Bakker68884e32013-01-07 18:20:04 +0100555 transform->minlen = transform->keylen;
556 if( transform->minlen < transform->maclen )
557 {
558 if( cipher_info->mode == POLARSSL_MODE_STREAM )
559 transform->minlen = transform->maclen;
560 else
561 transform->minlen += transform->keylen;
562 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 }
564
565 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000566 transform->keylen, transform->minlen, transform->ivlen,
567 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 /*
570 * Finally setup the cipher contexts, IVs and MAC secrets.
571 */
572 if( ssl->endpoint == SSL_IS_CLIENT )
573 {
Paul Bakker48916f92012-09-16 19:57:18 +0000574 key1 = keyblk + transform->maclen * 2;
575 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Paul Bakker68884e32013-01-07 18:20:04 +0100577 mac_enc = keyblk;
578 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000580 /*
581 * This is not used in TLS v1.1.
582 */
Paul Bakker48916f92012-09-16 19:57:18 +0000583 iv_copy_len = ( transform->fixed_ivlen ) ?
584 transform->fixed_ivlen : transform->ivlen;
585 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
586 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000587 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 }
589 else
590 {
Paul Bakker48916f92012-09-16 19:57:18 +0000591 key1 = keyblk + transform->maclen * 2 + transform->keylen;
592 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
Paul Bakker68884e32013-01-07 18:20:04 +0100594 mac_enc = keyblk + transform->maclen;
595 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000597 /*
598 * This is not used in TLS v1.1.
599 */
Paul Bakker48916f92012-09-16 19:57:18 +0000600 iv_copy_len = ( transform->fixed_ivlen ) ?
601 transform->fixed_ivlen : transform->ivlen;
602 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
603 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000604 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 }
606
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200607#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100608 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
609 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100610 if( transform->maclen > sizeof transform->mac_enc )
611 {
612 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200613 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100614 }
615
Paul Bakker68884e32013-01-07 18:20:04 +0100616 memcpy( transform->mac_enc, mac_enc, transform->maclen );
617 memcpy( transform->mac_dec, mac_dec, transform->maclen );
618 }
619 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200620#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200621#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
622 defined(POLARSSL_SSL_PROTO_TLS1_2)
623 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100624 {
625 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
626 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
627 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200628 else
629#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200630 {
631 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200632 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200633 }
Paul Bakker68884e32013-01-07 18:20:04 +0100634
Paul Bakker05ef8352012-05-08 09:17:57 +0000635#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200636 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000637 {
638 int ret = 0;
639
640 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
641
Paul Bakker07eb38b2012-12-19 14:42:06 +0100642 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
643 transform->iv_enc, transform->iv_dec,
644 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100645 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100646 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000647 {
648 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200649 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000650 }
651 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200652#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000653
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200654 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
655 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200657 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
658 return( ret );
659 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200660
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200661 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
662 cipher_info ) ) != 0 )
663 {
664 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
665 return( ret );
666 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200667
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200668 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
669 cipher_info->key_length,
670 POLARSSL_ENCRYPT ) ) != 0 )
671 {
672 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
673 return( ret );
674 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200675
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200676 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
677 cipher_info->key_length,
678 POLARSSL_DECRYPT ) ) != 0 )
679 {
680 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
681 return( ret );
682 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200683
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200684#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200685 if( cipher_info->mode == POLARSSL_MODE_CBC )
686 {
687 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
688 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200689 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200690 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
691 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200692 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200693
694 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
695 POLARSSL_PADDING_NONE ) ) != 0 )
696 {
697 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
698 return( ret );
699 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200701#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
Paul Bakker34617722014-06-13 17:20:13 +0200703 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
Paul Bakker2770fbd2012-07-03 13:30:23 +0000705#if defined(POLARSSL_ZLIB_SUPPORT)
706 // Initialize compression
707 //
Paul Bakker48916f92012-09-16 19:57:18 +0000708 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000709 {
Paul Bakker16770332013-10-11 09:59:44 +0200710 if( ssl->compress_buf == NULL )
711 {
712 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
713 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
714 if( ssl->compress_buf == NULL )
715 {
716 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
717 SSL_BUFFER_LEN ) );
718 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
719 }
720 }
721
Paul Bakker2770fbd2012-07-03 13:30:23 +0000722 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
723
Paul Bakker48916f92012-09-16 19:57:18 +0000724 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
725 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000726
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200727 if( deflateInit( &transform->ctx_deflate,
728 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000729 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000730 {
731 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
732 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
733 }
734 }
735#endif /* POLARSSL_ZLIB_SUPPORT */
736
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
738
739 return( 0 );
740}
741
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200742#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000743void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000744{
745 md5_context md5;
746 sha1_context sha1;
747 unsigned char pad_1[48];
748 unsigned char pad_2[48];
749
Paul Bakker380da532012-04-18 16:10:25 +0000750 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Paul Bakker48916f92012-09-16 19:57:18 +0000752 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
753 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
Paul Bakker380da532012-04-18 16:10:25 +0000755 memset( pad_1, 0x36, 48 );
756 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Paul Bakker48916f92012-09-16 19:57:18 +0000758 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000759 md5_update( &md5, pad_1, 48 );
760 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
Paul Bakker380da532012-04-18 16:10:25 +0000762 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000763 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000764 md5_update( &md5, pad_2, 48 );
765 md5_update( &md5, hash, 16 );
766 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
Paul Bakker48916f92012-09-16 19:57:18 +0000768 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000769 sha1_update( &sha1, pad_1, 40 );
770 sha1_finish( &sha1, hash + 16 );
771
772 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000773 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000774 sha1_update( &sha1, pad_2, 40 );
775 sha1_update( &sha1, hash + 16, 20 );
776 sha1_finish( &sha1, hash + 16 );
777
778 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
779 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
780
781 return;
782}
Paul Bakker9af723c2014-05-01 13:03:14 +0200783#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000784
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200785#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000786void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
787{
788 md5_context md5;
789 sha1_context sha1;
790
791 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
792
Paul Bakker48916f92012-09-16 19:57:18 +0000793 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
794 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000795
Paul Bakker48916f92012-09-16 19:57:18 +0000796 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000797 sha1_finish( &sha1, hash + 16 );
798
799 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
800 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
801
802 return;
803}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200804#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000805
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200806#if defined(POLARSSL_SSL_PROTO_TLS1_2)
807#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000808void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
809{
Paul Bakker9e36f042013-06-30 14:34:05 +0200810 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000811
812 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
813
Paul Bakker9e36f042013-06-30 14:34:05 +0200814 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
815 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000816
817 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
818 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
819
820 return;
821}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200822#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000823
Paul Bakker9e36f042013-06-30 14:34:05 +0200824#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000825void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
826{
Paul Bakker9e36f042013-06-30 14:34:05 +0200827 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000828
829 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
830
Paul Bakker9e36f042013-06-30 14:34:05 +0200831 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
832 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000833
Paul Bakkerca4ab492012-04-18 14:23:57 +0000834 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
836
837 return;
838}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200839#endif /* POLARSSL_SHA512_C */
840#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200842#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200843int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
844{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200845 unsigned char *p = ssl->handshake->premaster;
846 unsigned char *end = p + sizeof( ssl->handshake->premaster );
847
848 /*
849 * PMS = struct {
850 * opaque other_secret<0..2^16-1>;
851 * opaque psk<0..2^16-1>;
852 * };
853 * with "other_secret" depending on the particular key exchange
854 */
855#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
856 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
857 {
858 if( end - p < 2 + (int) ssl->psk_len )
859 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
860
861 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
862 *(p++) = (unsigned char)( ssl->psk_len );
863 p += ssl->psk_len;
864 }
865 else
866#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200867#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
868 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
869 {
870 /*
871 * other_secret already set by the ClientKeyExchange message,
872 * and is 48 bytes long
873 */
874 *p++ = 0;
875 *p++ = 48;
876 p += 48;
877 }
878 else
879#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200880#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
881 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
882 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200883 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200884 size_t len = ssl->handshake->dhm_ctx.len;
885
886 if( end - p < 2 + (int) len )
887 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
888
889 *(p++) = (unsigned char)( len >> 8 );
890 *(p++) = (unsigned char)( len );
891 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
892 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
893 {
894 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
895 return( ret );
896 }
897 p += len;
898
899 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
900 }
901 else
902#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
903#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
904 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
905 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200906 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200907 size_t zlen;
908
909 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200910 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200911 ssl->f_rng, ssl->p_rng ) ) != 0 )
912 {
913 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
914 return( ret );
915 }
916
917 *(p++) = (unsigned char)( zlen >> 8 );
918 *(p++) = (unsigned char)( zlen );
919 p += zlen;
920
921 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
922 }
923 else
924#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
925 {
926 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200927 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200928 }
929
930 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100931 if( end - p < 2 + (int) ssl->psk_len )
932 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
933
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200934 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
935 *(p++) = (unsigned char)( ssl->psk_len );
936 memcpy( p, ssl->psk, ssl->psk_len );
937 p += ssl->psk_len;
938
939 ssl->handshake->pmslen = p - ssl->handshake->premaster;
940
941 return( 0 );
942}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200943#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200944
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200945#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000946/*
947 * SSLv3.0 MAC functions
948 */
Paul Bakker68884e32013-01-07 18:20:04 +0100949static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
950 unsigned char *buf, size_t len,
951 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000952{
953 unsigned char header[11];
954 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100955 int padlen = 0;
956 int md_size = md_get_size( md_ctx->md_info );
957 int md_type = md_get_type( md_ctx->md_info );
958
959 if( md_type == POLARSSL_MD_MD5 )
960 padlen = 48;
961 else if( md_type == POLARSSL_MD_SHA1 )
962 padlen = 40;
963 else if( md_type == POLARSSL_MD_SHA256 )
964 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100965 else if( md_type == POLARSSL_MD_SHA384 )
966 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
968 memcpy( header, ctr, 8 );
969 header[ 8] = (unsigned char) type;
970 header[ 9] = (unsigned char)( len >> 8 );
971 header[10] = (unsigned char)( len );
972
Paul Bakker68884e32013-01-07 18:20:04 +0100973 memset( padding, 0x36, padlen );
974 md_starts( md_ctx );
975 md_update( md_ctx, secret, md_size );
976 md_update( md_ctx, padding, padlen );
977 md_update( md_ctx, header, 11 );
978 md_update( md_ctx, buf, len );
979 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Paul Bakker68884e32013-01-07 18:20:04 +0100981 memset( padding, 0x5C, padlen );
982 md_starts( md_ctx );
983 md_update( md_ctx, secret, md_size );
984 md_update( md_ctx, padding, padlen );
985 md_update( md_ctx, buf + len, md_size );
986 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000987}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200988#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000989
Paul Bakker5121ce52009-01-03 21:22:43 +0000990/*
991 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200992 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000993static int ssl_encrypt_buf( ssl_context *ssl )
994{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200995 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +0200996 const cipher_mode_t mode = cipher_get_cipher_mode(
997 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +0000998
999 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1000
1001 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001002 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001004#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1005 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1006 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001007 if( mode != POLARSSL_MODE_GCM &&
1008 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001010#if defined(POLARSSL_SSL_PROTO_SSL3)
1011 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1012 {
1013 ssl_mac( &ssl->transform_out->md_ctx_enc,
1014 ssl->transform_out->mac_enc,
1015 ssl->out_msg, ssl->out_msglen,
1016 ssl->out_ctr, ssl->out_msgtype );
1017 }
1018 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001019#endif
1020#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001021 defined(POLARSSL_SSL_PROTO_TLS1_2)
1022 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1023 {
1024 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1025 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1026 ssl->out_msg, ssl->out_msglen );
1027 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1028 ssl->out_msg + ssl->out_msglen );
1029 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1030 }
1031 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001032#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001033 {
1034 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001035 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001036 }
1037
1038 SSL_DEBUG_BUF( 4, "computed mac",
1039 ssl->out_msg + ssl->out_msglen,
1040 ssl->transform_out->maclen );
1041
1042 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001043 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001044#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001046 /*
1047 * Encrypt
1048 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001049#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001050 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001052 int ret;
1053 size_t olen = 0;
1054
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1056 "including %d bytes of padding",
1057 ssl->out_msglen, 0 ) );
1058
1059 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1060 ssl->out_msg, ssl->out_msglen );
1061
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001062 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001063 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001064 ssl->transform_out->ivlen,
1065 ssl->out_msg, ssl->out_msglen,
1066 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001067 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001068 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001069 return( ret );
1070 }
1071
1072 if( ssl->out_msglen != olen )
1073 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001074 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001075 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001076 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 }
Paul Bakker68884e32013-01-07 18:20:04 +01001078 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001079#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001080#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1081 if( mode == POLARSSL_MODE_GCM ||
1082 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001083 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001084 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001085 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001086 unsigned char *enc_msg;
1087 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001088 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1089 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001090
Paul Bakkerca4ab492012-04-18 14:23:57 +00001091 memcpy( add_data, ssl->out_ctr, 8 );
1092 add_data[8] = ssl->out_msgtype;
1093 add_data[9] = ssl->major_ver;
1094 add_data[10] = ssl->minor_ver;
1095 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1096 add_data[12] = ssl->out_msglen & 0xFF;
1097
1098 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1099 add_data, 13 );
1100
Paul Bakker68884e32013-01-07 18:20:04 +01001101 /*
1102 * Generate IV
1103 */
1104 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001105 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1106 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001107 if( ret != 0 )
1108 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001109
Paul Bakker68884e32013-01-07 18:20:04 +01001110 memcpy( ssl->out_iv,
1111 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1112 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001113
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001114 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001115 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001116
Paul Bakker68884e32013-01-07 18:20:04 +01001117 /*
1118 * Fix pointer positions and message length with added IV
1119 */
1120 enc_msg = ssl->out_msg;
1121 enc_msglen = ssl->out_msglen;
1122 ssl->out_msglen += ssl->transform_out->ivlen -
1123 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001124
Paul Bakker68884e32013-01-07 18:20:04 +01001125 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1126 "including %d bytes of padding",
1127 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001128
Paul Bakker68884e32013-01-07 18:20:04 +01001129 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1130 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001131
Paul Bakker68884e32013-01-07 18:20:04 +01001132 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001133 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001134 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001135 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1136 ssl->transform_out->iv_enc,
1137 ssl->transform_out->ivlen,
1138 add_data, 13,
1139 enc_msg, enc_msglen,
1140 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001141 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001142 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001143 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001144 return( ret );
1145 }
1146
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001147 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001148 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001149 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001150 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001151 }
1152
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001153 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001154
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001155 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001157 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001158#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001159#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1160 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001161 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001163 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001164 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001165 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001166
Paul Bakker48916f92012-09-16 19:57:18 +00001167 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1168 ssl->transform_out->ivlen;
1169 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 padlen = 0;
1171
1172 for( i = 0; i <= padlen; i++ )
1173 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1174
1175 ssl->out_msglen += padlen + 1;
1176
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001177 enc_msglen = ssl->out_msglen;
1178 enc_msg = ssl->out_msg;
1179
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001180#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001181 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001182 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1183 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001184 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001185 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001186 {
1187 /*
1188 * Generate IV
1189 */
Paul Bakker48916f92012-09-16 19:57:18 +00001190 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1191 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001192 if( ret != 0 )
1193 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001194
Paul Bakker92be97b2013-01-02 17:30:03 +01001195 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001196 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001197
1198 /*
1199 * Fix pointer positions and message length with added IV
1200 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001201 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001202 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001203 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001204 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001205#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001206
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001208 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001209 ssl->out_msglen, ssl->transform_out->ivlen,
1210 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
1212 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001213 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001215 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001216 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001217 ssl->transform_out->ivlen,
1218 enc_msg, enc_msglen,
1219 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001220 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001221 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001222 return( ret );
1223 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001224
Paul Bakkercca5b812013-08-31 17:40:26 +02001225 if( enc_msglen != olen )
1226 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001227 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001228 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001229 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001230
1231#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001232 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1233 {
1234 /*
1235 * Save IV in SSL3 and TLS1
1236 */
1237 memcpy( ssl->transform_out->iv_enc,
1238 ssl->transform_out->cipher_ctx_enc.iv,
1239 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001243 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001244#endif /* POLARSSL_CIPHER_MODE_CBC &&
1245 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001246 {
1247 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001248 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001249 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
Paul Bakkerca4ab492012-04-18 14:23:57 +00001251 for( i = 8; i > 0; i-- )
1252 if( ++ssl->out_ctr[i - 1] != 0 )
1253 break;
1254
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001255 /* The loops goes to its end iff the counter is wrapping */
1256 if( i == 0 )
1257 {
1258 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1259 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1260 }
1261
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1263
1264 return( 0 );
1265}
1266
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001267#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001268
Paul Bakker5121ce52009-01-03 21:22:43 +00001269static int ssl_decrypt_buf( ssl_context *ssl )
1270{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001271 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001272 const cipher_mode_t mode = cipher_get_cipher_mode(
1273 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001274#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1275 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1276 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1277 size_t padlen = 0, correct = 1;
1278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
1280 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1281
Paul Bakker48916f92012-09-16 19:57:18 +00001282 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 {
1284 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001285 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001286 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 }
1288
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001289#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001290 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001291 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001292 int ret;
1293 size_t olen = 0;
1294
Paul Bakker68884e32013-01-07 18:20:04 +01001295 padlen = 0;
1296
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001297 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001298 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001299 ssl->transform_in->ivlen,
1300 ssl->in_msg, ssl->in_msglen,
1301 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001302 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001303 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001304 return( ret );
1305 }
1306
1307 if( ssl->in_msglen != olen )
1308 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001309 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001310 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001311 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 }
Paul Bakker68884e32013-01-07 18:20:04 +01001313 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001314#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001315#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1316 if( mode == POLARSSL_MODE_GCM ||
1317 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001318 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001319 int ret;
1320 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001321 unsigned char *dec_msg;
1322 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001323 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001324 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1325 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001326 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1327 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001328
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001329 if( ssl->in_msglen < explicit_iv_len + taglen )
1330 {
1331 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1332 "+ taglen (%d)", ssl->in_msglen,
1333 explicit_iv_len, taglen ) );
1334 return( POLARSSL_ERR_SSL_INVALID_MAC );
1335 }
1336 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1337
Paul Bakker68884e32013-01-07 18:20:04 +01001338 dec_msg = ssl->in_msg;
1339 dec_msg_result = ssl->in_msg;
1340 ssl->in_msglen = dec_msglen;
1341
1342 memcpy( add_data, ssl->in_ctr, 8 );
1343 add_data[8] = ssl->in_msgtype;
1344 add_data[9] = ssl->major_ver;
1345 add_data[10] = ssl->minor_ver;
1346 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1347 add_data[12] = ssl->in_msglen & 0xFF;
1348
1349 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1350 add_data, 13 );
1351
1352 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1353 ssl->in_iv,
1354 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1355
1356 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1357 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001358 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001359
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001360 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001361 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001362 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001363 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1364 ssl->transform_in->iv_dec,
1365 ssl->transform_in->ivlen,
1366 add_data, 13,
1367 dec_msg, dec_msglen,
1368 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001369 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001370 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001371 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1372
1373 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1374 return( POLARSSL_ERR_SSL_INVALID_MAC );
1375
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001376 return( ret );
1377 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001378
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001379 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001380 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001381 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001382 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001383 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001384 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001386#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001387#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1388 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001389 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 {
Paul Bakker45829992013-01-03 14:52:21 +01001391 /*
1392 * Decrypt and check the padding
1393 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001394 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001395 unsigned char *dec_msg;
1396 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001397 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001398 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001399 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001400
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 /*
Paul Bakker45829992013-01-03 14:52:21 +01001402 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 */
Paul Bakker48916f92012-09-16 19:57:18 +00001404 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 {
1406 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001407 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001408 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
1410
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001411#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001412 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1413 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001414#endif
Paul Bakker45829992013-01-03 14:52:21 +01001415
1416 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1417 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1418 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001419 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1420 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1421 ssl->transform_in->ivlen,
1422 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001423 return( POLARSSL_ERR_SSL_INVALID_MAC );
1424 }
1425
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001426 dec_msglen = ssl->in_msglen;
1427 dec_msg = ssl->in_msg;
1428 dec_msg_result = ssl->in_msg;
1429
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001430#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001431 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001432 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001433 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001434 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001435 {
Paul Bakker48916f92012-09-16 19:57:18 +00001436 dec_msglen -= ssl->transform_in->ivlen;
1437 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001438
Paul Bakker48916f92012-09-16 19:57:18 +00001439 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001440 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001441 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001442#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001443
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001444 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001445 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001446 ssl->transform_in->ivlen,
1447 dec_msg, dec_msglen,
1448 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001449 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001450 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001451 return( ret );
1452 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001453
Paul Bakkercca5b812013-08-31 17:40:26 +02001454 if( dec_msglen != olen )
1455 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001456 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001457 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001458 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001459
1460#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001461 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1462 {
1463 /*
1464 * Save IV in SSL3 and TLS1
1465 */
1466 memcpy( ssl->transform_in->iv_dec,
1467 ssl->transform_in->cipher_ctx_dec.iv,
1468 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001470#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001471
1472 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001473
1474 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1475 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001476#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001477 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1478 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001479#endif
Paul Bakker45829992013-01-03 14:52:21 +01001480 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001481 correct = 0;
1482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001483
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001484#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1486 {
Paul Bakker48916f92012-09-16 19:57:18 +00001487 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001489#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1491 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001492 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001493#endif
Paul Bakker45829992013-01-03 14:52:21 +01001494 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001495 }
1496 }
1497 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001498#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001499#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1500 defined(POLARSSL_SSL_PROTO_TLS1_2)
1501 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 {
1503 /*
Paul Bakker45829992013-01-03 14:52:21 +01001504 * TLSv1+: always check the padding up to the first failure
1505 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001507 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001508 size_t padding_idx = ssl->in_msglen - padlen - 1;
1509
Paul Bakker956c9e02013-12-19 14:42:28 +01001510 /*
1511 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001512 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001513 *
Paul Bakker61885c72014-04-25 12:59:03 +02001514 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1515 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001516 *
1517 * In both cases we reset padding_idx to a safe value (0) to
1518 * prevent out-of-buffer reads.
1519 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001520 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001521 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1522 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001523
1524 padding_idx *= correct;
1525
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001526 for( i = 1; i <= 256; i++ )
1527 {
1528 real_count &= ( i <= padlen );
1529 pad_count += real_count *
1530 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1531 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001532
1533 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001534
Paul Bakkerd66f0702013-01-31 16:57:45 +01001535#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001536 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001537 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001538#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001539 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001541 else
1542#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1543 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001544 {
1545 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001546 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001547 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001549 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001550#endif /* POLARSSL_CIPHER_MODE_CBC &&
1551 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001552 {
1553 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001554 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001556
1557 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1558 ssl->in_msg, ssl->in_msglen );
1559
1560 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001561 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001563#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1564 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1565 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001566 if( mode != POLARSSL_MODE_GCM &&
1567 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001568 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001569 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1570
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001571 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001572
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001573 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1574 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001575
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001576 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001577
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001578#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001579 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1580 {
1581 ssl_mac( &ssl->transform_in->md_ctx_dec,
1582 ssl->transform_in->mac_dec,
1583 ssl->in_msg, ssl->in_msglen,
1584 ssl->in_ctr, ssl->in_msgtype );
1585 }
1586 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001587#endif /* POLARSSL_SSL_PROTO_SSL3 */
1588#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001589 defined(POLARSSL_SSL_PROTO_TLS1_2)
1590 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1591 {
1592 /*
1593 * Process MAC and always update for padlen afterwards to make
1594 * total time independent of padlen
1595 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001596 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001597 *
1598 * Known timing attacks:
1599 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1600 *
1601 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1602 * correctly. (We round down instead of up, so -56 is the correct
1603 * value for our calculations instead of -55)
1604 */
1605 size_t j, extra_run = 0;
1606 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1607 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001608
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001609 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001610
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001611 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1612 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1613 ssl->in_msglen );
1614 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1615 ssl->in_msg + ssl->in_msglen );
1616 for( j = 0; j < extra_run; j++ )
1617 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001618
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001619 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1620 }
1621 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001622#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001623 POLARSSL_SSL_PROTO_TLS1_2 */
1624 {
1625 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001626 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001627 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001629 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1630 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1631 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001632
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001633 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001634 ssl->transform_in->maclen ) != 0 )
1635 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001636#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001637 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001638#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001639 correct = 0;
1640 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001642 /*
1643 * Finally check the correct flag
1644 */
1645 if( correct == 0 )
1646 return( POLARSSL_ERR_SSL_INVALID_MAC );
1647 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001648#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
1650 if( ssl->in_msglen == 0 )
1651 {
1652 ssl->nb_zero++;
1653
1654 /*
1655 * Three or more empty messages may be a DoS attack
1656 * (excessive CPU consumption).
1657 */
1658 if( ssl->nb_zero > 3 )
1659 {
1660 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1661 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001662 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001663 }
1664 }
1665 else
1666 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001667
Paul Bakker23986e52011-04-24 08:57:21 +00001668 for( i = 8; i > 0; i-- )
1669 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 break;
1671
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001672 /* The loops goes to its end iff the counter is wrapping */
1673 if( i == 0 )
1674 {
1675 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1676 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1677 }
1678
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1680
1681 return( 0 );
1682}
1683
Paul Bakker2770fbd2012-07-03 13:30:23 +00001684#if defined(POLARSSL_ZLIB_SUPPORT)
1685/*
1686 * Compression/decompression functions
1687 */
1688static int ssl_compress_buf( ssl_context *ssl )
1689{
1690 int ret;
1691 unsigned char *msg_post = ssl->out_msg;
1692 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001693 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001694
1695 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1696
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001697 if( len_pre == 0 )
1698 return( 0 );
1699
Paul Bakker2770fbd2012-07-03 13:30:23 +00001700 memcpy( msg_pre, ssl->out_msg, len_pre );
1701
1702 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1703 ssl->out_msglen ) );
1704
1705 SSL_DEBUG_BUF( 4, "before compression: output payload",
1706 ssl->out_msg, ssl->out_msglen );
1707
Paul Bakker48916f92012-09-16 19:57:18 +00001708 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1709 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1710 ssl->transform_out->ctx_deflate.next_out = msg_post;
1711 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001712
Paul Bakker48916f92012-09-16 19:57:18 +00001713 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001714 if( ret != Z_OK )
1715 {
1716 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1717 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1718 }
1719
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001720 ssl->out_msglen = SSL_BUFFER_LEN -
1721 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001722
Paul Bakker2770fbd2012-07-03 13:30:23 +00001723 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1724 ssl->out_msglen ) );
1725
1726 SSL_DEBUG_BUF( 4, "after compression: output payload",
1727 ssl->out_msg, ssl->out_msglen );
1728
1729 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1730
1731 return( 0 );
1732}
1733
1734static int ssl_decompress_buf( ssl_context *ssl )
1735{
1736 int ret;
1737 unsigned char *msg_post = ssl->in_msg;
1738 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001739 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001740
1741 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1742
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001743 if( len_pre == 0 )
1744 return( 0 );
1745
Paul Bakker2770fbd2012-07-03 13:30:23 +00001746 memcpy( msg_pre, ssl->in_msg, len_pre );
1747
1748 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1749 ssl->in_msglen ) );
1750
1751 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1752 ssl->in_msg, ssl->in_msglen );
1753
Paul Bakker48916f92012-09-16 19:57:18 +00001754 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1755 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1756 ssl->transform_in->ctx_inflate.next_out = msg_post;
1757 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001758
Paul Bakker48916f92012-09-16 19:57:18 +00001759 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001760 if( ret != Z_OK )
1761 {
1762 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1763 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1764 }
1765
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001766 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1767 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001768
Paul Bakker2770fbd2012-07-03 13:30:23 +00001769 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1770 ssl->in_msglen ) );
1771
1772 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1773 ssl->in_msg, ssl->in_msglen );
1774
1775 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1776
1777 return( 0 );
1778}
1779#endif /* POLARSSL_ZLIB_SUPPORT */
1780
Paul Bakker5121ce52009-01-03 21:22:43 +00001781/*
1782 * Fill the input message buffer
1783 */
Paul Bakker23986e52011-04-24 08:57:21 +00001784int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001785{
Paul Bakker23986e52011-04-24 08:57:21 +00001786 int ret;
1787 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
1789 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1790
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001791 if( nb_want > SSL_BUFFER_LEN - 8 )
1792 {
1793 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1794 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1795 }
1796
Paul Bakker5121ce52009-01-03 21:22:43 +00001797 while( ssl->in_left < nb_want )
1798 {
1799 len = nb_want - ssl->in_left;
1800 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1801
1802 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1803 ssl->in_left, nb_want ) );
1804 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1805
Paul Bakker831a7552011-05-18 13:32:51 +00001806 if( ret == 0 )
1807 return( POLARSSL_ERR_SSL_CONN_EOF );
1808
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 if( ret < 0 )
1810 return( ret );
1811
1812 ssl->in_left += ret;
1813 }
1814
1815 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1816
1817 return( 0 );
1818}
1819
1820/*
1821 * Flush any data not yet written
1822 */
1823int ssl_flush_output( ssl_context *ssl )
1824{
1825 int ret;
1826 unsigned char *buf;
1827
1828 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1829
1830 while( ssl->out_left > 0 )
1831 {
1832 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1833 5 + ssl->out_msglen, ssl->out_left ) );
1834
Paul Bakker5bd42292012-12-19 14:40:42 +01001835 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001837
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1839
1840 if( ret <= 0 )
1841 return( ret );
1842
1843 ssl->out_left -= ret;
1844 }
1845
1846 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1847
1848 return( 0 );
1849}
1850
1851/*
1852 * Record layer functions
1853 */
1854int ssl_write_record( ssl_context *ssl )
1855{
Paul Bakker05ef8352012-05-08 09:17:57 +00001856 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001857 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
1859 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1860
Paul Bakker5121ce52009-01-03 21:22:43 +00001861 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1862 {
1863 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1864 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1865 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1866
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001867 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1868 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001869 }
1870
Paul Bakker2770fbd2012-07-03 13:30:23 +00001871#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001872 if( ssl->transform_out != NULL &&
1873 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001874 {
1875 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1876 {
1877 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1878 return( ret );
1879 }
1880
1881 len = ssl->out_msglen;
1882 }
1883#endif /*POLARSSL_ZLIB_SUPPORT */
1884
Paul Bakker05ef8352012-05-08 09:17:57 +00001885#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001886 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001887 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001888 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001889
Paul Bakker05ef8352012-05-08 09:17:57 +00001890 ret = ssl_hw_record_write( ssl );
1891 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1892 {
1893 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001894 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001895 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001896
1897 if( ret == 0 )
1898 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001899 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001900#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001901 if( !done )
1902 {
1903 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1904 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1905 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1907 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001908
Paul Bakker48916f92012-09-16 19:57:18 +00001909 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001910 {
1911 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1912 {
1913 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1914 return( ret );
1915 }
1916
1917 len = ssl->out_msglen;
1918 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1919 ssl->out_hdr[4] = (unsigned char)( len );
1920 }
1921
1922 ssl->out_left = 5 + ssl->out_msglen;
1923
1924 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1925 "version = [%d:%d], msglen = %d",
1926 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1927 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1928
1929 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001930 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 }
1932
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1934 {
1935 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1936 return( ret );
1937 }
1938
1939 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1940
1941 return( 0 );
1942}
1943
1944int ssl_read_record( ssl_context *ssl )
1945{
Paul Bakker05ef8352012-05-08 09:17:57 +00001946 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001947
1948 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1949
Paul Bakker68884e32013-01-07 18:20:04 +01001950 SSL_DEBUG_BUF( 4, "input record from network",
1951 ssl->in_hdr, 5 + ssl->in_msglen );
1952
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 if( ssl->in_hslen != 0 &&
1954 ssl->in_hslen < ssl->in_msglen )
1955 {
1956 /*
1957 * Get next Handshake message in the current record
1958 */
1959 ssl->in_msglen -= ssl->in_hslen;
1960
Paul Bakker8934a982011-08-05 11:11:53 +00001961 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001962 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001963
1964 ssl->in_hslen = 4;
1965 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1966
1967 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1968 " %d, type = %d, hslen = %d",
1969 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1970
1971 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1972 {
1973 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001974 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 }
1976
1977 if( ssl->in_msglen < ssl->in_hslen )
1978 {
1979 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001980 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001981 }
1982
Paul Bakker4224bc02014-04-08 14:36:50 +02001983 if( ssl->state != SSL_HANDSHAKE_OVER )
1984 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001985
1986 return( 0 );
1987 }
1988
1989 ssl->in_hslen = 0;
1990
1991 /*
1992 * Read the record header and validate it
1993 */
1994 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1995 {
1996 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1997 return( ret );
1998 }
1999
2000 ssl->in_msgtype = ssl->in_hdr[0];
2001 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2002
2003 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2004 "version = [%d:%d], msglen = %d",
2005 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2006 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2007
2008 if( ssl->in_hdr[1] != ssl->major_ver )
2009 {
2010 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002011 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 }
2013
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002014 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002015 {
2016 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002017 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 }
2019
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002020 /* Sanity check (outer boundaries) */
2021 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2022 {
2023 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2024 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2025 }
2026
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002028 * Make sure the message length is acceptable for the current transform
2029 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 */
Paul Bakker48916f92012-09-16 19:57:18 +00002031 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002033 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 {
2035 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002036 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 }
2038 }
2039 else
2040 {
Paul Bakker48916f92012-09-16 19:57:18 +00002041 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 {
2043 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002044 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 }
2046
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002047#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002049 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 {
2051 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002052 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002055
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002056#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2057 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 /*
2059 * TLS encrypted messages can have up to 256 bytes of padding
2060 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002061 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002062 ssl->in_msglen > ssl->transform_in->minlen +
2063 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 {
2065 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002066 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002068#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 }
2070
2071 /*
2072 * Read and optionally decrypt the message contents
2073 */
2074 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2075 {
2076 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2077 return( ret );
2078 }
2079
2080 SSL_DEBUG_BUF( 4, "input record from network",
2081 ssl->in_hdr, 5 + ssl->in_msglen );
2082
Paul Bakker05ef8352012-05-08 09:17:57 +00002083#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002084 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002085 {
2086 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2087
2088 ret = ssl_hw_record_read( ssl );
2089 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2090 {
2091 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002092 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002093 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002094
2095 if( ret == 0 )
2096 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002097 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002098#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002099 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 {
2101 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2102 {
Paul Bakker40865c82013-01-31 17:13:13 +01002103#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2104 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2105 {
2106 ssl_send_alert_message( ssl,
2107 SSL_ALERT_LEVEL_FATAL,
2108 SSL_ALERT_MSG_BAD_RECORD_MAC );
2109 }
2110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2112 return( ret );
2113 }
2114
2115 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2116 ssl->in_msg, ssl->in_msglen );
2117
2118 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2119 {
2120 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002121 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 }
2123 }
2124
Paul Bakker2770fbd2012-07-03 13:30:23 +00002125#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002126 if( ssl->transform_in != NULL &&
2127 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002128 {
2129 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2130 {
2131 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2132 return( ret );
2133 }
2134
2135 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2136 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2137 }
2138#endif /* POLARSSL_ZLIB_SUPPORT */
2139
Paul Bakker0a925182012-04-16 06:46:41 +00002140 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2141 ssl->in_msgtype != SSL_MSG_ALERT &&
2142 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2143 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2144 {
2145 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2146
Paul Bakker48916f92012-09-16 19:57:18 +00002147 if( ( ret = ssl_send_alert_message( ssl,
2148 SSL_ALERT_LEVEL_FATAL,
2149 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002150 {
2151 return( ret );
2152 }
2153
2154 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2155 }
2156
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2158 {
2159 ssl->in_hslen = 4;
2160 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2161
2162 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2163 " %d, type = %d, hslen = %d",
2164 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2165
2166 /*
2167 * Additional checks to validate the handshake header
2168 */
2169 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2170 {
2171 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002172 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 }
2174
2175 if( ssl->in_msglen < ssl->in_hslen )
2176 {
2177 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002178 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 }
2180
Paul Bakker48916f92012-09-16 19:57:18 +00002181 if( ssl->state != SSL_HANDSHAKE_OVER )
2182 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 }
2184
2185 if( ssl->in_msgtype == SSL_MSG_ALERT )
2186 {
2187 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2188 ssl->in_msg[0], ssl->in_msg[1] ) );
2189
2190 /*
2191 * Ignore non-fatal alerts, except close_notify
2192 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002193 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002195 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2196 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002197 /**
2198 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2199 * error identifier.
2200 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002201 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 }
2203
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002204 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2205 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 {
2207 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002208 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 }
2210 }
2211
2212 ssl->in_left = 0;
2213
2214 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2215
2216 return( 0 );
2217}
2218
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002219int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2220{
2221 int ret;
2222
2223 if( ( ret = ssl_send_alert_message( ssl,
2224 SSL_ALERT_LEVEL_FATAL,
2225 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2226 {
2227 return( ret );
2228 }
2229
2230 return( 0 );
2231}
2232
Paul Bakker0a925182012-04-16 06:46:41 +00002233int ssl_send_alert_message( ssl_context *ssl,
2234 unsigned char level,
2235 unsigned char message )
2236{
2237 int ret;
2238
2239 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2240
2241 ssl->out_msgtype = SSL_MSG_ALERT;
2242 ssl->out_msglen = 2;
2243 ssl->out_msg[0] = level;
2244 ssl->out_msg[1] = message;
2245
2246 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2247 {
2248 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2249 return( ret );
2250 }
2251
2252 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2253
2254 return( 0 );
2255}
2256
Paul Bakker5121ce52009-01-03 21:22:43 +00002257/*
2258 * Handshake functions
2259 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002260#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2261 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2262 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2263 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2264 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2265 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2266 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002267int ssl_write_certificate( ssl_context *ssl )
2268{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002269 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
2271 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2272
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002273 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002274 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2275 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002276 {
2277 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2278 ssl->state++;
2279 return( 0 );
2280 }
2281
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002282 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2283 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002284}
2285
2286int ssl_parse_certificate( ssl_context *ssl )
2287{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002288 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2289
2290 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2291
2292 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002293 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2294 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002295 {
2296 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2297 ssl->state++;
2298 return( 0 );
2299 }
2300
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002301 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2302 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002303}
2304#else
2305int ssl_write_certificate( ssl_context *ssl )
2306{
2307 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2308 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002309 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002310 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2311
2312 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2313
2314 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002315 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2316 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002317 {
2318 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2319 ssl->state++;
2320 return( 0 );
2321 }
2322
Paul Bakker5121ce52009-01-03 21:22:43 +00002323 if( ssl->endpoint == SSL_IS_CLIENT )
2324 {
2325 if( ssl->client_auth == 0 )
2326 {
2327 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2328 ssl->state++;
2329 return( 0 );
2330 }
2331
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002332#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002333 /*
2334 * If using SSLv3 and got no cert, send an Alert message
2335 * (otherwise an empty Certificate message will be sent).
2336 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002337 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002338 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2339 {
2340 ssl->out_msglen = 2;
2341 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002342 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2343 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002344
2345 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2346 goto write_msg;
2347 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002348#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002349 }
2350 else /* SSL_IS_SERVER */
2351 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002352 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002353 {
2354 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002355 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002356 }
2357 }
2358
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002359 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
2361 /*
2362 * 0 . 0 handshake type
2363 * 1 . 3 handshake length
2364 * 4 . 6 length of all certs
2365 * 7 . 9 length of cert. 1
2366 * 10 . n-1 peer certificate
2367 * n . n+2 length of cert. 2
2368 * n+3 . ... upper level cert, etc.
2369 */
2370 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002371 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002372
Paul Bakker29087132010-03-21 21:03:34 +00002373 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 {
2375 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002376 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 {
2378 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2379 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002380 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 }
2382
2383 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2384 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2385 ssl->out_msg[i + 2] = (unsigned char)( n );
2386
2387 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2388 i += n; crt = crt->next;
2389 }
2390
2391 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2392 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2393 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2394
2395 ssl->out_msglen = i;
2396 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2397 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2398
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002399#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002400write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002401#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
2403 ssl->state++;
2404
2405 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2406 {
2407 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2408 return( ret );
2409 }
2410
2411 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2412
Paul Bakkered27a042013-04-18 22:46:23 +02002413 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002414}
2415
2416int ssl_parse_certificate( ssl_context *ssl )
2417{
Paul Bakkered27a042013-04-18 22:46:23 +02002418 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002419 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002420 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
2422 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2423
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002424 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002425 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2426 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002427 {
2428 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2429 ssl->state++;
2430 return( 0 );
2431 }
2432
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002434 ( ssl->authmode == SSL_VERIFY_NONE ||
2435 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002437 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2439 ssl->state++;
2440 return( 0 );
2441 }
2442
2443 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2444 {
2445 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2446 return( ret );
2447 }
2448
2449 ssl->state++;
2450
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002451#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 /*
2453 * Check if the client sent an empty certificate
2454 */
2455 if( ssl->endpoint == SSL_IS_SERVER &&
2456 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2457 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002458 if( ssl->in_msglen == 2 &&
2459 ssl->in_msgtype == SSL_MSG_ALERT &&
2460 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2461 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 {
2463 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2464
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002465 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2467 return( 0 );
2468 else
Paul Bakker40e46942009-01-03 21:51:57 +00002469 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 }
2471 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002472#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002474#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2475 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 if( ssl->endpoint == SSL_IS_SERVER &&
2477 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2478 {
2479 if( ssl->in_hslen == 7 &&
2480 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2481 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2482 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2483 {
2484 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2485
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002486 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002488 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 else
2490 return( 0 );
2491 }
2492 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002493#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2494 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
2496 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2497 {
2498 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002499 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 }
2501
2502 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2503 {
2504 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002505 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 }
2507
2508 /*
2509 * Same message structure as in ssl_write_certificate()
2510 */
2511 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2512
2513 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2514 {
2515 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002516 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 }
2518
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002519 /* In case we tried to reuse a session but it failed */
2520 if( ssl->session_negotiate->peer_cert != NULL )
2521 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002522 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002523 polarssl_free( ssl->session_negotiate->peer_cert );
2524 }
2525
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002526 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2527 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 {
2529 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002530 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002531 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 }
2533
Paul Bakkerb6b09562013-09-18 14:17:41 +02002534 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
2536 i = 7;
2537
2538 while( i < ssl->in_hslen )
2539 {
2540 if( ssl->in_msg[i] != 0 )
2541 {
2542 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002543 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 }
2545
2546 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2547 | (unsigned int) ssl->in_msg[i + 2];
2548 i += 3;
2549
2550 if( n < 128 || i + n > ssl->in_hslen )
2551 {
2552 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002553 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555
Paul Bakkerddf26b42013-09-18 13:46:23 +02002556 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2557 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 if( ret != 0 )
2559 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002560 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 return( ret );
2562 }
2563
2564 i += n;
2565 }
2566
Paul Bakker48916f92012-09-16 19:57:18 +00002567 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002569 /*
2570 * On client, make sure the server cert doesn't change during renego to
2571 * avoid "triple handshake" attack: https://secure-resumption.com/
2572 */
2573 if( ssl->endpoint == SSL_IS_CLIENT &&
2574 ssl->renegotiation == SSL_RENEGOTIATION )
2575 {
2576 if( ssl->session->peer_cert == NULL )
2577 {
2578 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2579 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2580 }
2581
2582 if( ssl->session->peer_cert->raw.len !=
2583 ssl->session_negotiate->peer_cert->raw.len ||
2584 memcmp( ssl->session->peer_cert->raw.p,
2585 ssl->session_negotiate->peer_cert->raw.p,
2586 ssl->session->peer_cert->raw.len ) != 0 )
2587 {
2588 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2589 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2590 }
2591 }
2592
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 if( ssl->authmode != SSL_VERIFY_NONE )
2594 {
2595 if( ssl->ca_chain == NULL )
2596 {
2597 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002598 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
2600
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002601 /*
2602 * Main check: verify certificate
2603 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002604 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2605 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2606 &ssl->session_negotiate->verify_result,
2607 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
2609 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002610 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002612 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002613
2614 /*
2615 * Secondary checks: always done, but change 'ret' only if it was 0
2616 */
2617
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002618#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002619 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002620 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002621
2622 /* If certificate uses an EC key, make sure the curve is OK */
2623 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2624 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2625 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002626 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002627 if( ret == 0 )
2628 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002629 }
2630 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002631#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002633 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2634 ciphersuite_info,
2635 ! ssl->endpoint ) != 0 )
2636 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002637 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002638 if( ret == 0 )
2639 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2640 }
2641
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2643 ret = 0;
2644 }
2645
2646 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2647
2648 return( ret );
2649}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002650#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2651 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2652 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2653 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2654 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2655 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2656 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
2658int ssl_write_change_cipher_spec( ssl_context *ssl )
2659{
2660 int ret;
2661
2662 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2663
2664 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2665 ssl->out_msglen = 1;
2666 ssl->out_msg[0] = 1;
2667
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 ssl->state++;
2669
2670 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2671 {
2672 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2673 return( ret );
2674 }
2675
2676 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2677
2678 return( 0 );
2679}
2680
2681int ssl_parse_change_cipher_spec( ssl_context *ssl )
2682{
2683 int ret;
2684
2685 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2686
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2688 {
2689 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2690 return( ret );
2691 }
2692
2693 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2694 {
2695 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002696 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 }
2698
2699 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2700 {
2701 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002702 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 }
2704
2705 ssl->state++;
2706
2707 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2708
2709 return( 0 );
2710}
2711
Paul Bakker41c83d32013-03-20 14:39:14 +01002712void ssl_optimize_checksum( ssl_context *ssl,
2713 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002714{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002715 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002716
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002717#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2718 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002719 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002720 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002721 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002722#endif
2723#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2724#if defined(POLARSSL_SHA512_C)
2725 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2726 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2727 else
2728#endif
2729#if defined(POLARSSL_SHA256_C)
2730 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002731 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002732 else
2733#endif
2734#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002735 {
2736 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002737 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002738 }
Paul Bakker380da532012-04-18 16:10:25 +00002739}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002740
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002741static void ssl_update_checksum_start( ssl_context *ssl,
2742 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002743{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002744#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2745 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002746 md5_update( &ssl->handshake->fin_md5 , buf, len );
2747 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002748#endif
2749#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2750#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002751 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002752#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002753#if defined(POLARSSL_SHA512_C)
2754 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002755#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002756#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002757}
2758
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002759#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2760 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002761static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2762 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002763{
Paul Bakker48916f92012-09-16 19:57:18 +00002764 md5_update( &ssl->handshake->fin_md5 , buf, len );
2765 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002766}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002767#endif
Paul Bakker380da532012-04-18 16:10:25 +00002768
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002769#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2770#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002771static void ssl_update_checksum_sha256( ssl_context *ssl,
2772 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002773{
Paul Bakker9e36f042013-06-30 14:34:05 +02002774 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002775}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002776#endif
Paul Bakker380da532012-04-18 16:10:25 +00002777
Paul Bakker9e36f042013-06-30 14:34:05 +02002778#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002779static void ssl_update_checksum_sha384( ssl_context *ssl,
2780 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002781{
Paul Bakker9e36f042013-06-30 14:34:05 +02002782 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002783}
Paul Bakker769075d2012-11-24 11:26:46 +01002784#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002785#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002786
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002787#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002788static void ssl_calc_finished_ssl(
2789 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002790{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002791 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002792 md5_context md5;
2793 sha1_context sha1;
2794
Paul Bakker5121ce52009-01-03 21:22:43 +00002795 unsigned char padbuf[48];
2796 unsigned char md5sum[16];
2797 unsigned char sha1sum[20];
2798
Paul Bakker48916f92012-09-16 19:57:18 +00002799 ssl_session *session = ssl->session_negotiate;
2800 if( !session )
2801 session = ssl->session;
2802
Paul Bakker1ef83d62012-04-11 12:09:53 +00002803 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2804
Paul Bakker48916f92012-09-16 19:57:18 +00002805 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2806 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
2808 /*
2809 * SSLv3:
2810 * hash =
2811 * MD5( master + pad2 +
2812 * MD5( handshake + sender + master + pad1 ) )
2813 * + SHA1( master + pad2 +
2814 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 */
2816
Paul Bakker90995b52013-06-24 19:20:35 +02002817#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002819 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002820#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
Paul Bakker90995b52013-06-24 19:20:35 +02002822#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002824 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002825#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002826
Paul Bakker3c2122f2013-06-24 19:03:14 +02002827 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2828 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Paul Bakker1ef83d62012-04-11 12:09:53 +00002830 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Paul Bakker3c2122f2013-06-24 19:03:14 +02002832 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002833 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002834 md5_update( &md5, padbuf, 48 );
2835 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Paul Bakker3c2122f2013-06-24 19:03:14 +02002837 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002838 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002839 sha1_update( &sha1, padbuf, 40 );
2840 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Paul Bakker1ef83d62012-04-11 12:09:53 +00002842 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Paul Bakker1ef83d62012-04-11 12:09:53 +00002844 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002845 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002846 md5_update( &md5, padbuf, 48 );
2847 md5_update( &md5, md5sum, 16 );
2848 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Paul Bakker1ef83d62012-04-11 12:09:53 +00002850 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002851 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002852 sha1_update( &sha1, padbuf , 40 );
2853 sha1_update( &sha1, sha1sum, 20 );
2854 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Paul Bakker1ef83d62012-04-11 12:09:53 +00002856 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002857
Paul Bakker34617722014-06-13 17:20:13 +02002858 polarssl_zeroize( &md5, sizeof( md5_context ) );
2859 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Paul Bakker34617722014-06-13 17:20:13 +02002861 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2862 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2863 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
2865 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2866}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002867#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002869#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002870static void ssl_calc_finished_tls(
2871 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002872{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002874 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002875 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002876 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002877 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002878
Paul Bakker48916f92012-09-16 19:57:18 +00002879 ssl_session *session = ssl->session_negotiate;
2880 if( !session )
2881 session = ssl->session;
2882
Paul Bakker1ef83d62012-04-11 12:09:53 +00002883 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Paul Bakker48916f92012-09-16 19:57:18 +00002885 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2886 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Paul Bakker1ef83d62012-04-11 12:09:53 +00002888 /*
2889 * TLSv1:
2890 * hash = PRF( master, finished_label,
2891 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2892 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
Paul Bakker90995b52013-06-24 19:20:35 +02002894#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002895 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2896 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002897#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002898
Paul Bakker90995b52013-06-24 19:20:35 +02002899#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2901 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002902#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002903
2904 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002905 ? "client finished"
2906 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002907
2908 md5_finish( &md5, padbuf );
2909 sha1_finish( &sha1, padbuf + 16 );
2910
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002911 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002912 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002913
2914 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2915
Paul Bakker34617722014-06-13 17:20:13 +02002916 polarssl_zeroize( &md5, sizeof( md5_context ) );
2917 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002918
Paul Bakker34617722014-06-13 17:20:13 +02002919 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002920
2921 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2922}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002923#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002924
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002925#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2926#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002927static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002928 ssl_context *ssl, unsigned char *buf, int from )
2929{
2930 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002931 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002932 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002933 unsigned char padbuf[32];
2934
Paul Bakker48916f92012-09-16 19:57:18 +00002935 ssl_session *session = ssl->session_negotiate;
2936 if( !session )
2937 session = ssl->session;
2938
Paul Bakker380da532012-04-18 16:10:25 +00002939 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002940
Paul Bakker9e36f042013-06-30 14:34:05 +02002941 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002942
2943 /*
2944 * TLSv1.2:
2945 * hash = PRF( master, finished_label,
2946 * Hash( handshake ) )[0.11]
2947 */
2948
Paul Bakker9e36f042013-06-30 14:34:05 +02002949#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002950 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002951 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002952#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002953
2954 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002955 ? "client finished"
2956 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002957
Paul Bakker9e36f042013-06-30 14:34:05 +02002958 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002959
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002960 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002961 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962
2963 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2964
Paul Bakker34617722014-06-13 17:20:13 +02002965 polarssl_zeroize( &sha256, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002966
Paul Bakker34617722014-06-13 17:20:13 +02002967 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968
2969 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2970}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002971#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002972
Paul Bakker9e36f042013-06-30 14:34:05 +02002973#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002974static void ssl_calc_finished_tls_sha384(
2975 ssl_context *ssl, unsigned char *buf, int from )
2976{
2977 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002978 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002979 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002980 unsigned char padbuf[48];
2981
Paul Bakker48916f92012-09-16 19:57:18 +00002982 ssl_session *session = ssl->session_negotiate;
2983 if( !session )
2984 session = ssl->session;
2985
Paul Bakker380da532012-04-18 16:10:25 +00002986 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002987
Paul Bakker9e36f042013-06-30 14:34:05 +02002988 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002989
2990 /*
2991 * TLSv1.2:
2992 * hash = PRF( master, finished_label,
2993 * Hash( handshake ) )[0.11]
2994 */
2995
Paul Bakker9e36f042013-06-30 14:34:05 +02002996#if !defined(POLARSSL_SHA512_ALT)
2997 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
2998 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002999#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000
3001 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003002 ? "client finished"
3003 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003004
Paul Bakker9e36f042013-06-30 14:34:05 +02003005 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003006
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003007 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003008 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003009
3010 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3011
Paul Bakker34617722014-06-13 17:20:13 +02003012 polarssl_zeroize( &sha512, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003013
Paul Bakker34617722014-06-13 17:20:13 +02003014 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003015
3016 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3017}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003018#endif /* POLARSSL_SHA512_C */
3019#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003020
Paul Bakker48916f92012-09-16 19:57:18 +00003021void ssl_handshake_wrapup( ssl_context *ssl )
3022{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003023 int resume = ssl->handshake->resume;
3024
Paul Bakker48916f92012-09-16 19:57:18 +00003025 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3026
3027 /*
3028 * Free our handshake params
3029 */
3030 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003031 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003032 ssl->handshake = NULL;
3033
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003034 if( ssl->renegotiation == SSL_RENEGOTIATION )
3035 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3036
Paul Bakker48916f92012-09-16 19:57:18 +00003037 /*
3038 * Switch in our now active transform context
3039 */
3040 if( ssl->transform )
3041 {
3042 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003043 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003044 }
3045 ssl->transform = ssl->transform_negotiate;
3046 ssl->transform_negotiate = NULL;
3047
Paul Bakker0a597072012-09-25 21:55:46 +00003048 if( ssl->session )
3049 {
3050 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003051 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003052 }
3053 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003054 ssl->session_negotiate = NULL;
3055
Paul Bakker0a597072012-09-25 21:55:46 +00003056 /*
3057 * Add cache entry
3058 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003059 if( ssl->f_set_cache != NULL &&
3060 ssl->session->length != 0 &&
3061 resume == 0 )
3062 {
Paul Bakker0a597072012-09-25 21:55:46 +00003063 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3064 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003065 }
Paul Bakker0a597072012-09-25 21:55:46 +00003066
Paul Bakker48916f92012-09-16 19:57:18 +00003067 ssl->state++;
3068
3069 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3070}
3071
Paul Bakker1ef83d62012-04-11 12:09:53 +00003072int ssl_write_finished( ssl_context *ssl )
3073{
3074 int ret, hash_len;
3075
3076 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3077
Paul Bakker92be97b2013-01-02 17:30:03 +01003078 /*
3079 * Set the out_msg pointer to the correct location based on IV length
3080 */
3081 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3082 {
3083 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3084 ssl->transform_negotiate->fixed_ivlen;
3085 }
3086 else
3087 ssl->out_msg = ssl->out_iv;
3088
Paul Bakker48916f92012-09-16 19:57:18 +00003089 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003090
3091 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3093
Paul Bakker48916f92012-09-16 19:57:18 +00003094 ssl->verify_data_len = hash_len;
3095 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3096
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 ssl->out_msglen = 4 + hash_len;
3098 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3099 ssl->out_msg[0] = SSL_HS_FINISHED;
3100
3101 /*
3102 * In case of session resuming, invert the client and server
3103 * ChangeCipherSpec messages order.
3104 */
Paul Bakker0a597072012-09-25 21:55:46 +00003105 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003106 {
3107 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003108 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 else
3110 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3111 }
3112 else
3113 ssl->state++;
3114
Paul Bakker48916f92012-09-16 19:57:18 +00003115 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003116 * Switch to our negotiated transform and session parameters for outbound
3117 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003118 */
3119 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3120 ssl->transform_out = ssl->transform_negotiate;
3121 ssl->session_out = ssl->session_negotiate;
3122 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003123
Paul Bakker07eb38b2012-12-19 14:42:06 +01003124#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003125 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003126 {
3127 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3128 {
3129 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3130 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3131 }
3132 }
3133#endif
3134
Paul Bakker5121ce52009-01-03 21:22:43 +00003135 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3136 {
3137 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3138 return( ret );
3139 }
3140
3141 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3142
3143 return( 0 );
3144}
3145
3146int ssl_parse_finished( ssl_context *ssl )
3147{
Paul Bakker23986e52011-04-24 08:57:21 +00003148 int ret;
3149 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003150 unsigned char buf[36];
3151
3152 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3153
Paul Bakker48916f92012-09-16 19:57:18 +00003154 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Paul Bakker48916f92012-09-16 19:57:18 +00003156 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003157 * Switch to our negotiated transform and session parameters for inbound
3158 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003159 */
3160 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3161 ssl->transform_in = ssl->transform_negotiate;
3162 ssl->session_in = ssl->session_negotiate;
3163 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
Paul Bakker92be97b2013-01-02 17:30:03 +01003165 /*
3166 * Set the in_msg pointer to the correct location based on IV length
3167 */
3168 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3169 {
3170 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3171 ssl->transform_negotiate->fixed_ivlen;
3172 }
3173 else
3174 ssl->in_msg = ssl->in_iv;
3175
Paul Bakker07eb38b2012-12-19 14:42:06 +01003176#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003177 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003178 {
3179 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3180 {
3181 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3182 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3183 }
3184 }
3185#endif
3186
Paul Bakker5121ce52009-01-03 21:22:43 +00003187 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3188 {
3189 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3190 return( ret );
3191 }
3192
3193 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3194 {
3195 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003196 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003197 }
3198
Paul Bakker1ef83d62012-04-11 12:09:53 +00003199 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3201
3202 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3203 ssl->in_hslen != 4 + hash_len )
3204 {
3205 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003206 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 }
3208
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003209 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 {
3211 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003212 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 }
3214
Paul Bakker48916f92012-09-16 19:57:18 +00003215 ssl->verify_data_len = hash_len;
3216 memcpy( ssl->peer_verify_data, buf, hash_len );
3217
Paul Bakker0a597072012-09-25 21:55:46 +00003218 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 {
3220 if( ssl->endpoint == SSL_IS_CLIENT )
3221 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3222
3223 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003224 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003225 }
3226 else
3227 ssl->state++;
3228
3229 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3230
3231 return( 0 );
3232}
3233
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003234static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003235{
3236 if( ssl->transform_negotiate )
3237 ssl_transform_free( ssl->transform_negotiate );
3238 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003239 {
3240 ssl->transform_negotiate =
3241 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003242
3243 if( ssl->transform_negotiate != NULL )
3244 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003245 }
Paul Bakker48916f92012-09-16 19:57:18 +00003246
3247 if( ssl->session_negotiate )
3248 ssl_session_free( ssl->session_negotiate );
3249 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003250 {
3251 ssl->session_negotiate =
3252 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003253
3254 if( ssl->session_negotiate != NULL )
3255 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003256 }
Paul Bakker48916f92012-09-16 19:57:18 +00003257
3258 if( ssl->handshake )
3259 ssl_handshake_free( ssl->handshake );
3260 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003261 {
3262 ssl->handshake = (ssl_handshake_params *)
3263 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003264
3265 if( ssl->handshake != NULL )
3266 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003267 }
Paul Bakker48916f92012-09-16 19:57:18 +00003268
3269 if( ssl->handshake == NULL ||
3270 ssl->transform_negotiate == NULL ||
3271 ssl->session_negotiate == NULL )
3272 {
3273 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3274 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3275 }
3276
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003277#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3278 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003279 md5_starts( &ssl->handshake->fin_md5 );
3280 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003281#endif
3282#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3283#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003284 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003285#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003286#if defined(POLARSSL_SHA512_C)
3287 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003288#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003289#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003290
3291 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003292 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003293
Paul Bakker61d113b2013-07-04 11:51:43 +02003294#if defined(POLARSSL_ECDH_C)
3295 ecdh_init( &ssl->handshake->ecdh_ctx );
3296#endif
3297
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003298#if defined(POLARSSL_X509_CRT_PARSE_C)
3299 ssl->handshake->key_cert = ssl->key_cert;
3300#endif
3301
Paul Bakker48916f92012-09-16 19:57:18 +00003302 return( 0 );
3303}
3304
Paul Bakker5121ce52009-01-03 21:22:43 +00003305/*
3306 * Initialize an SSL context
3307 */
3308int ssl_init( ssl_context *ssl )
3309{
Paul Bakker48916f92012-09-16 19:57:18 +00003310 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003311 int len = SSL_BUFFER_LEN;
3312
3313 memset( ssl, 0, sizeof( ssl_context ) );
3314
Paul Bakker62f2dee2012-09-28 07:31:51 +00003315 /*
3316 * Sane defaults
3317 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003318 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3319 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3320 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3321 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003322
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003323 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003324
Paul Bakker62f2dee2012-09-28 07:31:51 +00003325#if defined(POLARSSL_DHM_C)
3326 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3327 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3328 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3329 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3330 {
3331 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3332 return( ret );
3333 }
3334#endif
3335
3336 /*
3337 * Prepare base structures
3338 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003339 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003340 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003341 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 ssl->in_msg = ssl->in_ctr + 13;
3343
3344 if( ssl->in_ctr == NULL )
3345 {
3346 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003347 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 }
3349
Paul Bakker6e339b52013-07-03 13:37:05 +02003350 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003352 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003353 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003354
3355 if( ssl->out_ctr == NULL )
3356 {
3357 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003358 polarssl_free( ssl->in_ctr );
3359 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003360 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 }
3362
3363 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3364 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3365
Paul Bakker606b4ba2013-08-14 16:52:14 +02003366#if defined(POLARSSL_SSL_SESSION_TICKETS)
3367 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3368#endif
3369
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003370#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003371 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003372#endif
3373
Paul Bakker48916f92012-09-16 19:57:18 +00003374 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3375 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003376
3377 return( 0 );
3378}
3379
3380/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003381 * Reset an initialized and used SSL context for re-use while retaining
3382 * all application-set variables, function pointers and data.
3383 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003384int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003385{
Paul Bakker48916f92012-09-16 19:57:18 +00003386 int ret;
3387
Paul Bakker7eb013f2011-10-06 12:37:39 +00003388 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003389 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3390 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3391
3392 ssl->verify_data_len = 0;
3393 memset( ssl->own_verify_data, 0, 36 );
3394 memset( ssl->peer_verify_data, 0, 36 );
3395
Paul Bakker7eb013f2011-10-06 12:37:39 +00003396 ssl->in_offt = NULL;
3397
Paul Bakker92be97b2013-01-02 17:30:03 +01003398 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003399 ssl->in_msgtype = 0;
3400 ssl->in_msglen = 0;
3401 ssl->in_left = 0;
3402
3403 ssl->in_hslen = 0;
3404 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003405 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003406
Paul Bakker92be97b2013-01-02 17:30:03 +01003407 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003408 ssl->out_msgtype = 0;
3409 ssl->out_msglen = 0;
3410 ssl->out_left = 0;
3411
Paul Bakker48916f92012-09-16 19:57:18 +00003412 ssl->transform_in = NULL;
3413 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003414
3415 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3416 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003417
3418#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003419 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003420 {
3421 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003422 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003423 {
3424 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3425 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3426 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003427 }
3428#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003429
Paul Bakker48916f92012-09-16 19:57:18 +00003430 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003431 {
Paul Bakker48916f92012-09-16 19:57:18 +00003432 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003433 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003434 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003435 }
Paul Bakker48916f92012-09-16 19:57:18 +00003436
Paul Bakkerc0463502013-02-14 11:19:38 +01003437 if( ssl->session )
3438 {
3439 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003440 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003441 ssl->session = NULL;
3442 }
3443
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003444#if defined(POLARSSL_SSL_ALPN)
3445 ssl->alpn_chosen = NULL;
3446#endif
3447
Paul Bakker48916f92012-09-16 19:57:18 +00003448 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3449 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003450
3451 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003452}
3453
Paul Bakkera503a632013-08-14 13:48:06 +02003454#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003455/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003456 * Allocate and initialize ticket keys
3457 */
3458static int ssl_ticket_keys_init( ssl_context *ssl )
3459{
3460 int ret;
3461 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003462 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003463
3464 if( ssl->ticket_keys != NULL )
3465 return( 0 );
3466
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003467 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3468 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003469 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3470
3471 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003472 {
3473 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003474 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003475 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003476
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003477 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3478 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3479 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3480 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003481 polarssl_free( tkeys );
3482 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003483 }
3484
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003485 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003486 {
3487 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003488 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003489 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003490
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003491 ssl->ticket_keys = tkeys;
3492
3493 return( 0 );
3494}
Paul Bakkera503a632013-08-14 13:48:06 +02003495#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003496
3497/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003498 * SSL set accessors
3499 */
3500void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3501{
3502 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003503
Paul Bakker606b4ba2013-08-14 16:52:14 +02003504#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003505 if( endpoint == SSL_IS_CLIENT )
3506 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003507#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003508}
3509
3510void ssl_set_authmode( ssl_context *ssl, int authmode )
3511{
3512 ssl->authmode = authmode;
3513}
3514
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003515#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003516void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003517 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003518 void *p_vrfy )
3519{
3520 ssl->f_vrfy = f_vrfy;
3521 ssl->p_vrfy = p_vrfy;
3522}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003523#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003524
Paul Bakker5121ce52009-01-03 21:22:43 +00003525void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003526 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003527 void *p_rng )
3528{
3529 ssl->f_rng = f_rng;
3530 ssl->p_rng = p_rng;
3531}
3532
3533void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003534 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003535 void *p_dbg )
3536{
3537 ssl->f_dbg = f_dbg;
3538 ssl->p_dbg = p_dbg;
3539}
3540
3541void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003542 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003543 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003544{
3545 ssl->f_recv = f_recv;
3546 ssl->f_send = f_send;
3547 ssl->p_recv = p_recv;
3548 ssl->p_send = p_send;
3549}
3550
Paul Bakker0a597072012-09-25 21:55:46 +00003551void ssl_set_session_cache( ssl_context *ssl,
3552 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3553 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003554{
Paul Bakker0a597072012-09-25 21:55:46 +00003555 ssl->f_get_cache = f_get_cache;
3556 ssl->p_get_cache = p_get_cache;
3557 ssl->f_set_cache = f_set_cache;
3558 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003559}
3560
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003561int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003562{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003563 int ret;
3564
3565 if( ssl == NULL ||
3566 session == NULL ||
3567 ssl->session_negotiate == NULL ||
3568 ssl->endpoint != SSL_IS_CLIENT )
3569 {
3570 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3571 }
3572
3573 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3574 return( ret );
3575
Paul Bakker0a597072012-09-25 21:55:46 +00003576 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003577
3578 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003579}
3580
Paul Bakkerb68cad62012-08-23 08:34:18 +00003581void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003582{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003583 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3584 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3585 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3586 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3587}
3588
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003589void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3590 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003591 int major, int minor )
3592{
3593 if( major != SSL_MAJOR_VERSION_3 )
3594 return;
3595
3596 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3597 return;
3598
3599 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003600}
3601
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003602#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003603/* Add a new (empty) key_cert entry an return a pointer to it */
3604static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3605{
3606 ssl_key_cert *key_cert, *last;
3607
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003608 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3609 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003610 return( NULL );
3611
3612 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3613
3614 /* Append the new key_cert to the (possibly empty) current list */
3615 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003616 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003617 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003618 if( ssl->handshake != NULL )
3619 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003620 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003621 else
3622 {
3623 last = ssl->key_cert;
3624 while( last->next != NULL )
3625 last = last->next;
3626 last->next = key_cert;
3627 }
3628
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003629 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003630}
3631
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003632void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003633 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003634{
3635 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003636 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 ssl->peer_cn = peer_cn;
3638}
3639
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003640int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003641 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003642{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003643 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3644
3645 if( key_cert == NULL )
3646 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3647
3648 key_cert->cert = own_cert;
3649 key_cert->key = pk_key;
3650
3651 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003652}
3653
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003654#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003655int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003656 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003657{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003658 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003659 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003660
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003661 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003662 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3663
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003664 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3665 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003666 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003667
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003668 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003669
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003670 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003671 if( ret != 0 )
3672 return( ret );
3673
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003674 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003675 return( ret );
3676
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003677 key_cert->cert = own_cert;
3678 key_cert->key_own_alloc = 1;
3679
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003680 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003681}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003682#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003683
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003684int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003685 void *rsa_key,
3686 rsa_decrypt_func rsa_decrypt,
3687 rsa_sign_func rsa_sign,
3688 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003689{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003690 int ret;
3691 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003692
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003693 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003694 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3695
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003696 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3697 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003698 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003699
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003700 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003701
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003702 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3703 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3704 return( ret );
3705
3706 key_cert->cert = own_cert;
3707 key_cert->key_own_alloc = 1;
3708
3709 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003710}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003711#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003712
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003713#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003714int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3715 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003716{
Paul Bakker6db455e2013-09-18 17:29:31 +02003717 if( psk == NULL || psk_identity == NULL )
3718 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3719
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003720 /*
3721 * The length will be check later anyway, but in case it is obviously
3722 * too large, better abort now. The PMS is as follows:
3723 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3724 */
3725 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3726 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3727
Paul Bakker6db455e2013-09-18 17:29:31 +02003728 if( ssl->psk != NULL )
3729 {
3730 polarssl_free( ssl->psk );
3731 polarssl_free( ssl->psk_identity );
3732 }
3733
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003734 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003735 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003736
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003737 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003738 ssl->psk_identity = (unsigned char *)
3739 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003740
3741 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3742 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3743
3744 memcpy( ssl->psk, psk, ssl->psk_len );
3745 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003746
3747 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003748}
3749
3750void ssl_set_psk_cb( ssl_context *ssl,
3751 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3752 size_t),
3753 void *p_psk )
3754{
3755 ssl->f_psk = f_psk;
3756 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003757}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003758#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003759
Paul Bakker48916f92012-09-16 19:57:18 +00003760#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003761int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003762{
3763 int ret;
3764
Paul Bakker48916f92012-09-16 19:57:18 +00003765 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003766 {
3767 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3768 return( ret );
3769 }
3770
Paul Bakker48916f92012-09-16 19:57:18 +00003771 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003772 {
3773 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3774 return( ret );
3775 }
3776
3777 return( 0 );
3778}
3779
Paul Bakker1b57b062011-01-06 15:48:19 +00003780int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3781{
3782 int ret;
3783
Paul Bakker66d5d072014-06-17 16:39:18 +02003784 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003785 {
3786 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3787 return( ret );
3788 }
3789
Paul Bakker66d5d072014-06-17 16:39:18 +02003790 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003791 {
3792 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3793 return( ret );
3794 }
3795
3796 return( 0 );
3797}
Paul Bakker48916f92012-09-16 19:57:18 +00003798#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003799
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003800#if defined(POLARSSL_SSL_SET_CURVES)
3801/*
3802 * Set the allowed elliptic curves
3803 */
3804void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3805{
3806 ssl->curve_list = curve_list;
3807}
3808#endif
3809
Paul Bakker0be444a2013-08-27 21:55:01 +02003810#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003811int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003812{
3813 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003814 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003815
3816 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003817
3818 if( ssl->hostname_len + 1 == 0 )
3819 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3820
Paul Bakker6e339b52013-07-03 13:37:05 +02003821 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003822
Paul Bakkerb15b8512012-01-13 13:44:06 +00003823 if( ssl->hostname == NULL )
3824 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3825
Paul Bakker3c2122f2013-06-24 19:03:14 +02003826 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003827 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003828
Paul Bakker40ea7de2009-05-03 10:18:48 +00003829 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003830
3831 return( 0 );
3832}
3833
Paul Bakker5701cdc2012-09-27 21:49:42 +00003834void ssl_set_sni( ssl_context *ssl,
3835 int (*f_sni)(void *, ssl_context *,
3836 const unsigned char *, size_t),
3837 void *p_sni )
3838{
3839 ssl->f_sni = f_sni;
3840 ssl->p_sni = p_sni;
3841}
Paul Bakker0be444a2013-08-27 21:55:01 +02003842#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003843
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003844#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003845int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003846{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003847 size_t cur_len, tot_len;
3848 const char **p;
3849
3850 /*
3851 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3852 * truncated". Check lengths now rather than later.
3853 */
3854 tot_len = 0;
3855 for( p = protos; *p != NULL; p++ )
3856 {
3857 cur_len = strlen( *p );
3858 tot_len += cur_len;
3859
3860 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3861 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3862 }
3863
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003864 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003865
3866 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003867}
3868
3869const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3870{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003871 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003872}
3873#endif /* POLARSSL_SSL_ALPN */
3874
Paul Bakker490ecc82011-10-06 13:04:09 +00003875void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3876{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003877 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3878 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3879 {
3880 ssl->max_major_ver = major;
3881 ssl->max_minor_ver = minor;
3882 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003883}
3884
Paul Bakker1d29fb52012-09-28 13:28:45 +00003885void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3886{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003887 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3888 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3889 {
3890 ssl->min_major_ver = major;
3891 ssl->min_minor_ver = minor;
3892 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003893}
3894
Paul Bakker05decb22013-08-15 13:33:48 +02003895#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003896int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3897{
Paul Bakker77e257e2013-12-16 15:29:52 +01003898 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003899 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003900 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003901 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003902 }
3903
3904 ssl->mfl_code = mfl_code;
3905
3906 return( 0 );
3907}
Paul Bakker05decb22013-08-15 13:33:48 +02003908#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003909
Paul Bakker1f2bc622013-08-15 13:45:55 +02003910#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003911int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003912{
3913 if( ssl->endpoint != SSL_IS_CLIENT )
3914 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3915
Paul Bakker8c1ede62013-07-19 14:14:37 +02003916 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003917
3918 return( 0 );
3919}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003920#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003921
Paul Bakker48916f92012-09-16 19:57:18 +00003922void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3923{
3924 ssl->disable_renegotiation = renegotiation;
3925}
3926
3927void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3928{
3929 ssl->allow_legacy_renegotiation = allow_legacy;
3930}
3931
Paul Bakkera503a632013-08-14 13:48:06 +02003932#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003933int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3934{
3935 ssl->session_tickets = use_tickets;
3936
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003937 if( ssl->endpoint == SSL_IS_CLIENT )
3938 return( 0 );
3939
3940 if( ssl->f_rng == NULL )
3941 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3942
3943 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003944}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003945
3946void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3947{
3948 ssl->ticket_lifetime = lifetime;
3949}
Paul Bakkera503a632013-08-14 13:48:06 +02003950#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003951
Paul Bakker5121ce52009-01-03 21:22:43 +00003952/*
3953 * SSL get accessors
3954 */
Paul Bakker23986e52011-04-24 08:57:21 +00003955size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003956{
3957 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3958}
3959
Paul Bakkerff60ee62010-03-16 21:09:09 +00003960int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003961{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003962 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003963}
3964
Paul Bakkere3166ce2011-01-27 17:40:50 +00003965const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003966{
Paul Bakker926c8e42013-03-06 10:23:34 +01003967 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003968 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003969
Paul Bakkere3166ce2011-01-27 17:40:50 +00003970 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003971}
3972
Paul Bakker43ca69c2011-01-15 17:35:19 +00003973const char *ssl_get_version( const ssl_context *ssl )
3974{
3975 switch( ssl->minor_ver )
3976 {
3977 case SSL_MINOR_VERSION_0:
3978 return( "SSLv3.0" );
3979
3980 case SSL_MINOR_VERSION_1:
3981 return( "TLSv1.0" );
3982
3983 case SSL_MINOR_VERSION_2:
3984 return( "TLSv1.1" );
3985
Paul Bakker1ef83d62012-04-11 12:09:53 +00003986 case SSL_MINOR_VERSION_3:
3987 return( "TLSv1.2" );
3988
Paul Bakker43ca69c2011-01-15 17:35:19 +00003989 default:
3990 break;
3991 }
3992 return( "unknown" );
3993}
3994
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003995#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003996const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003997{
3998 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003999 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004000
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004001 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004002}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004003#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004004
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004005int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4006{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004007 if( ssl == NULL ||
4008 dst == NULL ||
4009 ssl->session == NULL ||
4010 ssl->endpoint != SSL_IS_CLIENT )
4011 {
4012 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4013 }
4014
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004015 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004016}
4017
Paul Bakker5121ce52009-01-03 21:22:43 +00004018/*
Paul Bakker1961b702013-01-25 14:49:24 +01004019 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004020 */
Paul Bakker1961b702013-01-25 14:49:24 +01004021int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004022{
Paul Bakker40e46942009-01-03 21:51:57 +00004023 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004024
Paul Bakker40e46942009-01-03 21:51:57 +00004025#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004026 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004027 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004028#endif
4029
Paul Bakker40e46942009-01-03 21:51:57 +00004030#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004031 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004032 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004033#endif
4034
Paul Bakker1961b702013-01-25 14:49:24 +01004035 return( ret );
4036}
4037
4038/*
4039 * Perform the SSL handshake
4040 */
4041int ssl_handshake( ssl_context *ssl )
4042{
4043 int ret = 0;
4044
4045 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4046
4047 while( ssl->state != SSL_HANDSHAKE_OVER )
4048 {
4049 ret = ssl_handshake_step( ssl );
4050
4051 if( ret != 0 )
4052 break;
4053 }
4054
Paul Bakker5121ce52009-01-03 21:22:43 +00004055 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4056
4057 return( ret );
4058}
4059
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004060#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004061/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004062 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004063 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004064static int ssl_write_hello_request( ssl_context *ssl )
4065{
4066 int ret;
4067
4068 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4069
4070 ssl->out_msglen = 4;
4071 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4072 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4073
4074 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4075 {
4076 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4077 return( ret );
4078 }
4079
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004080 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4081
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004082 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4083
4084 return( 0 );
4085}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004086#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004087
4088/*
4089 * Actually renegotiate current connection, triggered by either:
4090 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004091 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004092 * - receiving any handshake message on server during ssl_read() after the
4093 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004094 * If the handshake doesn't complete due to waiting for I/O, it will continue
4095 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004096 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004097static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004098{
4099 int ret;
4100
4101 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4102
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004103 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4104 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004105
4106 ssl->state = SSL_HELLO_REQUEST;
4107 ssl->renegotiation = SSL_RENEGOTIATION;
4108
Paul Bakker48916f92012-09-16 19:57:18 +00004109 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4110 {
4111 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4112 return( ret );
4113 }
4114
4115 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4116
4117 return( 0 );
4118}
4119
4120/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004121 * Renegotiate current connection on client,
4122 * or request renegotiation on server
4123 */
4124int ssl_renegotiate( ssl_context *ssl )
4125{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004126 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004127
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004128#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004129 /* On server, just send the request */
4130 if( ssl->endpoint == SSL_IS_SERVER )
4131 {
4132 if( ssl->state != SSL_HANDSHAKE_OVER )
4133 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4134
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004135 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004136 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004137#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004138
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004139#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004140 /*
4141 * On client, either start the renegotiation process or,
4142 * if already in progress, continue the handshake
4143 */
4144 if( ssl->renegotiation != SSL_RENEGOTIATION )
4145 {
4146 if( ssl->state != SSL_HANDSHAKE_OVER )
4147 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4148
4149 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4150 {
4151 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4152 return( ret );
4153 }
4154 }
4155 else
4156 {
4157 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4158 {
4159 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4160 return( ret );
4161 }
4162 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004163#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004164
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004165 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004166}
4167
4168/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004169 * Receive application data decrypted from the SSL layer
4170 */
Paul Bakker23986e52011-04-24 08:57:21 +00004171int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004172{
Paul Bakker23986e52011-04-24 08:57:21 +00004173 int ret;
4174 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004175
4176 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4177
4178 if( ssl->state != SSL_HANDSHAKE_OVER )
4179 {
4180 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4181 {
4182 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4183 return( ret );
4184 }
4185 }
4186
4187 if( ssl->in_offt == NULL )
4188 {
4189 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4190 {
Paul Bakker831a7552011-05-18 13:32:51 +00004191 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4192 return( 0 );
4193
Paul Bakker5121ce52009-01-03 21:22:43 +00004194 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4195 return( ret );
4196 }
4197
4198 if( ssl->in_msglen == 0 &&
4199 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4200 {
4201 /*
4202 * OpenSSL sends empty messages to randomize the IV
4203 */
4204 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4205 {
Paul Bakker831a7552011-05-18 13:32:51 +00004206 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4207 return( 0 );
4208
Paul Bakker5121ce52009-01-03 21:22:43 +00004209 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4210 return( ret );
4211 }
4212 }
4213
Paul Bakker48916f92012-09-16 19:57:18 +00004214 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4215 {
4216 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4217
4218 if( ssl->endpoint == SSL_IS_CLIENT &&
4219 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4220 ssl->in_hslen != 4 ) )
4221 {
4222 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4223 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4224 }
4225
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004226 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4227 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004228 ssl->allow_legacy_renegotiation ==
4229 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004230 {
4231 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4232
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004233#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004234 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004235 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004236 /*
4237 * SSLv3 does not have a "no_renegotiation" alert
4238 */
4239 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4240 return( ret );
4241 }
4242 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004243#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004244#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4245 defined(POLARSSL_SSL_PROTO_TLS1_2)
4246 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004247 {
4248 if( ( ret = ssl_send_alert_message( ssl,
4249 SSL_ALERT_LEVEL_WARNING,
4250 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4251 {
4252 return( ret );
4253 }
Paul Bakker48916f92012-09-16 19:57:18 +00004254 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004255 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004256#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4257 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004258 {
4259 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004260 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004261 }
Paul Bakker48916f92012-09-16 19:57:18 +00004262 }
4263 else
4264 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004265 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004266 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004267 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004268 return( ret );
4269 }
4270
4271 return( POLARSSL_ERR_NET_WANT_READ );
4272 }
4273 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004274 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4275 {
4276 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4277 "but not honored by client" ) );
4278 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4279 }
Paul Bakker48916f92012-09-16 19:57:18 +00004280 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004281 {
4282 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004283 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004284 }
4285
4286 ssl->in_offt = ssl->in_msg;
4287 }
4288
4289 n = ( len < ssl->in_msglen )
4290 ? len : ssl->in_msglen;
4291
4292 memcpy( buf, ssl->in_offt, n );
4293 ssl->in_msglen -= n;
4294
4295 if( ssl->in_msglen == 0 )
4296 /* all bytes consumed */
4297 ssl->in_offt = NULL;
4298 else
4299 /* more data available */
4300 ssl->in_offt += n;
4301
4302 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4303
Paul Bakker23986e52011-04-24 08:57:21 +00004304 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004305}
4306
4307/*
4308 * Send application data to be encrypted by the SSL layer
4309 */
Paul Bakker23986e52011-04-24 08:57:21 +00004310int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004311{
Paul Bakker23986e52011-04-24 08:57:21 +00004312 int ret;
4313 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004314 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004315
4316 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4317
4318 if( ssl->state != SSL_HANDSHAKE_OVER )
4319 {
4320 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4321 {
4322 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4323 return( ret );
4324 }
4325 }
4326
Paul Bakker05decb22013-08-15 13:33:48 +02004327#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004328 /*
4329 * Assume mfl_code is correct since it was checked when set
4330 */
4331 max_len = mfl_code_to_length[ssl->mfl_code];
4332
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004333 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004334 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004335 */
4336 if( ssl->session_out != NULL &&
4337 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4338 {
4339 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4340 }
Paul Bakker05decb22013-08-15 13:33:48 +02004341#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004342
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004343 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004344
Paul Bakker5121ce52009-01-03 21:22:43 +00004345 if( ssl->out_left != 0 )
4346 {
4347 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4348 {
4349 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4350 return( ret );
4351 }
4352 }
Paul Bakker887bd502011-06-08 13:10:54 +00004353 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004354 {
Paul Bakker887bd502011-06-08 13:10:54 +00004355 ssl->out_msglen = n;
4356 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4357 memcpy( ssl->out_msg, buf, n );
4358
4359 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4360 {
4361 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4362 return( ret );
4363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004364 }
4365
4366 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4367
Paul Bakker23986e52011-04-24 08:57:21 +00004368 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004369}
4370
4371/*
4372 * Notify the peer that the connection is being closed
4373 */
4374int ssl_close_notify( ssl_context *ssl )
4375{
4376 int ret;
4377
4378 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4379
4380 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4381 {
4382 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4383 return( ret );
4384 }
4385
4386 if( ssl->state == SSL_HANDSHAKE_OVER )
4387 {
Paul Bakker48916f92012-09-16 19:57:18 +00004388 if( ( ret = ssl_send_alert_message( ssl,
4389 SSL_ALERT_LEVEL_WARNING,
4390 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004391 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004392 return( ret );
4393 }
4394 }
4395
4396 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4397
4398 return( ret );
4399}
4400
Paul Bakker48916f92012-09-16 19:57:18 +00004401void ssl_transform_free( ssl_transform *transform )
4402{
4403#if defined(POLARSSL_ZLIB_SUPPORT)
4404 deflateEnd( &transform->ctx_deflate );
4405 inflateEnd( &transform->ctx_inflate );
4406#endif
4407
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004408 cipher_free_ctx( &transform->cipher_ctx_enc );
4409 cipher_free_ctx( &transform->cipher_ctx_dec );
4410
Paul Bakker61d113b2013-07-04 11:51:43 +02004411 md_free_ctx( &transform->md_ctx_enc );
4412 md_free_ctx( &transform->md_ctx_dec );
4413
Paul Bakker34617722014-06-13 17:20:13 +02004414 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004415}
4416
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004417#if defined(POLARSSL_X509_CRT_PARSE_C)
4418static void ssl_key_cert_free( ssl_key_cert *key_cert )
4419{
4420 ssl_key_cert *cur = key_cert, *next;
4421
4422 while( cur != NULL )
4423 {
4424 next = cur->next;
4425
4426 if( cur->key_own_alloc )
4427 {
4428 pk_free( cur->key );
4429 polarssl_free( cur->key );
4430 }
4431 polarssl_free( cur );
4432
4433 cur = next;
4434 }
4435}
4436#endif /* POLARSSL_X509_CRT_PARSE_C */
4437
Paul Bakker48916f92012-09-16 19:57:18 +00004438void ssl_handshake_free( ssl_handshake_params *handshake )
4439{
4440#if defined(POLARSSL_DHM_C)
4441 dhm_free( &handshake->dhm_ctx );
4442#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004443#if defined(POLARSSL_ECDH_C)
4444 ecdh_free( &handshake->ecdh_ctx );
4445#endif
4446
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004447#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004448 /* explicit void pointer cast for buggy MS compiler */
4449 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004450#endif
4451
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004452#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4453 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4454 /*
4455 * Free only the linked list wrapper, not the keys themselves
4456 * since the belong to the SNI callback
4457 */
4458 if( handshake->sni_key_cert != NULL )
4459 {
4460 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4461
4462 while( cur != NULL )
4463 {
4464 next = cur->next;
4465 polarssl_free( cur );
4466 cur = next;
4467 }
4468 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004469#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004470
Paul Bakker34617722014-06-13 17:20:13 +02004471 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004472}
4473
4474void ssl_session_free( ssl_session *session )
4475{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004476#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004477 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004478 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004479 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004480 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004481 }
Paul Bakkered27a042013-04-18 22:46:23 +02004482#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004483
Paul Bakkera503a632013-08-14 13:48:06 +02004484#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004485 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004486#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004487
Paul Bakker34617722014-06-13 17:20:13 +02004488 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004489}
4490
Paul Bakker5121ce52009-01-03 21:22:43 +00004491/*
4492 * Free an SSL context
4493 */
4494void ssl_free( ssl_context *ssl )
4495{
4496 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4497
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 if( ssl->out_ctr != NULL )
4499 {
Paul Bakker34617722014-06-13 17:20:13 +02004500 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004501 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004502 }
4503
4504 if( ssl->in_ctr != NULL )
4505 {
Paul Bakker34617722014-06-13 17:20:13 +02004506 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004507 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004508 }
4509
Paul Bakker16770332013-10-11 09:59:44 +02004510#if defined(POLARSSL_ZLIB_SUPPORT)
4511 if( ssl->compress_buf != NULL )
4512 {
Paul Bakker34617722014-06-13 17:20:13 +02004513 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004514 polarssl_free( ssl->compress_buf );
4515 }
4516#endif
4517
Paul Bakker40e46942009-01-03 21:51:57 +00004518#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004519 mpi_free( &ssl->dhm_P );
4520 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004521#endif
4522
Paul Bakker48916f92012-09-16 19:57:18 +00004523 if( ssl->transform )
4524 {
4525 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004526 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004527 }
4528
4529 if( ssl->handshake )
4530 {
4531 ssl_handshake_free( ssl->handshake );
4532 ssl_transform_free( ssl->transform_negotiate );
4533 ssl_session_free( ssl->session_negotiate );
4534
Paul Bakker6e339b52013-07-03 13:37:05 +02004535 polarssl_free( ssl->handshake );
4536 polarssl_free( ssl->transform_negotiate );
4537 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004538 }
4539
Paul Bakkerc0463502013-02-14 11:19:38 +01004540 if( ssl->session )
4541 {
4542 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004543 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004544 }
4545
Paul Bakkera503a632013-08-14 13:48:06 +02004546#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004547 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004548#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004549
Paul Bakker0be444a2013-08-27 21:55:01 +02004550#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004551 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004552 {
Paul Bakker34617722014-06-13 17:20:13 +02004553 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004554 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004555 ssl->hostname_len = 0;
4556 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004557#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004558
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004559#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004560 if( ssl->psk != NULL )
4561 {
Paul Bakker34617722014-06-13 17:20:13 +02004562 polarssl_zeroize( ssl->psk, ssl->psk_len );
4563 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004564 polarssl_free( ssl->psk );
4565 polarssl_free( ssl->psk_identity );
4566 ssl->psk_len = 0;
4567 ssl->psk_identity_len = 0;
4568 }
4569#endif
4570
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004571#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004572 ssl_key_cert_free( ssl->key_cert );
4573#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004574
Paul Bakker05ef8352012-05-08 09:17:57 +00004575#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4576 if( ssl_hw_record_finish != NULL )
4577 {
4578 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4579 ssl_hw_record_finish( ssl );
4580 }
4581#endif
4582
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004584
Paul Bakker86f04f42013-02-14 11:20:09 +01004585 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004586 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004587}
4588
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004589#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004590/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004591 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004592 */
4593unsigned char ssl_sig_from_pk( pk_context *pk )
4594{
4595#if defined(POLARSSL_RSA_C)
4596 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4597 return( SSL_SIG_RSA );
4598#endif
4599#if defined(POLARSSL_ECDSA_C)
4600 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4601 return( SSL_SIG_ECDSA );
4602#endif
4603 return( SSL_SIG_ANON );
4604}
4605
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004606pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4607{
4608 switch( sig )
4609 {
4610#if defined(POLARSSL_RSA_C)
4611 case SSL_SIG_RSA:
4612 return( POLARSSL_PK_RSA );
4613#endif
4614#if defined(POLARSSL_ECDSA_C)
4615 case SSL_SIG_ECDSA:
4616 return( POLARSSL_PK_ECDSA );
4617#endif
4618 default:
4619 return( POLARSSL_PK_NONE );
4620 }
4621}
Paul Bakker9af723c2014-05-01 13:03:14 +02004622#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004623
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004624/*
4625 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4626 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004627md_type_t ssl_md_alg_from_hash( unsigned char hash )
4628{
4629 switch( hash )
4630 {
4631#if defined(POLARSSL_MD5_C)
4632 case SSL_HASH_MD5:
4633 return( POLARSSL_MD_MD5 );
4634#endif
4635#if defined(POLARSSL_SHA1_C)
4636 case SSL_HASH_SHA1:
4637 return( POLARSSL_MD_SHA1 );
4638#endif
4639#if defined(POLARSSL_SHA256_C)
4640 case SSL_HASH_SHA224:
4641 return( POLARSSL_MD_SHA224 );
4642 case SSL_HASH_SHA256:
4643 return( POLARSSL_MD_SHA256 );
4644#endif
4645#if defined(POLARSSL_SHA512_C)
4646 case SSL_HASH_SHA384:
4647 return( POLARSSL_MD_SHA384 );
4648 case SSL_HASH_SHA512:
4649 return( POLARSSL_MD_SHA512 );
4650#endif
4651 default:
4652 return( POLARSSL_MD_NONE );
4653 }
4654}
4655
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004656#if defined(POLARSSL_SSL_SET_CURVES)
4657/*
4658 * Check is a curve proposed by the peer is in our list.
4659 * Return 1 if we're willing to use it, 0 otherwise.
4660 */
4661int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4662{
4663 const ecp_group_id *gid;
4664
4665 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4666 if( *gid == grp_id )
4667 return( 1 );
4668
4669 return( 0 );
4670}
Paul Bakker9af723c2014-05-01 13:03:14 +02004671#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004672
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004673#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004674int ssl_check_cert_usage( const x509_crt *cert,
4675 const ssl_ciphersuite_t *ciphersuite,
4676 int cert_endpoint )
4677{
4678#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4679 int usage = 0;
4680#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004681#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4682 const char *ext_oid;
4683 size_t ext_len;
4684#endif
4685
4686#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4687 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4688 ((void) cert);
4689 ((void) cert_endpoint);
4690#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004691
4692#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4693 if( cert_endpoint == SSL_IS_SERVER )
4694 {
4695 /* Server part of the key exchange */
4696 switch( ciphersuite->key_exchange )
4697 {
4698 case POLARSSL_KEY_EXCHANGE_RSA:
4699 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4700 usage = KU_KEY_ENCIPHERMENT;
4701 break;
4702
4703 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4704 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4705 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4706 usage = KU_DIGITAL_SIGNATURE;
4707 break;
4708
4709 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4710 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4711 usage = KU_KEY_AGREEMENT;
4712 break;
4713
4714 /* Don't use default: we want warnings when adding new values */
4715 case POLARSSL_KEY_EXCHANGE_NONE:
4716 case POLARSSL_KEY_EXCHANGE_PSK:
4717 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4718 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4719 usage = 0;
4720 }
4721 }
4722 else
4723 {
4724 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4725 usage = KU_DIGITAL_SIGNATURE;
4726 }
4727
4728 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4729 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004730#else
4731 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004732#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4733
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004734#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4735 if( cert_endpoint == SSL_IS_SERVER )
4736 {
4737 ext_oid = OID_SERVER_AUTH;
4738 ext_len = OID_SIZE( OID_SERVER_AUTH );
4739 }
4740 else
4741 {
4742 ext_oid = OID_CLIENT_AUTH;
4743 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4744 }
4745
4746 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4747 return( -1 );
4748#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4749
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004750 return( 0 );
4751}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004752#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004753
4754#endif /* POLARSSL_SSL_TLS_C */