blob: 1c5249cd2b477af6f5483dd0cf070aeef2568bde [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
Paul Bakkerddf26b42013-09-18 13:46:23 +0200104 if( ( ret = x509_crt_parse( dst->peer_cert, src->peer_cert->raw.p,
Peter Vaskovicc2bbac92014-05-24 13:30:50 +0200105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
159 /*
160 * SSLv3:
161 * block =
162 * MD5( secret + SHA1( 'A' + secret + random ) ) +
163 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
164 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
165 * ...
166 */
167 for( i = 0; i < dlen / 16; i++ )
168 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200169 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000170
171 sha1_starts( &sha1 );
172 sha1_update( &sha1, padding, 1 + i );
173 sha1_update( &sha1, secret, slen );
174 sha1_update( &sha1, random, rlen );
175 sha1_finish( &sha1, sha1sum );
176
177 md5_starts( &md5 );
178 md5_update( &md5, secret, slen );
179 md5_update( &md5, sha1sum, 20 );
180 md5_finish( &md5, dstbuf + i * 16 );
181 }
182
Paul Bakker34617722014-06-13 17:20:13 +0200183 polarssl_zeroize( &md5, sizeof( md5 ) );
184 polarssl_zeroize( &sha1, sizeof( sha1 ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000185
Paul Bakker34617722014-06-13 17:20:13 +0200186 polarssl_zeroize( padding, sizeof( padding ) );
187 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
189 return( 0 );
190}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200191#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000192
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200193#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200194static int tls1_prf( const unsigned char *secret, size_t slen,
195 const char *label,
196 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000197 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
Paul Bakker23986e52011-04-24 08:57:21 +0000199 size_t nb, hs;
200 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200201 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 unsigned char tmp[128];
203 unsigned char h_i[20];
204
205 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000206 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 hs = ( slen + 1 ) / 2;
209 S1 = secret;
210 S2 = secret + slen - hs;
211
212 nb = strlen( label );
213 memcpy( tmp + 20, label, nb );
214 memcpy( tmp + 20 + nb, random, rlen );
215 nb += rlen;
216
217 /*
218 * First compute P_md5(secret,label+random)[0..dlen]
219 */
220 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
221
222 for( i = 0; i < dlen; i += 16 )
223 {
224 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
225 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
226
227 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
228
229 for( j = 0; j < k; j++ )
230 dstbuf[i + j] = h_i[j];
231 }
232
233 /*
234 * XOR out with P_sha1(secret,label+random)[0..dlen]
235 */
236 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
237
238 for( i = 0; i < dlen; i += 20 )
239 {
240 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
241 sha1_hmac( S2, hs, tmp, 20, tmp );
242
243 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
244
245 for( j = 0; j < k; j++ )
246 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
247 }
248
Paul Bakker34617722014-06-13 17:20:13 +0200249 polarssl_zeroize( tmp, sizeof( tmp ) );
250 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252 return( 0 );
253}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200254#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200256#if defined(POLARSSL_SSL_PROTO_TLS1_2)
257#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200258static int tls_prf_sha256( const unsigned char *secret, size_t slen,
259 const char *label,
260 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000261 unsigned char *dstbuf, size_t dlen )
262{
263 size_t nb;
264 size_t i, j, k;
265 unsigned char tmp[128];
266 unsigned char h_i[32];
267
268 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
270
271 nb = strlen( label );
272 memcpy( tmp + 32, label, nb );
273 memcpy( tmp + 32 + nb, random, rlen );
274 nb += rlen;
275
276 /*
277 * Compute P_<hash>(secret, label + random)[0..dlen]
278 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200279 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000280
281 for( i = 0; i < dlen; i += 32 )
282 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200283 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
284 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000285
286 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
287
288 for( j = 0; j < k; j++ )
289 dstbuf[i + j] = h_i[j];
290 }
291
Paul Bakker34617722014-06-13 17:20:13 +0200292 polarssl_zeroize( tmp, sizeof( tmp ) );
293 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000294
295 return( 0 );
296}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200297#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000298
Paul Bakker9e36f042013-06-30 14:34:05 +0200299#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200300static int tls_prf_sha384( const unsigned char *secret, size_t slen,
301 const char *label,
302 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000303 unsigned char *dstbuf, size_t dlen )
304{
305 size_t nb;
306 size_t i, j, k;
307 unsigned char tmp[128];
308 unsigned char h_i[48];
309
310 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
311 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
312
313 nb = strlen( label );
314 memcpy( tmp + 48, label, nb );
315 memcpy( tmp + 48 + nb, random, rlen );
316 nb += rlen;
317
318 /*
319 * Compute P_<hash>(secret, label + random)[0..dlen]
320 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200321 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000322
323 for( i = 0; i < dlen; i += 48 )
324 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200325 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
326 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000327
328 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
329
330 for( j = 0; j < k; j++ )
331 dstbuf[i + j] = h_i[j];
332 }
333
Paul Bakker34617722014-06-13 17:20:13 +0200334 polarssl_zeroize( tmp, sizeof( tmp ) );
335 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336
337 return( 0 );
338}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200339#endif /* POLARSSL_SHA512_C */
340#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000341
Paul Bakker66d5d072014-06-17 16:39:18 +0200342static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343
344#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
345 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200346static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
Paul Bakker380da532012-04-18 16:10:25 +0000348
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200349#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200350static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
351static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif
353
354#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200355static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
356static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200357#endif
358
359#if defined(POLARSSL_SSL_PROTO_TLS1_2)
360#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200361static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
362static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
363static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100365
Paul Bakker9e36f042013-06-30 14:34:05 +0200366#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200367static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
368static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
369static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100370#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200371#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000372
Paul Bakker5121ce52009-01-03 21:22:43 +0000373int ssl_derive_keys( ssl_context *ssl )
374{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200375 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char keyblk[256];
378 unsigned char *key1;
379 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100380 unsigned char *mac_enc;
381 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200382 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 const cipher_info_t *cipher_info;
384 const md_info_t *md_info;
385
Paul Bakker48916f92012-09-16 19:57:18 +0000386 ssl_session *session = ssl->session_negotiate;
387 ssl_transform *transform = ssl->transform_negotiate;
388 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
391
Paul Bakker68884e32013-01-07 18:20:04 +0100392 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
393 if( cipher_info == NULL )
394 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200395 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100396 transform->ciphersuite_info->cipher ) );
397 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
398 }
399
400 md_info = md_info_from_type( transform->ciphersuite_info->mac );
401 if( md_info == NULL )
402 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200403 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100404 transform->ciphersuite_info->mac ) );
405 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
406 }
407
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000409 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000410 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200411#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000412 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413 {
Paul Bakker48916f92012-09-16 19:57:18 +0000414 handshake->tls_prf = ssl3_prf;
415 handshake->calc_verify = ssl_calc_verify_ssl;
416 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200418 else
419#endif
420#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
421 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000422 {
Paul Bakker48916f92012-09-16 19:57:18 +0000423 handshake->tls_prf = tls1_prf;
424 handshake->calc_verify = ssl_calc_verify_tls;
425 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000426 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200427 else
428#endif
429#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200430#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
432 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000433 {
Paul Bakker48916f92012-09-16 19:57:18 +0000434 handshake->tls_prf = tls_prf_sha384;
435 handshake->calc_verify = ssl_calc_verify_tls_sha384;
436 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000438 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif
440#if defined(POLARSSL_SHA256_C)
441 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000442 {
Paul Bakker48916f92012-09-16 19:57:18 +0000443 handshake->tls_prf = tls_prf_sha256;
444 handshake->calc_verify = ssl_calc_verify_tls_sha256;
445 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000446 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447 else
448#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200449#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200450 {
451 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200452 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200453 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000454
455 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 * SSLv3:
457 * master =
458 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
459 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200461 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200462 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 * master = PRF( premaster, "master secret", randbytes )[0..47]
464 */
Paul Bakker0a597072012-09-25 21:55:46 +0000465 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 {
Paul Bakker48916f92012-09-16 19:57:18 +0000467 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
468 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
Paul Bakker48916f92012-09-16 19:57:18 +0000470 handshake->tls_prf( handshake->premaster, handshake->pmslen,
471 "master secret",
472 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Paul Bakker34617722014-06-13 17:20:13 +0200474 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 }
476 else
477 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
478
479 /*
480 * Swap the client and server random values.
481 */
Paul Bakker48916f92012-09-16 19:57:18 +0000482 memcpy( tmp, handshake->randbytes, 64 );
483 memcpy( handshake->randbytes, tmp + 32, 32 );
484 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200485 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487 /*
488 * SSLv3:
489 * key block =
490 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
491 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
492 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
493 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
494 * ...
495 *
496 * TLSv1:
497 * key block = PRF( master, "key expansion", randbytes )
498 */
Paul Bakker48916f92012-09-16 19:57:18 +0000499 handshake->tls_prf( session->master, 48, "key expansion",
500 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
Paul Bakker48916f92012-09-16 19:57:18 +0000502 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
503 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
504 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
505 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
507
Paul Bakker34617722014-06-13 17:20:13 +0200508 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 /*
511 * Determine the appropriate key, IV and MAC length.
512 */
Paul Bakker68884e32013-01-07 18:20:04 +0100513
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200514 if( cipher_info->mode == POLARSSL_MODE_GCM ||
515 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 {
Paul Bakker68884e32013-01-07 18:20:04 +0100517 transform->keylen = cipher_info->key_length;
518 transform->keylen /= 8;
519 transform->minlen = 1;
520 transform->ivlen = 12;
521 transform->fixed_ivlen = 4;
522 transform->maclen = 0;
523 }
524 else
525 {
526 if( md_info->type != POLARSSL_MD_NONE )
527 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200528 int ret;
529
Paul Bakker61d113b2013-07-04 11:51:43 +0200530 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 )
531 {
532 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
533 return( ret );
534 }
535
536 if( ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
537 {
538 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
539 return( ret );
540 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker68884e32013-01-07 18:20:04 +0100542 transform->maclen = md_get_size( md_info );
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200543
Paul Bakker1f2bc622013-08-15 13:45:55 +0200544#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard277f7f22013-07-19 12:19:21 +0200545 /*
546 * If HMAC is to be truncated, we shall keep the leftmost bytes,
547 * (rfc 6066 page 13 or rfc 2104 section 4),
548 * so we only need to adjust the length here.
549 */
550 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
551 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200552#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Paul Bakker68884e32013-01-07 18:20:04 +0100553 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
Paul Bakker68884e32013-01-07 18:20:04 +0100555 transform->keylen = cipher_info->key_length;
556 transform->keylen /= 8;
557 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
Paul Bakker68884e32013-01-07 18:20:04 +0100559 transform->minlen = transform->keylen;
560 if( transform->minlen < transform->maclen )
561 {
562 if( cipher_info->mode == POLARSSL_MODE_STREAM )
563 transform->minlen = transform->maclen;
564 else
565 transform->minlen += transform->keylen;
566 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 }
568
569 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000570 transform->keylen, transform->minlen, transform->ivlen,
571 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
573 /*
574 * Finally setup the cipher contexts, IVs and MAC secrets.
575 */
576 if( ssl->endpoint == SSL_IS_CLIENT )
577 {
Paul Bakker48916f92012-09-16 19:57:18 +0000578 key1 = keyblk + transform->maclen * 2;
579 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Paul Bakker68884e32013-01-07 18:20:04 +0100581 mac_enc = keyblk;
582 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000584 /*
585 * This is not used in TLS v1.1.
586 */
Paul Bakker48916f92012-09-16 19:57:18 +0000587 iv_copy_len = ( transform->fixed_ivlen ) ?
588 transform->fixed_ivlen : transform->ivlen;
589 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
590 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000591 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 }
593 else
594 {
Paul Bakker48916f92012-09-16 19:57:18 +0000595 key1 = keyblk + transform->maclen * 2 + transform->keylen;
596 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
Paul Bakker68884e32013-01-07 18:20:04 +0100598 mac_enc = keyblk + transform->maclen;
599 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000601 /*
602 * This is not used in TLS v1.1.
603 */
Paul Bakker48916f92012-09-16 19:57:18 +0000604 iv_copy_len = ( transform->fixed_ivlen ) ?
605 transform->fixed_ivlen : transform->ivlen;
606 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
607 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000608 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 }
610
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200611#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100612 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
613 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100614 if( transform->maclen > sizeof transform->mac_enc )
615 {
616 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200617 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100618 }
619
Paul Bakker68884e32013-01-07 18:20:04 +0100620 memcpy( transform->mac_enc, mac_enc, transform->maclen );
621 memcpy( transform->mac_dec, mac_dec, transform->maclen );
622 }
623 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200624#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200625#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
626 defined(POLARSSL_SSL_PROTO_TLS1_2)
627 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100628 {
629 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
630 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
631 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200632 else
633#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200634 {
635 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200636 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200637 }
Paul Bakker68884e32013-01-07 18:20:04 +0100638
Paul Bakker05ef8352012-05-08 09:17:57 +0000639#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200640 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000641 {
642 int ret = 0;
643
644 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
645
Paul Bakker07eb38b2012-12-19 14:42:06 +0100646 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
647 transform->iv_enc, transform->iv_dec,
648 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100649 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100650 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000651 {
652 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200653 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000654 }
655 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200656#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000657
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200658 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
659 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200661 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
662 return( ret );
663 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200664
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200665 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
666 cipher_info ) ) != 0 )
667 {
668 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
669 return( ret );
670 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200671
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200672 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
673 cipher_info->key_length,
674 POLARSSL_ENCRYPT ) ) != 0 )
675 {
676 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
677 return( ret );
678 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200679
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200680 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
681 cipher_info->key_length,
682 POLARSSL_DECRYPT ) ) != 0 )
683 {
684 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
685 return( ret );
686 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200687
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200688#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200689 if( cipher_info->mode == POLARSSL_MODE_CBC )
690 {
691 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
692 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200693 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200694 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
695 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200696 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200697
698 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
699 POLARSSL_PADDING_NONE ) ) != 0 )
700 {
701 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
702 return( ret );
703 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200705#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
Paul Bakker34617722014-06-13 17:20:13 +0200707 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
Paul Bakker2770fbd2012-07-03 13:30:23 +0000709#if defined(POLARSSL_ZLIB_SUPPORT)
710 // Initialize compression
711 //
Paul Bakker48916f92012-09-16 19:57:18 +0000712 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000713 {
Paul Bakker16770332013-10-11 09:59:44 +0200714 if( ssl->compress_buf == NULL )
715 {
716 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
717 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
718 if( ssl->compress_buf == NULL )
719 {
720 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
721 SSL_BUFFER_LEN ) );
722 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
723 }
724 }
725
Paul Bakker2770fbd2012-07-03 13:30:23 +0000726 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
727
Paul Bakker48916f92012-09-16 19:57:18 +0000728 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
729 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000730
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200731 if( deflateInit( &transform->ctx_deflate,
732 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000733 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000734 {
735 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
736 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
737 }
738 }
739#endif /* POLARSSL_ZLIB_SUPPORT */
740
Paul Bakker5121ce52009-01-03 21:22:43 +0000741 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
742
743 return( 0 );
744}
745
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200746#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000747void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000748{
749 md5_context md5;
750 sha1_context sha1;
751 unsigned char pad_1[48];
752 unsigned char pad_2[48];
753
Paul Bakker380da532012-04-18 16:10:25 +0000754 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Paul Bakker48916f92012-09-16 19:57:18 +0000756 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
757 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Paul Bakker380da532012-04-18 16:10:25 +0000759 memset( pad_1, 0x36, 48 );
760 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
Paul Bakker48916f92012-09-16 19:57:18 +0000762 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000763 md5_update( &md5, pad_1, 48 );
764 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Paul Bakker380da532012-04-18 16:10:25 +0000766 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000767 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000768 md5_update( &md5, pad_2, 48 );
769 md5_update( &md5, hash, 16 );
770 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
Paul Bakker48916f92012-09-16 19:57:18 +0000772 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000773 sha1_update( &sha1, pad_1, 40 );
774 sha1_finish( &sha1, hash + 16 );
775
776 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000777 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000778 sha1_update( &sha1, pad_2, 40 );
779 sha1_update( &sha1, hash + 16, 20 );
780 sha1_finish( &sha1, hash + 16 );
781
782 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
783 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
784
785 return;
786}
Paul Bakker9af723c2014-05-01 13:03:14 +0200787#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000788
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200789#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000790void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
791{
792 md5_context md5;
793 sha1_context sha1;
794
795 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
796
Paul Bakker48916f92012-09-16 19:57:18 +0000797 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
798 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000799
Paul Bakker48916f92012-09-16 19:57:18 +0000800 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000801 sha1_finish( &sha1, hash + 16 );
802
803 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
804 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
805
806 return;
807}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200808#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000809
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200810#if defined(POLARSSL_SSL_PROTO_TLS1_2)
811#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000812void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
813{
Paul Bakker9e36f042013-06-30 14:34:05 +0200814 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000815
816 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
817
Paul Bakker9e36f042013-06-30 14:34:05 +0200818 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
819 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000820
821 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
822 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
823
824 return;
825}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200826#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000827
Paul Bakker9e36f042013-06-30 14:34:05 +0200828#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000829void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
830{
Paul Bakker9e36f042013-06-30 14:34:05 +0200831 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000832
833 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
834
Paul Bakker9e36f042013-06-30 14:34:05 +0200835 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
836 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000837
Paul Bakkerca4ab492012-04-18 14:23:57 +0000838 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
840
841 return;
842}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200843#endif /* POLARSSL_SHA512_C */
844#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200846#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200847int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
848{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200849 unsigned char *p = ssl->handshake->premaster;
850 unsigned char *end = p + sizeof( ssl->handshake->premaster );
851
852 /*
853 * PMS = struct {
854 * opaque other_secret<0..2^16-1>;
855 * opaque psk<0..2^16-1>;
856 * };
857 * with "other_secret" depending on the particular key exchange
858 */
859#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
860 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
861 {
862 if( end - p < 2 + (int) ssl->psk_len )
863 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
864
865 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
866 *(p++) = (unsigned char)( ssl->psk_len );
867 p += ssl->psk_len;
868 }
869 else
870#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200871#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
872 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
873 {
874 /*
875 * other_secret already set by the ClientKeyExchange message,
876 * and is 48 bytes long
877 */
878 *p++ = 0;
879 *p++ = 48;
880 p += 48;
881 }
882 else
883#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200884#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
885 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
886 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200887 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200888 size_t len = ssl->handshake->dhm_ctx.len;
889
890 if( end - p < 2 + (int) len )
891 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
892
893 *(p++) = (unsigned char)( len >> 8 );
894 *(p++) = (unsigned char)( len );
895 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
896 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
897 {
898 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
899 return( ret );
900 }
901 p += len;
902
903 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
904 }
905 else
906#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
907#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
908 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
909 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200910 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200911 size_t zlen;
912
913 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200914 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200915 ssl->f_rng, ssl->p_rng ) ) != 0 )
916 {
917 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
918 return( ret );
919 }
920
921 *(p++) = (unsigned char)( zlen >> 8 );
922 *(p++) = (unsigned char)( zlen );
923 p += zlen;
924
925 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
926 }
927 else
928#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
929 {
930 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200931 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932 }
933
934 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100935 if( end - p < 2 + (int) ssl->psk_len )
936 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
937
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200938 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
939 *(p++) = (unsigned char)( ssl->psk_len );
940 memcpy( p, ssl->psk, ssl->psk_len );
941 p += ssl->psk_len;
942
943 ssl->handshake->pmslen = p - ssl->handshake->premaster;
944
945 return( 0 );
946}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200947#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200948
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200949#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000950/*
951 * SSLv3.0 MAC functions
952 */
Paul Bakker68884e32013-01-07 18:20:04 +0100953static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
954 unsigned char *buf, size_t len,
955 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000956{
957 unsigned char header[11];
958 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100959 int padlen = 0;
960 int md_size = md_get_size( md_ctx->md_info );
961 int md_type = md_get_type( md_ctx->md_info );
962
963 if( md_type == POLARSSL_MD_MD5 )
964 padlen = 48;
965 else if( md_type == POLARSSL_MD_SHA1 )
966 padlen = 40;
967 else if( md_type == POLARSSL_MD_SHA256 )
968 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100969 else if( md_type == POLARSSL_MD_SHA384 )
970 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
972 memcpy( header, ctr, 8 );
973 header[ 8] = (unsigned char) type;
974 header[ 9] = (unsigned char)( len >> 8 );
975 header[10] = (unsigned char)( len );
976
Paul Bakker68884e32013-01-07 18:20:04 +0100977 memset( padding, 0x36, padlen );
978 md_starts( md_ctx );
979 md_update( md_ctx, secret, md_size );
980 md_update( md_ctx, padding, padlen );
981 md_update( md_ctx, header, 11 );
982 md_update( md_ctx, buf, len );
983 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
Paul Bakker68884e32013-01-07 18:20:04 +0100985 memset( padding, 0x5C, padlen );
986 md_starts( md_ctx );
987 md_update( md_ctx, secret, md_size );
988 md_update( md_ctx, padding, padlen );
989 md_update( md_ctx, buf + len, md_size );
990 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +0000991}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200992#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000993
Paul Bakker5121ce52009-01-03 21:22:43 +0000994/*
995 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200996 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000997static int ssl_encrypt_buf( ssl_context *ssl )
998{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200999 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001000 const cipher_mode_t mode = cipher_get_cipher_mode(
1001 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
1003 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1004
1005 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001006 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001007 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001008#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1009 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1010 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001011 if( mode != POLARSSL_MODE_GCM &&
1012 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001014#if defined(POLARSSL_SSL_PROTO_SSL3)
1015 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1016 {
1017 ssl_mac( &ssl->transform_out->md_ctx_enc,
1018 ssl->transform_out->mac_enc,
1019 ssl->out_msg, ssl->out_msglen,
1020 ssl->out_ctr, ssl->out_msgtype );
1021 }
1022 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001023#endif
1024#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001025 defined(POLARSSL_SSL_PROTO_TLS1_2)
1026 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1027 {
1028 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1029 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1030 ssl->out_msg, ssl->out_msglen );
1031 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1032 ssl->out_msg + ssl->out_msglen );
1033 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1034 }
1035 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001036#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001037 {
1038 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001039 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001040 }
1041
1042 SSL_DEBUG_BUF( 4, "computed mac",
1043 ssl->out_msg + ssl->out_msglen,
1044 ssl->transform_out->maclen );
1045
1046 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001047 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001048#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001050 /*
1051 * Encrypt
1052 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001053#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001054 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001056 int ret;
1057 size_t olen = 0;
1058
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1060 "including %d bytes of padding",
1061 ssl->out_msglen, 0 ) );
1062
1063 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1064 ssl->out_msg, ssl->out_msglen );
1065
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001066 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001067 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001068 ssl->transform_out->ivlen,
1069 ssl->out_msg, ssl->out_msglen,
1070 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001071 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001072 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001073 return( ret );
1074 }
1075
1076 if( ssl->out_msglen != olen )
1077 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001078 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001079 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001080 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 }
Paul Bakker68884e32013-01-07 18:20:04 +01001082 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001083#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001084#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1085 if( mode == POLARSSL_MODE_GCM ||
1086 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001087 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001088 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001089 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001090 unsigned char *enc_msg;
1091 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001092 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1093 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001094
Paul Bakkerca4ab492012-04-18 14:23:57 +00001095 memcpy( add_data, ssl->out_ctr, 8 );
1096 add_data[8] = ssl->out_msgtype;
1097 add_data[9] = ssl->major_ver;
1098 add_data[10] = ssl->minor_ver;
1099 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1100 add_data[12] = ssl->out_msglen & 0xFF;
1101
1102 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1103 add_data, 13 );
1104
Paul Bakker68884e32013-01-07 18:20:04 +01001105 /*
1106 * Generate IV
1107 */
1108 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001109 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1110 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001111 if( ret != 0 )
1112 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001113
Paul Bakker68884e32013-01-07 18:20:04 +01001114 memcpy( ssl->out_iv,
1115 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1116 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001117
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001118 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001119 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001120
Paul Bakker68884e32013-01-07 18:20:04 +01001121 /*
1122 * Fix pointer positions and message length with added IV
1123 */
1124 enc_msg = ssl->out_msg;
1125 enc_msglen = ssl->out_msglen;
1126 ssl->out_msglen += ssl->transform_out->ivlen -
1127 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001128
Paul Bakker68884e32013-01-07 18:20:04 +01001129 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1130 "including %d bytes of padding",
1131 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001132
Paul Bakker68884e32013-01-07 18:20:04 +01001133 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1134 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001135
Paul Bakker68884e32013-01-07 18:20:04 +01001136 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001137 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001138 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001139 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1140 ssl->transform_out->iv_enc,
1141 ssl->transform_out->ivlen,
1142 add_data, 13,
1143 enc_msg, enc_msglen,
1144 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001145 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001146 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001147 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001148 return( ret );
1149 }
1150
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001151 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001152 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001153 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001154 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001155 }
1156
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001157 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001158
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001159 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001160 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001162#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001163#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1164 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001165 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001167 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001168 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001169 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001170
Paul Bakker48916f92012-09-16 19:57:18 +00001171 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1172 ssl->transform_out->ivlen;
1173 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 padlen = 0;
1175
1176 for( i = 0; i <= padlen; i++ )
1177 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1178
1179 ssl->out_msglen += padlen + 1;
1180
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001181 enc_msglen = ssl->out_msglen;
1182 enc_msg = ssl->out_msg;
1183
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001184#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001185 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001186 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1187 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001188 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001189 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001190 {
1191 /*
1192 * Generate IV
1193 */
Paul Bakker48916f92012-09-16 19:57:18 +00001194 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1195 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001196 if( ret != 0 )
1197 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001198
Paul Bakker92be97b2013-01-02 17:30:03 +01001199 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001200 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001201
1202 /*
1203 * Fix pointer positions and message length with added IV
1204 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001205 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001206 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001207 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001208 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001209#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001210
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001212 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001213 ssl->out_msglen, ssl->transform_out->ivlen,
1214 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
1216 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001217 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001219 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001220 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001221 ssl->transform_out->ivlen,
1222 enc_msg, enc_msglen,
1223 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001224 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001225 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001226 return( ret );
1227 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001228
Paul Bakkercca5b812013-08-31 17:40:26 +02001229 if( enc_msglen != olen )
1230 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001231 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001232 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001233 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001234
1235#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001236 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1237 {
1238 /*
1239 * Save IV in SSL3 and TLS1
1240 */
1241 memcpy( ssl->transform_out->iv_enc,
1242 ssl->transform_out->cipher_ctx_enc.iv,
1243 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001245#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001247 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001248#endif /* POLARSSL_CIPHER_MODE_CBC &&
1249 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001250 {
1251 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001252 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001253 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Paul Bakkerca4ab492012-04-18 14:23:57 +00001255 for( i = 8; i > 0; i-- )
1256 if( ++ssl->out_ctr[i - 1] != 0 )
1257 break;
1258
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001259 /* The loops goes to its end iff the counter is wrapping */
1260 if( i == 0 )
1261 {
1262 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1263 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1264 }
1265
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1267
1268 return( 0 );
1269}
1270
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001271#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001272
Paul Bakker5121ce52009-01-03 21:22:43 +00001273static int ssl_decrypt_buf( ssl_context *ssl )
1274{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001275 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001276 const cipher_mode_t mode = cipher_get_cipher_mode(
1277 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001278#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1279 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1280 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1281 size_t padlen = 0, correct = 1;
1282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
1284 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1285
Paul Bakker48916f92012-09-16 19:57:18 +00001286 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 {
1288 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001289 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001290 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 }
1292
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001293#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001294 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001295 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001296 int ret;
1297 size_t olen = 0;
1298
Paul Bakker68884e32013-01-07 18:20:04 +01001299 padlen = 0;
1300
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001301 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001302 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001303 ssl->transform_in->ivlen,
1304 ssl->in_msg, ssl->in_msglen,
1305 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001306 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001307 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001308 return( ret );
1309 }
1310
1311 if( ssl->in_msglen != olen )
1312 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001313 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001314 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 }
Paul Bakker68884e32013-01-07 18:20:04 +01001317 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001318#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001319#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1320 if( mode == POLARSSL_MODE_GCM ||
1321 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001322 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001323 int ret;
1324 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001325 unsigned char *dec_msg;
1326 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001327 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001328 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1329 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001330
Paul Bakker68884e32013-01-07 18:20:04 +01001331 dec_msglen = ssl->in_msglen - ( ssl->transform_in->ivlen -
1332 ssl->transform_in->fixed_ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001333 dec_msglen -= taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001334 dec_msg = ssl->in_msg;
1335 dec_msg_result = ssl->in_msg;
1336 ssl->in_msglen = dec_msglen;
1337
1338 memcpy( add_data, ssl->in_ctr, 8 );
1339 add_data[8] = ssl->in_msgtype;
1340 add_data[9] = ssl->major_ver;
1341 add_data[10] = ssl->minor_ver;
1342 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1343 add_data[12] = ssl->in_msglen & 0xFF;
1344
1345 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1346 add_data, 13 );
1347
1348 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1349 ssl->in_iv,
1350 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1351
1352 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1353 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001354 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001355
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001356 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001357 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001358 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001359 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1360 ssl->transform_in->iv_dec,
1361 ssl->transform_in->ivlen,
1362 add_data, 13,
1363 dec_msg, dec_msglen,
1364 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001365 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001366 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001367 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1368
1369 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1370 return( POLARSSL_ERR_SSL_INVALID_MAC );
1371
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001372 return( ret );
1373 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001374
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001375 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001376 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001377 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001378 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001379 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001382#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001383#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1384 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001385 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 {
Paul Bakker45829992013-01-03 14:52:21 +01001387 /*
1388 * Decrypt and check the padding
1389 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001390 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001391 unsigned char *dec_msg;
1392 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001393 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001394 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001395 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001396
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 /*
Paul Bakker45829992013-01-03 14:52:21 +01001398 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 */
Paul Bakker48916f92012-09-16 19:57:18 +00001400 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 {
1402 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001403 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001404 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 }
1406
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001407#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001408 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1409 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001410#endif
Paul Bakker45829992013-01-03 14:52:21 +01001411
1412 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1413 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1414 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001415 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1416 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1417 ssl->transform_in->ivlen,
1418 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001419 return( POLARSSL_ERR_SSL_INVALID_MAC );
1420 }
1421
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001422 dec_msglen = ssl->in_msglen;
1423 dec_msg = ssl->in_msg;
1424 dec_msg_result = ssl->in_msg;
1425
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001426#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001427 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001428 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001429 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001430 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001431 {
Paul Bakker48916f92012-09-16 19:57:18 +00001432 dec_msglen -= ssl->transform_in->ivlen;
1433 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001434
Paul Bakker48916f92012-09-16 19:57:18 +00001435 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001436 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001437 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001438#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001439
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001440 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001441 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001442 ssl->transform_in->ivlen,
1443 dec_msg, dec_msglen,
1444 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001445 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001446 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001447 return( ret );
1448 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001449
Paul Bakkercca5b812013-08-31 17:40:26 +02001450 if( dec_msglen != olen )
1451 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001453 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001454 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001455
1456#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001457 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1458 {
1459 /*
1460 * Save IV in SSL3 and TLS1
1461 */
1462 memcpy( ssl->transform_in->iv_dec,
1463 ssl->transform_in->cipher_ctx_dec.iv,
1464 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001465 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001466#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
1468 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001469
1470 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1471 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001472#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001473 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1474 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001475#endif
Paul Bakker45829992013-01-03 14:52:21 +01001476 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001477 correct = 0;
1478 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001479
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001480#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1482 {
Paul Bakker48916f92012-09-16 19:57:18 +00001483 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001485#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1487 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001488 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001489#endif
Paul Bakker45829992013-01-03 14:52:21 +01001490 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 }
1492 }
1493 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001494#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001495#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1496 defined(POLARSSL_SSL_PROTO_TLS1_2)
1497 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 {
1499 /*
Paul Bakker45829992013-01-03 14:52:21 +01001500 * TLSv1+: always check the padding up to the first failure
1501 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001503 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001504 size_t padding_idx = ssl->in_msglen - padlen - 1;
1505
Paul Bakker956c9e02013-12-19 14:42:28 +01001506 /*
1507 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001508 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001509 *
Paul Bakker61885c72014-04-25 12:59:03 +02001510 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1511 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001512 *
1513 * In both cases we reset padding_idx to a safe value (0) to
1514 * prevent out-of-buffer reads.
1515 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001516 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001517 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1518 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001519
1520 padding_idx *= correct;
1521
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001522 for( i = 1; i <= 256; i++ )
1523 {
1524 real_count &= ( i <= padlen );
1525 pad_count += real_count *
1526 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1527 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001528
1529 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001530
Paul Bakkerd66f0702013-01-31 16:57:45 +01001531#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001532 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001533 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001534#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001535 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001536 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001537 else
1538#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1539 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001540 {
1541 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001542 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001545 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001546#endif /* POLARSSL_CIPHER_MODE_CBC &&
1547 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001548 {
1549 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001550 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001552
1553 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1554 ssl->in_msg, ssl->in_msglen );
1555
1556 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001557 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001559#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1560 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1561 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001562 if( mode != POLARSSL_MODE_GCM &&
1563 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001564 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001565 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1566
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001567 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001569 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1570 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001572 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001573
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001574#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001575 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1576 {
1577 ssl_mac( &ssl->transform_in->md_ctx_dec,
1578 ssl->transform_in->mac_dec,
1579 ssl->in_msg, ssl->in_msglen,
1580 ssl->in_ctr, ssl->in_msgtype );
1581 }
1582 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001583#endif /* POLARSSL_SSL_PROTO_SSL3 */
1584#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001585 defined(POLARSSL_SSL_PROTO_TLS1_2)
1586 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1587 {
1588 /*
1589 * Process MAC and always update for padlen afterwards to make
1590 * total time independent of padlen
1591 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001592 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001593 *
1594 * Known timing attacks:
1595 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1596 *
1597 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1598 * correctly. (We round down instead of up, so -56 is the correct
1599 * value for our calculations instead of -55)
1600 */
1601 size_t j, extra_run = 0;
1602 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1603 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001604
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001605 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001606
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001607 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1608 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1609 ssl->in_msglen );
1610 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1611 ssl->in_msg + ssl->in_msglen );
1612 for( j = 0; j < extra_run; j++ )
1613 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001614
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001615 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1616 }
1617 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001618#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001619 POLARSSL_SSL_PROTO_TLS1_2 */
1620 {
1621 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001622 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001623 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001624
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001625 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1626 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1627 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001629 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001630 ssl->transform_in->maclen ) != 0 )
1631 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001632#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001633 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001634#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001635 correct = 0;
1636 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001637
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001638 /*
1639 * Finally check the correct flag
1640 */
1641 if( correct == 0 )
1642 return( POLARSSL_ERR_SSL_INVALID_MAC );
1643 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001644#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001645
1646 if( ssl->in_msglen == 0 )
1647 {
1648 ssl->nb_zero++;
1649
1650 /*
1651 * Three or more empty messages may be a DoS attack
1652 * (excessive CPU consumption).
1653 */
1654 if( ssl->nb_zero > 3 )
1655 {
1656 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1657 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001658 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659 }
1660 }
1661 else
1662 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001663
Paul Bakker23986e52011-04-24 08:57:21 +00001664 for( i = 8; i > 0; i-- )
1665 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 break;
1667
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001668 /* The loops goes to its end iff the counter is wrapping */
1669 if( i == 0 )
1670 {
1671 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1672 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1673 }
1674
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1676
1677 return( 0 );
1678}
1679
Paul Bakker2770fbd2012-07-03 13:30:23 +00001680#if defined(POLARSSL_ZLIB_SUPPORT)
1681/*
1682 * Compression/decompression functions
1683 */
1684static int ssl_compress_buf( ssl_context *ssl )
1685{
1686 int ret;
1687 unsigned char *msg_post = ssl->out_msg;
1688 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001689 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001690
1691 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1692
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001693 if( len_pre == 0 )
1694 return( 0 );
1695
Paul Bakker2770fbd2012-07-03 13:30:23 +00001696 memcpy( msg_pre, ssl->out_msg, len_pre );
1697
1698 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1699 ssl->out_msglen ) );
1700
1701 SSL_DEBUG_BUF( 4, "before compression: output payload",
1702 ssl->out_msg, ssl->out_msglen );
1703
Paul Bakker48916f92012-09-16 19:57:18 +00001704 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1705 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1706 ssl->transform_out->ctx_deflate.next_out = msg_post;
1707 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001708
Paul Bakker48916f92012-09-16 19:57:18 +00001709 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001710 if( ret != Z_OK )
1711 {
1712 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1713 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1714 }
1715
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001716 ssl->out_msglen = SSL_BUFFER_LEN -
1717 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001718
Paul Bakker2770fbd2012-07-03 13:30:23 +00001719 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1720 ssl->out_msglen ) );
1721
1722 SSL_DEBUG_BUF( 4, "after compression: output payload",
1723 ssl->out_msg, ssl->out_msglen );
1724
1725 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1726
1727 return( 0 );
1728}
1729
1730static int ssl_decompress_buf( ssl_context *ssl )
1731{
1732 int ret;
1733 unsigned char *msg_post = ssl->in_msg;
1734 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001735 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001736
1737 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1738
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001739 if( len_pre == 0 )
1740 return( 0 );
1741
Paul Bakker2770fbd2012-07-03 13:30:23 +00001742 memcpy( msg_pre, ssl->in_msg, len_pre );
1743
1744 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1745 ssl->in_msglen ) );
1746
1747 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1748 ssl->in_msg, ssl->in_msglen );
1749
Paul Bakker48916f92012-09-16 19:57:18 +00001750 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1751 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1752 ssl->transform_in->ctx_inflate.next_out = msg_post;
1753 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001754
Paul Bakker48916f92012-09-16 19:57:18 +00001755 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001756 if( ret != Z_OK )
1757 {
1758 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1759 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1760 }
1761
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001762 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1763 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764
Paul Bakker2770fbd2012-07-03 13:30:23 +00001765 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1766 ssl->in_msglen ) );
1767
1768 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1769 ssl->in_msg, ssl->in_msglen );
1770
1771 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1772
1773 return( 0 );
1774}
1775#endif /* POLARSSL_ZLIB_SUPPORT */
1776
Paul Bakker5121ce52009-01-03 21:22:43 +00001777/*
1778 * Fill the input message buffer
1779 */
Paul Bakker23986e52011-04-24 08:57:21 +00001780int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001781{
Paul Bakker23986e52011-04-24 08:57:21 +00001782 int ret;
1783 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
1785 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1786
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001787 if( nb_want > SSL_BUFFER_LEN - 8 )
1788 {
1789 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1790 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1791 }
1792
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 while( ssl->in_left < nb_want )
1794 {
1795 len = nb_want - ssl->in_left;
1796 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1797
1798 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1799 ssl->in_left, nb_want ) );
1800 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1801
Paul Bakker831a7552011-05-18 13:32:51 +00001802 if( ret == 0 )
1803 return( POLARSSL_ERR_SSL_CONN_EOF );
1804
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 if( ret < 0 )
1806 return( ret );
1807
1808 ssl->in_left += ret;
1809 }
1810
1811 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1812
1813 return( 0 );
1814}
1815
1816/*
1817 * Flush any data not yet written
1818 */
1819int ssl_flush_output( ssl_context *ssl )
1820{
1821 int ret;
1822 unsigned char *buf;
1823
1824 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1825
1826 while( ssl->out_left > 0 )
1827 {
1828 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1829 5 + ssl->out_msglen, ssl->out_left ) );
1830
Paul Bakker5bd42292012-12-19 14:40:42 +01001831 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001833
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1835
1836 if( ret <= 0 )
1837 return( ret );
1838
1839 ssl->out_left -= ret;
1840 }
1841
1842 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1843
1844 return( 0 );
1845}
1846
1847/*
1848 * Record layer functions
1849 */
1850int ssl_write_record( ssl_context *ssl )
1851{
Paul Bakker05ef8352012-05-08 09:17:57 +00001852 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001853 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
1855 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1856
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1858 {
1859 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1860 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1861 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1862
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001863 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1864 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 }
1866
Paul Bakker2770fbd2012-07-03 13:30:23 +00001867#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001868 if( ssl->transform_out != NULL &&
1869 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001870 {
1871 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1872 {
1873 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1874 return( ret );
1875 }
1876
1877 len = ssl->out_msglen;
1878 }
1879#endif /*POLARSSL_ZLIB_SUPPORT */
1880
Paul Bakker05ef8352012-05-08 09:17:57 +00001881#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001882 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001884 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
Paul Bakker05ef8352012-05-08 09:17:57 +00001886 ret = ssl_hw_record_write( ssl );
1887 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1888 {
1889 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001890 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001891 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001892
1893 if( ret == 0 )
1894 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001895 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001896#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001897 if( !done )
1898 {
1899 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1900 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1901 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1903 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001904
Paul Bakker48916f92012-09-16 19:57:18 +00001905 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001906 {
1907 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1908 {
1909 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1910 return( ret );
1911 }
1912
1913 len = ssl->out_msglen;
1914 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1915 ssl->out_hdr[4] = (unsigned char)( len );
1916 }
1917
1918 ssl->out_left = 5 + ssl->out_msglen;
1919
1920 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1921 "version = [%d:%d], msglen = %d",
1922 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1923 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1924
1925 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001926 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 }
1928
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1930 {
1931 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1932 return( ret );
1933 }
1934
1935 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1936
1937 return( 0 );
1938}
1939
1940int ssl_read_record( ssl_context *ssl )
1941{
Paul Bakker05ef8352012-05-08 09:17:57 +00001942 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
1944 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1945
Paul Bakker68884e32013-01-07 18:20:04 +01001946 SSL_DEBUG_BUF( 4, "input record from network",
1947 ssl->in_hdr, 5 + ssl->in_msglen );
1948
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 if( ssl->in_hslen != 0 &&
1950 ssl->in_hslen < ssl->in_msglen )
1951 {
1952 /*
1953 * Get next Handshake message in the current record
1954 */
1955 ssl->in_msglen -= ssl->in_hslen;
1956
Paul Bakker8934a982011-08-05 11:11:53 +00001957 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001958 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
1960 ssl->in_hslen = 4;
1961 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1962
1963 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1964 " %d, type = %d, hslen = %d",
1965 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1966
1967 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1968 {
1969 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001970 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971 }
1972
1973 if( ssl->in_msglen < ssl->in_hslen )
1974 {
1975 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001976 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 }
1978
Paul Bakker4224bc02014-04-08 14:36:50 +02001979 if( ssl->state != SSL_HANDSHAKE_OVER )
1980 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
1982 return( 0 );
1983 }
1984
1985 ssl->in_hslen = 0;
1986
1987 /*
1988 * Read the record header and validate it
1989 */
1990 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1991 {
1992 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1993 return( ret );
1994 }
1995
1996 ssl->in_msgtype = ssl->in_hdr[0];
1997 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
1998
1999 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2000 "version = [%d:%d], msglen = %d",
2001 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2002 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2003
2004 if( ssl->in_hdr[1] != ssl->major_ver )
2005 {
2006 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002007 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 }
2009
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002010 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 {
2012 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002013 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 }
2015
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002016 /* Sanity check (outer boundaries) */
2017 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2018 {
2019 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2020 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2021 }
2022
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002024 * Make sure the message length is acceptable for the current transform
2025 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 */
Paul Bakker48916f92012-09-16 19:57:18 +00002027 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002029 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 {
2031 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002032 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002033 }
2034 }
2035 else
2036 {
Paul Bakker48916f92012-09-16 19:57:18 +00002037 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 {
2039 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002040 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 }
2042
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002043#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002045 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 {
2047 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002048 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002051
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002052#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2053 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 /*
2055 * TLS encrypted messages can have up to 256 bytes of padding
2056 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002057 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002058 ssl->in_msglen > ssl->transform_in->minlen +
2059 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 {
2061 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002062 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002064#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 }
2066
2067 /*
2068 * Read and optionally decrypt the message contents
2069 */
2070 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2071 {
2072 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2073 return( ret );
2074 }
2075
2076 SSL_DEBUG_BUF( 4, "input record from network",
2077 ssl->in_hdr, 5 + ssl->in_msglen );
2078
Paul Bakker05ef8352012-05-08 09:17:57 +00002079#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002080 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002081 {
2082 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2083
2084 ret = ssl_hw_record_read( ssl );
2085 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2086 {
2087 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002088 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002089 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002090
2091 if( ret == 0 )
2092 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002093 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002094#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002095 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 {
2097 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2098 {
Paul Bakker40865c82013-01-31 17:13:13 +01002099#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2100 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2101 {
2102 ssl_send_alert_message( ssl,
2103 SSL_ALERT_LEVEL_FATAL,
2104 SSL_ALERT_MSG_BAD_RECORD_MAC );
2105 }
2106#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2108 return( ret );
2109 }
2110
2111 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2112 ssl->in_msg, ssl->in_msglen );
2113
2114 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2115 {
2116 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002117 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 }
2119 }
2120
Paul Bakker2770fbd2012-07-03 13:30:23 +00002121#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002122 if( ssl->transform_in != NULL &&
2123 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002124 {
2125 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2126 {
2127 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2128 return( ret );
2129 }
2130
2131 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2132 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2133 }
2134#endif /* POLARSSL_ZLIB_SUPPORT */
2135
Paul Bakker0a925182012-04-16 06:46:41 +00002136 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2137 ssl->in_msgtype != SSL_MSG_ALERT &&
2138 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2139 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2140 {
2141 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2142
Paul Bakker48916f92012-09-16 19:57:18 +00002143 if( ( ret = ssl_send_alert_message( ssl,
2144 SSL_ALERT_LEVEL_FATAL,
2145 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002146 {
2147 return( ret );
2148 }
2149
2150 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2151 }
2152
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2154 {
2155 ssl->in_hslen = 4;
2156 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2157
2158 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2159 " %d, type = %d, hslen = %d",
2160 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2161
2162 /*
2163 * Additional checks to validate the handshake header
2164 */
2165 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2166 {
2167 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002168 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 }
2170
2171 if( ssl->in_msglen < ssl->in_hslen )
2172 {
2173 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002174 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 }
2176
Paul Bakker48916f92012-09-16 19:57:18 +00002177 if( ssl->state != SSL_HANDSHAKE_OVER )
2178 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 }
2180
2181 if( ssl->in_msgtype == SSL_MSG_ALERT )
2182 {
2183 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2184 ssl->in_msg[0], ssl->in_msg[1] ) );
2185
2186 /*
2187 * Ignore non-fatal alerts, except close_notify
2188 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002189 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002191 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2192 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002193 /**
2194 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2195 * error identifier.
2196 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002197 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
2199
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002200 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2201 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 {
2203 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002204 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002205 }
2206 }
2207
2208 ssl->in_left = 0;
2209
2210 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2211
2212 return( 0 );
2213}
2214
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002215int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2216{
2217 int ret;
2218
2219 if( ( ret = ssl_send_alert_message( ssl,
2220 SSL_ALERT_LEVEL_FATAL,
2221 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2222 {
2223 return( ret );
2224 }
2225
2226 return( 0 );
2227}
2228
Paul Bakker0a925182012-04-16 06:46:41 +00002229int ssl_send_alert_message( ssl_context *ssl,
2230 unsigned char level,
2231 unsigned char message )
2232{
2233 int ret;
2234
2235 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2236
2237 ssl->out_msgtype = SSL_MSG_ALERT;
2238 ssl->out_msglen = 2;
2239 ssl->out_msg[0] = level;
2240 ssl->out_msg[1] = message;
2241
2242 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2243 {
2244 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2245 return( ret );
2246 }
2247
2248 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2249
2250 return( 0 );
2251}
2252
Paul Bakker5121ce52009-01-03 21:22:43 +00002253/*
2254 * Handshake functions
2255 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002256#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2257 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2258 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2259 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2260 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2261 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2262 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002263int ssl_write_certificate( ssl_context *ssl )
2264{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002265 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002266
2267 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2268
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002269 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002270 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2271 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002272 {
2273 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2274 ssl->state++;
2275 return( 0 );
2276 }
2277
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002278 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2279 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002280}
2281
2282int ssl_parse_certificate( ssl_context *ssl )
2283{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002284 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2285
2286 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2287
2288 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002289 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2290 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002291 {
2292 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2293 ssl->state++;
2294 return( 0 );
2295 }
2296
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002297 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2298 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002299}
2300#else
2301int ssl_write_certificate( ssl_context *ssl )
2302{
2303 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2304 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002305 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002306 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2307
2308 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2309
2310 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002311 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2312 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002313 {
2314 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2315 ssl->state++;
2316 return( 0 );
2317 }
2318
Paul Bakker5121ce52009-01-03 21:22:43 +00002319 if( ssl->endpoint == SSL_IS_CLIENT )
2320 {
2321 if( ssl->client_auth == 0 )
2322 {
2323 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2324 ssl->state++;
2325 return( 0 );
2326 }
2327
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002328#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 /*
2330 * If using SSLv3 and got no cert, send an Alert message
2331 * (otherwise an empty Certificate message will be sent).
2332 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002333 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2335 {
2336 ssl->out_msglen = 2;
2337 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002338 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2339 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002340
2341 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2342 goto write_msg;
2343 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002344#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 }
2346 else /* SSL_IS_SERVER */
2347 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002348 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002349 {
2350 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002351 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 }
2353 }
2354
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002355 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
2357 /*
2358 * 0 . 0 handshake type
2359 * 1 . 3 handshake length
2360 * 4 . 6 length of all certs
2361 * 7 . 9 length of cert. 1
2362 * 10 . n-1 peer certificate
2363 * n . n+2 length of cert. 2
2364 * n+3 . ... upper level cert, etc.
2365 */
2366 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002367 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
Paul Bakker29087132010-03-21 21:03:34 +00002369 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 {
2371 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002372 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 {
2374 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2375 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002376 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 }
2378
2379 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2380 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2381 ssl->out_msg[i + 2] = (unsigned char)( n );
2382
2383 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2384 i += n; crt = crt->next;
2385 }
2386
2387 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2388 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2389 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2390
2391 ssl->out_msglen = i;
2392 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2393 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2394
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002395#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002396write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002397#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
2399 ssl->state++;
2400
2401 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2402 {
2403 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2404 return( ret );
2405 }
2406
2407 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2408
Paul Bakkered27a042013-04-18 22:46:23 +02002409 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002410}
2411
2412int ssl_parse_certificate( ssl_context *ssl )
2413{
Paul Bakkered27a042013-04-18 22:46:23 +02002414 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002415 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002416 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
2418 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2419
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002421 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2422 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002423 {
2424 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2425 ssl->state++;
2426 return( 0 );
2427 }
2428
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002430 ( ssl->authmode == SSL_VERIFY_NONE ||
2431 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002433 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2435 ssl->state++;
2436 return( 0 );
2437 }
2438
2439 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2440 {
2441 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2442 return( ret );
2443 }
2444
2445 ssl->state++;
2446
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002447#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 /*
2449 * Check if the client sent an empty certificate
2450 */
2451 if( ssl->endpoint == SSL_IS_SERVER &&
2452 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2453 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002454 if( ssl->in_msglen == 2 &&
2455 ssl->in_msgtype == SSL_MSG_ALERT &&
2456 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2457 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 {
2459 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2460
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002461 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2463 return( 0 );
2464 else
Paul Bakker40e46942009-01-03 21:51:57 +00002465 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 }
2467 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002468#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002470#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2471 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 if( ssl->endpoint == SSL_IS_SERVER &&
2473 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2474 {
2475 if( ssl->in_hslen == 7 &&
2476 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2477 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2478 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2479 {
2480 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2481
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002482 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002484 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 else
2486 return( 0 );
2487 }
2488 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002489#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2490 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
2492 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2493 {
2494 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002495 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 }
2497
2498 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2499 {
2500 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002501 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 }
2503
2504 /*
2505 * Same message structure as in ssl_write_certificate()
2506 */
2507 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2508
2509 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2510 {
2511 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002512 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 }
2514
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002515 /* In case we tried to reuse a session but it failed */
2516 if( ssl->session_negotiate->peer_cert != NULL )
2517 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002518 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002519 polarssl_free( ssl->session_negotiate->peer_cert );
2520 }
2521
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002522 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2523 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 {
2525 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002526 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002527 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 }
2529
Paul Bakkerb6b09562013-09-18 14:17:41 +02002530 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
2532 i = 7;
2533
2534 while( i < ssl->in_hslen )
2535 {
2536 if( ssl->in_msg[i] != 0 )
2537 {
2538 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002539 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 }
2541
2542 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2543 | (unsigned int) ssl->in_msg[i + 2];
2544 i += 3;
2545
2546 if( n < 128 || i + n > ssl->in_hslen )
2547 {
2548 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002549 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 }
2551
Paul Bakkerddf26b42013-09-18 13:46:23 +02002552 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2553 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 if( ret != 0 )
2555 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002556 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 return( ret );
2558 }
2559
2560 i += n;
2561 }
2562
Paul Bakker48916f92012-09-16 19:57:18 +00002563 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002564
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002565 /*
2566 * On client, make sure the server cert doesn't change during renego to
2567 * avoid "triple handshake" attack: https://secure-resumption.com/
2568 */
2569 if( ssl->endpoint == SSL_IS_CLIENT &&
2570 ssl->renegotiation == SSL_RENEGOTIATION )
2571 {
2572 if( ssl->session->peer_cert == NULL )
2573 {
2574 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2575 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2576 }
2577
2578 if( ssl->session->peer_cert->raw.len !=
2579 ssl->session_negotiate->peer_cert->raw.len ||
2580 memcmp( ssl->session->peer_cert->raw.p,
2581 ssl->session_negotiate->peer_cert->raw.p,
2582 ssl->session->peer_cert->raw.len ) != 0 )
2583 {
2584 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2585 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2586 }
2587 }
2588
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 if( ssl->authmode != SSL_VERIFY_NONE )
2590 {
2591 if( ssl->ca_chain == NULL )
2592 {
2593 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002594 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 }
2596
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002597 /*
2598 * Main check: verify certificate
2599 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002600 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2601 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2602 &ssl->session_negotiate->verify_result,
2603 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
2605 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002606 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002607 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002608 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002609
2610 /*
2611 * Secondary checks: always done, but change 'ret' only if it was 0
2612 */
2613
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002614#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002615 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002616 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002617
2618 /* If certificate uses an EC key, make sure the curve is OK */
2619 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2620 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2621 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002622 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002623 if( ret == 0 )
2624 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002625 }
2626 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002627#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002628
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002629 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2630 ciphersuite_info,
2631 ! ssl->endpoint ) != 0 )
2632 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002633 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002634 if( ret == 0 )
2635 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2636 }
2637
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2639 ret = 0;
2640 }
2641
2642 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2643
2644 return( ret );
2645}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002646#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2647 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2648 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2649 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2650 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2651 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2652 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
2654int ssl_write_change_cipher_spec( ssl_context *ssl )
2655{
2656 int ret;
2657
2658 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2659
2660 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2661 ssl->out_msglen = 1;
2662 ssl->out_msg[0] = 1;
2663
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 ssl->state++;
2665
2666 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2667 {
2668 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2669 return( ret );
2670 }
2671
2672 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2673
2674 return( 0 );
2675}
2676
2677int ssl_parse_change_cipher_spec( ssl_context *ssl )
2678{
2679 int ret;
2680
2681 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2682
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2684 {
2685 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2686 return( ret );
2687 }
2688
2689 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2690 {
2691 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002692 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 }
2694
2695 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2696 {
2697 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002698 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
2700
2701 ssl->state++;
2702
2703 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2704
2705 return( 0 );
2706}
2707
Paul Bakker41c83d32013-03-20 14:39:14 +01002708void ssl_optimize_checksum( ssl_context *ssl,
2709 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002710{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002711 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002712
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002713#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2714 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002715 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002716 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002717 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002718#endif
2719#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2720#if defined(POLARSSL_SHA512_C)
2721 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2722 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2723 else
2724#endif
2725#if defined(POLARSSL_SHA256_C)
2726 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002727 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002728 else
2729#endif
2730#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002731 {
2732 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002733 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002734 }
Paul Bakker380da532012-04-18 16:10:25 +00002735}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002736
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002737static void ssl_update_checksum_start( ssl_context *ssl,
2738 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002739{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002740#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2741 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002742 md5_update( &ssl->handshake->fin_md5 , buf, len );
2743 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002744#endif
2745#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2746#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002747 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002748#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002749#if defined(POLARSSL_SHA512_C)
2750 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002751#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002752#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002753}
2754
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002755#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2756 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002757static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2758 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002759{
Paul Bakker48916f92012-09-16 19:57:18 +00002760 md5_update( &ssl->handshake->fin_md5 , buf, len );
2761 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002762}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002763#endif
Paul Bakker380da532012-04-18 16:10:25 +00002764
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002765#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2766#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002767static void ssl_update_checksum_sha256( ssl_context *ssl,
2768 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002769{
Paul Bakker9e36f042013-06-30 14:34:05 +02002770 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002771}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002772#endif
Paul Bakker380da532012-04-18 16:10:25 +00002773
Paul Bakker9e36f042013-06-30 14:34:05 +02002774#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002775static void ssl_update_checksum_sha384( ssl_context *ssl,
2776 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002777{
Paul Bakker9e36f042013-06-30 14:34:05 +02002778 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002779}
Paul Bakker769075d2012-11-24 11:26:46 +01002780#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002781#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002782
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002783#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002784static void ssl_calc_finished_ssl(
2785 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002786{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002787 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002788 md5_context md5;
2789 sha1_context sha1;
2790
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 unsigned char padbuf[48];
2792 unsigned char md5sum[16];
2793 unsigned char sha1sum[20];
2794
Paul Bakker48916f92012-09-16 19:57:18 +00002795 ssl_session *session = ssl->session_negotiate;
2796 if( !session )
2797 session = ssl->session;
2798
Paul Bakker1ef83d62012-04-11 12:09:53 +00002799 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2800
Paul Bakker48916f92012-09-16 19:57:18 +00002801 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2802 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
2804 /*
2805 * SSLv3:
2806 * hash =
2807 * MD5( master + pad2 +
2808 * MD5( handshake + sender + master + pad1 ) )
2809 * + SHA1( master + pad2 +
2810 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 */
2812
Paul Bakker90995b52013-06-24 19:20:35 +02002813#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002815 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002816#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002817
Paul Bakker90995b52013-06-24 19:20:35 +02002818#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002820 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002821#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
Paul Bakker3c2122f2013-06-24 19:03:14 +02002823 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2824 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Paul Bakker1ef83d62012-04-11 12:09:53 +00002826 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
Paul Bakker3c2122f2013-06-24 19:03:14 +02002828 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002829 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002830 md5_update( &md5, padbuf, 48 );
2831 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002832
Paul Bakker3c2122f2013-06-24 19:03:14 +02002833 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002834 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002835 sha1_update( &sha1, padbuf, 40 );
2836 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
Paul Bakker1ef83d62012-04-11 12:09:53 +00002838 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Paul Bakker1ef83d62012-04-11 12:09:53 +00002840 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002841 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002842 md5_update( &md5, padbuf, 48 );
2843 md5_update( &md5, md5sum, 16 );
2844 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Paul Bakker1ef83d62012-04-11 12:09:53 +00002846 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002847 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002848 sha1_update( &sha1, padbuf , 40 );
2849 sha1_update( &sha1, sha1sum, 20 );
2850 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002851
Paul Bakker1ef83d62012-04-11 12:09:53 +00002852 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Paul Bakker34617722014-06-13 17:20:13 +02002854 polarssl_zeroize( &md5, sizeof( md5_context ) );
2855 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Paul Bakker34617722014-06-13 17:20:13 +02002857 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2858 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2859 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
2861 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2862}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002863#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002865#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002866static void ssl_calc_finished_tls(
2867 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002868{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002870 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Paul Bakker48916f92012-09-16 19:57:18 +00002875 ssl_session *session = ssl->session_negotiate;
2876 if( !session )
2877 session = ssl->session;
2878
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
Paul Bakker48916f92012-09-16 19:57:18 +00002881 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2882 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Paul Bakker1ef83d62012-04-11 12:09:53 +00002884 /*
2885 * TLSv1:
2886 * hash = PRF( master, finished_label,
2887 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2888 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002889
Paul Bakker90995b52013-06-24 19:20:35 +02002890#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002891 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2892 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002893#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894
Paul Bakker90995b52013-06-24 19:20:35 +02002895#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2897 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002898#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002899
2900 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002901 ? "client finished"
2902 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002903
2904 md5_finish( &md5, padbuf );
2905 sha1_finish( &sha1, padbuf + 16 );
2906
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002907 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002908 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909
2910 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2911
Paul Bakker34617722014-06-13 17:20:13 +02002912 polarssl_zeroize( &md5, sizeof( md5_context ) );
2913 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002914
Paul Bakker34617722014-06-13 17:20:13 +02002915 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002916
2917 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2918}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002919#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002920
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002921#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2922#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002923static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002924 ssl_context *ssl, unsigned char *buf, int from )
2925{
2926 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002927 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002928 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002929 unsigned char padbuf[32];
2930
Paul Bakker48916f92012-09-16 19:57:18 +00002931 ssl_session *session = ssl->session_negotiate;
2932 if( !session )
2933 session = ssl->session;
2934
Paul Bakker380da532012-04-18 16:10:25 +00002935 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936
Paul Bakker9e36f042013-06-30 14:34:05 +02002937 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002938
2939 /*
2940 * TLSv1.2:
2941 * hash = PRF( master, finished_label,
2942 * Hash( handshake ) )[0.11]
2943 */
2944
Paul Bakker9e36f042013-06-30 14:34:05 +02002945#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002946 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002947 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002948#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949
2950 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002951 ? "client finished"
2952 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002953
Paul Bakker9e36f042013-06-30 14:34:05 +02002954 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002955
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002956 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002957 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958
2959 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2960
Paul Bakker34617722014-06-13 17:20:13 +02002961 polarssl_zeroize( &sha256, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962
Paul Bakker34617722014-06-13 17:20:13 +02002963 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964
2965 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2966}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002967#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968
Paul Bakker9e36f042013-06-30 14:34:05 +02002969#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002970static void ssl_calc_finished_tls_sha384(
2971 ssl_context *ssl, unsigned char *buf, int from )
2972{
2973 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002974 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002975 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002976 unsigned char padbuf[48];
2977
Paul Bakker48916f92012-09-16 19:57:18 +00002978 ssl_session *session = ssl->session_negotiate;
2979 if( !session )
2980 session = ssl->session;
2981
Paul Bakker380da532012-04-18 16:10:25 +00002982 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002983
Paul Bakker9e36f042013-06-30 14:34:05 +02002984 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002985
2986 /*
2987 * TLSv1.2:
2988 * hash = PRF( master, finished_label,
2989 * Hash( handshake ) )[0.11]
2990 */
2991
Paul Bakker9e36f042013-06-30 14:34:05 +02002992#if !defined(POLARSSL_SHA512_ALT)
2993 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
2994 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002995#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00002996
2997 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002998 ? "client finished"
2999 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000
Paul Bakker9e36f042013-06-30 14:34:05 +02003001 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003002
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003003 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003004 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003005
3006 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3007
Paul Bakker34617722014-06-13 17:20:13 +02003008 polarssl_zeroize( &sha512, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003009
Paul Bakker34617722014-06-13 17:20:13 +02003010 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003011
3012 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3013}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003014#endif /* POLARSSL_SHA512_C */
3015#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003016
Paul Bakker48916f92012-09-16 19:57:18 +00003017void ssl_handshake_wrapup( ssl_context *ssl )
3018{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003019 int resume = ssl->handshake->resume;
3020
Paul Bakker48916f92012-09-16 19:57:18 +00003021 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3022
3023 /*
3024 * Free our handshake params
3025 */
3026 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003027 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003028 ssl->handshake = NULL;
3029
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003030 if( ssl->renegotiation == SSL_RENEGOTIATION )
3031 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3032
Paul Bakker48916f92012-09-16 19:57:18 +00003033 /*
3034 * Switch in our now active transform context
3035 */
3036 if( ssl->transform )
3037 {
3038 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003039 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003040 }
3041 ssl->transform = ssl->transform_negotiate;
3042 ssl->transform_negotiate = NULL;
3043
Paul Bakker0a597072012-09-25 21:55:46 +00003044 if( ssl->session )
3045 {
3046 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003047 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003048 }
3049 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003050 ssl->session_negotiate = NULL;
3051
Paul Bakker0a597072012-09-25 21:55:46 +00003052 /*
3053 * Add cache entry
3054 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003055 if( ssl->f_set_cache != NULL &&
3056 ssl->session->length != 0 &&
3057 resume == 0 )
3058 {
Paul Bakker0a597072012-09-25 21:55:46 +00003059 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3060 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003061 }
Paul Bakker0a597072012-09-25 21:55:46 +00003062
Paul Bakker48916f92012-09-16 19:57:18 +00003063 ssl->state++;
3064
3065 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3066}
3067
Paul Bakker1ef83d62012-04-11 12:09:53 +00003068int ssl_write_finished( ssl_context *ssl )
3069{
3070 int ret, hash_len;
3071
3072 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3073
Paul Bakker92be97b2013-01-02 17:30:03 +01003074 /*
3075 * Set the out_msg pointer to the correct location based on IV length
3076 */
3077 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3078 {
3079 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3080 ssl->transform_negotiate->fixed_ivlen;
3081 }
3082 else
3083 ssl->out_msg = ssl->out_iv;
3084
Paul Bakker48916f92012-09-16 19:57:18 +00003085 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003086
3087 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003088 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3089
Paul Bakker48916f92012-09-16 19:57:18 +00003090 ssl->verify_data_len = hash_len;
3091 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3092
Paul Bakker5121ce52009-01-03 21:22:43 +00003093 ssl->out_msglen = 4 + hash_len;
3094 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3095 ssl->out_msg[0] = SSL_HS_FINISHED;
3096
3097 /*
3098 * In case of session resuming, invert the client and server
3099 * ChangeCipherSpec messages order.
3100 */
Paul Bakker0a597072012-09-25 21:55:46 +00003101 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 {
3103 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003104 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 else
3106 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3107 }
3108 else
3109 ssl->state++;
3110
Paul Bakker48916f92012-09-16 19:57:18 +00003111 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003112 * Switch to our negotiated transform and session parameters for outbound
3113 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003114 */
3115 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3116 ssl->transform_out = ssl->transform_negotiate;
3117 ssl->session_out = ssl->session_negotiate;
3118 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003119
Paul Bakker07eb38b2012-12-19 14:42:06 +01003120#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003121 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003122 {
3123 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3124 {
3125 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3126 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3127 }
3128 }
3129#endif
3130
Paul Bakker5121ce52009-01-03 21:22:43 +00003131 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3132 {
3133 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3134 return( ret );
3135 }
3136
3137 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3138
3139 return( 0 );
3140}
3141
3142int ssl_parse_finished( ssl_context *ssl )
3143{
Paul Bakker23986e52011-04-24 08:57:21 +00003144 int ret;
3145 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 unsigned char buf[36];
3147
3148 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3149
Paul Bakker48916f92012-09-16 19:57:18 +00003150 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003151
Paul Bakker48916f92012-09-16 19:57:18 +00003152 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003153 * Switch to our negotiated transform and session parameters for inbound
3154 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003155 */
3156 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3157 ssl->transform_in = ssl->transform_negotiate;
3158 ssl->session_in = ssl->session_negotiate;
3159 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160
Paul Bakker92be97b2013-01-02 17:30:03 +01003161 /*
3162 * Set the in_msg pointer to the correct location based on IV length
3163 */
3164 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3165 {
3166 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3167 ssl->transform_negotiate->fixed_ivlen;
3168 }
3169 else
3170 ssl->in_msg = ssl->in_iv;
3171
Paul Bakker07eb38b2012-12-19 14:42:06 +01003172#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003173 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003174 {
3175 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3176 {
3177 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3178 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3179 }
3180 }
3181#endif
3182
Paul Bakker5121ce52009-01-03 21:22:43 +00003183 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3184 {
3185 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3186 return( ret );
3187 }
3188
3189 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3190 {
3191 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003192 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 }
3194
Paul Bakker1ef83d62012-04-11 12:09:53 +00003195 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3197
3198 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3199 ssl->in_hslen != 4 + hash_len )
3200 {
3201 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003202 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 }
3204
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003205 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 {
3207 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003208 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 }
3210
Paul Bakker48916f92012-09-16 19:57:18 +00003211 ssl->verify_data_len = hash_len;
3212 memcpy( ssl->peer_verify_data, buf, hash_len );
3213
Paul Bakker0a597072012-09-25 21:55:46 +00003214 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 {
3216 if( ssl->endpoint == SSL_IS_CLIENT )
3217 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3218
3219 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003220 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003221 }
3222 else
3223 ssl->state++;
3224
3225 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3226
3227 return( 0 );
3228}
3229
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003230static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003231{
3232 if( ssl->transform_negotiate )
3233 ssl_transform_free( ssl->transform_negotiate );
3234 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003235 {
3236 ssl->transform_negotiate =
3237 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003238
3239 if( ssl->transform_negotiate != NULL )
3240 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003241 }
Paul Bakker48916f92012-09-16 19:57:18 +00003242
3243 if( ssl->session_negotiate )
3244 ssl_session_free( ssl->session_negotiate );
3245 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003246 {
3247 ssl->session_negotiate =
3248 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003249
3250 if( ssl->session_negotiate != NULL )
3251 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003252 }
Paul Bakker48916f92012-09-16 19:57:18 +00003253
3254 if( ssl->handshake )
3255 ssl_handshake_free( ssl->handshake );
3256 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003257 {
3258 ssl->handshake = (ssl_handshake_params *)
3259 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003260
3261 if( ssl->handshake != NULL )
3262 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003263 }
Paul Bakker48916f92012-09-16 19:57:18 +00003264
3265 if( ssl->handshake == NULL ||
3266 ssl->transform_negotiate == NULL ||
3267 ssl->session_negotiate == NULL )
3268 {
3269 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3270 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3271 }
3272
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003273#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3274 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003275 md5_starts( &ssl->handshake->fin_md5 );
3276 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003277#endif
3278#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3279#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003280 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003281#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003282#if defined(POLARSSL_SHA512_C)
3283 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003284#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003285#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003286
3287 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003288 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003289
Paul Bakker61d113b2013-07-04 11:51:43 +02003290#if defined(POLARSSL_ECDH_C)
3291 ecdh_init( &ssl->handshake->ecdh_ctx );
3292#endif
3293
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003294#if defined(POLARSSL_X509_CRT_PARSE_C)
3295 ssl->handshake->key_cert = ssl->key_cert;
3296#endif
3297
Paul Bakker48916f92012-09-16 19:57:18 +00003298 return( 0 );
3299}
3300
Paul Bakker5121ce52009-01-03 21:22:43 +00003301/*
3302 * Initialize an SSL context
3303 */
3304int ssl_init( ssl_context *ssl )
3305{
Paul Bakker48916f92012-09-16 19:57:18 +00003306 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 int len = SSL_BUFFER_LEN;
3308
3309 memset( ssl, 0, sizeof( ssl_context ) );
3310
Paul Bakker62f2dee2012-09-28 07:31:51 +00003311 /*
3312 * Sane defaults
3313 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003314 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3315 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3316 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3317 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003318
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003319 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003320
Paul Bakker62f2dee2012-09-28 07:31:51 +00003321#if defined(POLARSSL_DHM_C)
3322 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3323 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3324 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3325 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3326 {
3327 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3328 return( ret );
3329 }
3330#endif
3331
3332 /*
3333 * Prepare base structures
3334 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003335 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003337 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003338 ssl->in_msg = ssl->in_ctr + 13;
3339
3340 if( ssl->in_ctr == NULL )
3341 {
3342 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003343 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 }
3345
Paul Bakker6e339b52013-07-03 13:37:05 +02003346 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003348 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003349 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003350
3351 if( ssl->out_ctr == NULL )
3352 {
3353 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003354 polarssl_free( ssl->in_ctr );
3355 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003356 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 }
3358
3359 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3360 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3361
Paul Bakker606b4ba2013-08-14 16:52:14 +02003362#if defined(POLARSSL_SSL_SESSION_TICKETS)
3363 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3364#endif
3365
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003366#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003367 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003368#endif
3369
Paul Bakker48916f92012-09-16 19:57:18 +00003370 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3371 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003372
3373 return( 0 );
3374}
3375
3376/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003377 * Reset an initialized and used SSL context for re-use while retaining
3378 * all application-set variables, function pointers and data.
3379 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003380int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003381{
Paul Bakker48916f92012-09-16 19:57:18 +00003382 int ret;
3383
Paul Bakker7eb013f2011-10-06 12:37:39 +00003384 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003385 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3386 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3387
3388 ssl->verify_data_len = 0;
3389 memset( ssl->own_verify_data, 0, 36 );
3390 memset( ssl->peer_verify_data, 0, 36 );
3391
Paul Bakker7eb013f2011-10-06 12:37:39 +00003392 ssl->in_offt = NULL;
3393
Paul Bakker92be97b2013-01-02 17:30:03 +01003394 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003395 ssl->in_msgtype = 0;
3396 ssl->in_msglen = 0;
3397 ssl->in_left = 0;
3398
3399 ssl->in_hslen = 0;
3400 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003401 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003402
Paul Bakker92be97b2013-01-02 17:30:03 +01003403 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003404 ssl->out_msgtype = 0;
3405 ssl->out_msglen = 0;
3406 ssl->out_left = 0;
3407
Paul Bakker48916f92012-09-16 19:57:18 +00003408 ssl->transform_in = NULL;
3409 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003410
3411 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3412 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003413
3414#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003415 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003416 {
3417 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003418 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003419 {
3420 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3421 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3422 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003423 }
3424#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003425
Paul Bakker48916f92012-09-16 19:57:18 +00003426 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003427 {
Paul Bakker48916f92012-09-16 19:57:18 +00003428 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003429 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003430 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003431 }
Paul Bakker48916f92012-09-16 19:57:18 +00003432
Paul Bakkerc0463502013-02-14 11:19:38 +01003433 if( ssl->session )
3434 {
3435 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003436 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003437 ssl->session = NULL;
3438 }
3439
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003440#if defined(POLARSSL_SSL_ALPN)
3441 ssl->alpn_chosen = NULL;
3442#endif
3443
Paul Bakker48916f92012-09-16 19:57:18 +00003444 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3445 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003446
3447 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003448}
3449
Paul Bakkera503a632013-08-14 13:48:06 +02003450#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003451/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003452 * Allocate and initialize ticket keys
3453 */
3454static int ssl_ticket_keys_init( ssl_context *ssl )
3455{
3456 int ret;
3457 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003458 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003459
3460 if( ssl->ticket_keys != NULL )
3461 return( 0 );
3462
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003463 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3464 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003465 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3466
3467 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003468 {
3469 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003470 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003471 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003472
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003473 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3474 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3475 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3476 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003477 polarssl_free( tkeys );
3478 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003479 }
3480
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003481 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003482 {
3483 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003484 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003485 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003486
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003487 ssl->ticket_keys = tkeys;
3488
3489 return( 0 );
3490}
Paul Bakkera503a632013-08-14 13:48:06 +02003491#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003492
3493/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003494 * SSL set accessors
3495 */
3496void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3497{
3498 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003499
Paul Bakker606b4ba2013-08-14 16:52:14 +02003500#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003501 if( endpoint == SSL_IS_CLIENT )
3502 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003503#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003504}
3505
3506void ssl_set_authmode( ssl_context *ssl, int authmode )
3507{
3508 ssl->authmode = authmode;
3509}
3510
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003511#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003512void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003513 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003514 void *p_vrfy )
3515{
3516 ssl->f_vrfy = f_vrfy;
3517 ssl->p_vrfy = p_vrfy;
3518}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003519#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003520
Paul Bakker5121ce52009-01-03 21:22:43 +00003521void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003522 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003523 void *p_rng )
3524{
3525 ssl->f_rng = f_rng;
3526 ssl->p_rng = p_rng;
3527}
3528
3529void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003530 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003531 void *p_dbg )
3532{
3533 ssl->f_dbg = f_dbg;
3534 ssl->p_dbg = p_dbg;
3535}
3536
3537void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003538 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003539 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003540{
3541 ssl->f_recv = f_recv;
3542 ssl->f_send = f_send;
3543 ssl->p_recv = p_recv;
3544 ssl->p_send = p_send;
3545}
3546
Paul Bakker0a597072012-09-25 21:55:46 +00003547void ssl_set_session_cache( ssl_context *ssl,
3548 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3549 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003550{
Paul Bakker0a597072012-09-25 21:55:46 +00003551 ssl->f_get_cache = f_get_cache;
3552 ssl->p_get_cache = p_get_cache;
3553 ssl->f_set_cache = f_set_cache;
3554 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003555}
3556
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003557int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003558{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003559 int ret;
3560
3561 if( ssl == NULL ||
3562 session == NULL ||
3563 ssl->session_negotiate == NULL ||
3564 ssl->endpoint != SSL_IS_CLIENT )
3565 {
3566 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3567 }
3568
3569 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3570 return( ret );
3571
Paul Bakker0a597072012-09-25 21:55:46 +00003572 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003573
3574 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003575}
3576
Paul Bakkerb68cad62012-08-23 08:34:18 +00003577void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003578{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003579 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3580 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3581 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3582 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3583}
3584
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003585void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3586 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003587 int major, int minor )
3588{
3589 if( major != SSL_MAJOR_VERSION_3 )
3590 return;
3591
3592 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3593 return;
3594
3595 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003596}
3597
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003598#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003599/* Add a new (empty) key_cert entry an return a pointer to it */
3600static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3601{
3602 ssl_key_cert *key_cert, *last;
3603
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003604 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3605 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003606 return( NULL );
3607
3608 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3609
3610 /* Append the new key_cert to the (possibly empty) current list */
3611 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003612 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003613 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003614 if( ssl->handshake != NULL )
3615 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003616 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003617 else
3618 {
3619 last = ssl->key_cert;
3620 while( last->next != NULL )
3621 last = last->next;
3622 last->next = key_cert;
3623 }
3624
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003625 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003626}
3627
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003628void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003629 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003630{
3631 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003632 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003633 ssl->peer_cn = peer_cn;
3634}
3635
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003636int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003637 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003638{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003639 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3640
3641 if( key_cert == NULL )
3642 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3643
3644 key_cert->cert = own_cert;
3645 key_cert->key = pk_key;
3646
3647 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003648}
3649
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003650#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003651int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003652 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003653{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003654 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003655 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003656
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003657 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003658 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3659
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003660 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3661 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003662 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003663
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003664 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003665
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003666 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003667 if( ret != 0 )
3668 return( ret );
3669
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003670 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003671 return( ret );
3672
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003673 key_cert->cert = own_cert;
3674 key_cert->key_own_alloc = 1;
3675
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003676 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003677}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003678#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003679
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003680int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003681 void *rsa_key,
3682 rsa_decrypt_func rsa_decrypt,
3683 rsa_sign_func rsa_sign,
3684 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003685{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003686 int ret;
3687 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003688
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003689 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003690 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3691
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003692 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3693 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003694 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003695
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003696 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003697
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003698 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3699 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3700 return( ret );
3701
3702 key_cert->cert = own_cert;
3703 key_cert->key_own_alloc = 1;
3704
3705 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003706}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003707#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003708
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003709#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003710int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3711 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003712{
Paul Bakker6db455e2013-09-18 17:29:31 +02003713 if( psk == NULL || psk_identity == NULL )
3714 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3715
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003716 /*
3717 * The length will be check later anyway, but in case it is obviously
3718 * too large, better abort now. The PMS is as follows:
3719 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3720 */
3721 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3722 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3723
Paul Bakker6db455e2013-09-18 17:29:31 +02003724 if( ssl->psk != NULL )
3725 {
3726 polarssl_free( ssl->psk );
3727 polarssl_free( ssl->psk_identity );
3728 }
3729
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003730 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003731 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003732
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003733 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003734 ssl->psk_identity = (unsigned char *)
3735 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003736
3737 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3738 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3739
3740 memcpy( ssl->psk, psk, ssl->psk_len );
3741 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003742
3743 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003744}
3745
3746void ssl_set_psk_cb( ssl_context *ssl,
3747 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3748 size_t),
3749 void *p_psk )
3750{
3751 ssl->f_psk = f_psk;
3752 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003753}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003754#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003755
Paul Bakker48916f92012-09-16 19:57:18 +00003756#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003757int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003758{
3759 int ret;
3760
Paul Bakker48916f92012-09-16 19:57:18 +00003761 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003762 {
3763 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3764 return( ret );
3765 }
3766
Paul Bakker48916f92012-09-16 19:57:18 +00003767 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 {
3769 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3770 return( ret );
3771 }
3772
3773 return( 0 );
3774}
3775
Paul Bakker1b57b062011-01-06 15:48:19 +00003776int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3777{
3778 int ret;
3779
Paul Bakker66d5d072014-06-17 16:39:18 +02003780 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003781 {
3782 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3783 return( ret );
3784 }
3785
Paul Bakker66d5d072014-06-17 16:39:18 +02003786 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003787 {
3788 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3789 return( ret );
3790 }
3791
3792 return( 0 );
3793}
Paul Bakker48916f92012-09-16 19:57:18 +00003794#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003795
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003796#if defined(POLARSSL_SSL_SET_CURVES)
3797/*
3798 * Set the allowed elliptic curves
3799 */
3800void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3801{
3802 ssl->curve_list = curve_list;
3803}
3804#endif
3805
Paul Bakker0be444a2013-08-27 21:55:01 +02003806#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003807int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003808{
3809 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003810 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003811
3812 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003813
3814 if( ssl->hostname_len + 1 == 0 )
3815 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3816
Paul Bakker6e339b52013-07-03 13:37:05 +02003817 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003818
Paul Bakkerb15b8512012-01-13 13:44:06 +00003819 if( ssl->hostname == NULL )
3820 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3821
Paul Bakker3c2122f2013-06-24 19:03:14 +02003822 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003823 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003824
Paul Bakker40ea7de2009-05-03 10:18:48 +00003825 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003826
3827 return( 0 );
3828}
3829
Paul Bakker5701cdc2012-09-27 21:49:42 +00003830void ssl_set_sni( ssl_context *ssl,
3831 int (*f_sni)(void *, ssl_context *,
3832 const unsigned char *, size_t),
3833 void *p_sni )
3834{
3835 ssl->f_sni = f_sni;
3836 ssl->p_sni = p_sni;
3837}
Paul Bakker0be444a2013-08-27 21:55:01 +02003838#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003839
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003840#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003841int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003842{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003843 size_t cur_len, tot_len;
3844 const char **p;
3845
3846 /*
3847 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3848 * truncated". Check lengths now rather than later.
3849 */
3850 tot_len = 0;
3851 for( p = protos; *p != NULL; p++ )
3852 {
3853 cur_len = strlen( *p );
3854 tot_len += cur_len;
3855
3856 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3857 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3858 }
3859
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003860 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003861
3862 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003863}
3864
3865const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3866{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003867 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003868}
3869#endif /* POLARSSL_SSL_ALPN */
3870
Paul Bakker490ecc82011-10-06 13:04:09 +00003871void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3872{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003873 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3874 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3875 {
3876 ssl->max_major_ver = major;
3877 ssl->max_minor_ver = minor;
3878 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003879}
3880
Paul Bakker1d29fb52012-09-28 13:28:45 +00003881void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3882{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003883 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3884 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3885 {
3886 ssl->min_major_ver = major;
3887 ssl->min_minor_ver = minor;
3888 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003889}
3890
Paul Bakker05decb22013-08-15 13:33:48 +02003891#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003892int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3893{
Paul Bakker77e257e2013-12-16 15:29:52 +01003894 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003895 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003896 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003897 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003898 }
3899
3900 ssl->mfl_code = mfl_code;
3901
3902 return( 0 );
3903}
Paul Bakker05decb22013-08-15 13:33:48 +02003904#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003905
Paul Bakker1f2bc622013-08-15 13:45:55 +02003906#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003907int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003908{
3909 if( ssl->endpoint != SSL_IS_CLIENT )
3910 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3911
Paul Bakker8c1ede62013-07-19 14:14:37 +02003912 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003913
3914 return( 0 );
3915}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003916#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003917
Paul Bakker48916f92012-09-16 19:57:18 +00003918void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3919{
3920 ssl->disable_renegotiation = renegotiation;
3921}
3922
3923void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3924{
3925 ssl->allow_legacy_renegotiation = allow_legacy;
3926}
3927
Paul Bakkera503a632013-08-14 13:48:06 +02003928#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003929int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3930{
3931 ssl->session_tickets = use_tickets;
3932
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003933 if( ssl->endpoint == SSL_IS_CLIENT )
3934 return( 0 );
3935
3936 if( ssl->f_rng == NULL )
3937 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3938
3939 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003940}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003941
3942void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3943{
3944 ssl->ticket_lifetime = lifetime;
3945}
Paul Bakkera503a632013-08-14 13:48:06 +02003946#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003947
Paul Bakker5121ce52009-01-03 21:22:43 +00003948/*
3949 * SSL get accessors
3950 */
Paul Bakker23986e52011-04-24 08:57:21 +00003951size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003952{
3953 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3954}
3955
Paul Bakkerff60ee62010-03-16 21:09:09 +00003956int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003957{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003958 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003959}
3960
Paul Bakkere3166ce2011-01-27 17:40:50 +00003961const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003962{
Paul Bakker926c8e42013-03-06 10:23:34 +01003963 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003964 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003965
Paul Bakkere3166ce2011-01-27 17:40:50 +00003966 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003967}
3968
Paul Bakker43ca69c2011-01-15 17:35:19 +00003969const char *ssl_get_version( const ssl_context *ssl )
3970{
3971 switch( ssl->minor_ver )
3972 {
3973 case SSL_MINOR_VERSION_0:
3974 return( "SSLv3.0" );
3975
3976 case SSL_MINOR_VERSION_1:
3977 return( "TLSv1.0" );
3978
3979 case SSL_MINOR_VERSION_2:
3980 return( "TLSv1.1" );
3981
Paul Bakker1ef83d62012-04-11 12:09:53 +00003982 case SSL_MINOR_VERSION_3:
3983 return( "TLSv1.2" );
3984
Paul Bakker43ca69c2011-01-15 17:35:19 +00003985 default:
3986 break;
3987 }
3988 return( "unknown" );
3989}
3990
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003991#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003992const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00003993{
3994 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003995 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003996
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003997 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00003998}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003999#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004000
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004001int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4002{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004003 if( ssl == NULL ||
4004 dst == NULL ||
4005 ssl->session == NULL ||
4006 ssl->endpoint != SSL_IS_CLIENT )
4007 {
4008 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4009 }
4010
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004011 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004012}
4013
Paul Bakker5121ce52009-01-03 21:22:43 +00004014/*
Paul Bakker1961b702013-01-25 14:49:24 +01004015 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004016 */
Paul Bakker1961b702013-01-25 14:49:24 +01004017int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004018{
Paul Bakker40e46942009-01-03 21:51:57 +00004019 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004020
Paul Bakker40e46942009-01-03 21:51:57 +00004021#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004023 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004024#endif
4025
Paul Bakker40e46942009-01-03 21:51:57 +00004026#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004027 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004028 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004029#endif
4030
Paul Bakker1961b702013-01-25 14:49:24 +01004031 return( ret );
4032}
4033
4034/*
4035 * Perform the SSL handshake
4036 */
4037int ssl_handshake( ssl_context *ssl )
4038{
4039 int ret = 0;
4040
4041 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4042
4043 while( ssl->state != SSL_HANDSHAKE_OVER )
4044 {
4045 ret = ssl_handshake_step( ssl );
4046
4047 if( ret != 0 )
4048 break;
4049 }
4050
Paul Bakker5121ce52009-01-03 21:22:43 +00004051 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4052
4053 return( ret );
4054}
4055
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004056#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004057/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004058 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004059 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004060static int ssl_write_hello_request( ssl_context *ssl )
4061{
4062 int ret;
4063
4064 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4065
4066 ssl->out_msglen = 4;
4067 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4068 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4069
4070 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4071 {
4072 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4073 return( ret );
4074 }
4075
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004076 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4077
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004078 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4079
4080 return( 0 );
4081}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004082#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004083
4084/*
4085 * Actually renegotiate current connection, triggered by either:
4086 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004087 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004088 * - receiving any handshake message on server during ssl_read() after the
4089 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004090 * If the handshake doesn't complete due to waiting for I/O, it will continue
4091 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004092 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004093static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004094{
4095 int ret;
4096
4097 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4098
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004099 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4100 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004101
4102 ssl->state = SSL_HELLO_REQUEST;
4103 ssl->renegotiation = SSL_RENEGOTIATION;
4104
Paul Bakker48916f92012-09-16 19:57:18 +00004105 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4106 {
4107 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4108 return( ret );
4109 }
4110
4111 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4112
4113 return( 0 );
4114}
4115
4116/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004117 * Renegotiate current connection on client,
4118 * or request renegotiation on server
4119 */
4120int ssl_renegotiate( ssl_context *ssl )
4121{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004122 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004123
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004124#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004125 /* On server, just send the request */
4126 if( ssl->endpoint == SSL_IS_SERVER )
4127 {
4128 if( ssl->state != SSL_HANDSHAKE_OVER )
4129 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4130
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004131 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004132 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004133#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004134
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004135#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004136 /*
4137 * On client, either start the renegotiation process or,
4138 * if already in progress, continue the handshake
4139 */
4140 if( ssl->renegotiation != SSL_RENEGOTIATION )
4141 {
4142 if( ssl->state != SSL_HANDSHAKE_OVER )
4143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4144
4145 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4146 {
4147 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4148 return( ret );
4149 }
4150 }
4151 else
4152 {
4153 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4154 {
4155 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4156 return( ret );
4157 }
4158 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004159#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004160
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004161 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004162}
4163
4164/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004165 * Receive application data decrypted from the SSL layer
4166 */
Paul Bakker23986e52011-04-24 08:57:21 +00004167int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004168{
Paul Bakker23986e52011-04-24 08:57:21 +00004169 int ret;
4170 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004171
4172 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4173
4174 if( ssl->state != SSL_HANDSHAKE_OVER )
4175 {
4176 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4177 {
4178 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4179 return( ret );
4180 }
4181 }
4182
4183 if( ssl->in_offt == NULL )
4184 {
4185 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4186 {
Paul Bakker831a7552011-05-18 13:32:51 +00004187 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4188 return( 0 );
4189
Paul Bakker5121ce52009-01-03 21:22:43 +00004190 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4191 return( ret );
4192 }
4193
4194 if( ssl->in_msglen == 0 &&
4195 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4196 {
4197 /*
4198 * OpenSSL sends empty messages to randomize the IV
4199 */
4200 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4201 {
Paul Bakker831a7552011-05-18 13:32:51 +00004202 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4203 return( 0 );
4204
Paul Bakker5121ce52009-01-03 21:22:43 +00004205 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4206 return( ret );
4207 }
4208 }
4209
Paul Bakker48916f92012-09-16 19:57:18 +00004210 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4211 {
4212 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4213
4214 if( ssl->endpoint == SSL_IS_CLIENT &&
4215 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4216 ssl->in_hslen != 4 ) )
4217 {
4218 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4219 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4220 }
4221
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004222 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4223 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004224 ssl->allow_legacy_renegotiation ==
4225 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004226 {
4227 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4228
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004229#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004230 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004231 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004232 /*
4233 * SSLv3 does not have a "no_renegotiation" alert
4234 */
4235 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4236 return( ret );
4237 }
4238 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004239#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004240#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4241 defined(POLARSSL_SSL_PROTO_TLS1_2)
4242 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004243 {
4244 if( ( ret = ssl_send_alert_message( ssl,
4245 SSL_ALERT_LEVEL_WARNING,
4246 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4247 {
4248 return( ret );
4249 }
Paul Bakker48916f92012-09-16 19:57:18 +00004250 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004251 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004252#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4253 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004254 {
4255 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004256 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004257 }
Paul Bakker48916f92012-09-16 19:57:18 +00004258 }
4259 else
4260 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004261 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004262 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004263 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004264 return( ret );
4265 }
4266
4267 return( POLARSSL_ERR_NET_WANT_READ );
4268 }
4269 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004270 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4271 {
4272 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4273 "but not honored by client" ) );
4274 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4275 }
Paul Bakker48916f92012-09-16 19:57:18 +00004276 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004277 {
4278 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004279 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004280 }
4281
4282 ssl->in_offt = ssl->in_msg;
4283 }
4284
4285 n = ( len < ssl->in_msglen )
4286 ? len : ssl->in_msglen;
4287
4288 memcpy( buf, ssl->in_offt, n );
4289 ssl->in_msglen -= n;
4290
4291 if( ssl->in_msglen == 0 )
4292 /* all bytes consumed */
4293 ssl->in_offt = NULL;
4294 else
4295 /* more data available */
4296 ssl->in_offt += n;
4297
4298 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4299
Paul Bakker23986e52011-04-24 08:57:21 +00004300 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004301}
4302
4303/*
4304 * Send application data to be encrypted by the SSL layer
4305 */
Paul Bakker23986e52011-04-24 08:57:21 +00004306int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004307{
Paul Bakker23986e52011-04-24 08:57:21 +00004308 int ret;
4309 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004310 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004311
4312 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4313
4314 if( ssl->state != SSL_HANDSHAKE_OVER )
4315 {
4316 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4317 {
4318 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4319 return( ret );
4320 }
4321 }
4322
Paul Bakker05decb22013-08-15 13:33:48 +02004323#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004324 /*
4325 * Assume mfl_code is correct since it was checked when set
4326 */
4327 max_len = mfl_code_to_length[ssl->mfl_code];
4328
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004329 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004330 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004331 */
4332 if( ssl->session_out != NULL &&
4333 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4334 {
4335 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4336 }
Paul Bakker05decb22013-08-15 13:33:48 +02004337#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004338
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004339 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004340
Paul Bakker5121ce52009-01-03 21:22:43 +00004341 if( ssl->out_left != 0 )
4342 {
4343 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4344 {
4345 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4346 return( ret );
4347 }
4348 }
Paul Bakker887bd502011-06-08 13:10:54 +00004349 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004350 {
Paul Bakker887bd502011-06-08 13:10:54 +00004351 ssl->out_msglen = n;
4352 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4353 memcpy( ssl->out_msg, buf, n );
4354
4355 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4356 {
4357 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4358 return( ret );
4359 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004360 }
4361
4362 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4363
Paul Bakker23986e52011-04-24 08:57:21 +00004364 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004365}
4366
4367/*
4368 * Notify the peer that the connection is being closed
4369 */
4370int ssl_close_notify( ssl_context *ssl )
4371{
4372 int ret;
4373
4374 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4375
4376 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4377 {
4378 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4379 return( ret );
4380 }
4381
4382 if( ssl->state == SSL_HANDSHAKE_OVER )
4383 {
Paul Bakker48916f92012-09-16 19:57:18 +00004384 if( ( ret = ssl_send_alert_message( ssl,
4385 SSL_ALERT_LEVEL_WARNING,
4386 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004387 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004388 return( ret );
4389 }
4390 }
4391
4392 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4393
4394 return( ret );
4395}
4396
Paul Bakker48916f92012-09-16 19:57:18 +00004397void ssl_transform_free( ssl_transform *transform )
4398{
4399#if defined(POLARSSL_ZLIB_SUPPORT)
4400 deflateEnd( &transform->ctx_deflate );
4401 inflateEnd( &transform->ctx_inflate );
4402#endif
4403
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004404 cipher_free_ctx( &transform->cipher_ctx_enc );
4405 cipher_free_ctx( &transform->cipher_ctx_dec );
4406
Paul Bakker61d113b2013-07-04 11:51:43 +02004407 md_free_ctx( &transform->md_ctx_enc );
4408 md_free_ctx( &transform->md_ctx_dec );
4409
Paul Bakker34617722014-06-13 17:20:13 +02004410 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004411}
4412
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004413#if defined(POLARSSL_X509_CRT_PARSE_C)
4414static void ssl_key_cert_free( ssl_key_cert *key_cert )
4415{
4416 ssl_key_cert *cur = key_cert, *next;
4417
4418 while( cur != NULL )
4419 {
4420 next = cur->next;
4421
4422 if( cur->key_own_alloc )
4423 {
4424 pk_free( cur->key );
4425 polarssl_free( cur->key );
4426 }
4427 polarssl_free( cur );
4428
4429 cur = next;
4430 }
4431}
4432#endif /* POLARSSL_X509_CRT_PARSE_C */
4433
Paul Bakker48916f92012-09-16 19:57:18 +00004434void ssl_handshake_free( ssl_handshake_params *handshake )
4435{
4436#if defined(POLARSSL_DHM_C)
4437 dhm_free( &handshake->dhm_ctx );
4438#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004439#if defined(POLARSSL_ECDH_C)
4440 ecdh_free( &handshake->ecdh_ctx );
4441#endif
4442
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004443#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004444 /* explicit void pointer cast for buggy MS compiler */
4445 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004446#endif
4447
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004448#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4449 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4450 /*
4451 * Free only the linked list wrapper, not the keys themselves
4452 * since the belong to the SNI callback
4453 */
4454 if( handshake->sni_key_cert != NULL )
4455 {
4456 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4457
4458 while( cur != NULL )
4459 {
4460 next = cur->next;
4461 polarssl_free( cur );
4462 cur = next;
4463 }
4464 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004465#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004466
Paul Bakker34617722014-06-13 17:20:13 +02004467 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004468}
4469
4470void ssl_session_free( ssl_session *session )
4471{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004472#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004473 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004474 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004475 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004476 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004477 }
Paul Bakkered27a042013-04-18 22:46:23 +02004478#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004479
Paul Bakkera503a632013-08-14 13:48:06 +02004480#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004481 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004482#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004483
Paul Bakker34617722014-06-13 17:20:13 +02004484 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004485}
4486
Paul Bakker5121ce52009-01-03 21:22:43 +00004487/*
4488 * Free an SSL context
4489 */
4490void ssl_free( ssl_context *ssl )
4491{
4492 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4493
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 if( ssl->out_ctr != NULL )
4495 {
Paul Bakker34617722014-06-13 17:20:13 +02004496 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004497 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004498 }
4499
4500 if( ssl->in_ctr != NULL )
4501 {
Paul Bakker34617722014-06-13 17:20:13 +02004502 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004503 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004504 }
4505
Paul Bakker16770332013-10-11 09:59:44 +02004506#if defined(POLARSSL_ZLIB_SUPPORT)
4507 if( ssl->compress_buf != NULL )
4508 {
Paul Bakker34617722014-06-13 17:20:13 +02004509 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004510 polarssl_free( ssl->compress_buf );
4511 }
4512#endif
4513
Paul Bakker40e46942009-01-03 21:51:57 +00004514#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004515 mpi_free( &ssl->dhm_P );
4516 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004517#endif
4518
Paul Bakker48916f92012-09-16 19:57:18 +00004519 if( ssl->transform )
4520 {
4521 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004522 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004523 }
4524
4525 if( ssl->handshake )
4526 {
4527 ssl_handshake_free( ssl->handshake );
4528 ssl_transform_free( ssl->transform_negotiate );
4529 ssl_session_free( ssl->session_negotiate );
4530
Paul Bakker6e339b52013-07-03 13:37:05 +02004531 polarssl_free( ssl->handshake );
4532 polarssl_free( ssl->transform_negotiate );
4533 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004534 }
4535
Paul Bakkerc0463502013-02-14 11:19:38 +01004536 if( ssl->session )
4537 {
4538 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004539 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004540 }
4541
Paul Bakkera503a632013-08-14 13:48:06 +02004542#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004543 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004544#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004545
Paul Bakker0be444a2013-08-27 21:55:01 +02004546#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004547 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004548 {
Paul Bakker34617722014-06-13 17:20:13 +02004549 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004550 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004551 ssl->hostname_len = 0;
4552 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004553#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004554
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004555#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004556 if( ssl->psk != NULL )
4557 {
Paul Bakker34617722014-06-13 17:20:13 +02004558 polarssl_zeroize( ssl->psk, ssl->psk_len );
4559 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004560 polarssl_free( ssl->psk );
4561 polarssl_free( ssl->psk_identity );
4562 ssl->psk_len = 0;
4563 ssl->psk_identity_len = 0;
4564 }
4565#endif
4566
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004567#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004568 ssl_key_cert_free( ssl->key_cert );
4569#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004570
Paul Bakker05ef8352012-05-08 09:17:57 +00004571#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4572 if( ssl_hw_record_finish != NULL )
4573 {
4574 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4575 ssl_hw_record_finish( ssl );
4576 }
4577#endif
4578
Paul Bakker5121ce52009-01-03 21:22:43 +00004579 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004580
Paul Bakker86f04f42013-02-14 11:20:09 +01004581 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004582 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004583}
4584
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004585#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004586/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004587 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004588 */
4589unsigned char ssl_sig_from_pk( pk_context *pk )
4590{
4591#if defined(POLARSSL_RSA_C)
4592 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4593 return( SSL_SIG_RSA );
4594#endif
4595#if defined(POLARSSL_ECDSA_C)
4596 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4597 return( SSL_SIG_ECDSA );
4598#endif
4599 return( SSL_SIG_ANON );
4600}
4601
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004602pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4603{
4604 switch( sig )
4605 {
4606#if defined(POLARSSL_RSA_C)
4607 case SSL_SIG_RSA:
4608 return( POLARSSL_PK_RSA );
4609#endif
4610#if defined(POLARSSL_ECDSA_C)
4611 case SSL_SIG_ECDSA:
4612 return( POLARSSL_PK_ECDSA );
4613#endif
4614 default:
4615 return( POLARSSL_PK_NONE );
4616 }
4617}
Paul Bakker9af723c2014-05-01 13:03:14 +02004618#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004619
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004620/*
4621 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4622 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004623md_type_t ssl_md_alg_from_hash( unsigned char hash )
4624{
4625 switch( hash )
4626 {
4627#if defined(POLARSSL_MD5_C)
4628 case SSL_HASH_MD5:
4629 return( POLARSSL_MD_MD5 );
4630#endif
4631#if defined(POLARSSL_SHA1_C)
4632 case SSL_HASH_SHA1:
4633 return( POLARSSL_MD_SHA1 );
4634#endif
4635#if defined(POLARSSL_SHA256_C)
4636 case SSL_HASH_SHA224:
4637 return( POLARSSL_MD_SHA224 );
4638 case SSL_HASH_SHA256:
4639 return( POLARSSL_MD_SHA256 );
4640#endif
4641#if defined(POLARSSL_SHA512_C)
4642 case SSL_HASH_SHA384:
4643 return( POLARSSL_MD_SHA384 );
4644 case SSL_HASH_SHA512:
4645 return( POLARSSL_MD_SHA512 );
4646#endif
4647 default:
4648 return( POLARSSL_MD_NONE );
4649 }
4650}
4651
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004652#if defined(POLARSSL_SSL_SET_CURVES)
4653/*
4654 * Check is a curve proposed by the peer is in our list.
4655 * Return 1 if we're willing to use it, 0 otherwise.
4656 */
4657int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4658{
4659 const ecp_group_id *gid;
4660
4661 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4662 if( *gid == grp_id )
4663 return( 1 );
4664
4665 return( 0 );
4666}
Paul Bakker9af723c2014-05-01 13:03:14 +02004667#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004668
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004669#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004670int ssl_check_cert_usage( const x509_crt *cert,
4671 const ssl_ciphersuite_t *ciphersuite,
4672 int cert_endpoint )
4673{
4674#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4675 int usage = 0;
4676#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004677#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4678 const char *ext_oid;
4679 size_t ext_len;
4680#endif
4681
4682#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4683 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4684 ((void) cert);
4685 ((void) cert_endpoint);
4686#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004687
4688#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4689 if( cert_endpoint == SSL_IS_SERVER )
4690 {
4691 /* Server part of the key exchange */
4692 switch( ciphersuite->key_exchange )
4693 {
4694 case POLARSSL_KEY_EXCHANGE_RSA:
4695 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4696 usage = KU_KEY_ENCIPHERMENT;
4697 break;
4698
4699 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4700 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4701 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4702 usage = KU_DIGITAL_SIGNATURE;
4703 break;
4704
4705 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4706 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4707 usage = KU_KEY_AGREEMENT;
4708 break;
4709
4710 /* Don't use default: we want warnings when adding new values */
4711 case POLARSSL_KEY_EXCHANGE_NONE:
4712 case POLARSSL_KEY_EXCHANGE_PSK:
4713 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4714 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4715 usage = 0;
4716 }
4717 }
4718 else
4719 {
4720 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4721 usage = KU_DIGITAL_SIGNATURE;
4722 }
4723
4724 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4725 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004726#else
4727 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004728#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4729
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004730#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4731 if( cert_endpoint == SSL_IS_SERVER )
4732 {
4733 ext_oid = OID_SERVER_AUTH;
4734 ext_len = OID_SIZE( OID_SERVER_AUTH );
4735 }
4736 else
4737 {
4738 ext_oid = OID_CLIENT_AUTH;
4739 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4740 }
4741
4742 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4743 return( -1 );
4744#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4745
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004746 return( 0 );
4747}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004748#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004749
4750#endif /* POLARSSL_SSL_TLS_C */