blob: 7c8f3064b03f05b9703301c574d6b229dfb20a93 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
159 /*
160 * SSLv3:
161 * block =
162 * MD5( secret + SHA1( 'A' + secret + random ) ) +
163 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
164 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
165 * ...
166 */
167 for( i = 0; i < dlen / 16; i++ )
168 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200169 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000170
171 sha1_starts( &sha1 );
172 sha1_update( &sha1, padding, 1 + i );
173 sha1_update( &sha1, secret, slen );
174 sha1_update( &sha1, random, rlen );
175 sha1_finish( &sha1, sha1sum );
176
177 md5_starts( &md5 );
178 md5_update( &md5, secret, slen );
179 md5_update( &md5, sha1sum, 20 );
180 md5_finish( &md5, dstbuf + i * 16 );
181 }
182
Paul Bakker34617722014-06-13 17:20:13 +0200183 polarssl_zeroize( &md5, sizeof( md5 ) );
184 polarssl_zeroize( &sha1, sizeof( sha1 ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000185
Paul Bakker34617722014-06-13 17:20:13 +0200186 polarssl_zeroize( padding, sizeof( padding ) );
187 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
189 return( 0 );
190}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200191#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000192
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200193#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200194static int tls1_prf( const unsigned char *secret, size_t slen,
195 const char *label,
196 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000197 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
Paul Bakker23986e52011-04-24 08:57:21 +0000199 size_t nb, hs;
200 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200201 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 unsigned char tmp[128];
203 unsigned char h_i[20];
204
205 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000206 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 hs = ( slen + 1 ) / 2;
209 S1 = secret;
210 S2 = secret + slen - hs;
211
212 nb = strlen( label );
213 memcpy( tmp + 20, label, nb );
214 memcpy( tmp + 20 + nb, random, rlen );
215 nb += rlen;
216
217 /*
218 * First compute P_md5(secret,label+random)[0..dlen]
219 */
220 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
221
222 for( i = 0; i < dlen; i += 16 )
223 {
224 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
225 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
226
227 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
228
229 for( j = 0; j < k; j++ )
230 dstbuf[i + j] = h_i[j];
231 }
232
233 /*
234 * XOR out with P_sha1(secret,label+random)[0..dlen]
235 */
236 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
237
238 for( i = 0; i < dlen; i += 20 )
239 {
240 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
241 sha1_hmac( S2, hs, tmp, 20, tmp );
242
243 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
244
245 for( j = 0; j < k; j++ )
246 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
247 }
248
Paul Bakker34617722014-06-13 17:20:13 +0200249 polarssl_zeroize( tmp, sizeof( tmp ) );
250 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252 return( 0 );
253}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200254#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200256#if defined(POLARSSL_SSL_PROTO_TLS1_2)
257#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200258static int tls_prf_sha256( const unsigned char *secret, size_t slen,
259 const char *label,
260 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000261 unsigned char *dstbuf, size_t dlen )
262{
263 size_t nb;
264 size_t i, j, k;
265 unsigned char tmp[128];
266 unsigned char h_i[32];
267
268 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
270
271 nb = strlen( label );
272 memcpy( tmp + 32, label, nb );
273 memcpy( tmp + 32 + nb, random, rlen );
274 nb += rlen;
275
276 /*
277 * Compute P_<hash>(secret, label + random)[0..dlen]
278 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200279 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000280
281 for( i = 0; i < dlen; i += 32 )
282 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200283 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
284 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000285
286 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
287
288 for( j = 0; j < k; j++ )
289 dstbuf[i + j] = h_i[j];
290 }
291
Paul Bakker34617722014-06-13 17:20:13 +0200292 polarssl_zeroize( tmp, sizeof( tmp ) );
293 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000294
295 return( 0 );
296}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200297#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000298
Paul Bakker9e36f042013-06-30 14:34:05 +0200299#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200300static int tls_prf_sha384( const unsigned char *secret, size_t slen,
301 const char *label,
302 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000303 unsigned char *dstbuf, size_t dlen )
304{
305 size_t nb;
306 size_t i, j, k;
307 unsigned char tmp[128];
308 unsigned char h_i[48];
309
310 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
311 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
312
313 nb = strlen( label );
314 memcpy( tmp + 48, label, nb );
315 memcpy( tmp + 48 + nb, random, rlen );
316 nb += rlen;
317
318 /*
319 * Compute P_<hash>(secret, label + random)[0..dlen]
320 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200321 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000322
323 for( i = 0; i < dlen; i += 48 )
324 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200325 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
326 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000327
328 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
329
330 for( j = 0; j < k; j++ )
331 dstbuf[i + j] = h_i[j];
332 }
333
Paul Bakker34617722014-06-13 17:20:13 +0200334 polarssl_zeroize( tmp, sizeof( tmp ) );
335 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336
337 return( 0 );
338}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200339#endif /* POLARSSL_SHA512_C */
340#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000341
Paul Bakker66d5d072014-06-17 16:39:18 +0200342static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343
344#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
345 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200346static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
Paul Bakker380da532012-04-18 16:10:25 +0000348
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200349#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200350static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
351static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif
353
354#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200355static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
356static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200357#endif
358
359#if defined(POLARSSL_SSL_PROTO_TLS1_2)
360#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200361static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
362static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
363static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100365
Paul Bakker9e36f042013-06-30 14:34:05 +0200366#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200367static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
368static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
369static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100370#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200371#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000372
Paul Bakker5121ce52009-01-03 21:22:43 +0000373int ssl_derive_keys( ssl_context *ssl )
374{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200375 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char keyblk[256];
378 unsigned char *key1;
379 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100380 unsigned char *mac_enc;
381 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200382 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 const cipher_info_t *cipher_info;
384 const md_info_t *md_info;
385
Paul Bakker48916f92012-09-16 19:57:18 +0000386 ssl_session *session = ssl->session_negotiate;
387 ssl_transform *transform = ssl->transform_negotiate;
388 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
391
Paul Bakker68884e32013-01-07 18:20:04 +0100392 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
393 if( cipher_info == NULL )
394 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200395 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100396 transform->ciphersuite_info->cipher ) );
397 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
398 }
399
400 md_info = md_info_from_type( transform->ciphersuite_info->mac );
401 if( md_info == NULL )
402 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200403 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100404 transform->ciphersuite_info->mac ) );
405 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
406 }
407
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000409 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000410 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200411#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000412 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413 {
Paul Bakker48916f92012-09-16 19:57:18 +0000414 handshake->tls_prf = ssl3_prf;
415 handshake->calc_verify = ssl_calc_verify_ssl;
416 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200418 else
419#endif
420#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
421 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000422 {
Paul Bakker48916f92012-09-16 19:57:18 +0000423 handshake->tls_prf = tls1_prf;
424 handshake->calc_verify = ssl_calc_verify_tls;
425 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000426 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200427 else
428#endif
429#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200430#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
432 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000433 {
Paul Bakker48916f92012-09-16 19:57:18 +0000434 handshake->tls_prf = tls_prf_sha384;
435 handshake->calc_verify = ssl_calc_verify_tls_sha384;
436 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000438 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif
440#if defined(POLARSSL_SHA256_C)
441 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000442 {
Paul Bakker48916f92012-09-16 19:57:18 +0000443 handshake->tls_prf = tls_prf_sha256;
444 handshake->calc_verify = ssl_calc_verify_tls_sha256;
445 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000446 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447 else
448#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200449#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200450 {
451 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200452 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200453 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000454
455 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 * SSLv3:
457 * master =
458 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
459 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200461 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200462 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 * master = PRF( premaster, "master secret", randbytes )[0..47]
464 */
Paul Bakker0a597072012-09-25 21:55:46 +0000465 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 {
Paul Bakker48916f92012-09-16 19:57:18 +0000467 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
468 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
Paul Bakker48916f92012-09-16 19:57:18 +0000470 handshake->tls_prf( handshake->premaster, handshake->pmslen,
471 "master secret",
472 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Paul Bakker34617722014-06-13 17:20:13 +0200474 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 }
476 else
477 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
478
479 /*
480 * Swap the client and server random values.
481 */
Paul Bakker48916f92012-09-16 19:57:18 +0000482 memcpy( tmp, handshake->randbytes, 64 );
483 memcpy( handshake->randbytes, tmp + 32, 32 );
484 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200485 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487 /*
488 * SSLv3:
489 * key block =
490 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
491 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
492 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
493 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
494 * ...
495 *
496 * TLSv1:
497 * key block = PRF( master, "key expansion", randbytes )
498 */
Paul Bakker48916f92012-09-16 19:57:18 +0000499 handshake->tls_prf( session->master, 48, "key expansion",
500 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
Paul Bakker48916f92012-09-16 19:57:18 +0000502 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
503 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
504 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
505 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
507
Paul Bakker34617722014-06-13 17:20:13 +0200508 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 /*
511 * Determine the appropriate key, IV and MAC length.
512 */
Paul Bakker68884e32013-01-07 18:20:04 +0100513
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200514 transform->keylen = cipher_info->key_length / 8;
515
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200516 if( cipher_info->mode == POLARSSL_MODE_GCM ||
517 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200519 transform->maclen = 0;
520
Paul Bakker68884e32013-01-07 18:20:04 +0100521 transform->ivlen = 12;
522 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200523
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200524 /* Minimum length is expicit IV + tag */
525 transform->minlen = transform->ivlen - transform->fixed_ivlen
526 + ( transform->ciphersuite_info->flags &
527 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100528 }
529 else
530 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200531 int ret;
532
533 /* Initialize HMAC contexts */
534 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
535 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100536 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200537 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
538 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100539 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200541 /* Get MAC length */
542 transform->maclen = md_get_size( md_info );
543
544#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
545 /*
546 * If HMAC is to be truncated, we shall keep the leftmost bytes,
547 * (rfc 6066 page 13 or rfc 2104 section 4),
548 * so we only need to adjust the length here.
549 */
550 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
551 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
552#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
553
554 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100555 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200557 /* Minimum length */
558 if( cipher_info->mode == POLARSSL_MODE_STREAM )
559 transform->minlen = transform->maclen;
560 else
Paul Bakker68884e32013-01-07 18:20:04 +0100561 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200562 /*
563 * GenericBlockCipher:
564 * first multiple of blocklen greater than maclen
565 * + IV except for SSL3 and TLS 1.0
566 */
567 transform->minlen = transform->maclen
568 + cipher_info->block_size
569 - transform->maclen % cipher_info->block_size;
570
571#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
572 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
573 ssl->minor_ver == SSL_MINOR_VERSION_1 )
574 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100575 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200576#endif
577#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
578 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
579 ssl->minor_ver == SSL_MINOR_VERSION_3 )
580 {
581 transform->minlen += transform->ivlen;
582 }
583 else
584#endif
585 {
586 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
587 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
588 }
Paul Bakker68884e32013-01-07 18:20:04 +0100589 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 }
591
592 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000593 transform->keylen, transform->minlen, transform->ivlen,
594 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
596 /*
597 * Finally setup the cipher contexts, IVs and MAC secrets.
598 */
599 if( ssl->endpoint == SSL_IS_CLIENT )
600 {
Paul Bakker48916f92012-09-16 19:57:18 +0000601 key1 = keyblk + transform->maclen * 2;
602 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
Paul Bakker68884e32013-01-07 18:20:04 +0100604 mac_enc = keyblk;
605 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000607 /*
608 * This is not used in TLS v1.1.
609 */
Paul Bakker48916f92012-09-16 19:57:18 +0000610 iv_copy_len = ( transform->fixed_ivlen ) ?
611 transform->fixed_ivlen : transform->ivlen;
612 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
613 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000614 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 }
616 else
617 {
Paul Bakker48916f92012-09-16 19:57:18 +0000618 key1 = keyblk + transform->maclen * 2 + transform->keylen;
619 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Paul Bakker68884e32013-01-07 18:20:04 +0100621 mac_enc = keyblk + transform->maclen;
622 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000624 /*
625 * This is not used in TLS v1.1.
626 */
Paul Bakker48916f92012-09-16 19:57:18 +0000627 iv_copy_len = ( transform->fixed_ivlen ) ?
628 transform->fixed_ivlen : transform->ivlen;
629 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
630 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000631 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 }
633
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200634#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100635 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
636 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100637 if( transform->maclen > sizeof transform->mac_enc )
638 {
639 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200640 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100641 }
642
Paul Bakker68884e32013-01-07 18:20:04 +0100643 memcpy( transform->mac_enc, mac_enc, transform->maclen );
644 memcpy( transform->mac_dec, mac_dec, transform->maclen );
645 }
646 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200647#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200648#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
649 defined(POLARSSL_SSL_PROTO_TLS1_2)
650 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100651 {
652 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
653 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
654 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200655 else
656#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200657 {
658 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200659 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200660 }
Paul Bakker68884e32013-01-07 18:20:04 +0100661
Paul Bakker05ef8352012-05-08 09:17:57 +0000662#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200663 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000664 {
665 int ret = 0;
666
667 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
668
Paul Bakker07eb38b2012-12-19 14:42:06 +0100669 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
670 transform->iv_enc, transform->iv_dec,
671 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100672 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100673 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000674 {
675 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200676 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000677 }
678 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200679#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000680
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200681 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
682 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200684 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
685 return( ret );
686 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200687
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200688 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
689 cipher_info ) ) != 0 )
690 {
691 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
692 return( ret );
693 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200694
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200695 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
696 cipher_info->key_length,
697 POLARSSL_ENCRYPT ) ) != 0 )
698 {
699 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
700 return( ret );
701 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200702
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200703 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
704 cipher_info->key_length,
705 POLARSSL_DECRYPT ) ) != 0 )
706 {
707 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
708 return( ret );
709 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200710
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200711#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200712 if( cipher_info->mode == POLARSSL_MODE_CBC )
713 {
714 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
715 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200716 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200717 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
718 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200719 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200720
721 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
722 POLARSSL_PADDING_NONE ) ) != 0 )
723 {
724 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
725 return( ret );
726 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200728#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Paul Bakker34617722014-06-13 17:20:13 +0200730 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Paul Bakker2770fbd2012-07-03 13:30:23 +0000732#if defined(POLARSSL_ZLIB_SUPPORT)
733 // Initialize compression
734 //
Paul Bakker48916f92012-09-16 19:57:18 +0000735 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000736 {
Paul Bakker16770332013-10-11 09:59:44 +0200737 if( ssl->compress_buf == NULL )
738 {
739 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
740 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
741 if( ssl->compress_buf == NULL )
742 {
743 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
744 SSL_BUFFER_LEN ) );
745 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
746 }
747 }
748
Paul Bakker2770fbd2012-07-03 13:30:23 +0000749 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
750
Paul Bakker48916f92012-09-16 19:57:18 +0000751 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
752 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000753
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200754 if( deflateInit( &transform->ctx_deflate,
755 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000756 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000757 {
758 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
759 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
760 }
761 }
762#endif /* POLARSSL_ZLIB_SUPPORT */
763
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
765
766 return( 0 );
767}
768
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200769#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000770void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000771{
772 md5_context md5;
773 sha1_context sha1;
774 unsigned char pad_1[48];
775 unsigned char pad_2[48];
776
Paul Bakker380da532012-04-18 16:10:25 +0000777 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Paul Bakker48916f92012-09-16 19:57:18 +0000779 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
780 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
Paul Bakker380da532012-04-18 16:10:25 +0000782 memset( pad_1, 0x36, 48 );
783 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Paul Bakker48916f92012-09-16 19:57:18 +0000785 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000786 md5_update( &md5, pad_1, 48 );
787 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker380da532012-04-18 16:10:25 +0000789 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000790 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000791 md5_update( &md5, pad_2, 48 );
792 md5_update( &md5, hash, 16 );
793 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Paul Bakker48916f92012-09-16 19:57:18 +0000795 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000796 sha1_update( &sha1, pad_1, 40 );
797 sha1_finish( &sha1, hash + 16 );
798
799 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000800 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000801 sha1_update( &sha1, pad_2, 40 );
802 sha1_update( &sha1, hash + 16, 20 );
803 sha1_finish( &sha1, hash + 16 );
804
805 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
806 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
807
808 return;
809}
Paul Bakker9af723c2014-05-01 13:03:14 +0200810#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000811
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200812#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000813void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
814{
815 md5_context md5;
816 sha1_context sha1;
817
818 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
819
Paul Bakker48916f92012-09-16 19:57:18 +0000820 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
821 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000822
Paul Bakker48916f92012-09-16 19:57:18 +0000823 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000824 sha1_finish( &sha1, hash + 16 );
825
826 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
827 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
828
829 return;
830}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200831#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000832
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200833#if defined(POLARSSL_SSL_PROTO_TLS1_2)
834#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000835void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
836{
Paul Bakker9e36f042013-06-30 14:34:05 +0200837 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000838
839 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
840
Paul Bakker9e36f042013-06-30 14:34:05 +0200841 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
842 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000843
844 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
845 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
846
847 return;
848}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200849#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000850
Paul Bakker9e36f042013-06-30 14:34:05 +0200851#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000852void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
853{
Paul Bakker9e36f042013-06-30 14:34:05 +0200854 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000855
856 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
857
Paul Bakker9e36f042013-06-30 14:34:05 +0200858 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
859 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakkerca4ab492012-04-18 14:23:57 +0000861 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
863
864 return;
865}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200866#endif /* POLARSSL_SHA512_C */
867#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000868
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200869#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200870int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
871{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200872 unsigned char *p = ssl->handshake->premaster;
873 unsigned char *end = p + sizeof( ssl->handshake->premaster );
874
875 /*
876 * PMS = struct {
877 * opaque other_secret<0..2^16-1>;
878 * opaque psk<0..2^16-1>;
879 * };
880 * with "other_secret" depending on the particular key exchange
881 */
882#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
883 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
884 {
885 if( end - p < 2 + (int) ssl->psk_len )
886 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
887
888 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
889 *(p++) = (unsigned char)( ssl->psk_len );
890 p += ssl->psk_len;
891 }
892 else
893#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200894#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
895 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
896 {
897 /*
898 * other_secret already set by the ClientKeyExchange message,
899 * and is 48 bytes long
900 */
901 *p++ = 0;
902 *p++ = 48;
903 p += 48;
904 }
905 else
906#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200907#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
908 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
909 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200910 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200911 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200912
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200913 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200914 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200915 p + 2, &len,
916 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200917 {
918 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
919 return( ret );
920 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200921 *(p++) = (unsigned char)( len >> 8 );
922 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200923 p += len;
924
925 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
926 }
927 else
928#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
929#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
930 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
931 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200932 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200933 size_t zlen;
934
935 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200936 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937 ssl->f_rng, ssl->p_rng ) ) != 0 )
938 {
939 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
940 return( ret );
941 }
942
943 *(p++) = (unsigned char)( zlen >> 8 );
944 *(p++) = (unsigned char)( zlen );
945 p += zlen;
946
947 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
948 }
949 else
950#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
951 {
952 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200953 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200954 }
955
956 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100957 if( end - p < 2 + (int) ssl->psk_len )
958 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
959
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200960 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
961 *(p++) = (unsigned char)( ssl->psk_len );
962 memcpy( p, ssl->psk, ssl->psk_len );
963 p += ssl->psk_len;
964
965 ssl->handshake->pmslen = p - ssl->handshake->premaster;
966
967 return( 0 );
968}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200969#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200970
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200971#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000972/*
973 * SSLv3.0 MAC functions
974 */
Paul Bakker68884e32013-01-07 18:20:04 +0100975static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
976 unsigned char *buf, size_t len,
977 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000978{
979 unsigned char header[11];
980 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100981 int padlen = 0;
982 int md_size = md_get_size( md_ctx->md_info );
983 int md_type = md_get_type( md_ctx->md_info );
984
985 if( md_type == POLARSSL_MD_MD5 )
986 padlen = 48;
987 else if( md_type == POLARSSL_MD_SHA1 )
988 padlen = 40;
989 else if( md_type == POLARSSL_MD_SHA256 )
990 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100991 else if( md_type == POLARSSL_MD_SHA384 )
992 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
994 memcpy( header, ctr, 8 );
995 header[ 8] = (unsigned char) type;
996 header[ 9] = (unsigned char)( len >> 8 );
997 header[10] = (unsigned char)( len );
998
Paul Bakker68884e32013-01-07 18:20:04 +0100999 memset( padding, 0x36, padlen );
1000 md_starts( md_ctx );
1001 md_update( md_ctx, secret, md_size );
1002 md_update( md_ctx, padding, padlen );
1003 md_update( md_ctx, header, 11 );
1004 md_update( md_ctx, buf, len );
1005 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
Paul Bakker68884e32013-01-07 18:20:04 +01001007 memset( padding, 0x5C, padlen );
1008 md_starts( md_ctx );
1009 md_update( md_ctx, secret, md_size );
1010 md_update( md_ctx, padding, padlen );
1011 md_update( md_ctx, buf + len, md_size );
1012 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001013}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001014#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001015
Paul Bakker5121ce52009-01-03 21:22:43 +00001016/*
1017 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001018 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001019static int ssl_encrypt_buf( ssl_context *ssl )
1020{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001021 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001022 const cipher_mode_t mode = cipher_get_cipher_mode(
1023 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
1025 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1026
1027 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001028 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001030#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1031 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1032 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001033 if( mode != POLARSSL_MODE_GCM &&
1034 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001036#if defined(POLARSSL_SSL_PROTO_SSL3)
1037 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1038 {
1039 ssl_mac( &ssl->transform_out->md_ctx_enc,
1040 ssl->transform_out->mac_enc,
1041 ssl->out_msg, ssl->out_msglen,
1042 ssl->out_ctr, ssl->out_msgtype );
1043 }
1044 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001045#endif
1046#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001047 defined(POLARSSL_SSL_PROTO_TLS1_2)
1048 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1049 {
1050 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1051 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1052 ssl->out_msg, ssl->out_msglen );
1053 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1054 ssl->out_msg + ssl->out_msglen );
1055 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1056 }
1057 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001058#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001059 {
1060 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001061 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001062 }
1063
1064 SSL_DEBUG_BUF( 4, "computed mac",
1065 ssl->out_msg + ssl->out_msglen,
1066 ssl->transform_out->maclen );
1067
1068 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001069 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001070#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001072 /*
1073 * Encrypt
1074 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001075#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001076 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001078 int ret;
1079 size_t olen = 0;
1080
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1082 "including %d bytes of padding",
1083 ssl->out_msglen, 0 ) );
1084
1085 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1086 ssl->out_msg, ssl->out_msglen );
1087
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001088 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001089 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001090 ssl->transform_out->ivlen,
1091 ssl->out_msg, ssl->out_msglen,
1092 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001093 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001094 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001095 return( ret );
1096 }
1097
1098 if( ssl->out_msglen != olen )
1099 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001100 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001101 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001102 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001103 }
Paul Bakker68884e32013-01-07 18:20:04 +01001104 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001105#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001106#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1107 if( mode == POLARSSL_MODE_GCM ||
1108 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001109 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001110 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001111 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001112 unsigned char *enc_msg;
1113 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001114 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1115 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001116
Paul Bakkerca4ab492012-04-18 14:23:57 +00001117 memcpy( add_data, ssl->out_ctr, 8 );
1118 add_data[8] = ssl->out_msgtype;
1119 add_data[9] = ssl->major_ver;
1120 add_data[10] = ssl->minor_ver;
1121 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1122 add_data[12] = ssl->out_msglen & 0xFF;
1123
1124 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1125 add_data, 13 );
1126
Paul Bakker68884e32013-01-07 18:20:04 +01001127 /*
1128 * Generate IV
1129 */
1130 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001131 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1132 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001133 if( ret != 0 )
1134 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001135
Paul Bakker68884e32013-01-07 18:20:04 +01001136 memcpy( ssl->out_iv,
1137 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1138 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001139
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001140 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001141 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001142
Paul Bakker68884e32013-01-07 18:20:04 +01001143 /*
1144 * Fix pointer positions and message length with added IV
1145 */
1146 enc_msg = ssl->out_msg;
1147 enc_msglen = ssl->out_msglen;
1148 ssl->out_msglen += ssl->transform_out->ivlen -
1149 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001150
Paul Bakker68884e32013-01-07 18:20:04 +01001151 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1152 "including %d bytes of padding",
1153 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001154
Paul Bakker68884e32013-01-07 18:20:04 +01001155 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1156 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001157
Paul Bakker68884e32013-01-07 18:20:04 +01001158 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001159 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001160 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001161 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1162 ssl->transform_out->iv_enc,
1163 ssl->transform_out->ivlen,
1164 add_data, 13,
1165 enc_msg, enc_msglen,
1166 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001167 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001168 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001169 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001170 return( ret );
1171 }
1172
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001173 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001174 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001175 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001176 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001177 }
1178
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001179 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001180
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001181 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001184#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001185#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1186 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001187 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001189 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001190 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001191 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001192
Paul Bakker48916f92012-09-16 19:57:18 +00001193 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1194 ssl->transform_out->ivlen;
1195 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 padlen = 0;
1197
1198 for( i = 0; i <= padlen; i++ )
1199 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1200
1201 ssl->out_msglen += padlen + 1;
1202
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001203 enc_msglen = ssl->out_msglen;
1204 enc_msg = ssl->out_msg;
1205
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001206#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001207 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001208 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1209 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001210 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001211 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001212 {
1213 /*
1214 * Generate IV
1215 */
Paul Bakker48916f92012-09-16 19:57:18 +00001216 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1217 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001218 if( ret != 0 )
1219 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220
Paul Bakker92be97b2013-01-02 17:30:03 +01001221 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001222 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001223
1224 /*
1225 * Fix pointer positions and message length with added IV
1226 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001227 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001228 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001229 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001231#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001232
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001234 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001235 ssl->out_msglen, ssl->transform_out->ivlen,
1236 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
1238 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001239 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001241 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001242 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001243 ssl->transform_out->ivlen,
1244 enc_msg, enc_msglen,
1245 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001246 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001247 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001248 return( ret );
1249 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001250
Paul Bakkercca5b812013-08-31 17:40:26 +02001251 if( enc_msglen != olen )
1252 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001253 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001254 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001255 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001256
1257#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001258 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1259 {
1260 /*
1261 * Save IV in SSL3 and TLS1
1262 */
1263 memcpy( ssl->transform_out->iv_enc,
1264 ssl->transform_out->cipher_ctx_enc.iv,
1265 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001267#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001269 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001270#endif /* POLARSSL_CIPHER_MODE_CBC &&
1271 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001272 {
1273 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001274 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001276
Paul Bakkerca4ab492012-04-18 14:23:57 +00001277 for( i = 8; i > 0; i-- )
1278 if( ++ssl->out_ctr[i - 1] != 0 )
1279 break;
1280
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001281 /* The loops goes to its end iff the counter is wrapping */
1282 if( i == 0 )
1283 {
1284 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1285 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1286 }
1287
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1289
1290 return( 0 );
1291}
1292
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001293#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001294
Paul Bakker5121ce52009-01-03 21:22:43 +00001295static int ssl_decrypt_buf( ssl_context *ssl )
1296{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001297 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001298 const cipher_mode_t mode = cipher_get_cipher_mode(
1299 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001300#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1301 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1302 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1303 size_t padlen = 0, correct = 1;
1304#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001305
1306 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1307
Paul Bakker48916f92012-09-16 19:57:18 +00001308 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 {
1310 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001311 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001312 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 }
1314
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001315#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001316 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001317 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001318 int ret;
1319 size_t olen = 0;
1320
Paul Bakker68884e32013-01-07 18:20:04 +01001321 padlen = 0;
1322
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001323 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001324 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001325 ssl->transform_in->ivlen,
1326 ssl->in_msg, ssl->in_msglen,
1327 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001328 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001329 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001330 return( ret );
1331 }
1332
1333 if( ssl->in_msglen != olen )
1334 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001335 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001336 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 }
Paul Bakker68884e32013-01-07 18:20:04 +01001339 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001340#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001341#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1342 if( mode == POLARSSL_MODE_GCM ||
1343 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001344 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001345 int ret;
1346 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001347 unsigned char *dec_msg;
1348 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001349 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001350 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1351 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001352 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1353 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001354
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001355 if( ssl->in_msglen < explicit_iv_len + taglen )
1356 {
1357 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1358 "+ taglen (%d)", ssl->in_msglen,
1359 explicit_iv_len, taglen ) );
1360 return( POLARSSL_ERR_SSL_INVALID_MAC );
1361 }
1362 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1363
Paul Bakker68884e32013-01-07 18:20:04 +01001364 dec_msg = ssl->in_msg;
1365 dec_msg_result = ssl->in_msg;
1366 ssl->in_msglen = dec_msglen;
1367
1368 memcpy( add_data, ssl->in_ctr, 8 );
1369 add_data[8] = ssl->in_msgtype;
1370 add_data[9] = ssl->major_ver;
1371 add_data[10] = ssl->minor_ver;
1372 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1373 add_data[12] = ssl->in_msglen & 0xFF;
1374
1375 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1376 add_data, 13 );
1377
1378 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1379 ssl->in_iv,
1380 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1381
1382 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1383 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001384 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001385
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001386 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001387 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001388 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001389 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1390 ssl->transform_in->iv_dec,
1391 ssl->transform_in->ivlen,
1392 add_data, 13,
1393 dec_msg, dec_msglen,
1394 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001395 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001396 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001397 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1398
1399 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1400 return( POLARSSL_ERR_SSL_INVALID_MAC );
1401
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001402 return( ret );
1403 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001404
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001405 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001406 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001407 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001408 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001409 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001412#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001413#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1414 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001415 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 {
Paul Bakker45829992013-01-03 14:52:21 +01001417 /*
1418 * Decrypt and check the padding
1419 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001420 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001421 unsigned char *dec_msg;
1422 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001423 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001424 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001425 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001426
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 /*
Paul Bakker45829992013-01-03 14:52:21 +01001428 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 */
Paul Bakker48916f92012-09-16 19:57:18 +00001430 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 {
1432 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001433 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001434 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 }
1436
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001437#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001438 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1439 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001440#endif
Paul Bakker45829992013-01-03 14:52:21 +01001441
1442 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1443 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1444 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001445 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1446 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1447 ssl->transform_in->ivlen,
1448 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001449 return( POLARSSL_ERR_SSL_INVALID_MAC );
1450 }
1451
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001452 dec_msglen = ssl->in_msglen;
1453 dec_msg = ssl->in_msg;
1454 dec_msg_result = ssl->in_msg;
1455
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001456#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001457 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001458 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001459 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001460 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001461 {
Paul Bakker48916f92012-09-16 19:57:18 +00001462 dec_msglen -= ssl->transform_in->ivlen;
1463 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001464
Paul Bakker48916f92012-09-16 19:57:18 +00001465 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001466 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001467 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001468#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001469
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001470 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001471 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001472 ssl->transform_in->ivlen,
1473 dec_msg, dec_msglen,
1474 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001475 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001476 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001477 return( ret );
1478 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001479
Paul Bakkercca5b812013-08-31 17:40:26 +02001480 if( dec_msglen != olen )
1481 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001482 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001483 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001484 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001485
1486#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001487 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1488 {
1489 /*
1490 * Save IV in SSL3 and TLS1
1491 */
1492 memcpy( ssl->transform_in->iv_dec,
1493 ssl->transform_in->cipher_ctx_dec.iv,
1494 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001495 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001496#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001497
1498 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001499
1500 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1501 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001502#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001503 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1504 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001505#endif
Paul Bakker45829992013-01-03 14:52:21 +01001506 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001507 correct = 0;
1508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001510#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1512 {
Paul Bakker48916f92012-09-16 19:57:18 +00001513 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001515#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1517 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001518 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001519#endif
Paul Bakker45829992013-01-03 14:52:21 +01001520 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 }
1522 }
1523 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001524#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001525#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1526 defined(POLARSSL_SSL_PROTO_TLS1_2)
1527 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 {
1529 /*
Paul Bakker45829992013-01-03 14:52:21 +01001530 * TLSv1+: always check the padding up to the first failure
1531 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001533 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001534 size_t padding_idx = ssl->in_msglen - padlen - 1;
1535
Paul Bakker956c9e02013-12-19 14:42:28 +01001536 /*
1537 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001538 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001539 *
Paul Bakker61885c72014-04-25 12:59:03 +02001540 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1541 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001542 *
1543 * In both cases we reset padding_idx to a safe value (0) to
1544 * prevent out-of-buffer reads.
1545 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001546 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001547 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1548 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001549
1550 padding_idx *= correct;
1551
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001552 for( i = 1; i <= 256; i++ )
1553 {
1554 real_count &= ( i <= padlen );
1555 pad_count += real_count *
1556 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1557 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001558
1559 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001560
Paul Bakkerd66f0702013-01-31 16:57:45 +01001561#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001562 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001563 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001564#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001565 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001567 else
1568#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1569 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001570 {
1571 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001572 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001573 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001574 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001575 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001576#endif /* POLARSSL_CIPHER_MODE_CBC &&
1577 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001578 {
1579 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001580 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001582
1583 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1584 ssl->in_msg, ssl->in_msglen );
1585
1586 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001587 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001589#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1590 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1591 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001592 if( mode != POLARSSL_MODE_GCM &&
1593 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001594 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001595 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1596
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001597 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001598
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001599 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1600 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001602 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001604#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001605 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1606 {
1607 ssl_mac( &ssl->transform_in->md_ctx_dec,
1608 ssl->transform_in->mac_dec,
1609 ssl->in_msg, ssl->in_msglen,
1610 ssl->in_ctr, ssl->in_msgtype );
1611 }
1612 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001613#endif /* POLARSSL_SSL_PROTO_SSL3 */
1614#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001615 defined(POLARSSL_SSL_PROTO_TLS1_2)
1616 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1617 {
1618 /*
1619 * Process MAC and always update for padlen afterwards to make
1620 * total time independent of padlen
1621 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001622 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001623 *
1624 * Known timing attacks:
1625 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1626 *
1627 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1628 * correctly. (We round down instead of up, so -56 is the correct
1629 * value for our calculations instead of -55)
1630 */
1631 size_t j, extra_run = 0;
1632 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1633 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001634
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001635 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001636
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001637 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1638 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1639 ssl->in_msglen );
1640 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1641 ssl->in_msg + ssl->in_msglen );
1642 for( j = 0; j < extra_run; j++ )
1643 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001644
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001645 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1646 }
1647 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001648#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001649 POLARSSL_SSL_PROTO_TLS1_2 */
1650 {
1651 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001652 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001654
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001655 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1656 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1657 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001659 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001660 ssl->transform_in->maclen ) != 0 )
1661 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001662#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001664#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001665 correct = 0;
1666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 /*
1669 * Finally check the correct flag
1670 */
1671 if( correct == 0 )
1672 return( POLARSSL_ERR_SSL_INVALID_MAC );
1673 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001674#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
1676 if( ssl->in_msglen == 0 )
1677 {
1678 ssl->nb_zero++;
1679
1680 /*
1681 * Three or more empty messages may be a DoS attack
1682 * (excessive CPU consumption).
1683 */
1684 if( ssl->nb_zero > 3 )
1685 {
1686 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1687 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001688 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001689 }
1690 }
1691 else
1692 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001693
Paul Bakker23986e52011-04-24 08:57:21 +00001694 for( i = 8; i > 0; i-- )
1695 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001696 break;
1697
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001698 /* The loops goes to its end iff the counter is wrapping */
1699 if( i == 0 )
1700 {
1701 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1702 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1703 }
1704
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1706
1707 return( 0 );
1708}
1709
Paul Bakker2770fbd2012-07-03 13:30:23 +00001710#if defined(POLARSSL_ZLIB_SUPPORT)
1711/*
1712 * Compression/decompression functions
1713 */
1714static int ssl_compress_buf( ssl_context *ssl )
1715{
1716 int ret;
1717 unsigned char *msg_post = ssl->out_msg;
1718 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001719 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001720
1721 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1722
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001723 if( len_pre == 0 )
1724 return( 0 );
1725
Paul Bakker2770fbd2012-07-03 13:30:23 +00001726 memcpy( msg_pre, ssl->out_msg, len_pre );
1727
1728 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1729 ssl->out_msglen ) );
1730
1731 SSL_DEBUG_BUF( 4, "before compression: output payload",
1732 ssl->out_msg, ssl->out_msglen );
1733
Paul Bakker48916f92012-09-16 19:57:18 +00001734 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1735 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1736 ssl->transform_out->ctx_deflate.next_out = msg_post;
1737 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001738
Paul Bakker48916f92012-09-16 19:57:18 +00001739 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001740 if( ret != Z_OK )
1741 {
1742 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1743 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1744 }
1745
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001746 ssl->out_msglen = SSL_BUFFER_LEN -
1747 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001748
Paul Bakker2770fbd2012-07-03 13:30:23 +00001749 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1750 ssl->out_msglen ) );
1751
1752 SSL_DEBUG_BUF( 4, "after compression: output payload",
1753 ssl->out_msg, ssl->out_msglen );
1754
1755 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1756
1757 return( 0 );
1758}
1759
1760static int ssl_decompress_buf( ssl_context *ssl )
1761{
1762 int ret;
1763 unsigned char *msg_post = ssl->in_msg;
1764 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001765 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001766
1767 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1768
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001769 if( len_pre == 0 )
1770 return( 0 );
1771
Paul Bakker2770fbd2012-07-03 13:30:23 +00001772 memcpy( msg_pre, ssl->in_msg, len_pre );
1773
1774 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1775 ssl->in_msglen ) );
1776
1777 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1778 ssl->in_msg, ssl->in_msglen );
1779
Paul Bakker48916f92012-09-16 19:57:18 +00001780 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1781 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1782 ssl->transform_in->ctx_inflate.next_out = msg_post;
1783 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001784
Paul Bakker48916f92012-09-16 19:57:18 +00001785 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001786 if( ret != Z_OK )
1787 {
1788 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1789 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1790 }
1791
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001792 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1793 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001794
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1796 ssl->in_msglen ) );
1797
1798 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1799 ssl->in_msg, ssl->in_msglen );
1800
1801 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1802
1803 return( 0 );
1804}
1805#endif /* POLARSSL_ZLIB_SUPPORT */
1806
Paul Bakker5121ce52009-01-03 21:22:43 +00001807/*
1808 * Fill the input message buffer
1809 */
Paul Bakker23986e52011-04-24 08:57:21 +00001810int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001811{
Paul Bakker23986e52011-04-24 08:57:21 +00001812 int ret;
1813 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001814
1815 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1816
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001817 if( nb_want > SSL_BUFFER_LEN - 8 )
1818 {
1819 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1820 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1821 }
1822
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 while( ssl->in_left < nb_want )
1824 {
1825 len = nb_want - ssl->in_left;
1826 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1827
1828 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1829 ssl->in_left, nb_want ) );
1830 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1831
Paul Bakker831a7552011-05-18 13:32:51 +00001832 if( ret == 0 )
1833 return( POLARSSL_ERR_SSL_CONN_EOF );
1834
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 if( ret < 0 )
1836 return( ret );
1837
1838 ssl->in_left += ret;
1839 }
1840
1841 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1842
1843 return( 0 );
1844}
1845
1846/*
1847 * Flush any data not yet written
1848 */
1849int ssl_flush_output( ssl_context *ssl )
1850{
1851 int ret;
1852 unsigned char *buf;
1853
1854 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1855
1856 while( ssl->out_left > 0 )
1857 {
1858 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1859 5 + ssl->out_msglen, ssl->out_left ) );
1860
Paul Bakker5bd42292012-12-19 14:40:42 +01001861 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001863
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1865
1866 if( ret <= 0 )
1867 return( ret );
1868
1869 ssl->out_left -= ret;
1870 }
1871
1872 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1873
1874 return( 0 );
1875}
1876
1877/*
1878 * Record layer functions
1879 */
1880int ssl_write_record( ssl_context *ssl )
1881{
Paul Bakker05ef8352012-05-08 09:17:57 +00001882 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001883 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
1885 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1886
Paul Bakker5121ce52009-01-03 21:22:43 +00001887 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1888 {
1889 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1890 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1891 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1892
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001893 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1894 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 }
1896
Paul Bakker2770fbd2012-07-03 13:30:23 +00001897#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001898 if( ssl->transform_out != NULL &&
1899 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001900 {
1901 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1902 {
1903 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1904 return( ret );
1905 }
1906
1907 len = ssl->out_msglen;
1908 }
1909#endif /*POLARSSL_ZLIB_SUPPORT */
1910
Paul Bakker05ef8352012-05-08 09:17:57 +00001911#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001912 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001914 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
Paul Bakker05ef8352012-05-08 09:17:57 +00001916 ret = ssl_hw_record_write( ssl );
1917 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1918 {
1919 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001920 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001921 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001922
1923 if( ret == 0 )
1924 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001925 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001926#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001927 if( !done )
1928 {
1929 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1930 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1931 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001932 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1933 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001934
Paul Bakker48916f92012-09-16 19:57:18 +00001935 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001936 {
1937 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1938 {
1939 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1940 return( ret );
1941 }
1942
1943 len = ssl->out_msglen;
1944 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1945 ssl->out_hdr[4] = (unsigned char)( len );
1946 }
1947
1948 ssl->out_left = 5 + ssl->out_msglen;
1949
1950 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1951 "version = [%d:%d], msglen = %d",
1952 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1953 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1954
1955 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001956 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 }
1958
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1960 {
1961 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1962 return( ret );
1963 }
1964
1965 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1966
1967 return( 0 );
1968}
1969
1970int ssl_read_record( ssl_context *ssl )
1971{
Paul Bakker05ef8352012-05-08 09:17:57 +00001972 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001973
1974 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1975
1976 if( ssl->in_hslen != 0 &&
1977 ssl->in_hslen < ssl->in_msglen )
1978 {
1979 /*
1980 * Get next Handshake message in the current record
1981 */
1982 ssl->in_msglen -= ssl->in_hslen;
1983
Paul Bakker8934a982011-08-05 11:11:53 +00001984 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001985 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
1987 ssl->in_hslen = 4;
1988 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1989
1990 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1991 " %d, type = %d, hslen = %d",
1992 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1993
1994 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1995 {
1996 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001997 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998 }
1999
2000 if( ssl->in_msglen < ssl->in_hslen )
2001 {
2002 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002003 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 }
2005
Paul Bakker4224bc02014-04-08 14:36:50 +02002006 if( ssl->state != SSL_HANDSHAKE_OVER )
2007 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
2009 return( 0 );
2010 }
2011
2012 ssl->in_hslen = 0;
2013
2014 /*
2015 * Read the record header and validate it
2016 */
2017 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2018 {
2019 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2020 return( ret );
2021 }
2022
2023 ssl->in_msgtype = ssl->in_hdr[0];
2024 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2025
2026 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2027 "version = [%d:%d], msglen = %d",
2028 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2029 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2030
2031 if( ssl->in_hdr[1] != ssl->major_ver )
2032 {
2033 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002034 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 }
2036
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002037 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 {
2039 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002040 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 }
2042
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002043 /* Sanity check (outer boundaries) */
2044 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2045 {
2046 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2047 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2048 }
2049
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002051 * Make sure the message length is acceptable for the current transform
2052 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 */
Paul Bakker48916f92012-09-16 19:57:18 +00002054 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002056 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 {
2058 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002059 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 }
2061 }
2062 else
2063 {
Paul Bakker48916f92012-09-16 19:57:18 +00002064 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 {
2066 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002067 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
2069
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002070#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002072 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 {
2074 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002075 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002078
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002079#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2080 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 /*
2082 * TLS encrypted messages can have up to 256 bytes of padding
2083 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002084 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002085 ssl->in_msglen > ssl->transform_in->minlen +
2086 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 {
2088 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002089 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002091#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093
2094 /*
2095 * Read and optionally decrypt the message contents
2096 */
2097 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2098 {
2099 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2100 return( ret );
2101 }
2102
2103 SSL_DEBUG_BUF( 4, "input record from network",
2104 ssl->in_hdr, 5 + ssl->in_msglen );
2105
Paul Bakker05ef8352012-05-08 09:17:57 +00002106#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002107 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002108 {
2109 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2110
2111 ret = ssl_hw_record_read( ssl );
2112 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2113 {
2114 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002115 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002116 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002117
2118 if( ret == 0 )
2119 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002120 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002121#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002122 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 {
2124 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2125 {
Paul Bakker40865c82013-01-31 17:13:13 +01002126#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2127 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2128 {
2129 ssl_send_alert_message( ssl,
2130 SSL_ALERT_LEVEL_FATAL,
2131 SSL_ALERT_MSG_BAD_RECORD_MAC );
2132 }
2133#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2135 return( ret );
2136 }
2137
2138 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2139 ssl->in_msg, ssl->in_msglen );
2140
2141 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2142 {
2143 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002144 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 }
2146 }
2147
Paul Bakker2770fbd2012-07-03 13:30:23 +00002148#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002149 if( ssl->transform_in != NULL &&
2150 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002151 {
2152 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2153 {
2154 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2155 return( ret );
2156 }
2157
2158 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2159 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2160 }
2161#endif /* POLARSSL_ZLIB_SUPPORT */
2162
Paul Bakker0a925182012-04-16 06:46:41 +00002163 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2164 ssl->in_msgtype != SSL_MSG_ALERT &&
2165 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2166 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2167 {
2168 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2169
Paul Bakker48916f92012-09-16 19:57:18 +00002170 if( ( ret = ssl_send_alert_message( ssl,
2171 SSL_ALERT_LEVEL_FATAL,
2172 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002173 {
2174 return( ret );
2175 }
2176
2177 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2178 }
2179
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2181 {
2182 ssl->in_hslen = 4;
2183 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2184
2185 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2186 " %d, type = %d, hslen = %d",
2187 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2188
2189 /*
2190 * Additional checks to validate the handshake header
2191 */
2192 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2193 {
2194 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002195 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 }
2197
2198 if( ssl->in_msglen < ssl->in_hslen )
2199 {
2200 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002201 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 }
2203
Paul Bakker48916f92012-09-16 19:57:18 +00002204 if( ssl->state != SSL_HANDSHAKE_OVER )
2205 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
2207
2208 if( ssl->in_msgtype == SSL_MSG_ALERT )
2209 {
2210 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2211 ssl->in_msg[0], ssl->in_msg[1] ) );
2212
2213 /*
2214 * Ignore non-fatal alerts, except close_notify
2215 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002216 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002218 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2219 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002220 /**
2221 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2222 * error identifier.
2223 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002224 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 }
2226
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002227 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2228 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 {
2230 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002231 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 }
2233 }
2234
2235 ssl->in_left = 0;
2236
2237 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2238
2239 return( 0 );
2240}
2241
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002242int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2243{
2244 int ret;
2245
2246 if( ( ret = ssl_send_alert_message( ssl,
2247 SSL_ALERT_LEVEL_FATAL,
2248 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2249 {
2250 return( ret );
2251 }
2252
2253 return( 0 );
2254}
2255
Paul Bakker0a925182012-04-16 06:46:41 +00002256int ssl_send_alert_message( ssl_context *ssl,
2257 unsigned char level,
2258 unsigned char message )
2259{
2260 int ret;
2261
2262 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2263
2264 ssl->out_msgtype = SSL_MSG_ALERT;
2265 ssl->out_msglen = 2;
2266 ssl->out_msg[0] = level;
2267 ssl->out_msg[1] = message;
2268
2269 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2270 {
2271 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2272 return( ret );
2273 }
2274
2275 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2276
2277 return( 0 );
2278}
2279
Paul Bakker5121ce52009-01-03 21:22:43 +00002280/*
2281 * Handshake functions
2282 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002283#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2284 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2285 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2286 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2287 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2288 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2289 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002290int ssl_write_certificate( ssl_context *ssl )
2291{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002292 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
2294 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2295
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002296 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002297 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2298 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002299 {
2300 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2301 ssl->state++;
2302 return( 0 );
2303 }
2304
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002305 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2306 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307}
2308
2309int ssl_parse_certificate( ssl_context *ssl )
2310{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002311 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2312
2313 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2314
2315 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002316 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2317 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002318 {
2319 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2320 ssl->state++;
2321 return( 0 );
2322 }
2323
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002324 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2325 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002326}
2327#else
2328int ssl_write_certificate( ssl_context *ssl )
2329{
2330 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2331 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002332 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002333 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2334
2335 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2336
2337 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002338 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2339 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002340 {
2341 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2342 ssl->state++;
2343 return( 0 );
2344 }
2345
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 if( ssl->endpoint == SSL_IS_CLIENT )
2347 {
2348 if( ssl->client_auth == 0 )
2349 {
2350 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2351 ssl->state++;
2352 return( 0 );
2353 }
2354
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002355#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002356 /*
2357 * If using SSLv3 and got no cert, send an Alert message
2358 * (otherwise an empty Certificate message will be sent).
2359 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002360 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2362 {
2363 ssl->out_msglen = 2;
2364 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002365 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2366 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
2368 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2369 goto write_msg;
2370 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002371#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 }
2373 else /* SSL_IS_SERVER */
2374 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002375 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 {
2377 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002378 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 }
2380 }
2381
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002382 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002383
2384 /*
2385 * 0 . 0 handshake type
2386 * 1 . 3 handshake length
2387 * 4 . 6 length of all certs
2388 * 7 . 9 length of cert. 1
2389 * 10 . n-1 peer certificate
2390 * n . n+2 length of cert. 2
2391 * n+3 . ... upper level cert, etc.
2392 */
2393 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002394 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
Paul Bakker29087132010-03-21 21:03:34 +00002396 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 {
2398 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002399 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 {
2401 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2402 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002403 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 }
2405
2406 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2407 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2408 ssl->out_msg[i + 2] = (unsigned char)( n );
2409
2410 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2411 i += n; crt = crt->next;
2412 }
2413
2414 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2415 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2416 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2417
2418 ssl->out_msglen = i;
2419 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2420 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2421
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002422#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002423write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002424#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
2426 ssl->state++;
2427
2428 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2429 {
2430 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2431 return( ret );
2432 }
2433
2434 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2435
Paul Bakkered27a042013-04-18 22:46:23 +02002436 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437}
2438
2439int ssl_parse_certificate( ssl_context *ssl )
2440{
Paul Bakkered27a042013-04-18 22:46:23 +02002441 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002442 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002443 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2446
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002447 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002448 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2449 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002450 {
2451 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2452 ssl->state++;
2453 return( 0 );
2454 }
2455
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002457 ( ssl->authmode == SSL_VERIFY_NONE ||
2458 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002460 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2462 ssl->state++;
2463 return( 0 );
2464 }
2465
2466 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2467 {
2468 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2469 return( ret );
2470 }
2471
2472 ssl->state++;
2473
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002474#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 /*
2476 * Check if the client sent an empty certificate
2477 */
2478 if( ssl->endpoint == SSL_IS_SERVER &&
2479 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2480 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002481 if( ssl->in_msglen == 2 &&
2482 ssl->in_msgtype == SSL_MSG_ALERT &&
2483 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2484 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 {
2486 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2487
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002488 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2490 return( 0 );
2491 else
Paul Bakker40e46942009-01-03 21:51:57 +00002492 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 }
2494 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002495#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002497#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2498 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 if( ssl->endpoint == SSL_IS_SERVER &&
2500 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2501 {
2502 if( ssl->in_hslen == 7 &&
2503 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2504 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2505 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2506 {
2507 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2508
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002509 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002511 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 else
2513 return( 0 );
2514 }
2515 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002516#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2517 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
2519 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2520 {
2521 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002522 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 }
2524
2525 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2526 {
2527 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002528 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 }
2530
2531 /*
2532 * Same message structure as in ssl_write_certificate()
2533 */
2534 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2535
2536 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
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
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002542 /* In case we tried to reuse a session but it failed */
2543 if( ssl->session_negotiate->peer_cert != NULL )
2544 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002545 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002546 polarssl_free( ssl->session_negotiate->peer_cert );
2547 }
2548
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002549 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2550 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 {
2552 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002553 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002554 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 }
2556
Paul Bakkerb6b09562013-09-18 14:17:41 +02002557 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
2559 i = 7;
2560
2561 while( i < ssl->in_hslen )
2562 {
2563 if( ssl->in_msg[i] != 0 )
2564 {
2565 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002566 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 }
2568
2569 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2570 | (unsigned int) ssl->in_msg[i + 2];
2571 i += 3;
2572
2573 if( n < 128 || i + n > ssl->in_hslen )
2574 {
2575 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002576 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 }
2578
Paul Bakkerddf26b42013-09-18 13:46:23 +02002579 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2580 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 if( ret != 0 )
2582 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002583 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 return( ret );
2585 }
2586
2587 i += n;
2588 }
2589
Paul Bakker48916f92012-09-16 19:57:18 +00002590 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002592 /*
2593 * On client, make sure the server cert doesn't change during renego to
2594 * avoid "triple handshake" attack: https://secure-resumption.com/
2595 */
2596 if( ssl->endpoint == SSL_IS_CLIENT &&
2597 ssl->renegotiation == SSL_RENEGOTIATION )
2598 {
2599 if( ssl->session->peer_cert == NULL )
2600 {
2601 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2602 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2603 }
2604
2605 if( ssl->session->peer_cert->raw.len !=
2606 ssl->session_negotiate->peer_cert->raw.len ||
2607 memcmp( ssl->session->peer_cert->raw.p,
2608 ssl->session_negotiate->peer_cert->raw.p,
2609 ssl->session->peer_cert->raw.len ) != 0 )
2610 {
2611 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2612 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2613 }
2614 }
2615
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 if( ssl->authmode != SSL_VERIFY_NONE )
2617 {
2618 if( ssl->ca_chain == NULL )
2619 {
2620 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002621 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 }
2623
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002624 /*
2625 * Main check: verify certificate
2626 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002627 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2628 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2629 &ssl->session_negotiate->verify_result,
2630 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
2632 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002633 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002635 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002636
2637 /*
2638 * Secondary checks: always done, but change 'ret' only if it was 0
2639 */
2640
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002641#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002642 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002643 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002644
2645 /* If certificate uses an EC key, make sure the curve is OK */
2646 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2647 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2648 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002649 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002650 if( ret == 0 )
2651 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002652 }
2653 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002654#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002656 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2657 ciphersuite_info,
2658 ! ssl->endpoint ) != 0 )
2659 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002660 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002661 if( ret == 0 )
2662 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2663 }
2664
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2666 ret = 0;
2667 }
2668
2669 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2670
2671 return( ret );
2672}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002673#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2674 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2675 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2676 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2677 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2678 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2679 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
2681int ssl_write_change_cipher_spec( ssl_context *ssl )
2682{
2683 int ret;
2684
2685 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2686
2687 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2688 ssl->out_msglen = 1;
2689 ssl->out_msg[0] = 1;
2690
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 ssl->state++;
2692
2693 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2694 {
2695 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2696 return( ret );
2697 }
2698
2699 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2700
2701 return( 0 );
2702}
2703
2704int ssl_parse_change_cipher_spec( ssl_context *ssl )
2705{
2706 int ret;
2707
2708 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2709
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2711 {
2712 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2713 return( ret );
2714 }
2715
2716 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2717 {
2718 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002719 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
2722 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2723 {
2724 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002725 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
2728 ssl->state++;
2729
2730 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2731
2732 return( 0 );
2733}
2734
Paul Bakker41c83d32013-03-20 14:39:14 +01002735void ssl_optimize_checksum( ssl_context *ssl,
2736 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002737{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002738 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002739
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 Bakker380da532012-04-18 16:10:25 +00002742 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002743 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002744 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002745#endif
2746#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2747#if defined(POLARSSL_SHA512_C)
2748 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2749 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2750 else
2751#endif
2752#if defined(POLARSSL_SHA256_C)
2753 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002754 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002755 else
2756#endif
2757#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002758 {
2759 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002760 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002761 }
Paul Bakker380da532012-04-18 16:10:25 +00002762}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002763
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002764static void ssl_update_checksum_start( ssl_context *ssl,
2765 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002766{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002767#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2768 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002769 md5_update( &ssl->handshake->fin_md5 , buf, len );
2770 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002771#endif
2772#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2773#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002774 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002775#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002776#if defined(POLARSSL_SHA512_C)
2777 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002778#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002779#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002780}
2781
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002782#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2783 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002784static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2785 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002786{
Paul Bakker48916f92012-09-16 19:57:18 +00002787 md5_update( &ssl->handshake->fin_md5 , buf, len );
2788 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002789}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002790#endif
Paul Bakker380da532012-04-18 16:10:25 +00002791
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002792#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2793#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002794static void ssl_update_checksum_sha256( ssl_context *ssl,
2795 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002796{
Paul Bakker9e36f042013-06-30 14:34:05 +02002797 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002798}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002799#endif
Paul Bakker380da532012-04-18 16:10:25 +00002800
Paul Bakker9e36f042013-06-30 14:34:05 +02002801#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002802static void ssl_update_checksum_sha384( ssl_context *ssl,
2803 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002804{
Paul Bakker9e36f042013-06-30 14:34:05 +02002805 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002806}
Paul Bakker769075d2012-11-24 11:26:46 +01002807#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002808#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002809
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002810#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002811static void ssl_calc_finished_ssl(
2812 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002813{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002814 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002815 md5_context md5;
2816 sha1_context sha1;
2817
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 unsigned char padbuf[48];
2819 unsigned char md5sum[16];
2820 unsigned char sha1sum[20];
2821
Paul Bakker48916f92012-09-16 19:57:18 +00002822 ssl_session *session = ssl->session_negotiate;
2823 if( !session )
2824 session = ssl->session;
2825
Paul Bakker1ef83d62012-04-11 12:09:53 +00002826 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2827
Paul Bakker48916f92012-09-16 19:57:18 +00002828 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2829 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
2831 /*
2832 * SSLv3:
2833 * hash =
2834 * MD5( master + pad2 +
2835 * MD5( handshake + sender + master + pad1 ) )
2836 * + SHA1( master + pad2 +
2837 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002838 */
2839
Paul Bakker90995b52013-06-24 19:20:35 +02002840#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002842 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002843#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Paul Bakker90995b52013-06-24 19:20:35 +02002845#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002846 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002847 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002848#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Paul Bakker3c2122f2013-06-24 19:03:14 +02002850 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2851 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
Paul Bakker1ef83d62012-04-11 12:09:53 +00002853 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
Paul Bakker3c2122f2013-06-24 19:03:14 +02002855 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002856 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002857 md5_update( &md5, padbuf, 48 );
2858 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Paul Bakker3c2122f2013-06-24 19:03:14 +02002860 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002861 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002862 sha1_update( &sha1, padbuf, 40 );
2863 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
Paul Bakker1ef83d62012-04-11 12:09:53 +00002867 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002868 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 md5_update( &md5, padbuf, 48 );
2870 md5_update( &md5, md5sum, 16 );
2871 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002874 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002875 sha1_update( &sha1, padbuf , 40 );
2876 sha1_update( &sha1, sha1sum, 20 );
2877 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002878
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
Paul Bakker34617722014-06-13 17:20:13 +02002881 polarssl_zeroize( &md5, sizeof( md5_context ) );
2882 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Paul Bakker34617722014-06-13 17:20:13 +02002884 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2885 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2886 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
2888 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2889}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002890#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002892#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002893static void ssl_calc_finished_tls(
2894 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002895{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002897 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002898 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
Paul Bakker48916f92012-09-16 19:57:18 +00002902 ssl_session *session = ssl->session_negotiate;
2903 if( !session )
2904 session = ssl->session;
2905
Paul Bakker1ef83d62012-04-11 12:09:53 +00002906 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
Paul Bakker48916f92012-09-16 19:57:18 +00002908 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2909 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911 /*
2912 * TLSv1:
2913 * hash = PRF( master, finished_label,
2914 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2915 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Paul Bakker90995b52013-06-24 19:20:35 +02002917#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002918 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2919 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002920#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002921
Paul Bakker90995b52013-06-24 19:20:35 +02002922#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2924 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002925#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002926
2927 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002928 ? "client finished"
2929 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002930
2931 md5_finish( &md5, padbuf );
2932 sha1_finish( &sha1, padbuf + 16 );
2933
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002934 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002935 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936
2937 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2938
Paul Bakker34617722014-06-13 17:20:13 +02002939 polarssl_zeroize( &md5, sizeof( md5_context ) );
2940 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002941
Paul Bakker34617722014-06-13 17:20:13 +02002942 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002943
2944 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2945}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002947
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002948#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2949#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002950static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951 ssl_context *ssl, unsigned char *buf, int from )
2952{
2953 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002954 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002955 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956 unsigned char padbuf[32];
2957
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ssl_session *session = ssl->session_negotiate;
2959 if( !session )
2960 session = ssl->session;
2961
Paul Bakker380da532012-04-18 16:10:25 +00002962 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963
Paul Bakker9e36f042013-06-30 14:34:05 +02002964 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002965
2966 /*
2967 * TLSv1.2:
2968 * hash = PRF( master, finished_label,
2969 * Hash( handshake ) )[0.11]
2970 */
2971
Paul Bakker9e36f042013-06-30 14:34:05 +02002972#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002973 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002974 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002975#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976
2977 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002978 ? "client finished"
2979 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002980
Paul Bakker9e36f042013-06-30 14:34:05 +02002981 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002983 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002984 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002985
2986 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2987
Paul Bakker34617722014-06-13 17:20:13 +02002988 polarssl_zeroize( &sha256, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989
Paul Bakker34617722014-06-13 17:20:13 +02002990 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002991
2992 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2993}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002994#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995
Paul Bakker9e36f042013-06-30 14:34:05 +02002996#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002997static void ssl_calc_finished_tls_sha384(
2998 ssl_context *ssl, unsigned char *buf, int from )
2999{
3000 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003001 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003002 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003003 unsigned char padbuf[48];
3004
Paul Bakker48916f92012-09-16 19:57:18 +00003005 ssl_session *session = ssl->session_negotiate;
3006 if( !session )
3007 session = ssl->session;
3008
Paul Bakker380da532012-04-18 16:10:25 +00003009 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003010
Paul Bakker9e36f042013-06-30 14:34:05 +02003011 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003012
3013 /*
3014 * TLSv1.2:
3015 * hash = PRF( master, finished_label,
3016 * Hash( handshake ) )[0.11]
3017 */
3018
Paul Bakker9e36f042013-06-30 14:34:05 +02003019#if !defined(POLARSSL_SHA512_ALT)
3020 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3021 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003022#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003023
3024 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003025 ? "client finished"
3026 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003027
Paul Bakker9e36f042013-06-30 14:34:05 +02003028 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003029
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003030 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003031 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003032
3033 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3034
Paul Bakker34617722014-06-13 17:20:13 +02003035 polarssl_zeroize( &sha512, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003036
Paul Bakker34617722014-06-13 17:20:13 +02003037 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003038
3039 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3040}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003041#endif /* POLARSSL_SHA512_C */
3042#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003043
Paul Bakker48916f92012-09-16 19:57:18 +00003044void ssl_handshake_wrapup( ssl_context *ssl )
3045{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003046 int resume = ssl->handshake->resume;
3047
Paul Bakker48916f92012-09-16 19:57:18 +00003048 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3049
3050 /*
3051 * Free our handshake params
3052 */
3053 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003054 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003055 ssl->handshake = NULL;
3056
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003057 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003058 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003059 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003060 ssl->renego_records_seen = 0;
3061 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003062
Paul Bakker48916f92012-09-16 19:57:18 +00003063 /*
3064 * Switch in our now active transform context
3065 */
3066 if( ssl->transform )
3067 {
3068 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003069 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003070 }
3071 ssl->transform = ssl->transform_negotiate;
3072 ssl->transform_negotiate = NULL;
3073
Paul Bakker0a597072012-09-25 21:55:46 +00003074 if( ssl->session )
3075 {
3076 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003077 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003078 }
3079 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003080 ssl->session_negotiate = NULL;
3081
Paul Bakker0a597072012-09-25 21:55:46 +00003082 /*
3083 * Add cache entry
3084 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003085 if( ssl->f_set_cache != NULL &&
3086 ssl->session->length != 0 &&
3087 resume == 0 )
3088 {
Paul Bakker0a597072012-09-25 21:55:46 +00003089 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3090 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003091 }
Paul Bakker0a597072012-09-25 21:55:46 +00003092
Paul Bakker48916f92012-09-16 19:57:18 +00003093 ssl->state++;
3094
3095 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3096}
3097
Paul Bakker1ef83d62012-04-11 12:09:53 +00003098int ssl_write_finished( ssl_context *ssl )
3099{
3100 int ret, hash_len;
3101
3102 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3103
Paul Bakker92be97b2013-01-02 17:30:03 +01003104 /*
3105 * Set the out_msg pointer to the correct location based on IV length
3106 */
3107 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3108 {
3109 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3110 ssl->transform_negotiate->fixed_ivlen;
3111 }
3112 else
3113 ssl->out_msg = ssl->out_iv;
3114
Paul Bakker48916f92012-09-16 19:57:18 +00003115 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003116
3117 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003118 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3119
Paul Bakker48916f92012-09-16 19:57:18 +00003120 ssl->verify_data_len = hash_len;
3121 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3122
Paul Bakker5121ce52009-01-03 21:22:43 +00003123 ssl->out_msglen = 4 + hash_len;
3124 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3125 ssl->out_msg[0] = SSL_HS_FINISHED;
3126
3127 /*
3128 * In case of session resuming, invert the client and server
3129 * ChangeCipherSpec messages order.
3130 */
Paul Bakker0a597072012-09-25 21:55:46 +00003131 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 {
3133 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003134 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003135 else
3136 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3137 }
3138 else
3139 ssl->state++;
3140
Paul Bakker48916f92012-09-16 19:57:18 +00003141 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003142 * Switch to our negotiated transform and session parameters for outbound
3143 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003144 */
3145 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3146 ssl->transform_out = ssl->transform_negotiate;
3147 ssl->session_out = ssl->session_negotiate;
3148 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003149
Paul Bakker07eb38b2012-12-19 14:42:06 +01003150#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003151 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003152 {
3153 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3154 {
3155 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3156 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3157 }
3158 }
3159#endif
3160
Paul Bakker5121ce52009-01-03 21:22:43 +00003161 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3162 {
3163 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3164 return( ret );
3165 }
3166
3167 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3168
3169 return( 0 );
3170}
3171
3172int ssl_parse_finished( ssl_context *ssl )
3173{
Paul Bakker23986e52011-04-24 08:57:21 +00003174 int ret;
3175 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 unsigned char buf[36];
3177
3178 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3179
Paul Bakker48916f92012-09-16 19:57:18 +00003180 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Paul Bakker48916f92012-09-16 19:57:18 +00003182 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003183 * Switch to our negotiated transform and session parameters for inbound
3184 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003185 */
3186 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3187 ssl->transform_in = ssl->transform_negotiate;
3188 ssl->session_in = ssl->session_negotiate;
3189 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003190
Paul Bakker92be97b2013-01-02 17:30:03 +01003191 /*
3192 * Set the in_msg pointer to the correct location based on IV length
3193 */
3194 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3195 {
3196 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3197 ssl->transform_negotiate->fixed_ivlen;
3198 }
3199 else
3200 ssl->in_msg = ssl->in_iv;
3201
Paul Bakker07eb38b2012-12-19 14:42:06 +01003202#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003203 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003204 {
3205 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3206 {
3207 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3208 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3209 }
3210 }
3211#endif
3212
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3214 {
3215 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3216 return( ret );
3217 }
3218
3219 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3220 {
3221 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003222 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 }
3224
Paul Bakker1ef83d62012-04-11 12:09:53 +00003225 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3227
3228 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3229 ssl->in_hslen != 4 + hash_len )
3230 {
3231 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003232 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 }
3234
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003235 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 {
3237 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003238 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 }
3240
Paul Bakker48916f92012-09-16 19:57:18 +00003241 ssl->verify_data_len = hash_len;
3242 memcpy( ssl->peer_verify_data, buf, hash_len );
3243
Paul Bakker0a597072012-09-25 21:55:46 +00003244 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 {
3246 if( ssl->endpoint == SSL_IS_CLIENT )
3247 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3248
3249 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003250 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 }
3252 else
3253 ssl->state++;
3254
3255 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3256
3257 return( 0 );
3258}
3259
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003260static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003261{
3262 if( ssl->transform_negotiate )
3263 ssl_transform_free( ssl->transform_negotiate );
3264 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003265 {
3266 ssl->transform_negotiate =
3267 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003268
3269 if( ssl->transform_negotiate != NULL )
3270 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003271 }
Paul Bakker48916f92012-09-16 19:57:18 +00003272
3273 if( ssl->session_negotiate )
3274 ssl_session_free( ssl->session_negotiate );
3275 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003276 {
3277 ssl->session_negotiate =
3278 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003279
3280 if( ssl->session_negotiate != NULL )
3281 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003282 }
Paul Bakker48916f92012-09-16 19:57:18 +00003283
3284 if( ssl->handshake )
3285 ssl_handshake_free( ssl->handshake );
3286 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003287 {
3288 ssl->handshake = (ssl_handshake_params *)
3289 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003290
3291 if( ssl->handshake != NULL )
3292 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003293 }
Paul Bakker48916f92012-09-16 19:57:18 +00003294
3295 if( ssl->handshake == NULL ||
3296 ssl->transform_negotiate == NULL ||
3297 ssl->session_negotiate == NULL )
3298 {
3299 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3300 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3301 }
3302
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003303#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3304 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003305 md5_starts( &ssl->handshake->fin_md5 );
3306 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003307#endif
3308#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3309#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003310 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003311#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003312#if defined(POLARSSL_SHA512_C)
3313 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003314#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003315#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003316
3317 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003318 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003319
Paul Bakker61d113b2013-07-04 11:51:43 +02003320#if defined(POLARSSL_ECDH_C)
3321 ecdh_init( &ssl->handshake->ecdh_ctx );
3322#endif
3323
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003324#if defined(POLARSSL_X509_CRT_PARSE_C)
3325 ssl->handshake->key_cert = ssl->key_cert;
3326#endif
3327
Paul Bakker48916f92012-09-16 19:57:18 +00003328 return( 0 );
3329}
3330
Paul Bakker5121ce52009-01-03 21:22:43 +00003331/*
3332 * Initialize an SSL context
3333 */
3334int ssl_init( ssl_context *ssl )
3335{
Paul Bakker48916f92012-09-16 19:57:18 +00003336 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 int len = SSL_BUFFER_LEN;
3338
3339 memset( ssl, 0, sizeof( ssl_context ) );
3340
Paul Bakker62f2dee2012-09-28 07:31:51 +00003341 /*
3342 * Sane defaults
3343 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003344 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3345 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3346 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3347 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003348
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003349 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003350
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003351 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3352
Paul Bakker62f2dee2012-09-28 07:31:51 +00003353#if defined(POLARSSL_DHM_C)
3354 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3355 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3356 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3357 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3358 {
3359 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3360 return( ret );
3361 }
3362#endif
3363
3364 /*
3365 * Prepare base structures
3366 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003367 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003368 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003369 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 ssl->in_msg = ssl->in_ctr + 13;
3371
3372 if( ssl->in_ctr == NULL )
3373 {
3374 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003375 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 }
3377
Paul Bakker6e339b52013-07-03 13:37:05 +02003378 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003379 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003380 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003381 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003382
3383 if( ssl->out_ctr == NULL )
3384 {
3385 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003386 polarssl_free( ssl->in_ctr );
3387 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003388 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003389 }
3390
3391 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3392 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3393
Paul Bakker606b4ba2013-08-14 16:52:14 +02003394#if defined(POLARSSL_SSL_SESSION_TICKETS)
3395 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3396#endif
3397
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003398#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003399 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003400#endif
3401
Paul Bakker48916f92012-09-16 19:57:18 +00003402 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3403 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003404
3405 return( 0 );
3406}
3407
3408/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003409 * Reset an initialized and used SSL context for re-use while retaining
3410 * all application-set variables, function pointers and data.
3411 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003412int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003413{
Paul Bakker48916f92012-09-16 19:57:18 +00003414 int ret;
3415
Paul Bakker7eb013f2011-10-06 12:37:39 +00003416 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003417 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3418 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3419
3420 ssl->verify_data_len = 0;
3421 memset( ssl->own_verify_data, 0, 36 );
3422 memset( ssl->peer_verify_data, 0, 36 );
3423
Paul Bakker7eb013f2011-10-06 12:37:39 +00003424 ssl->in_offt = NULL;
3425
Paul Bakker92be97b2013-01-02 17:30:03 +01003426 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003427 ssl->in_msgtype = 0;
3428 ssl->in_msglen = 0;
3429 ssl->in_left = 0;
3430
3431 ssl->in_hslen = 0;
3432 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003433 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003434
Paul Bakker92be97b2013-01-02 17:30:03 +01003435 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003436 ssl->out_msgtype = 0;
3437 ssl->out_msglen = 0;
3438 ssl->out_left = 0;
3439
Paul Bakker48916f92012-09-16 19:57:18 +00003440 ssl->transform_in = NULL;
3441 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003442
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003443 ssl->renego_records_seen = 0;
3444
Paul Bakker7eb013f2011-10-06 12:37:39 +00003445 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3446 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003447
3448#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003449 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003450 {
3451 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003452 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003453 {
3454 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3455 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3456 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003457 }
3458#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003459
Paul Bakker48916f92012-09-16 19:57:18 +00003460 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003461 {
Paul Bakker48916f92012-09-16 19:57:18 +00003462 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003463 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003464 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003465 }
Paul Bakker48916f92012-09-16 19:57:18 +00003466
Paul Bakkerc0463502013-02-14 11:19:38 +01003467 if( ssl->session )
3468 {
3469 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003470 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003471 ssl->session = NULL;
3472 }
3473
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003474#if defined(POLARSSL_SSL_ALPN)
3475 ssl->alpn_chosen = NULL;
3476#endif
3477
Paul Bakker48916f92012-09-16 19:57:18 +00003478 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3479 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003480
3481 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003482}
3483
Paul Bakkera503a632013-08-14 13:48:06 +02003484#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003485/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003486 * Allocate and initialize ticket keys
3487 */
3488static int ssl_ticket_keys_init( ssl_context *ssl )
3489{
3490 int ret;
3491 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003492 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003493
3494 if( ssl->ticket_keys != NULL )
3495 return( 0 );
3496
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003497 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3498 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003499 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3500
3501 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003502 {
3503 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003504 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003505 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003506
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003507 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3508 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3509 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3510 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003511 polarssl_free( tkeys );
3512 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003513 }
3514
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003515 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003516 {
3517 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003518 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003519 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003520
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003521 ssl->ticket_keys = tkeys;
3522
3523 return( 0 );
3524}
Paul Bakkera503a632013-08-14 13:48:06 +02003525#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003526
3527/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003528 * SSL set accessors
3529 */
3530void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3531{
3532 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003533
Paul Bakker606b4ba2013-08-14 16:52:14 +02003534#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003535 if( endpoint == SSL_IS_CLIENT )
3536 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003537#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003538}
3539
3540void ssl_set_authmode( ssl_context *ssl, int authmode )
3541{
3542 ssl->authmode = authmode;
3543}
3544
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003545#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003546void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003547 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003548 void *p_vrfy )
3549{
3550 ssl->f_vrfy = f_vrfy;
3551 ssl->p_vrfy = p_vrfy;
3552}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003553#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003554
Paul Bakker5121ce52009-01-03 21:22:43 +00003555void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003556 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 void *p_rng )
3558{
3559 ssl->f_rng = f_rng;
3560 ssl->p_rng = p_rng;
3561}
3562
3563void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003564 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 void *p_dbg )
3566{
3567 ssl->f_dbg = f_dbg;
3568 ssl->p_dbg = p_dbg;
3569}
3570
3571void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003572 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003573 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003574{
3575 ssl->f_recv = f_recv;
3576 ssl->f_send = f_send;
3577 ssl->p_recv = p_recv;
3578 ssl->p_send = p_send;
3579}
3580
Paul Bakker0a597072012-09-25 21:55:46 +00003581void ssl_set_session_cache( ssl_context *ssl,
3582 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3583 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003584{
Paul Bakker0a597072012-09-25 21:55:46 +00003585 ssl->f_get_cache = f_get_cache;
3586 ssl->p_get_cache = p_get_cache;
3587 ssl->f_set_cache = f_set_cache;
3588 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003589}
3590
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003591int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003592{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003593 int ret;
3594
3595 if( ssl == NULL ||
3596 session == NULL ||
3597 ssl->session_negotiate == NULL ||
3598 ssl->endpoint != SSL_IS_CLIENT )
3599 {
3600 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3601 }
3602
3603 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3604 return( ret );
3605
Paul Bakker0a597072012-09-25 21:55:46 +00003606 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003607
3608 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003609}
3610
Paul Bakkerb68cad62012-08-23 08:34:18 +00003611void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003612{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003613 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3614 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3615 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3616 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3617}
3618
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003619void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3620 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003621 int major, int minor )
3622{
3623 if( major != SSL_MAJOR_VERSION_3 )
3624 return;
3625
3626 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3627 return;
3628
3629 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003630}
3631
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003632#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003633/* Add a new (empty) key_cert entry an return a pointer to it */
3634static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3635{
3636 ssl_key_cert *key_cert, *last;
3637
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003638 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3639 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003640 return( NULL );
3641
3642 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3643
3644 /* Append the new key_cert to the (possibly empty) current list */
3645 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003646 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003647 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003648 if( ssl->handshake != NULL )
3649 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003650 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003651 else
3652 {
3653 last = ssl->key_cert;
3654 while( last->next != NULL )
3655 last = last->next;
3656 last->next = key_cert;
3657 }
3658
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003659 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003660}
3661
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003662void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003663 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003664{
3665 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003666 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003667 ssl->peer_cn = peer_cn;
3668}
3669
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003670int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003671 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003672{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003673 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3674
3675 if( key_cert == NULL )
3676 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3677
3678 key_cert->cert = own_cert;
3679 key_cert->key = pk_key;
3680
3681 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003682}
3683
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003684#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003685int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003686 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003687{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003688 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003689 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003690
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003691 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003692 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3693
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003694 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3695 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003696 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003697
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003698 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003699
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003700 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003701 if( ret != 0 )
3702 return( ret );
3703
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003704 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003705 return( ret );
3706
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003707 key_cert->cert = own_cert;
3708 key_cert->key_own_alloc = 1;
3709
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003710 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003711}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003712#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003714int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003715 void *rsa_key,
3716 rsa_decrypt_func rsa_decrypt,
3717 rsa_sign_func rsa_sign,
3718 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003719{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003720 int ret;
3721 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003722
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003723 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003724 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3725
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003726 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3727 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003728 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003729
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003730 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003731
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003732 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3733 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3734 return( ret );
3735
3736 key_cert->cert = own_cert;
3737 key_cert->key_own_alloc = 1;
3738
3739 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003740}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003741#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003742
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003743#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003744int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3745 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003746{
Paul Bakker6db455e2013-09-18 17:29:31 +02003747 if( psk == NULL || psk_identity == NULL )
3748 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3749
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003750 /*
3751 * The length will be check later anyway, but in case it is obviously
3752 * too large, better abort now. The PMS is as follows:
3753 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3754 */
3755 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3756 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3757
Paul Bakker6db455e2013-09-18 17:29:31 +02003758 if( ssl->psk != NULL )
3759 {
3760 polarssl_free( ssl->psk );
3761 polarssl_free( ssl->psk_identity );
3762 }
3763
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003764 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003765 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003766
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003767 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003768 ssl->psk_identity = (unsigned char *)
3769 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003770
3771 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3772 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3773
3774 memcpy( ssl->psk, psk, ssl->psk_len );
3775 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003776
3777 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003778}
3779
3780void ssl_set_psk_cb( ssl_context *ssl,
3781 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3782 size_t),
3783 void *p_psk )
3784{
3785 ssl->f_psk = f_psk;
3786 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003787}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003788#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003789
Paul Bakker48916f92012-09-16 19:57:18 +00003790#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003791int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003792{
3793 int ret;
3794
Paul Bakker48916f92012-09-16 19:57:18 +00003795 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003796 {
3797 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3798 return( ret );
3799 }
3800
Paul Bakker48916f92012-09-16 19:57:18 +00003801 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003802 {
3803 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3804 return( ret );
3805 }
3806
3807 return( 0 );
3808}
3809
Paul Bakker1b57b062011-01-06 15:48:19 +00003810int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3811{
3812 int ret;
3813
Paul Bakker66d5d072014-06-17 16:39:18 +02003814 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003815 {
3816 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3817 return( ret );
3818 }
3819
Paul Bakker66d5d072014-06-17 16:39:18 +02003820 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003821 {
3822 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3823 return( ret );
3824 }
3825
3826 return( 0 );
3827}
Paul Bakker48916f92012-09-16 19:57:18 +00003828#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003829
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003830#if defined(POLARSSL_SSL_SET_CURVES)
3831/*
3832 * Set the allowed elliptic curves
3833 */
3834void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3835{
3836 ssl->curve_list = curve_list;
3837}
3838#endif
3839
Paul Bakker0be444a2013-08-27 21:55:01 +02003840#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003841int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003842{
3843 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003844 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003845
3846 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003847
3848 if( ssl->hostname_len + 1 == 0 )
3849 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3850
Paul Bakker6e339b52013-07-03 13:37:05 +02003851 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003852
Paul Bakkerb15b8512012-01-13 13:44:06 +00003853 if( ssl->hostname == NULL )
3854 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3855
Paul Bakker3c2122f2013-06-24 19:03:14 +02003856 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003858
Paul Bakker40ea7de2009-05-03 10:18:48 +00003859 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003860
3861 return( 0 );
3862}
3863
Paul Bakker5701cdc2012-09-27 21:49:42 +00003864void ssl_set_sni( ssl_context *ssl,
3865 int (*f_sni)(void *, ssl_context *,
3866 const unsigned char *, size_t),
3867 void *p_sni )
3868{
3869 ssl->f_sni = f_sni;
3870 ssl->p_sni = p_sni;
3871}
Paul Bakker0be444a2013-08-27 21:55:01 +02003872#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003873
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003874#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003875int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003876{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003877 size_t cur_len, tot_len;
3878 const char **p;
3879
3880 /*
3881 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3882 * truncated". Check lengths now rather than later.
3883 */
3884 tot_len = 0;
3885 for( p = protos; *p != NULL; p++ )
3886 {
3887 cur_len = strlen( *p );
3888 tot_len += cur_len;
3889
3890 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3891 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3892 }
3893
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003894 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003895
3896 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003897}
3898
3899const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3900{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003901 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003902}
3903#endif /* POLARSSL_SSL_ALPN */
3904
Paul Bakker490ecc82011-10-06 13:04:09 +00003905void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3906{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003907 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3908 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3909 {
3910 ssl->max_major_ver = major;
3911 ssl->max_minor_ver = minor;
3912 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003913}
3914
Paul Bakker1d29fb52012-09-28 13:28:45 +00003915void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3916{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003917 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3918 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3919 {
3920 ssl->min_major_ver = major;
3921 ssl->min_minor_ver = minor;
3922 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003923}
3924
Paul Bakker05decb22013-08-15 13:33:48 +02003925#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003926int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3927{
Paul Bakker77e257e2013-12-16 15:29:52 +01003928 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003929 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003930 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003931 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003932 }
3933
3934 ssl->mfl_code = mfl_code;
3935
3936 return( 0 );
3937}
Paul Bakker05decb22013-08-15 13:33:48 +02003938#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003939
Paul Bakker1f2bc622013-08-15 13:45:55 +02003940#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003941int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003942{
3943 if( ssl->endpoint != SSL_IS_CLIENT )
3944 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3945
Paul Bakker8c1ede62013-07-19 14:14:37 +02003946 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003947
3948 return( 0 );
3949}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003950#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003951
Paul Bakker48916f92012-09-16 19:57:18 +00003952void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3953{
3954 ssl->disable_renegotiation = renegotiation;
3955}
3956
3957void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3958{
3959 ssl->allow_legacy_renegotiation = allow_legacy;
3960}
3961
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003962void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
3963{
3964 ssl->renego_max_records = max_records;
3965}
3966
Paul Bakkera503a632013-08-14 13:48:06 +02003967#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003968int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3969{
3970 ssl->session_tickets = use_tickets;
3971
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003972 if( ssl->endpoint == SSL_IS_CLIENT )
3973 return( 0 );
3974
3975 if( ssl->f_rng == NULL )
3976 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3977
3978 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003979}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003980
3981void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3982{
3983 ssl->ticket_lifetime = lifetime;
3984}
Paul Bakkera503a632013-08-14 13:48:06 +02003985#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003986
Paul Bakker5121ce52009-01-03 21:22:43 +00003987/*
3988 * SSL get accessors
3989 */
Paul Bakker23986e52011-04-24 08:57:21 +00003990size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003991{
3992 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3993}
3994
Paul Bakkerff60ee62010-03-16 21:09:09 +00003995int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003996{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003997 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003998}
3999
Paul Bakkere3166ce2011-01-27 17:40:50 +00004000const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004001{
Paul Bakker926c8e42013-03-06 10:23:34 +01004002 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004003 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004004
Paul Bakkere3166ce2011-01-27 17:40:50 +00004005 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004006}
4007
Paul Bakker43ca69c2011-01-15 17:35:19 +00004008const char *ssl_get_version( const ssl_context *ssl )
4009{
4010 switch( ssl->minor_ver )
4011 {
4012 case SSL_MINOR_VERSION_0:
4013 return( "SSLv3.0" );
4014
4015 case SSL_MINOR_VERSION_1:
4016 return( "TLSv1.0" );
4017
4018 case SSL_MINOR_VERSION_2:
4019 return( "TLSv1.1" );
4020
Paul Bakker1ef83d62012-04-11 12:09:53 +00004021 case SSL_MINOR_VERSION_3:
4022 return( "TLSv1.2" );
4023
Paul Bakker43ca69c2011-01-15 17:35:19 +00004024 default:
4025 break;
4026 }
4027 return( "unknown" );
4028}
4029
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004030#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004031const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004032{
4033 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004034 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004035
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004036 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004037}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004038#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004039
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004040int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4041{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004042 if( ssl == NULL ||
4043 dst == NULL ||
4044 ssl->session == NULL ||
4045 ssl->endpoint != SSL_IS_CLIENT )
4046 {
4047 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4048 }
4049
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004050 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004051}
4052
Paul Bakker5121ce52009-01-03 21:22:43 +00004053/*
Paul Bakker1961b702013-01-25 14:49:24 +01004054 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004055 */
Paul Bakker1961b702013-01-25 14:49:24 +01004056int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004057{
Paul Bakker40e46942009-01-03 21:51:57 +00004058 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004059
Paul Bakker40e46942009-01-03 21:51:57 +00004060#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004061 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004062 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004063#endif
4064
Paul Bakker40e46942009-01-03 21:51:57 +00004065#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004066 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004067 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004068#endif
4069
Paul Bakker1961b702013-01-25 14:49:24 +01004070 return( ret );
4071}
4072
4073/*
4074 * Perform the SSL handshake
4075 */
4076int ssl_handshake( ssl_context *ssl )
4077{
4078 int ret = 0;
4079
4080 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4081
4082 while( ssl->state != SSL_HANDSHAKE_OVER )
4083 {
4084 ret = ssl_handshake_step( ssl );
4085
4086 if( ret != 0 )
4087 break;
4088 }
4089
Paul Bakker5121ce52009-01-03 21:22:43 +00004090 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4091
4092 return( ret );
4093}
4094
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004095#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004096/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004097 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004098 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004099static int ssl_write_hello_request( ssl_context *ssl )
4100{
4101 int ret;
4102
4103 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4104
4105 ssl->out_msglen = 4;
4106 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4107 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4108
4109 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4110 {
4111 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4112 return( ret );
4113 }
4114
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004115 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4116
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004117 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4118
4119 return( 0 );
4120}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004121#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004122
4123/*
4124 * Actually renegotiate current connection, triggered by either:
4125 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004126 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004127 * - receiving any handshake message on server during ssl_read() after the
4128 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004129 * If the handshake doesn't complete due to waiting for I/O, it will continue
4130 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004131 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004132static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004133{
4134 int ret;
4135
4136 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4137
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004138 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4139 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004140
4141 ssl->state = SSL_HELLO_REQUEST;
4142 ssl->renegotiation = SSL_RENEGOTIATION;
4143
Paul Bakker48916f92012-09-16 19:57:18 +00004144 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4145 {
4146 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4147 return( ret );
4148 }
4149
4150 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4151
4152 return( 0 );
4153}
4154
4155/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004156 * Renegotiate current connection on client,
4157 * or request renegotiation on server
4158 */
4159int ssl_renegotiate( ssl_context *ssl )
4160{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004161 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004162
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004163#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004164 /* On server, just send the request */
4165 if( ssl->endpoint == SSL_IS_SERVER )
4166 {
4167 if( ssl->state != SSL_HANDSHAKE_OVER )
4168 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4169
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004170 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004171 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004172#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004173
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004174#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004175 /*
4176 * On client, either start the renegotiation process or,
4177 * if already in progress, continue the handshake
4178 */
4179 if( ssl->renegotiation != SSL_RENEGOTIATION )
4180 {
4181 if( ssl->state != SSL_HANDSHAKE_OVER )
4182 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4183
4184 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4185 {
4186 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4187 return( ret );
4188 }
4189 }
4190 else
4191 {
4192 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4193 {
4194 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4195 return( ret );
4196 }
4197 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004198#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004199
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004200 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004201}
4202
4203/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004204 * Receive application data decrypted from the SSL layer
4205 */
Paul Bakker23986e52011-04-24 08:57:21 +00004206int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004207{
Paul Bakker23986e52011-04-24 08:57:21 +00004208 int ret;
4209 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004210
4211 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4212
4213 if( ssl->state != SSL_HANDSHAKE_OVER )
4214 {
4215 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4216 {
4217 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4218 return( ret );
4219 }
4220 }
4221
4222 if( ssl->in_offt == NULL )
4223 {
4224 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4225 {
Paul Bakker831a7552011-05-18 13:32:51 +00004226 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4227 return( 0 );
4228
Paul Bakker5121ce52009-01-03 21:22:43 +00004229 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4230 return( ret );
4231 }
4232
4233 if( ssl->in_msglen == 0 &&
4234 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4235 {
4236 /*
4237 * OpenSSL sends empty messages to randomize the IV
4238 */
4239 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4240 {
Paul Bakker831a7552011-05-18 13:32:51 +00004241 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4242 return( 0 );
4243
Paul Bakker5121ce52009-01-03 21:22:43 +00004244 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4245 return( ret );
4246 }
4247 }
4248
Paul Bakker48916f92012-09-16 19:57:18 +00004249 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4250 {
4251 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4252
4253 if( ssl->endpoint == SSL_IS_CLIENT &&
4254 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4255 ssl->in_hslen != 4 ) )
4256 {
4257 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4258 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4259 }
4260
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004261 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4262 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004263 ssl->allow_legacy_renegotiation ==
4264 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004265 {
4266 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4267
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004268#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004269 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004270 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004271 /*
4272 * SSLv3 does not have a "no_renegotiation" alert
4273 */
4274 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4275 return( ret );
4276 }
4277 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004278#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004279#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4280 defined(POLARSSL_SSL_PROTO_TLS1_2)
4281 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004282 {
4283 if( ( ret = ssl_send_alert_message( ssl,
4284 SSL_ALERT_LEVEL_WARNING,
4285 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4286 {
4287 return( ret );
4288 }
Paul Bakker48916f92012-09-16 19:57:18 +00004289 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004290 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004291#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4292 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004293 {
4294 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004295 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004296 }
Paul Bakker48916f92012-09-16 19:57:18 +00004297 }
4298 else
4299 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004300 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004301 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004302 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004303 return( ret );
4304 }
4305
4306 return( POLARSSL_ERR_NET_WANT_READ );
4307 }
4308 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004309 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4310 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004311 ssl->renego_records_seen++;
4312
4313 if( ssl->renego_max_records >= 0 &&
4314 ssl->renego_records_seen > ssl->renego_max_records )
4315 {
4316 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4317 "but not honored by client" ) );
4318 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4319 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004320 }
Paul Bakker48916f92012-09-16 19:57:18 +00004321 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 {
4323 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004324 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004325 }
4326
4327 ssl->in_offt = ssl->in_msg;
4328 }
4329
4330 n = ( len < ssl->in_msglen )
4331 ? len : ssl->in_msglen;
4332
4333 memcpy( buf, ssl->in_offt, n );
4334 ssl->in_msglen -= n;
4335
4336 if( ssl->in_msglen == 0 )
4337 /* all bytes consumed */
4338 ssl->in_offt = NULL;
4339 else
4340 /* more data available */
4341 ssl->in_offt += n;
4342
4343 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4344
Paul Bakker23986e52011-04-24 08:57:21 +00004345 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004346}
4347
4348/*
4349 * Send application data to be encrypted by the SSL layer
4350 */
Paul Bakker23986e52011-04-24 08:57:21 +00004351int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004352{
Paul Bakker23986e52011-04-24 08:57:21 +00004353 int ret;
4354 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004355 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004356
4357 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4358
4359 if( ssl->state != SSL_HANDSHAKE_OVER )
4360 {
4361 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4362 {
4363 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4364 return( ret );
4365 }
4366 }
4367
Paul Bakker05decb22013-08-15 13:33:48 +02004368#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004369 /*
4370 * Assume mfl_code is correct since it was checked when set
4371 */
4372 max_len = mfl_code_to_length[ssl->mfl_code];
4373
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004374 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004375 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004376 */
4377 if( ssl->session_out != NULL &&
4378 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4379 {
4380 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4381 }
Paul Bakker05decb22013-08-15 13:33:48 +02004382#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004383
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004384 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004385
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 if( ssl->out_left != 0 )
4387 {
4388 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4389 {
4390 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4391 return( ret );
4392 }
4393 }
Paul Bakker887bd502011-06-08 13:10:54 +00004394 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004395 {
Paul Bakker887bd502011-06-08 13:10:54 +00004396 ssl->out_msglen = n;
4397 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4398 memcpy( ssl->out_msg, buf, n );
4399
4400 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4401 {
4402 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4403 return( ret );
4404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004405 }
4406
4407 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4408
Paul Bakker23986e52011-04-24 08:57:21 +00004409 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004410}
4411
4412/*
4413 * Notify the peer that the connection is being closed
4414 */
4415int ssl_close_notify( ssl_context *ssl )
4416{
4417 int ret;
4418
4419 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4420
4421 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4422 {
4423 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4424 return( ret );
4425 }
4426
4427 if( ssl->state == SSL_HANDSHAKE_OVER )
4428 {
Paul Bakker48916f92012-09-16 19:57:18 +00004429 if( ( ret = ssl_send_alert_message( ssl,
4430 SSL_ALERT_LEVEL_WARNING,
4431 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004433 return( ret );
4434 }
4435 }
4436
4437 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4438
4439 return( ret );
4440}
4441
Paul Bakker48916f92012-09-16 19:57:18 +00004442void ssl_transform_free( ssl_transform *transform )
4443{
4444#if defined(POLARSSL_ZLIB_SUPPORT)
4445 deflateEnd( &transform->ctx_deflate );
4446 inflateEnd( &transform->ctx_inflate );
4447#endif
4448
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004449 cipher_free_ctx( &transform->cipher_ctx_enc );
4450 cipher_free_ctx( &transform->cipher_ctx_dec );
4451
Paul Bakker61d113b2013-07-04 11:51:43 +02004452 md_free_ctx( &transform->md_ctx_enc );
4453 md_free_ctx( &transform->md_ctx_dec );
4454
Paul Bakker34617722014-06-13 17:20:13 +02004455 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004456}
4457
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004458#if defined(POLARSSL_X509_CRT_PARSE_C)
4459static void ssl_key_cert_free( ssl_key_cert *key_cert )
4460{
4461 ssl_key_cert *cur = key_cert, *next;
4462
4463 while( cur != NULL )
4464 {
4465 next = cur->next;
4466
4467 if( cur->key_own_alloc )
4468 {
4469 pk_free( cur->key );
4470 polarssl_free( cur->key );
4471 }
4472 polarssl_free( cur );
4473
4474 cur = next;
4475 }
4476}
4477#endif /* POLARSSL_X509_CRT_PARSE_C */
4478
Paul Bakker48916f92012-09-16 19:57:18 +00004479void ssl_handshake_free( ssl_handshake_params *handshake )
4480{
4481#if defined(POLARSSL_DHM_C)
4482 dhm_free( &handshake->dhm_ctx );
4483#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004484#if defined(POLARSSL_ECDH_C)
4485 ecdh_free( &handshake->ecdh_ctx );
4486#endif
4487
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004488#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004489 /* explicit void pointer cast for buggy MS compiler */
4490 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004491#endif
4492
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004493#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4494 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4495 /*
4496 * Free only the linked list wrapper, not the keys themselves
4497 * since the belong to the SNI callback
4498 */
4499 if( handshake->sni_key_cert != NULL )
4500 {
4501 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4502
4503 while( cur != NULL )
4504 {
4505 next = cur->next;
4506 polarssl_free( cur );
4507 cur = next;
4508 }
4509 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004510#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004511
Paul Bakker34617722014-06-13 17:20:13 +02004512 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004513}
4514
4515void ssl_session_free( ssl_session *session )
4516{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004517#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004518 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004519 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004520 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004521 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004522 }
Paul Bakkered27a042013-04-18 22:46:23 +02004523#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004524
Paul Bakkera503a632013-08-14 13:48:06 +02004525#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004526 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004527#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004528
Paul Bakker34617722014-06-13 17:20:13 +02004529 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004530}
4531
Paul Bakker5121ce52009-01-03 21:22:43 +00004532/*
4533 * Free an SSL context
4534 */
4535void ssl_free( ssl_context *ssl )
4536{
4537 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4538
Paul Bakker5121ce52009-01-03 21:22:43 +00004539 if( ssl->out_ctr != NULL )
4540 {
Paul Bakker34617722014-06-13 17:20:13 +02004541 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004542 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004543 }
4544
4545 if( ssl->in_ctr != NULL )
4546 {
Paul Bakker34617722014-06-13 17:20:13 +02004547 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004548 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004549 }
4550
Paul Bakker16770332013-10-11 09:59:44 +02004551#if defined(POLARSSL_ZLIB_SUPPORT)
4552 if( ssl->compress_buf != NULL )
4553 {
Paul Bakker34617722014-06-13 17:20:13 +02004554 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004555 polarssl_free( ssl->compress_buf );
4556 }
4557#endif
4558
Paul Bakker40e46942009-01-03 21:51:57 +00004559#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004560 mpi_free( &ssl->dhm_P );
4561 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004562#endif
4563
Paul Bakker48916f92012-09-16 19:57:18 +00004564 if( ssl->transform )
4565 {
4566 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004567 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004568 }
4569
4570 if( ssl->handshake )
4571 {
4572 ssl_handshake_free( ssl->handshake );
4573 ssl_transform_free( ssl->transform_negotiate );
4574 ssl_session_free( ssl->session_negotiate );
4575
Paul Bakker6e339b52013-07-03 13:37:05 +02004576 polarssl_free( ssl->handshake );
4577 polarssl_free( ssl->transform_negotiate );
4578 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004579 }
4580
Paul Bakkerc0463502013-02-14 11:19:38 +01004581 if( ssl->session )
4582 {
4583 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004584 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004585 }
4586
Paul Bakkera503a632013-08-14 13:48:06 +02004587#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004588 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004589#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004590
Paul Bakker0be444a2013-08-27 21:55:01 +02004591#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004592 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004593 {
Paul Bakker34617722014-06-13 17:20:13 +02004594 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004595 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004596 ssl->hostname_len = 0;
4597 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004598#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004599
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004600#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004601 if( ssl->psk != NULL )
4602 {
Paul Bakker34617722014-06-13 17:20:13 +02004603 polarssl_zeroize( ssl->psk, ssl->psk_len );
4604 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004605 polarssl_free( ssl->psk );
4606 polarssl_free( ssl->psk_identity );
4607 ssl->psk_len = 0;
4608 ssl->psk_identity_len = 0;
4609 }
4610#endif
4611
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004612#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004613 ssl_key_cert_free( ssl->key_cert );
4614#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004615
Paul Bakker05ef8352012-05-08 09:17:57 +00004616#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4617 if( ssl_hw_record_finish != NULL )
4618 {
4619 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4620 ssl_hw_record_finish( ssl );
4621 }
4622#endif
4623
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004625
Paul Bakker86f04f42013-02-14 11:20:09 +01004626 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004627 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004628}
4629
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004630#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004631/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004632 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004633 */
4634unsigned char ssl_sig_from_pk( pk_context *pk )
4635{
4636#if defined(POLARSSL_RSA_C)
4637 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4638 return( SSL_SIG_RSA );
4639#endif
4640#if defined(POLARSSL_ECDSA_C)
4641 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4642 return( SSL_SIG_ECDSA );
4643#endif
4644 return( SSL_SIG_ANON );
4645}
4646
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004647pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4648{
4649 switch( sig )
4650 {
4651#if defined(POLARSSL_RSA_C)
4652 case SSL_SIG_RSA:
4653 return( POLARSSL_PK_RSA );
4654#endif
4655#if defined(POLARSSL_ECDSA_C)
4656 case SSL_SIG_ECDSA:
4657 return( POLARSSL_PK_ECDSA );
4658#endif
4659 default:
4660 return( POLARSSL_PK_NONE );
4661 }
4662}
Paul Bakker9af723c2014-05-01 13:03:14 +02004663#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004664
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004665/*
4666 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4667 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004668md_type_t ssl_md_alg_from_hash( unsigned char hash )
4669{
4670 switch( hash )
4671 {
4672#if defined(POLARSSL_MD5_C)
4673 case SSL_HASH_MD5:
4674 return( POLARSSL_MD_MD5 );
4675#endif
4676#if defined(POLARSSL_SHA1_C)
4677 case SSL_HASH_SHA1:
4678 return( POLARSSL_MD_SHA1 );
4679#endif
4680#if defined(POLARSSL_SHA256_C)
4681 case SSL_HASH_SHA224:
4682 return( POLARSSL_MD_SHA224 );
4683 case SSL_HASH_SHA256:
4684 return( POLARSSL_MD_SHA256 );
4685#endif
4686#if defined(POLARSSL_SHA512_C)
4687 case SSL_HASH_SHA384:
4688 return( POLARSSL_MD_SHA384 );
4689 case SSL_HASH_SHA512:
4690 return( POLARSSL_MD_SHA512 );
4691#endif
4692 default:
4693 return( POLARSSL_MD_NONE );
4694 }
4695}
4696
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004697#if defined(POLARSSL_SSL_SET_CURVES)
4698/*
4699 * Check is a curve proposed by the peer is in our list.
4700 * Return 1 if we're willing to use it, 0 otherwise.
4701 */
4702int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4703{
4704 const ecp_group_id *gid;
4705
4706 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4707 if( *gid == grp_id )
4708 return( 1 );
4709
4710 return( 0 );
4711}
Paul Bakker9af723c2014-05-01 13:03:14 +02004712#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004713
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004714#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004715int ssl_check_cert_usage( const x509_crt *cert,
4716 const ssl_ciphersuite_t *ciphersuite,
4717 int cert_endpoint )
4718{
4719#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4720 int usage = 0;
4721#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004722#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4723 const char *ext_oid;
4724 size_t ext_len;
4725#endif
4726
4727#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4728 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4729 ((void) cert);
4730 ((void) cert_endpoint);
4731#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004732
4733#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4734 if( cert_endpoint == SSL_IS_SERVER )
4735 {
4736 /* Server part of the key exchange */
4737 switch( ciphersuite->key_exchange )
4738 {
4739 case POLARSSL_KEY_EXCHANGE_RSA:
4740 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4741 usage = KU_KEY_ENCIPHERMENT;
4742 break;
4743
4744 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4745 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4746 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4747 usage = KU_DIGITAL_SIGNATURE;
4748 break;
4749
4750 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4751 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4752 usage = KU_KEY_AGREEMENT;
4753 break;
4754
4755 /* Don't use default: we want warnings when adding new values */
4756 case POLARSSL_KEY_EXCHANGE_NONE:
4757 case POLARSSL_KEY_EXCHANGE_PSK:
4758 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4759 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4760 usage = 0;
4761 }
4762 }
4763 else
4764 {
4765 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4766 usage = KU_DIGITAL_SIGNATURE;
4767 }
4768
4769 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4770 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004771#else
4772 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004773#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4774
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004775#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4776 if( cert_endpoint == SSL_IS_SERVER )
4777 {
4778 ext_oid = OID_SERVER_AUTH;
4779 ext_len = OID_SIZE( OID_SERVER_AUTH );
4780 }
4781 else
4782 {
4783 ext_oid = OID_CLIENT_AUTH;
4784 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4785 }
4786
4787 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4788 return( -1 );
4789#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4790
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004791 return( 0 );
4792}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004793#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004794
4795#endif /* POLARSSL_SSL_TLS_C */