blob: ac2aca75c7f426b349e71c92383c74c3a7acf543 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
159 /*
160 * SSLv3:
161 * block =
162 * MD5( secret + SHA1( 'A' + secret + random ) ) +
163 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
164 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
165 * ...
166 */
167 for( i = 0; i < dlen / 16; i++ )
168 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200169 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000170
171 sha1_starts( &sha1 );
172 sha1_update( &sha1, padding, 1 + i );
173 sha1_update( &sha1, secret, slen );
174 sha1_update( &sha1, random, rlen );
175 sha1_finish( &sha1, sha1sum );
176
177 md5_starts( &md5 );
178 md5_update( &md5, secret, slen );
179 md5_update( &md5, sha1sum, 20 );
180 md5_finish( &md5, dstbuf + i * 16 );
181 }
182
Paul Bakker34617722014-06-13 17:20:13 +0200183 polarssl_zeroize( &md5, sizeof( md5 ) );
184 polarssl_zeroize( &sha1, sizeof( sha1 ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000185
Paul Bakker34617722014-06-13 17:20:13 +0200186 polarssl_zeroize( padding, sizeof( padding ) );
187 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
189 return( 0 );
190}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200191#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000192
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200193#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200194static int tls1_prf( const unsigned char *secret, size_t slen,
195 const char *label,
196 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000197 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
Paul Bakker23986e52011-04-24 08:57:21 +0000199 size_t nb, hs;
200 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200201 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 unsigned char tmp[128];
203 unsigned char h_i[20];
204
205 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000206 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 hs = ( slen + 1 ) / 2;
209 S1 = secret;
210 S2 = secret + slen - hs;
211
212 nb = strlen( label );
213 memcpy( tmp + 20, label, nb );
214 memcpy( tmp + 20 + nb, random, rlen );
215 nb += rlen;
216
217 /*
218 * First compute P_md5(secret,label+random)[0..dlen]
219 */
220 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
221
222 for( i = 0; i < dlen; i += 16 )
223 {
224 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
225 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
226
227 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
228
229 for( j = 0; j < k; j++ )
230 dstbuf[i + j] = h_i[j];
231 }
232
233 /*
234 * XOR out with P_sha1(secret,label+random)[0..dlen]
235 */
236 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
237
238 for( i = 0; i < dlen; i += 20 )
239 {
240 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
241 sha1_hmac( S2, hs, tmp, 20, tmp );
242
243 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
244
245 for( j = 0; j < k; j++ )
246 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
247 }
248
Paul Bakker34617722014-06-13 17:20:13 +0200249 polarssl_zeroize( tmp, sizeof( tmp ) );
250 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252 return( 0 );
253}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200254#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200256#if defined(POLARSSL_SSL_PROTO_TLS1_2)
257#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200258static int tls_prf_sha256( const unsigned char *secret, size_t slen,
259 const char *label,
260 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000261 unsigned char *dstbuf, size_t dlen )
262{
263 size_t nb;
264 size_t i, j, k;
265 unsigned char tmp[128];
266 unsigned char h_i[32];
267
268 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
270
271 nb = strlen( label );
272 memcpy( tmp + 32, label, nb );
273 memcpy( tmp + 32 + nb, random, rlen );
274 nb += rlen;
275
276 /*
277 * Compute P_<hash>(secret, label + random)[0..dlen]
278 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200279 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000280
281 for( i = 0; i < dlen; i += 32 )
282 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200283 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
284 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000285
286 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
287
288 for( j = 0; j < k; j++ )
289 dstbuf[i + j] = h_i[j];
290 }
291
Paul Bakker34617722014-06-13 17:20:13 +0200292 polarssl_zeroize( tmp, sizeof( tmp ) );
293 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000294
295 return( 0 );
296}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200297#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000298
Paul Bakker9e36f042013-06-30 14:34:05 +0200299#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200300static int tls_prf_sha384( const unsigned char *secret, size_t slen,
301 const char *label,
302 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000303 unsigned char *dstbuf, size_t dlen )
304{
305 size_t nb;
306 size_t i, j, k;
307 unsigned char tmp[128];
308 unsigned char h_i[48];
309
310 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
311 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
312
313 nb = strlen( label );
314 memcpy( tmp + 48, label, nb );
315 memcpy( tmp + 48 + nb, random, rlen );
316 nb += rlen;
317
318 /*
319 * Compute P_<hash>(secret, label + random)[0..dlen]
320 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200321 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000322
323 for( i = 0; i < dlen; i += 48 )
324 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200325 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
326 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000327
328 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
329
330 for( j = 0; j < k; j++ )
331 dstbuf[i + j] = h_i[j];
332 }
333
Paul Bakker34617722014-06-13 17:20:13 +0200334 polarssl_zeroize( tmp, sizeof( tmp ) );
335 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336
337 return( 0 );
338}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200339#endif /* POLARSSL_SHA512_C */
340#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000341
Paul Bakker66d5d072014-06-17 16:39:18 +0200342static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343
344#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
345 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200346static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
Paul Bakker380da532012-04-18 16:10:25 +0000348
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200349#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200350static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
351static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif
353
354#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200355static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
356static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200357#endif
358
359#if defined(POLARSSL_SSL_PROTO_TLS1_2)
360#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200361static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
362static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
363static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100365
Paul Bakker9e36f042013-06-30 14:34:05 +0200366#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200367static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
368static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
369static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100370#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200371#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000372
Paul Bakker5121ce52009-01-03 21:22:43 +0000373int ssl_derive_keys( ssl_context *ssl )
374{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200375 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char keyblk[256];
378 unsigned char *key1;
379 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100380 unsigned char *mac_enc;
381 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200382 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 const cipher_info_t *cipher_info;
384 const md_info_t *md_info;
385
Paul Bakker48916f92012-09-16 19:57:18 +0000386 ssl_session *session = ssl->session_negotiate;
387 ssl_transform *transform = ssl->transform_negotiate;
388 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
391
Paul Bakker68884e32013-01-07 18:20:04 +0100392 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
393 if( cipher_info == NULL )
394 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200395 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100396 transform->ciphersuite_info->cipher ) );
397 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
398 }
399
400 md_info = md_info_from_type( transform->ciphersuite_info->mac );
401 if( md_info == NULL )
402 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200403 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100404 transform->ciphersuite_info->mac ) );
405 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
406 }
407
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000409 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000410 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200411#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000412 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413 {
Paul Bakker48916f92012-09-16 19:57:18 +0000414 handshake->tls_prf = ssl3_prf;
415 handshake->calc_verify = ssl_calc_verify_ssl;
416 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200418 else
419#endif
420#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
421 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000422 {
Paul Bakker48916f92012-09-16 19:57:18 +0000423 handshake->tls_prf = tls1_prf;
424 handshake->calc_verify = ssl_calc_verify_tls;
425 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000426 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200427 else
428#endif
429#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200430#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
432 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000433 {
Paul Bakker48916f92012-09-16 19:57:18 +0000434 handshake->tls_prf = tls_prf_sha384;
435 handshake->calc_verify = ssl_calc_verify_tls_sha384;
436 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000438 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif
440#if defined(POLARSSL_SHA256_C)
441 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000442 {
Paul Bakker48916f92012-09-16 19:57:18 +0000443 handshake->tls_prf = tls_prf_sha256;
444 handshake->calc_verify = ssl_calc_verify_tls_sha256;
445 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000446 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447 else
448#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200449#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200450 {
451 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200452 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200453 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000454
455 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 * SSLv3:
457 * master =
458 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
459 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200461 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200462 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 * master = PRF( premaster, "master secret", randbytes )[0..47]
464 */
Paul Bakker0a597072012-09-25 21:55:46 +0000465 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 {
Paul Bakker48916f92012-09-16 19:57:18 +0000467 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
468 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
Paul Bakker48916f92012-09-16 19:57:18 +0000470 handshake->tls_prf( handshake->premaster, handshake->pmslen,
471 "master secret",
472 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Paul Bakker34617722014-06-13 17:20:13 +0200474 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 }
476 else
477 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
478
479 /*
480 * Swap the client and server random values.
481 */
Paul Bakker48916f92012-09-16 19:57:18 +0000482 memcpy( tmp, handshake->randbytes, 64 );
483 memcpy( handshake->randbytes, tmp + 32, 32 );
484 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200485 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487 /*
488 * SSLv3:
489 * key block =
490 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
491 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
492 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
493 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
494 * ...
495 *
496 * TLSv1:
497 * key block = PRF( master, "key expansion", randbytes )
498 */
Paul Bakker48916f92012-09-16 19:57:18 +0000499 handshake->tls_prf( session->master, 48, "key expansion",
500 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
Paul Bakker48916f92012-09-16 19:57:18 +0000502 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
503 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
504 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
505 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
507
Paul Bakker34617722014-06-13 17:20:13 +0200508 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 /*
511 * Determine the appropriate key, IV and MAC length.
512 */
Paul Bakker68884e32013-01-07 18:20:04 +0100513
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200514 transform->keylen = cipher_info->key_length / 8;
515
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200516 if( cipher_info->mode == POLARSSL_MODE_GCM ||
517 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200519 transform->maclen = 0;
520
Paul Bakker68884e32013-01-07 18:20:04 +0100521 transform->ivlen = 12;
522 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200523
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200524 /* Minimum length is expicit IV + tag */
525 transform->minlen = transform->ivlen - transform->fixed_ivlen
526 + ( transform->ciphersuite_info->flags &
527 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100528 }
529 else
530 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200531 int ret;
532
533 /* Initialize HMAC contexts */
534 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
535 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100536 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200537 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
538 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100539 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200541 /* Get MAC length */
542 transform->maclen = md_get_size( md_info );
543
544#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
545 /*
546 * If HMAC is to be truncated, we shall keep the leftmost bytes,
547 * (rfc 6066 page 13 or rfc 2104 section 4),
548 * so we only need to adjust the length here.
549 */
550 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
551 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
552#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
553
554 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100555 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200557 /* Minimum length */
558 if( cipher_info->mode == POLARSSL_MODE_STREAM )
559 transform->minlen = transform->maclen;
560 else
Paul Bakker68884e32013-01-07 18:20:04 +0100561 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200562 /*
563 * GenericBlockCipher:
564 * first multiple of blocklen greater than maclen
565 * + IV except for SSL3 and TLS 1.0
566 */
567 transform->minlen = transform->maclen
568 + cipher_info->block_size
569 - transform->maclen % cipher_info->block_size;
570
571#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
572 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
573 ssl->minor_ver == SSL_MINOR_VERSION_1 )
574 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100575 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200576#endif
577#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
578 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
579 ssl->minor_ver == SSL_MINOR_VERSION_3 )
580 {
581 transform->minlen += transform->ivlen;
582 }
583 else
584#endif
585 {
586 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
587 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
588 }
Paul Bakker68884e32013-01-07 18:20:04 +0100589 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 }
591
592 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000593 transform->keylen, transform->minlen, transform->ivlen,
594 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
596 /*
597 * Finally setup the cipher contexts, IVs and MAC secrets.
598 */
599 if( ssl->endpoint == SSL_IS_CLIENT )
600 {
Paul Bakker48916f92012-09-16 19:57:18 +0000601 key1 = keyblk + transform->maclen * 2;
602 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
Paul Bakker68884e32013-01-07 18:20:04 +0100604 mac_enc = keyblk;
605 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000607 /*
608 * This is not used in TLS v1.1.
609 */
Paul Bakker48916f92012-09-16 19:57:18 +0000610 iv_copy_len = ( transform->fixed_ivlen ) ?
611 transform->fixed_ivlen : transform->ivlen;
612 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
613 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000614 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 }
616 else
617 {
Paul Bakker48916f92012-09-16 19:57:18 +0000618 key1 = keyblk + transform->maclen * 2 + transform->keylen;
619 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Paul Bakker68884e32013-01-07 18:20:04 +0100621 mac_enc = keyblk + transform->maclen;
622 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000624 /*
625 * This is not used in TLS v1.1.
626 */
Paul Bakker48916f92012-09-16 19:57:18 +0000627 iv_copy_len = ( transform->fixed_ivlen ) ?
628 transform->fixed_ivlen : transform->ivlen;
629 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
630 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000631 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 }
633
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200634#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100635 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
636 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100637 if( transform->maclen > sizeof transform->mac_enc )
638 {
639 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200640 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100641 }
642
Paul Bakker68884e32013-01-07 18:20:04 +0100643 memcpy( transform->mac_enc, mac_enc, transform->maclen );
644 memcpy( transform->mac_dec, mac_dec, transform->maclen );
645 }
646 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200647#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200648#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
649 defined(POLARSSL_SSL_PROTO_TLS1_2)
650 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100651 {
652 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
653 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
654 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200655 else
656#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200657 {
658 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200659 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200660 }
Paul Bakker68884e32013-01-07 18:20:04 +0100661
Paul Bakker05ef8352012-05-08 09:17:57 +0000662#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200663 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000664 {
665 int ret = 0;
666
667 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
668
Paul Bakker07eb38b2012-12-19 14:42:06 +0100669 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
670 transform->iv_enc, transform->iv_dec,
671 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100672 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100673 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000674 {
675 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200676 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000677 }
678 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200679#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000680
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200681 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
682 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200684 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
685 return( ret );
686 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200687
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200688 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
689 cipher_info ) ) != 0 )
690 {
691 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
692 return( ret );
693 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200694
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200695 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
696 cipher_info->key_length,
697 POLARSSL_ENCRYPT ) ) != 0 )
698 {
699 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
700 return( ret );
701 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200702
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200703 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
704 cipher_info->key_length,
705 POLARSSL_DECRYPT ) ) != 0 )
706 {
707 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
708 return( ret );
709 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200710
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200711#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200712 if( cipher_info->mode == POLARSSL_MODE_CBC )
713 {
714 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
715 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200716 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200717 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
718 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200719 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200720
721 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
722 POLARSSL_PADDING_NONE ) ) != 0 )
723 {
724 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
725 return( ret );
726 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200728#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Paul Bakker34617722014-06-13 17:20:13 +0200730 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Paul Bakker2770fbd2012-07-03 13:30:23 +0000732#if defined(POLARSSL_ZLIB_SUPPORT)
733 // Initialize compression
734 //
Paul Bakker48916f92012-09-16 19:57:18 +0000735 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000736 {
Paul Bakker16770332013-10-11 09:59:44 +0200737 if( ssl->compress_buf == NULL )
738 {
739 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
740 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
741 if( ssl->compress_buf == NULL )
742 {
743 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
744 SSL_BUFFER_LEN ) );
745 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
746 }
747 }
748
Paul Bakker2770fbd2012-07-03 13:30:23 +0000749 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
750
Paul Bakker48916f92012-09-16 19:57:18 +0000751 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
752 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000753
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200754 if( deflateInit( &transform->ctx_deflate,
755 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000756 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000757 {
758 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
759 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
760 }
761 }
762#endif /* POLARSSL_ZLIB_SUPPORT */
763
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
765
766 return( 0 );
767}
768
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200769#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000770void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000771{
772 md5_context md5;
773 sha1_context sha1;
774 unsigned char pad_1[48];
775 unsigned char pad_2[48];
776
Paul Bakker380da532012-04-18 16:10:25 +0000777 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Paul Bakker48916f92012-09-16 19:57:18 +0000779 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
780 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
Paul Bakker380da532012-04-18 16:10:25 +0000782 memset( pad_1, 0x36, 48 );
783 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Paul Bakker48916f92012-09-16 19:57:18 +0000785 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000786 md5_update( &md5, pad_1, 48 );
787 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker380da532012-04-18 16:10:25 +0000789 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000790 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000791 md5_update( &md5, pad_2, 48 );
792 md5_update( &md5, hash, 16 );
793 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Paul Bakker48916f92012-09-16 19:57:18 +0000795 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000796 sha1_update( &sha1, pad_1, 40 );
797 sha1_finish( &sha1, hash + 16 );
798
799 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000800 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000801 sha1_update( &sha1, pad_2, 40 );
802 sha1_update( &sha1, hash + 16, 20 );
803 sha1_finish( &sha1, hash + 16 );
804
805 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
806 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
807
808 return;
809}
Paul Bakker9af723c2014-05-01 13:03:14 +0200810#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000811
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200812#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000813void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
814{
815 md5_context md5;
816 sha1_context sha1;
817
818 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
819
Paul Bakker48916f92012-09-16 19:57:18 +0000820 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
821 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000822
Paul Bakker48916f92012-09-16 19:57:18 +0000823 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000824 sha1_finish( &sha1, hash + 16 );
825
826 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
827 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
828
829 return;
830}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200831#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000832
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200833#if defined(POLARSSL_SSL_PROTO_TLS1_2)
834#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000835void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
836{
Paul Bakker9e36f042013-06-30 14:34:05 +0200837 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000838
839 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
840
Paul Bakker9e36f042013-06-30 14:34:05 +0200841 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
842 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000843
844 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
845 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
846
847 return;
848}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200849#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000850
Paul Bakker9e36f042013-06-30 14:34:05 +0200851#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000852void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
853{
Paul Bakker9e36f042013-06-30 14:34:05 +0200854 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000855
856 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
857
Paul Bakker9e36f042013-06-30 14:34:05 +0200858 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
859 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakkerca4ab492012-04-18 14:23:57 +0000861 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
863
864 return;
865}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200866#endif /* POLARSSL_SHA512_C */
867#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000868
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200869#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200870int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
871{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200872 unsigned char *p = ssl->handshake->premaster;
873 unsigned char *end = p + sizeof( ssl->handshake->premaster );
874
875 /*
876 * PMS = struct {
877 * opaque other_secret<0..2^16-1>;
878 * opaque psk<0..2^16-1>;
879 * };
880 * with "other_secret" depending on the particular key exchange
881 */
882#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
883 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
884 {
885 if( end - p < 2 + (int) ssl->psk_len )
886 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
887
888 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
889 *(p++) = (unsigned char)( ssl->psk_len );
890 p += ssl->psk_len;
891 }
892 else
893#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200894#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
895 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
896 {
897 /*
898 * other_secret already set by the ClientKeyExchange message,
899 * and is 48 bytes long
900 */
901 *p++ = 0;
902 *p++ = 48;
903 p += 48;
904 }
905 else
906#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200907#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
908 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
909 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200910 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200911 size_t len = ssl->handshake->dhm_ctx.len;
912
913 if( end - p < 2 + (int) len )
914 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
915
916 *(p++) = (unsigned char)( len >> 8 );
917 *(p++) = (unsigned char)( len );
918 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
919 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
920 {
921 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
922 return( ret );
923 }
924 p += len;
925
926 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
927 }
928 else
929#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
930#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
931 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
932 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200933 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200934 size_t zlen;
935
936 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200937 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200938 ssl->f_rng, ssl->p_rng ) ) != 0 )
939 {
940 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
941 return( ret );
942 }
943
944 *(p++) = (unsigned char)( zlen >> 8 );
945 *(p++) = (unsigned char)( zlen );
946 p += zlen;
947
948 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
949 }
950 else
951#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
952 {
953 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200954 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200955 }
956
957 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100958 if( end - p < 2 + (int) ssl->psk_len )
959 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
960
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200961 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
962 *(p++) = (unsigned char)( ssl->psk_len );
963 memcpy( p, ssl->psk, ssl->psk_len );
964 p += ssl->psk_len;
965
966 ssl->handshake->pmslen = p - ssl->handshake->premaster;
967
968 return( 0 );
969}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200970#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200971
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200972#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000973/*
974 * SSLv3.0 MAC functions
975 */
Paul Bakker68884e32013-01-07 18:20:04 +0100976static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
977 unsigned char *buf, size_t len,
978 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000979{
980 unsigned char header[11];
981 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100982 int padlen = 0;
983 int md_size = md_get_size( md_ctx->md_info );
984 int md_type = md_get_type( md_ctx->md_info );
985
986 if( md_type == POLARSSL_MD_MD5 )
987 padlen = 48;
988 else if( md_type == POLARSSL_MD_SHA1 )
989 padlen = 40;
990 else if( md_type == POLARSSL_MD_SHA256 )
991 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100992 else if( md_type == POLARSSL_MD_SHA384 )
993 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000994
995 memcpy( header, ctr, 8 );
996 header[ 8] = (unsigned char) type;
997 header[ 9] = (unsigned char)( len >> 8 );
998 header[10] = (unsigned char)( len );
999
Paul Bakker68884e32013-01-07 18:20:04 +01001000 memset( padding, 0x36, padlen );
1001 md_starts( md_ctx );
1002 md_update( md_ctx, secret, md_size );
1003 md_update( md_ctx, padding, padlen );
1004 md_update( md_ctx, header, 11 );
1005 md_update( md_ctx, buf, len );
1006 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
Paul Bakker68884e32013-01-07 18:20:04 +01001008 memset( padding, 0x5C, padlen );
1009 md_starts( md_ctx );
1010 md_update( md_ctx, secret, md_size );
1011 md_update( md_ctx, padding, padlen );
1012 md_update( md_ctx, buf + len, md_size );
1013 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001014}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001015#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001016
Paul Bakker5121ce52009-01-03 21:22:43 +00001017/*
1018 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001019 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001020static int ssl_encrypt_buf( ssl_context *ssl )
1021{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001022 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001023 const cipher_mode_t mode = cipher_get_cipher_mode(
1024 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001025
1026 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1027
1028 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001029 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001031#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1032 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1033 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001034 if( mode != POLARSSL_MODE_GCM &&
1035 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001037#if defined(POLARSSL_SSL_PROTO_SSL3)
1038 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1039 {
1040 ssl_mac( &ssl->transform_out->md_ctx_enc,
1041 ssl->transform_out->mac_enc,
1042 ssl->out_msg, ssl->out_msglen,
1043 ssl->out_ctr, ssl->out_msgtype );
1044 }
1045 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001046#endif
1047#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001048 defined(POLARSSL_SSL_PROTO_TLS1_2)
1049 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1050 {
1051 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1052 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1053 ssl->out_msg, ssl->out_msglen );
1054 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1055 ssl->out_msg + ssl->out_msglen );
1056 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1057 }
1058 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001059#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001060 {
1061 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001062 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001063 }
1064
1065 SSL_DEBUG_BUF( 4, "computed mac",
1066 ssl->out_msg + ssl->out_msglen,
1067 ssl->transform_out->maclen );
1068
1069 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001070 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001071#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001073 /*
1074 * Encrypt
1075 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001076#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001077 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001079 int ret;
1080 size_t olen = 0;
1081
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1083 "including %d bytes of padding",
1084 ssl->out_msglen, 0 ) );
1085
1086 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1087 ssl->out_msg, ssl->out_msglen );
1088
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001089 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001090 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001091 ssl->transform_out->ivlen,
1092 ssl->out_msg, ssl->out_msglen,
1093 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001094 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001095 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001096 return( ret );
1097 }
1098
1099 if( ssl->out_msglen != olen )
1100 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001101 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001102 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001103 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 }
Paul Bakker68884e32013-01-07 18:20:04 +01001105 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001106#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001107#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1108 if( mode == POLARSSL_MODE_GCM ||
1109 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001110 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001111 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001112 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001113 unsigned char *enc_msg;
1114 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001115 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1116 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001117
Paul Bakkerca4ab492012-04-18 14:23:57 +00001118 memcpy( add_data, ssl->out_ctr, 8 );
1119 add_data[8] = ssl->out_msgtype;
1120 add_data[9] = ssl->major_ver;
1121 add_data[10] = ssl->minor_ver;
1122 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1123 add_data[12] = ssl->out_msglen & 0xFF;
1124
1125 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1126 add_data, 13 );
1127
Paul Bakker68884e32013-01-07 18:20:04 +01001128 /*
1129 * Generate IV
1130 */
1131 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001132 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1133 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001134 if( ret != 0 )
1135 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001136
Paul Bakker68884e32013-01-07 18:20:04 +01001137 memcpy( ssl->out_iv,
1138 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1139 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001140
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001141 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001142 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001143
Paul Bakker68884e32013-01-07 18:20:04 +01001144 /*
1145 * Fix pointer positions and message length with added IV
1146 */
1147 enc_msg = ssl->out_msg;
1148 enc_msglen = ssl->out_msglen;
1149 ssl->out_msglen += ssl->transform_out->ivlen -
1150 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001151
Paul Bakker68884e32013-01-07 18:20:04 +01001152 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1153 "including %d bytes of padding",
1154 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001155
Paul Bakker68884e32013-01-07 18:20:04 +01001156 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1157 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001158
Paul Bakker68884e32013-01-07 18:20:04 +01001159 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001160 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001161 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001162 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1163 ssl->transform_out->iv_enc,
1164 ssl->transform_out->ivlen,
1165 add_data, 13,
1166 enc_msg, enc_msglen,
1167 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001168 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001169 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001170 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001171 return( ret );
1172 }
1173
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001174 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001175 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001176 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001177 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001178 }
1179
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001180 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001181
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001182 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001185#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001186#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1187 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001188 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001190 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001191 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001192 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001193
Paul Bakker48916f92012-09-16 19:57:18 +00001194 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1195 ssl->transform_out->ivlen;
1196 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 padlen = 0;
1198
1199 for( i = 0; i <= padlen; i++ )
1200 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1201
1202 ssl->out_msglen += padlen + 1;
1203
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001204 enc_msglen = ssl->out_msglen;
1205 enc_msg = ssl->out_msg;
1206
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001207#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001208 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001209 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1210 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001211 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001212 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 {
1214 /*
1215 * Generate IV
1216 */
Paul Bakker48916f92012-09-16 19:57:18 +00001217 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1218 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001219 if( ret != 0 )
1220 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001221
Paul Bakker92be97b2013-01-02 17:30:03 +01001222 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001223 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001224
1225 /*
1226 * Fix pointer positions and message length with added IV
1227 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001228 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001229 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001230 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001231 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001232#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001236 ssl->out_msglen, ssl->transform_out->ivlen,
1237 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
1239 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001240 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001242 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001243 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001244 ssl->transform_out->ivlen,
1245 enc_msg, enc_msglen,
1246 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001247 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001248 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001249 return( ret );
1250 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001251
Paul Bakkercca5b812013-08-31 17:40:26 +02001252 if( enc_msglen != olen )
1253 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001254 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001255 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001256 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001257
1258#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001259 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1260 {
1261 /*
1262 * Save IV in SSL3 and TLS1
1263 */
1264 memcpy( ssl->transform_out->iv_enc,
1265 ssl->transform_out->cipher_ctx_enc.iv,
1266 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001268#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001270 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001271#endif /* POLARSSL_CIPHER_MODE_CBC &&
1272 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001273 {
1274 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001275 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001276 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
Paul Bakkerca4ab492012-04-18 14:23:57 +00001278 for( i = 8; i > 0; i-- )
1279 if( ++ssl->out_ctr[i - 1] != 0 )
1280 break;
1281
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001282 /* The loops goes to its end iff the counter is wrapping */
1283 if( i == 0 )
1284 {
1285 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1286 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1287 }
1288
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1290
1291 return( 0 );
1292}
1293
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001294#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001295
Paul Bakker5121ce52009-01-03 21:22:43 +00001296static int ssl_decrypt_buf( ssl_context *ssl )
1297{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001298 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001299 const cipher_mode_t mode = cipher_get_cipher_mode(
1300 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001301#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1302 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1303 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1304 size_t padlen = 0, correct = 1;
1305#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
1307 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1308
Paul Bakker48916f92012-09-16 19:57:18 +00001309 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 {
1311 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001312 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001313 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 }
1315
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001316#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001317 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001318 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001319 int ret;
1320 size_t olen = 0;
1321
Paul Bakker68884e32013-01-07 18:20:04 +01001322 padlen = 0;
1323
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001324 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001325 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001326 ssl->transform_in->ivlen,
1327 ssl->in_msg, ssl->in_msglen,
1328 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001329 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001330 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001331 return( ret );
1332 }
1333
1334 if( ssl->in_msglen != olen )
1335 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001336 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001337 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
Paul Bakker68884e32013-01-07 18:20:04 +01001340 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001341#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001342#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1343 if( mode == POLARSSL_MODE_GCM ||
1344 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001345 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001346 int ret;
1347 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001348 unsigned char *dec_msg;
1349 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001350 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001351 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1352 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001353 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1354 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001355
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001356 if( ssl->in_msglen < explicit_iv_len + taglen )
1357 {
1358 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1359 "+ taglen (%d)", ssl->in_msglen,
1360 explicit_iv_len, taglen ) );
1361 return( POLARSSL_ERR_SSL_INVALID_MAC );
1362 }
1363 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1364
Paul Bakker68884e32013-01-07 18:20:04 +01001365 dec_msg = ssl->in_msg;
1366 dec_msg_result = ssl->in_msg;
1367 ssl->in_msglen = dec_msglen;
1368
1369 memcpy( add_data, ssl->in_ctr, 8 );
1370 add_data[8] = ssl->in_msgtype;
1371 add_data[9] = ssl->major_ver;
1372 add_data[10] = ssl->minor_ver;
1373 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1374 add_data[12] = ssl->in_msglen & 0xFF;
1375
1376 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1377 add_data, 13 );
1378
1379 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1380 ssl->in_iv,
1381 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1382
1383 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1384 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001385 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001386
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001387 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001388 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001389 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001390 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1391 ssl->transform_in->iv_dec,
1392 ssl->transform_in->ivlen,
1393 add_data, 13,
1394 dec_msg, dec_msglen,
1395 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001396 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001397 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001398 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1399
1400 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1401 return( POLARSSL_ERR_SSL_INVALID_MAC );
1402
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001403 return( ret );
1404 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001405
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001406 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001407 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001408 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001409 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001410 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001413#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001414#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1415 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001416 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 {
Paul Bakker45829992013-01-03 14:52:21 +01001418 /*
1419 * Decrypt and check the padding
1420 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001421 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001422 unsigned char *dec_msg;
1423 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001424 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001425 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001426 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001427
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 /*
Paul Bakker45829992013-01-03 14:52:21 +01001429 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 */
Paul Bakker48916f92012-09-16 19:57:18 +00001431 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 {
1433 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001434 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001435 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 }
1437
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001438#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001439 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1440 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001441#endif
Paul Bakker45829992013-01-03 14:52:21 +01001442
1443 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1444 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1445 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001446 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1447 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1448 ssl->transform_in->ivlen,
1449 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001450 return( POLARSSL_ERR_SSL_INVALID_MAC );
1451 }
1452
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001453 dec_msglen = ssl->in_msglen;
1454 dec_msg = ssl->in_msg;
1455 dec_msg_result = ssl->in_msg;
1456
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001457#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001458 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001459 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001460 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001461 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001462 {
Paul Bakker48916f92012-09-16 19:57:18 +00001463 dec_msglen -= ssl->transform_in->ivlen;
1464 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001465
Paul Bakker48916f92012-09-16 19:57:18 +00001466 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001467 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001468 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001469#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001470
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001471 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001472 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001473 ssl->transform_in->ivlen,
1474 dec_msg, dec_msglen,
1475 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001476 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001477 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001478 return( ret );
1479 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001480
Paul Bakkercca5b812013-08-31 17:40:26 +02001481 if( dec_msglen != olen )
1482 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001483 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001484 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001485 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001486
1487#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001488 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1489 {
1490 /*
1491 * Save IV in SSL3 and TLS1
1492 */
1493 memcpy( ssl->transform_in->iv_dec,
1494 ssl->transform_in->cipher_ctx_dec.iv,
1495 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001498
1499 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001500
1501 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1502 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001503#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001504 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1505 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001506#endif
Paul Bakker45829992013-01-03 14:52:21 +01001507 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001508 correct = 0;
1509 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001511#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1513 {
Paul Bakker48916f92012-09-16 19:57:18 +00001514 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001515 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001516#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001517 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1518 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001519 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001520#endif
Paul Bakker45829992013-01-03 14:52:21 +01001521 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 }
1523 }
1524 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001525#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001526#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1527 defined(POLARSSL_SSL_PROTO_TLS1_2)
1528 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001529 {
1530 /*
Paul Bakker45829992013-01-03 14:52:21 +01001531 * TLSv1+: always check the padding up to the first failure
1532 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001534 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001535 size_t padding_idx = ssl->in_msglen - padlen - 1;
1536
Paul Bakker956c9e02013-12-19 14:42:28 +01001537 /*
1538 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001539 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001540 *
Paul Bakker61885c72014-04-25 12:59:03 +02001541 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1542 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001543 *
1544 * In both cases we reset padding_idx to a safe value (0) to
1545 * prevent out-of-buffer reads.
1546 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001547 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001548 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1549 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001550
1551 padding_idx *= correct;
1552
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001553 for( i = 1; i <= 256; i++ )
1554 {
1555 real_count &= ( i <= padlen );
1556 pad_count += real_count *
1557 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1558 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001559
1560 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001561
Paul Bakkerd66f0702013-01-31 16:57:45 +01001562#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001563 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001564 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001565#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001566 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001568 else
1569#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1570 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001571 {
1572 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001573 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001576 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001577#endif /* POLARSSL_CIPHER_MODE_CBC &&
1578 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001579 {
1580 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001581 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583
1584 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1585 ssl->in_msg, ssl->in_msglen );
1586
1587 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001588 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001590#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1591 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1592 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001593 if( mode != POLARSSL_MODE_GCM &&
1594 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001595 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001596 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1597
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001598 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001599
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001600 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1601 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001602
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001603 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001605#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001606 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1607 {
1608 ssl_mac( &ssl->transform_in->md_ctx_dec,
1609 ssl->transform_in->mac_dec,
1610 ssl->in_msg, ssl->in_msglen,
1611 ssl->in_ctr, ssl->in_msgtype );
1612 }
1613 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001614#endif /* POLARSSL_SSL_PROTO_SSL3 */
1615#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001616 defined(POLARSSL_SSL_PROTO_TLS1_2)
1617 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1618 {
1619 /*
1620 * Process MAC and always update for padlen afterwards to make
1621 * total time independent of padlen
1622 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001623 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001624 *
1625 * Known timing attacks:
1626 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1627 *
1628 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1629 * correctly. (We round down instead of up, so -56 is the correct
1630 * value for our calculations instead of -55)
1631 */
1632 size_t j, extra_run = 0;
1633 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1634 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001635
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001636 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001637
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001638 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1639 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1640 ssl->in_msglen );
1641 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1642 ssl->in_msg + ssl->in_msglen );
1643 for( j = 0; j < extra_run; j++ )
1644 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001645
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001646 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1647 }
1648 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001649#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001650 POLARSSL_SSL_PROTO_TLS1_2 */
1651 {
1652 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001653 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001656 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1657 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1658 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001660 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001661 ssl->transform_in->maclen ) != 0 )
1662 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001663#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001664 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001665#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001666 correct = 0;
1667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001669 /*
1670 * Finally check the correct flag
1671 */
1672 if( correct == 0 )
1673 return( POLARSSL_ERR_SSL_INVALID_MAC );
1674 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001675#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
1677 if( ssl->in_msglen == 0 )
1678 {
1679 ssl->nb_zero++;
1680
1681 /*
1682 * Three or more empty messages may be a DoS attack
1683 * (excessive CPU consumption).
1684 */
1685 if( ssl->nb_zero > 3 )
1686 {
1687 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1688 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001689 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690 }
1691 }
1692 else
1693 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001694
Paul Bakker23986e52011-04-24 08:57:21 +00001695 for( i = 8; i > 0; i-- )
1696 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 break;
1698
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001699 /* The loops goes to its end iff the counter is wrapping */
1700 if( i == 0 )
1701 {
1702 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1703 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1704 }
1705
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1707
1708 return( 0 );
1709}
1710
Paul Bakker2770fbd2012-07-03 13:30:23 +00001711#if defined(POLARSSL_ZLIB_SUPPORT)
1712/*
1713 * Compression/decompression functions
1714 */
1715static int ssl_compress_buf( ssl_context *ssl )
1716{
1717 int ret;
1718 unsigned char *msg_post = ssl->out_msg;
1719 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001720 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001721
1722 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1723
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001724 if( len_pre == 0 )
1725 return( 0 );
1726
Paul Bakker2770fbd2012-07-03 13:30:23 +00001727 memcpy( msg_pre, ssl->out_msg, len_pre );
1728
1729 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1730 ssl->out_msglen ) );
1731
1732 SSL_DEBUG_BUF( 4, "before compression: output payload",
1733 ssl->out_msg, ssl->out_msglen );
1734
Paul Bakker48916f92012-09-16 19:57:18 +00001735 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1736 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1737 ssl->transform_out->ctx_deflate.next_out = msg_post;
1738 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001739
Paul Bakker48916f92012-09-16 19:57:18 +00001740 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001741 if( ret != Z_OK )
1742 {
1743 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1744 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1745 }
1746
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001747 ssl->out_msglen = SSL_BUFFER_LEN -
1748 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001749
Paul Bakker2770fbd2012-07-03 13:30:23 +00001750 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1751 ssl->out_msglen ) );
1752
1753 SSL_DEBUG_BUF( 4, "after compression: output payload",
1754 ssl->out_msg, ssl->out_msglen );
1755
1756 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1757
1758 return( 0 );
1759}
1760
1761static int ssl_decompress_buf( ssl_context *ssl )
1762{
1763 int ret;
1764 unsigned char *msg_post = ssl->in_msg;
1765 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001766 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001767
1768 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1769
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001770 if( len_pre == 0 )
1771 return( 0 );
1772
Paul Bakker2770fbd2012-07-03 13:30:23 +00001773 memcpy( msg_pre, ssl->in_msg, len_pre );
1774
1775 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1776 ssl->in_msglen ) );
1777
1778 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1779 ssl->in_msg, ssl->in_msglen );
1780
Paul Bakker48916f92012-09-16 19:57:18 +00001781 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1782 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1783 ssl->transform_in->ctx_inflate.next_out = msg_post;
1784 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001785
Paul Bakker48916f92012-09-16 19:57:18 +00001786 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001787 if( ret != Z_OK )
1788 {
1789 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1790 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1791 }
1792
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001793 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1794 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795
Paul Bakker2770fbd2012-07-03 13:30:23 +00001796 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1797 ssl->in_msglen ) );
1798
1799 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1800 ssl->in_msg, ssl->in_msglen );
1801
1802 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1803
1804 return( 0 );
1805}
1806#endif /* POLARSSL_ZLIB_SUPPORT */
1807
Paul Bakker5121ce52009-01-03 21:22:43 +00001808/*
1809 * Fill the input message buffer
1810 */
Paul Bakker23986e52011-04-24 08:57:21 +00001811int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001812{
Paul Bakker23986e52011-04-24 08:57:21 +00001813 int ret;
1814 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001815
1816 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1817
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001818 if( nb_want > SSL_BUFFER_LEN - 8 )
1819 {
1820 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1821 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1822 }
1823
Paul Bakker5121ce52009-01-03 21:22:43 +00001824 while( ssl->in_left < nb_want )
1825 {
1826 len = nb_want - ssl->in_left;
1827 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1828
1829 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1830 ssl->in_left, nb_want ) );
1831 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1832
Paul Bakker831a7552011-05-18 13:32:51 +00001833 if( ret == 0 )
1834 return( POLARSSL_ERR_SSL_CONN_EOF );
1835
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 if( ret < 0 )
1837 return( ret );
1838
1839 ssl->in_left += ret;
1840 }
1841
1842 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1843
1844 return( 0 );
1845}
1846
1847/*
1848 * Flush any data not yet written
1849 */
1850int ssl_flush_output( ssl_context *ssl )
1851{
1852 int ret;
1853 unsigned char *buf;
1854
1855 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1856
1857 while( ssl->out_left > 0 )
1858 {
1859 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1860 5 + ssl->out_msglen, ssl->out_left ) );
1861
Paul Bakker5bd42292012-12-19 14:40:42 +01001862 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001864
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1866
1867 if( ret <= 0 )
1868 return( ret );
1869
1870 ssl->out_left -= ret;
1871 }
1872
1873 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1874
1875 return( 0 );
1876}
1877
1878/*
1879 * Record layer functions
1880 */
1881int ssl_write_record( ssl_context *ssl )
1882{
Paul Bakker05ef8352012-05-08 09:17:57 +00001883 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001884 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
1886 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1887
Paul Bakker5121ce52009-01-03 21:22:43 +00001888 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1889 {
1890 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1891 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1892 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1893
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001894 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1895 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 }
1897
Paul Bakker2770fbd2012-07-03 13:30:23 +00001898#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001899 if( ssl->transform_out != NULL &&
1900 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001901 {
1902 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1903 {
1904 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1905 return( ret );
1906 }
1907
1908 len = ssl->out_msglen;
1909 }
1910#endif /*POLARSSL_ZLIB_SUPPORT */
1911
Paul Bakker05ef8352012-05-08 09:17:57 +00001912#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001913 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001915 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001916
Paul Bakker05ef8352012-05-08 09:17:57 +00001917 ret = ssl_hw_record_write( ssl );
1918 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1919 {
1920 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001921 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001922 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001923
1924 if( ret == 0 )
1925 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001926 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001927#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001928 if( !done )
1929 {
1930 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1931 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1932 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1934 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001935
Paul Bakker48916f92012-09-16 19:57:18 +00001936 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001937 {
1938 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1939 {
1940 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1941 return( ret );
1942 }
1943
1944 len = ssl->out_msglen;
1945 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1946 ssl->out_hdr[4] = (unsigned char)( len );
1947 }
1948
1949 ssl->out_left = 5 + ssl->out_msglen;
1950
1951 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1952 "version = [%d:%d], msglen = %d",
1953 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1954 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1955
1956 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001957 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 }
1959
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1961 {
1962 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1963 return( ret );
1964 }
1965
1966 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1967
1968 return( 0 );
1969}
1970
1971int ssl_read_record( ssl_context *ssl )
1972{
Paul Bakker05ef8352012-05-08 09:17:57 +00001973 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001974
1975 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1976
Paul Bakker68884e32013-01-07 18:20:04 +01001977 SSL_DEBUG_BUF( 4, "input record from network",
1978 ssl->in_hdr, 5 + ssl->in_msglen );
1979
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 if( ssl->in_hslen != 0 &&
1981 ssl->in_hslen < ssl->in_msglen )
1982 {
1983 /*
1984 * Get next Handshake message in the current record
1985 */
1986 ssl->in_msglen -= ssl->in_hslen;
1987
Paul Bakker8934a982011-08-05 11:11:53 +00001988 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001989 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001990
1991 ssl->in_hslen = 4;
1992 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1993
1994 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1995 " %d, type = %d, hslen = %d",
1996 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1997
1998 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1999 {
2000 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002001 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 }
2003
2004 if( ssl->in_msglen < ssl->in_hslen )
2005 {
2006 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002007 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 }
2009
Paul Bakker4224bc02014-04-08 14:36:50 +02002010 if( ssl->state != SSL_HANDSHAKE_OVER )
2011 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002012
2013 return( 0 );
2014 }
2015
2016 ssl->in_hslen = 0;
2017
2018 /*
2019 * Read the record header and validate it
2020 */
2021 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2022 {
2023 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2024 return( ret );
2025 }
2026
2027 ssl->in_msgtype = ssl->in_hdr[0];
2028 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2029
2030 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2031 "version = [%d:%d], msglen = %d",
2032 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2033 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2034
2035 if( ssl->in_hdr[1] != ssl->major_ver )
2036 {
2037 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002038 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 }
2040
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002041 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 {
2043 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002044 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 }
2046
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002047 /* Sanity check (outer boundaries) */
2048 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2049 {
2050 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2051 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2052 }
2053
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002055 * Make sure the message length is acceptable for the current transform
2056 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 */
Paul Bakker48916f92012-09-16 19:57:18 +00002058 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002060 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 {
2062 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002063 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 }
2065 }
2066 else
2067 {
Paul Bakker48916f92012-09-16 19:57:18 +00002068 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 {
2070 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002071 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 }
2073
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002074#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002076 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 {
2078 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002079 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002082
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002083#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2084 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 /*
2086 * TLS encrypted messages can have up to 256 bytes of padding
2087 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002088 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002089 ssl->in_msglen > ssl->transform_in->minlen +
2090 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 {
2092 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002093 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002095#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 }
2097
2098 /*
2099 * Read and optionally decrypt the message contents
2100 */
2101 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2102 {
2103 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2104 return( ret );
2105 }
2106
2107 SSL_DEBUG_BUF( 4, "input record from network",
2108 ssl->in_hdr, 5 + ssl->in_msglen );
2109
Paul Bakker05ef8352012-05-08 09:17:57 +00002110#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002111 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002112 {
2113 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2114
2115 ret = ssl_hw_record_read( ssl );
2116 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2117 {
2118 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002119 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002120 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002121
2122 if( ret == 0 )
2123 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002124 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002125#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002126 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 {
2128 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2129 {
Paul Bakker40865c82013-01-31 17:13:13 +01002130#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2131 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2132 {
2133 ssl_send_alert_message( ssl,
2134 SSL_ALERT_LEVEL_FATAL,
2135 SSL_ALERT_MSG_BAD_RECORD_MAC );
2136 }
2137#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2139 return( ret );
2140 }
2141
2142 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2143 ssl->in_msg, ssl->in_msglen );
2144
2145 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2146 {
2147 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002148 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 }
2150 }
2151
Paul Bakker2770fbd2012-07-03 13:30:23 +00002152#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002153 if( ssl->transform_in != NULL &&
2154 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002155 {
2156 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2157 {
2158 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2159 return( ret );
2160 }
2161
2162 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2163 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2164 }
2165#endif /* POLARSSL_ZLIB_SUPPORT */
2166
Paul Bakker0a925182012-04-16 06:46:41 +00002167 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2168 ssl->in_msgtype != SSL_MSG_ALERT &&
2169 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2170 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2171 {
2172 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2173
Paul Bakker48916f92012-09-16 19:57:18 +00002174 if( ( ret = ssl_send_alert_message( ssl,
2175 SSL_ALERT_LEVEL_FATAL,
2176 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002177 {
2178 return( ret );
2179 }
2180
2181 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2182 }
2183
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2185 {
2186 ssl->in_hslen = 4;
2187 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2188
2189 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2190 " %d, type = %d, hslen = %d",
2191 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2192
2193 /*
2194 * Additional checks to validate the handshake header
2195 */
2196 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2197 {
2198 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002199 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 }
2201
2202 if( ssl->in_msglen < ssl->in_hslen )
2203 {
2204 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002205 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
2207
Paul Bakker48916f92012-09-16 19:57:18 +00002208 if( ssl->state != SSL_HANDSHAKE_OVER )
2209 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 }
2211
2212 if( ssl->in_msgtype == SSL_MSG_ALERT )
2213 {
2214 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2215 ssl->in_msg[0], ssl->in_msg[1] ) );
2216
2217 /*
2218 * Ignore non-fatal alerts, except close_notify
2219 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002220 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002222 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2223 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002224 /**
2225 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2226 * error identifier.
2227 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002228 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 }
2230
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002231 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2232 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 {
2234 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002235 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 }
2237 }
2238
2239 ssl->in_left = 0;
2240
2241 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2242
2243 return( 0 );
2244}
2245
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002246int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2247{
2248 int ret;
2249
2250 if( ( ret = ssl_send_alert_message( ssl,
2251 SSL_ALERT_LEVEL_FATAL,
2252 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2253 {
2254 return( ret );
2255 }
2256
2257 return( 0 );
2258}
2259
Paul Bakker0a925182012-04-16 06:46:41 +00002260int ssl_send_alert_message( ssl_context *ssl,
2261 unsigned char level,
2262 unsigned char message )
2263{
2264 int ret;
2265
2266 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2267
2268 ssl->out_msgtype = SSL_MSG_ALERT;
2269 ssl->out_msglen = 2;
2270 ssl->out_msg[0] = level;
2271 ssl->out_msg[1] = message;
2272
2273 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2274 {
2275 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2276 return( ret );
2277 }
2278
2279 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2280
2281 return( 0 );
2282}
2283
Paul Bakker5121ce52009-01-03 21:22:43 +00002284/*
2285 * Handshake functions
2286 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002287#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2288 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2289 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2290 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2291 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2292 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2293 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002294int ssl_write_certificate( ssl_context *ssl )
2295{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002296 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
2298 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2299
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002300 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002301 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2302 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002303 {
2304 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2305 ssl->state++;
2306 return( 0 );
2307 }
2308
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002309 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2310 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002311}
2312
2313int ssl_parse_certificate( ssl_context *ssl )
2314{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002315 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2316
2317 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2318
2319 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002320 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2321 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002322 {
2323 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2324 ssl->state++;
2325 return( 0 );
2326 }
2327
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002328 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2329 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002330}
2331#else
2332int ssl_write_certificate( ssl_context *ssl )
2333{
2334 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2335 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002336 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002337 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2338
2339 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2340
2341 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002342 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2343 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002344 {
2345 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2346 ssl->state++;
2347 return( 0 );
2348 }
2349
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 if( ssl->endpoint == SSL_IS_CLIENT )
2351 {
2352 if( ssl->client_auth == 0 )
2353 {
2354 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2355 ssl->state++;
2356 return( 0 );
2357 }
2358
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002359#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002360 /*
2361 * If using SSLv3 and got no cert, send an Alert message
2362 * (otherwise an empty Certificate message will be sent).
2363 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002364 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2366 {
2367 ssl->out_msglen = 2;
2368 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002369 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2370 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002371
2372 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2373 goto write_msg;
2374 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002375#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 }
2377 else /* SSL_IS_SERVER */
2378 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002379 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 {
2381 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002382 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 }
2384 }
2385
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002386 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
2388 /*
2389 * 0 . 0 handshake type
2390 * 1 . 3 handshake length
2391 * 4 . 6 length of all certs
2392 * 7 . 9 length of cert. 1
2393 * 10 . n-1 peer certificate
2394 * n . n+2 length of cert. 2
2395 * n+3 . ... upper level cert, etc.
2396 */
2397 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002398 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
Paul Bakker29087132010-03-21 21:03:34 +00002400 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 {
2402 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002403 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 {
2405 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2406 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002407 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 }
2409
2410 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2411 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2412 ssl->out_msg[i + 2] = (unsigned char)( n );
2413
2414 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2415 i += n; crt = crt->next;
2416 }
2417
2418 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2419 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2420 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2421
2422 ssl->out_msglen = i;
2423 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2424 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2425
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002426#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002427write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002428#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
2430 ssl->state++;
2431
2432 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2433 {
2434 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2435 return( ret );
2436 }
2437
2438 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2439
Paul Bakkered27a042013-04-18 22:46:23 +02002440 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441}
2442
2443int ssl_parse_certificate( ssl_context *ssl )
2444{
Paul Bakkered27a042013-04-18 22:46:23 +02002445 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002446 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002447 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
2449 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2450
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002451 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002452 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2453 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002454 {
2455 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2456 ssl->state++;
2457 return( 0 );
2458 }
2459
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002461 ( ssl->authmode == SSL_VERIFY_NONE ||
2462 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002464 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2466 ssl->state++;
2467 return( 0 );
2468 }
2469
2470 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2471 {
2472 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2473 return( ret );
2474 }
2475
2476 ssl->state++;
2477
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002478#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 /*
2480 * Check if the client sent an empty certificate
2481 */
2482 if( ssl->endpoint == SSL_IS_SERVER &&
2483 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2484 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002485 if( ssl->in_msglen == 2 &&
2486 ssl->in_msgtype == SSL_MSG_ALERT &&
2487 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2488 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 {
2490 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2491
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002492 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2494 return( 0 );
2495 else
Paul Bakker40e46942009-01-03 21:51:57 +00002496 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 }
2498 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002499#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002501#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2502 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 if( ssl->endpoint == SSL_IS_SERVER &&
2504 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2505 {
2506 if( ssl->in_hslen == 7 &&
2507 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2508 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2509 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2510 {
2511 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2512
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002513 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002515 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 else
2517 return( 0 );
2518 }
2519 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002520#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2521 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
2523 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2524 {
2525 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002526 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 }
2528
2529 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2530 {
2531 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002532 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 }
2534
2535 /*
2536 * Same message structure as in ssl_write_certificate()
2537 */
2538 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2539
2540 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2541 {
2542 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002543 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 }
2545
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002546 /* In case we tried to reuse a session but it failed */
2547 if( ssl->session_negotiate->peer_cert != NULL )
2548 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002549 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002550 polarssl_free( ssl->session_negotiate->peer_cert );
2551 }
2552
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002553 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2554 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 {
2556 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002557 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002558 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 }
2560
Paul Bakkerb6b09562013-09-18 14:17:41 +02002561 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
2563 i = 7;
2564
2565 while( i < ssl->in_hslen )
2566 {
2567 if( ssl->in_msg[i] != 0 )
2568 {
2569 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002570 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 }
2572
2573 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2574 | (unsigned int) ssl->in_msg[i + 2];
2575 i += 3;
2576
2577 if( n < 128 || i + n > ssl->in_hslen )
2578 {
2579 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002580 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 }
2582
Paul Bakkerddf26b42013-09-18 13:46:23 +02002583 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2584 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 if( ret != 0 )
2586 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002587 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 return( ret );
2589 }
2590
2591 i += n;
2592 }
2593
Paul Bakker48916f92012-09-16 19:57:18 +00002594 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002596 /*
2597 * On client, make sure the server cert doesn't change during renego to
2598 * avoid "triple handshake" attack: https://secure-resumption.com/
2599 */
2600 if( ssl->endpoint == SSL_IS_CLIENT &&
2601 ssl->renegotiation == SSL_RENEGOTIATION )
2602 {
2603 if( ssl->session->peer_cert == NULL )
2604 {
2605 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2606 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2607 }
2608
2609 if( ssl->session->peer_cert->raw.len !=
2610 ssl->session_negotiate->peer_cert->raw.len ||
2611 memcmp( ssl->session->peer_cert->raw.p,
2612 ssl->session_negotiate->peer_cert->raw.p,
2613 ssl->session->peer_cert->raw.len ) != 0 )
2614 {
2615 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2616 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2617 }
2618 }
2619
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 if( ssl->authmode != SSL_VERIFY_NONE )
2621 {
2622 if( ssl->ca_chain == NULL )
2623 {
2624 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002625 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 }
2627
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002628 /*
2629 * Main check: verify certificate
2630 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002631 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2632 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2633 &ssl->session_negotiate->verify_result,
2634 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
2636 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002637 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002639 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002640
2641 /*
2642 * Secondary checks: always done, but change 'ret' only if it was 0
2643 */
2644
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002645#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002646 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002647 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002648
2649 /* If certificate uses an EC key, make sure the curve is OK */
2650 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2651 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2652 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002653 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002654 if( ret == 0 )
2655 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002656 }
2657 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002658#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002660 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2661 ciphersuite_info,
2662 ! ssl->endpoint ) != 0 )
2663 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002664 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002665 if( ret == 0 )
2666 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2667 }
2668
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2670 ret = 0;
2671 }
2672
2673 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2674
2675 return( ret );
2676}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002677#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2678 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2679 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2680 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2681 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2682 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2683 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
2685int ssl_write_change_cipher_spec( ssl_context *ssl )
2686{
2687 int ret;
2688
2689 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2690
2691 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2692 ssl->out_msglen = 1;
2693 ssl->out_msg[0] = 1;
2694
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 ssl->state++;
2696
2697 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2698 {
2699 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2700 return( ret );
2701 }
2702
2703 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2704
2705 return( 0 );
2706}
2707
2708int ssl_parse_change_cipher_spec( ssl_context *ssl )
2709{
2710 int ret;
2711
2712 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2713
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2715 {
2716 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2717 return( ret );
2718 }
2719
2720 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2721 {
2722 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002723 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 }
2725
2726 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2727 {
2728 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002729 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 }
2731
2732 ssl->state++;
2733
2734 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2735
2736 return( 0 );
2737}
2738
Paul Bakker41c83d32013-03-20 14:39:14 +01002739void ssl_optimize_checksum( ssl_context *ssl,
2740 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002741{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002742 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002743
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002744#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2745 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002746 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002747 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002748 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002749#endif
2750#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2751#if defined(POLARSSL_SHA512_C)
2752 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2753 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2754 else
2755#endif
2756#if defined(POLARSSL_SHA256_C)
2757 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002758 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002759 else
2760#endif
2761#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002762 {
2763 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002764 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002765 }
Paul Bakker380da532012-04-18 16:10:25 +00002766}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002767
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002768static void ssl_update_checksum_start( ssl_context *ssl,
2769 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002770{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002771#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2772 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002773 md5_update( &ssl->handshake->fin_md5 , buf, len );
2774 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002775#endif
2776#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2777#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002778 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002779#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002780#if defined(POLARSSL_SHA512_C)
2781 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002782#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002783#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002784}
2785
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002786#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2787 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002788static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2789 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002790{
Paul Bakker48916f92012-09-16 19:57:18 +00002791 md5_update( &ssl->handshake->fin_md5 , buf, len );
2792 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002793}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794#endif
Paul Bakker380da532012-04-18 16:10:25 +00002795
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002796#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2797#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002798static void ssl_update_checksum_sha256( ssl_context *ssl,
2799 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002800{
Paul Bakker9e36f042013-06-30 14:34:05 +02002801 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002802}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002803#endif
Paul Bakker380da532012-04-18 16:10:25 +00002804
Paul Bakker9e36f042013-06-30 14:34:05 +02002805#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002806static void ssl_update_checksum_sha384( ssl_context *ssl,
2807 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002808{
Paul Bakker9e36f042013-06-30 14:34:05 +02002809 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002810}
Paul Bakker769075d2012-11-24 11:26:46 +01002811#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002812#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002813
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002814#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002815static void ssl_calc_finished_ssl(
2816 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002817{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002818 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002819 md5_context md5;
2820 sha1_context sha1;
2821
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 unsigned char padbuf[48];
2823 unsigned char md5sum[16];
2824 unsigned char sha1sum[20];
2825
Paul Bakker48916f92012-09-16 19:57:18 +00002826 ssl_session *session = ssl->session_negotiate;
2827 if( !session )
2828 session = ssl->session;
2829
Paul Bakker1ef83d62012-04-11 12:09:53 +00002830 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2831
Paul Bakker48916f92012-09-16 19:57:18 +00002832 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2833 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
2835 /*
2836 * SSLv3:
2837 * hash =
2838 * MD5( master + pad2 +
2839 * MD5( handshake + sender + master + pad1 ) )
2840 * + SHA1( master + pad2 +
2841 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002842 */
2843
Paul Bakker90995b52013-06-24 19:20:35 +02002844#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002846 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002847#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
Paul Bakker90995b52013-06-24 19:20:35 +02002849#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002851 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002852#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Paul Bakker3c2122f2013-06-24 19:03:14 +02002854 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2855 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Paul Bakker1ef83d62012-04-11 12:09:53 +00002857 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Paul Bakker3c2122f2013-06-24 19:03:14 +02002859 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002860 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002861 md5_update( &md5, padbuf, 48 );
2862 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Paul Bakker3c2122f2013-06-24 19:03:14 +02002864 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002865 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002866 sha1_update( &sha1, padbuf, 40 );
2867 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002872 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873 md5_update( &md5, padbuf, 48 );
2874 md5_update( &md5, md5sum, 16 );
2875 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002876
Paul Bakker1ef83d62012-04-11 12:09:53 +00002877 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002878 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 sha1_update( &sha1, padbuf , 40 );
2880 sha1_update( &sha1, sha1sum, 20 );
2881 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Paul Bakker1ef83d62012-04-11 12:09:53 +00002883 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Paul Bakker34617722014-06-13 17:20:13 +02002885 polarssl_zeroize( &md5, sizeof( md5_context ) );
2886 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Paul Bakker34617722014-06-13 17:20:13 +02002888 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2889 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2890 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
2892 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2893}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002894#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002895
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002896#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002897static void ssl_calc_finished_tls(
2898 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002899{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002901 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002902 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002903 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002904 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Paul Bakker48916f92012-09-16 19:57:18 +00002906 ssl_session *session = ssl->session_negotiate;
2907 if( !session )
2908 session = ssl->session;
2909
Paul Bakker1ef83d62012-04-11 12:09:53 +00002910 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Paul Bakker48916f92012-09-16 19:57:18 +00002912 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2913 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Paul Bakker1ef83d62012-04-11 12:09:53 +00002915 /*
2916 * TLSv1:
2917 * hash = PRF( master, finished_label,
2918 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2919 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Paul Bakker90995b52013-06-24 19:20:35 +02002921#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002922 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2923 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002924#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925
Paul Bakker90995b52013-06-24 19:20:35 +02002926#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002927 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2928 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002929#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002930
2931 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002932 ? "client finished"
2933 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002934
2935 md5_finish( &md5, padbuf );
2936 sha1_finish( &sha1, padbuf + 16 );
2937
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002938 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002939 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002940
2941 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2942
Paul Bakker34617722014-06-13 17:20:13 +02002943 polarssl_zeroize( &md5, sizeof( md5_context ) );
2944 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945
Paul Bakker34617722014-06-13 17:20:13 +02002946 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002947
2948 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2949}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002950#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002952#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2953#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002954static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002955 ssl_context *ssl, unsigned char *buf, int from )
2956{
2957 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002958 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002959 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960 unsigned char padbuf[32];
2961
Paul Bakker48916f92012-09-16 19:57:18 +00002962 ssl_session *session = ssl->session_negotiate;
2963 if( !session )
2964 session = ssl->session;
2965
Paul Bakker380da532012-04-18 16:10:25 +00002966 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002967
Paul Bakker9e36f042013-06-30 14:34:05 +02002968 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969
2970 /*
2971 * TLSv1.2:
2972 * hash = PRF( master, finished_label,
2973 * Hash( handshake ) )[0.11]
2974 */
2975
Paul Bakker9e36f042013-06-30 14:34:05 +02002976#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002977 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002978 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002979#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002980
2981 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002982 ? "client finished"
2983 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002984
Paul Bakker9e36f042013-06-30 14:34:05 +02002985 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002987 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002988 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989
2990 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2991
Paul Bakker34617722014-06-13 17:20:13 +02002992 polarssl_zeroize( &sha256, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002993
Paul Bakker34617722014-06-13 17:20:13 +02002994 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995
2996 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2997}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002998#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002999
Paul Bakker9e36f042013-06-30 14:34:05 +02003000#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003001static void ssl_calc_finished_tls_sha384(
3002 ssl_context *ssl, unsigned char *buf, int from )
3003{
3004 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003005 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003006 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003007 unsigned char padbuf[48];
3008
Paul Bakker48916f92012-09-16 19:57:18 +00003009 ssl_session *session = ssl->session_negotiate;
3010 if( !session )
3011 session = ssl->session;
3012
Paul Bakker380da532012-04-18 16:10:25 +00003013 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003014
Paul Bakker9e36f042013-06-30 14:34:05 +02003015 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003016
3017 /*
3018 * TLSv1.2:
3019 * hash = PRF( master, finished_label,
3020 * Hash( handshake ) )[0.11]
3021 */
3022
Paul Bakker9e36f042013-06-30 14:34:05 +02003023#if !defined(POLARSSL_SHA512_ALT)
3024 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3025 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003026#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003027
3028 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003029 ? "client finished"
3030 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003031
Paul Bakker9e36f042013-06-30 14:34:05 +02003032 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003033
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003034 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003035 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003036
3037 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3038
Paul Bakker34617722014-06-13 17:20:13 +02003039 polarssl_zeroize( &sha512, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003040
Paul Bakker34617722014-06-13 17:20:13 +02003041 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003042
3043 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3044}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003045#endif /* POLARSSL_SHA512_C */
3046#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003047
Paul Bakker48916f92012-09-16 19:57:18 +00003048void ssl_handshake_wrapup( ssl_context *ssl )
3049{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003050 int resume = ssl->handshake->resume;
3051
Paul Bakker48916f92012-09-16 19:57:18 +00003052 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3053
3054 /*
3055 * Free our handshake params
3056 */
3057 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003058 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003059 ssl->handshake = NULL;
3060
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003061 if( ssl->renegotiation == SSL_RENEGOTIATION )
3062 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3063
Paul Bakker48916f92012-09-16 19:57:18 +00003064 /*
3065 * Switch in our now active transform context
3066 */
3067 if( ssl->transform )
3068 {
3069 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003070 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003071 }
3072 ssl->transform = ssl->transform_negotiate;
3073 ssl->transform_negotiate = NULL;
3074
Paul Bakker0a597072012-09-25 21:55:46 +00003075 if( ssl->session )
3076 {
3077 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003078 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003079 }
3080 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003081 ssl->session_negotiate = NULL;
3082
Paul Bakker0a597072012-09-25 21:55:46 +00003083 /*
3084 * Add cache entry
3085 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003086 if( ssl->f_set_cache != NULL &&
3087 ssl->session->length != 0 &&
3088 resume == 0 )
3089 {
Paul Bakker0a597072012-09-25 21:55:46 +00003090 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3091 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003092 }
Paul Bakker0a597072012-09-25 21:55:46 +00003093
Paul Bakker48916f92012-09-16 19:57:18 +00003094 ssl->state++;
3095
3096 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3097}
3098
Paul Bakker1ef83d62012-04-11 12:09:53 +00003099int ssl_write_finished( ssl_context *ssl )
3100{
3101 int ret, hash_len;
3102
3103 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3104
Paul Bakker92be97b2013-01-02 17:30:03 +01003105 /*
3106 * Set the out_msg pointer to the correct location based on IV length
3107 */
3108 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3109 {
3110 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3111 ssl->transform_negotiate->fixed_ivlen;
3112 }
3113 else
3114 ssl->out_msg = ssl->out_iv;
3115
Paul Bakker48916f92012-09-16 19:57:18 +00003116 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003117
3118 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003119 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3120
Paul Bakker48916f92012-09-16 19:57:18 +00003121 ssl->verify_data_len = hash_len;
3122 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3123
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 ssl->out_msglen = 4 + hash_len;
3125 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3126 ssl->out_msg[0] = SSL_HS_FINISHED;
3127
3128 /*
3129 * In case of session resuming, invert the client and server
3130 * ChangeCipherSpec messages order.
3131 */
Paul Bakker0a597072012-09-25 21:55:46 +00003132 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003133 {
3134 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003135 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003136 else
3137 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3138 }
3139 else
3140 ssl->state++;
3141
Paul Bakker48916f92012-09-16 19:57:18 +00003142 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003143 * Switch to our negotiated transform and session parameters for outbound
3144 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003145 */
3146 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3147 ssl->transform_out = ssl->transform_negotiate;
3148 ssl->session_out = ssl->session_negotiate;
3149 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
Paul Bakker07eb38b2012-12-19 14:42:06 +01003151#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003152 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003153 {
3154 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3155 {
3156 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3157 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3158 }
3159 }
3160#endif
3161
Paul Bakker5121ce52009-01-03 21:22:43 +00003162 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3163 {
3164 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3165 return( ret );
3166 }
3167
3168 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3169
3170 return( 0 );
3171}
3172
3173int ssl_parse_finished( ssl_context *ssl )
3174{
Paul Bakker23986e52011-04-24 08:57:21 +00003175 int ret;
3176 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003177 unsigned char buf[36];
3178
3179 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3180
Paul Bakker48916f92012-09-16 19:57:18 +00003181 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
Paul Bakker48916f92012-09-16 19:57:18 +00003183 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003184 * Switch to our negotiated transform and session parameters for inbound
3185 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003186 */
3187 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3188 ssl->transform_in = ssl->transform_negotiate;
3189 ssl->session_in = ssl->session_negotiate;
3190 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003191
Paul Bakker92be97b2013-01-02 17:30:03 +01003192 /*
3193 * Set the in_msg pointer to the correct location based on IV length
3194 */
3195 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3196 {
3197 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3198 ssl->transform_negotiate->fixed_ivlen;
3199 }
3200 else
3201 ssl->in_msg = ssl->in_iv;
3202
Paul Bakker07eb38b2012-12-19 14:42:06 +01003203#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003204 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003205 {
3206 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3207 {
3208 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3209 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3210 }
3211 }
3212#endif
3213
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3215 {
3216 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3217 return( ret );
3218 }
3219
3220 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3221 {
3222 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003223 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224 }
3225
Paul Bakker1ef83d62012-04-11 12:09:53 +00003226 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3228
3229 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3230 ssl->in_hslen != 4 + hash_len )
3231 {
3232 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003233 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003234 }
3235
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003236 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003237 {
3238 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003239 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003240 }
3241
Paul Bakker48916f92012-09-16 19:57:18 +00003242 ssl->verify_data_len = hash_len;
3243 memcpy( ssl->peer_verify_data, buf, hash_len );
3244
Paul Bakker0a597072012-09-25 21:55:46 +00003245 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003246 {
3247 if( ssl->endpoint == SSL_IS_CLIENT )
3248 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3249
3250 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003251 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 }
3253 else
3254 ssl->state++;
3255
3256 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3257
3258 return( 0 );
3259}
3260
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003261static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003262{
3263 if( ssl->transform_negotiate )
3264 ssl_transform_free( ssl->transform_negotiate );
3265 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003266 {
3267 ssl->transform_negotiate =
3268 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003269
3270 if( ssl->transform_negotiate != NULL )
3271 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003272 }
Paul Bakker48916f92012-09-16 19:57:18 +00003273
3274 if( ssl->session_negotiate )
3275 ssl_session_free( ssl->session_negotiate );
3276 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003277 {
3278 ssl->session_negotiate =
3279 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003280
3281 if( ssl->session_negotiate != NULL )
3282 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003283 }
Paul Bakker48916f92012-09-16 19:57:18 +00003284
3285 if( ssl->handshake )
3286 ssl_handshake_free( ssl->handshake );
3287 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003288 {
3289 ssl->handshake = (ssl_handshake_params *)
3290 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003291
3292 if( ssl->handshake != NULL )
3293 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003294 }
Paul Bakker48916f92012-09-16 19:57:18 +00003295
3296 if( ssl->handshake == NULL ||
3297 ssl->transform_negotiate == NULL ||
3298 ssl->session_negotiate == NULL )
3299 {
3300 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3301 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3302 }
3303
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003304#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3305 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003306 md5_starts( &ssl->handshake->fin_md5 );
3307 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003308#endif
3309#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3310#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003311 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003312#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003313#if defined(POLARSSL_SHA512_C)
3314 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003315#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003316#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003317
3318 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003319 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003320
Paul Bakker61d113b2013-07-04 11:51:43 +02003321#if defined(POLARSSL_ECDH_C)
3322 ecdh_init( &ssl->handshake->ecdh_ctx );
3323#endif
3324
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003325#if defined(POLARSSL_X509_CRT_PARSE_C)
3326 ssl->handshake->key_cert = ssl->key_cert;
3327#endif
3328
Paul Bakker48916f92012-09-16 19:57:18 +00003329 return( 0 );
3330}
3331
Paul Bakker5121ce52009-01-03 21:22:43 +00003332/*
3333 * Initialize an SSL context
3334 */
3335int ssl_init( ssl_context *ssl )
3336{
Paul Bakker48916f92012-09-16 19:57:18 +00003337 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003338 int len = SSL_BUFFER_LEN;
3339
3340 memset( ssl, 0, sizeof( ssl_context ) );
3341
Paul Bakker62f2dee2012-09-28 07:31:51 +00003342 /*
3343 * Sane defaults
3344 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003345 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3346 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3347 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3348 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003349
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003350 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003351
Paul Bakker62f2dee2012-09-28 07:31:51 +00003352#if defined(POLARSSL_DHM_C)
3353 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3354 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3355 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3356 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3357 {
3358 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3359 return( ret );
3360 }
3361#endif
3362
3363 /*
3364 * Prepare base structures
3365 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003366 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003367 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003368 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003369 ssl->in_msg = ssl->in_ctr + 13;
3370
3371 if( ssl->in_ctr == NULL )
3372 {
3373 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003374 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003375 }
3376
Paul Bakker6e339b52013-07-03 13:37:05 +02003377 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003378 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003379 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003380 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003381
3382 if( ssl->out_ctr == NULL )
3383 {
3384 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003385 polarssl_free( ssl->in_ctr );
3386 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003387 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 }
3389
3390 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3391 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3392
Paul Bakker606b4ba2013-08-14 16:52:14 +02003393#if defined(POLARSSL_SSL_SESSION_TICKETS)
3394 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3395#endif
3396
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003397#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003398 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003399#endif
3400
Paul Bakker48916f92012-09-16 19:57:18 +00003401 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3402 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003403
3404 return( 0 );
3405}
3406
3407/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003408 * Reset an initialized and used SSL context for re-use while retaining
3409 * all application-set variables, function pointers and data.
3410 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003411int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003412{
Paul Bakker48916f92012-09-16 19:57:18 +00003413 int ret;
3414
Paul Bakker7eb013f2011-10-06 12:37:39 +00003415 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003416 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3417 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3418
3419 ssl->verify_data_len = 0;
3420 memset( ssl->own_verify_data, 0, 36 );
3421 memset( ssl->peer_verify_data, 0, 36 );
3422
Paul Bakker7eb013f2011-10-06 12:37:39 +00003423 ssl->in_offt = NULL;
3424
Paul Bakker92be97b2013-01-02 17:30:03 +01003425 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003426 ssl->in_msgtype = 0;
3427 ssl->in_msglen = 0;
3428 ssl->in_left = 0;
3429
3430 ssl->in_hslen = 0;
3431 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003432 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003433
Paul Bakker92be97b2013-01-02 17:30:03 +01003434 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003435 ssl->out_msgtype = 0;
3436 ssl->out_msglen = 0;
3437 ssl->out_left = 0;
3438
Paul Bakker48916f92012-09-16 19:57:18 +00003439 ssl->transform_in = NULL;
3440 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003441
3442 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3443 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003444
3445#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003446 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003447 {
3448 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003449 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003450 {
3451 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3452 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3453 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003454 }
3455#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003456
Paul Bakker48916f92012-09-16 19:57:18 +00003457 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003458 {
Paul Bakker48916f92012-09-16 19:57:18 +00003459 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003460 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003461 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003462 }
Paul Bakker48916f92012-09-16 19:57:18 +00003463
Paul Bakkerc0463502013-02-14 11:19:38 +01003464 if( ssl->session )
3465 {
3466 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003467 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003468 ssl->session = NULL;
3469 }
3470
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003471#if defined(POLARSSL_SSL_ALPN)
3472 ssl->alpn_chosen = NULL;
3473#endif
3474
Paul Bakker48916f92012-09-16 19:57:18 +00003475 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3476 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003477
3478 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003479}
3480
Paul Bakkera503a632013-08-14 13:48:06 +02003481#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003482/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003483 * Allocate and initialize ticket keys
3484 */
3485static int ssl_ticket_keys_init( ssl_context *ssl )
3486{
3487 int ret;
3488 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003489 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003490
3491 if( ssl->ticket_keys != NULL )
3492 return( 0 );
3493
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003494 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3495 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003496 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3497
3498 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003499 {
3500 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003501 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003502 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003503
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003504 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3505 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3506 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3507 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003508 polarssl_free( tkeys );
3509 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003510 }
3511
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003512 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003513 {
3514 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003515 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003516 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003517
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003518 ssl->ticket_keys = tkeys;
3519
3520 return( 0 );
3521}
Paul Bakkera503a632013-08-14 13:48:06 +02003522#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003523
3524/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003525 * SSL set accessors
3526 */
3527void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3528{
3529 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003530
Paul Bakker606b4ba2013-08-14 16:52:14 +02003531#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003532 if( endpoint == SSL_IS_CLIENT )
3533 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003534#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003535}
3536
3537void ssl_set_authmode( ssl_context *ssl, int authmode )
3538{
3539 ssl->authmode = authmode;
3540}
3541
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003542#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003543void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003544 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003545 void *p_vrfy )
3546{
3547 ssl->f_vrfy = f_vrfy;
3548 ssl->p_vrfy = p_vrfy;
3549}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003550#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003551
Paul Bakker5121ce52009-01-03 21:22:43 +00003552void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003553 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003554 void *p_rng )
3555{
3556 ssl->f_rng = f_rng;
3557 ssl->p_rng = p_rng;
3558}
3559
3560void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003561 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 void *p_dbg )
3563{
3564 ssl->f_dbg = f_dbg;
3565 ssl->p_dbg = p_dbg;
3566}
3567
3568void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003569 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003570 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003571{
3572 ssl->f_recv = f_recv;
3573 ssl->f_send = f_send;
3574 ssl->p_recv = p_recv;
3575 ssl->p_send = p_send;
3576}
3577
Paul Bakker0a597072012-09-25 21:55:46 +00003578void ssl_set_session_cache( ssl_context *ssl,
3579 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3580 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003581{
Paul Bakker0a597072012-09-25 21:55:46 +00003582 ssl->f_get_cache = f_get_cache;
3583 ssl->p_get_cache = p_get_cache;
3584 ssl->f_set_cache = f_set_cache;
3585 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003586}
3587
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003588int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003589{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003590 int ret;
3591
3592 if( ssl == NULL ||
3593 session == NULL ||
3594 ssl->session_negotiate == NULL ||
3595 ssl->endpoint != SSL_IS_CLIENT )
3596 {
3597 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3598 }
3599
3600 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3601 return( ret );
3602
Paul Bakker0a597072012-09-25 21:55:46 +00003603 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003604
3605 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003606}
3607
Paul Bakkerb68cad62012-08-23 08:34:18 +00003608void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003609{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003610 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3611 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3612 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3613 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3614}
3615
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003616void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3617 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003618 int major, int minor )
3619{
3620 if( major != SSL_MAJOR_VERSION_3 )
3621 return;
3622
3623 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3624 return;
3625
3626 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003627}
3628
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003629#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003630/* Add a new (empty) key_cert entry an return a pointer to it */
3631static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3632{
3633 ssl_key_cert *key_cert, *last;
3634
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003635 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3636 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003637 return( NULL );
3638
3639 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3640
3641 /* Append the new key_cert to the (possibly empty) current list */
3642 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003643 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003644 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003645 if( ssl->handshake != NULL )
3646 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003647 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003648 else
3649 {
3650 last = ssl->key_cert;
3651 while( last->next != NULL )
3652 last = last->next;
3653 last->next = key_cert;
3654 }
3655
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003656 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003657}
3658
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003659void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003660 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003661{
3662 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003663 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003664 ssl->peer_cn = peer_cn;
3665}
3666
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003667int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003668 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003669{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003670 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3671
3672 if( key_cert == NULL )
3673 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3674
3675 key_cert->cert = own_cert;
3676 key_cert->key = pk_key;
3677
3678 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003679}
3680
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003681#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003682int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003683 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003684{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003685 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003686 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003687
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003688 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003689 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3690
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003691 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3692 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003693 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003694
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003695 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003696
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003697 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003698 if( ret != 0 )
3699 return( ret );
3700
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003701 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003702 return( ret );
3703
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003704 key_cert->cert = own_cert;
3705 key_cert->key_own_alloc = 1;
3706
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003707 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003708}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003709#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003710
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003711int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003712 void *rsa_key,
3713 rsa_decrypt_func rsa_decrypt,
3714 rsa_sign_func rsa_sign,
3715 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003716{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003717 int ret;
3718 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003719
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003720 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003721 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3722
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003723 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3724 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003725 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003726
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003727 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003728
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003729 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3730 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3731 return( ret );
3732
3733 key_cert->cert = own_cert;
3734 key_cert->key_own_alloc = 1;
3735
3736 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003737}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003738#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003739
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003740#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003741int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3742 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003743{
Paul Bakker6db455e2013-09-18 17:29:31 +02003744 if( psk == NULL || psk_identity == NULL )
3745 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3746
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003747 /*
3748 * The length will be check later anyway, but in case it is obviously
3749 * too large, better abort now. The PMS is as follows:
3750 * other_len (2 bytes) + other + psk_len (2 bytes) + psk
3751 */
3752 if( psk_len + 4 > POLARSSL_PREMASTER_SIZE )
3753 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3754
Paul Bakker6db455e2013-09-18 17:29:31 +02003755 if( ssl->psk != NULL )
3756 {
3757 polarssl_free( ssl->psk );
3758 polarssl_free( ssl->psk_identity );
3759 }
3760
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003761 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003762 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003763
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003764 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003765 ssl->psk_identity = (unsigned char *)
3766 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003767
3768 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3769 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3770
3771 memcpy( ssl->psk, psk, ssl->psk_len );
3772 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003773
3774 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003775}
3776
3777void ssl_set_psk_cb( ssl_context *ssl,
3778 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3779 size_t),
3780 void *p_psk )
3781{
3782 ssl->f_psk = f_psk;
3783 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003784}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003785#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003786
Paul Bakker48916f92012-09-16 19:57:18 +00003787#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003788int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003789{
3790 int ret;
3791
Paul Bakker48916f92012-09-16 19:57:18 +00003792 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003793 {
3794 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3795 return( ret );
3796 }
3797
Paul Bakker48916f92012-09-16 19:57:18 +00003798 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003799 {
3800 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3801 return( ret );
3802 }
3803
3804 return( 0 );
3805}
3806
Paul Bakker1b57b062011-01-06 15:48:19 +00003807int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3808{
3809 int ret;
3810
Paul Bakker66d5d072014-06-17 16:39:18 +02003811 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003812 {
3813 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3814 return( ret );
3815 }
3816
Paul Bakker66d5d072014-06-17 16:39:18 +02003817 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003818 {
3819 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3820 return( ret );
3821 }
3822
3823 return( 0 );
3824}
Paul Bakker48916f92012-09-16 19:57:18 +00003825#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003826
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003827#if defined(POLARSSL_SSL_SET_CURVES)
3828/*
3829 * Set the allowed elliptic curves
3830 */
3831void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3832{
3833 ssl->curve_list = curve_list;
3834}
3835#endif
3836
Paul Bakker0be444a2013-08-27 21:55:01 +02003837#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003838int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003839{
3840 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003841 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003842
3843 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003844
3845 if( ssl->hostname_len + 1 == 0 )
3846 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3847
Paul Bakker6e339b52013-07-03 13:37:05 +02003848 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003849
Paul Bakkerb15b8512012-01-13 13:44:06 +00003850 if( ssl->hostname == NULL )
3851 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3852
Paul Bakker3c2122f2013-06-24 19:03:14 +02003853 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003855
Paul Bakker40ea7de2009-05-03 10:18:48 +00003856 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003857
3858 return( 0 );
3859}
3860
Paul Bakker5701cdc2012-09-27 21:49:42 +00003861void ssl_set_sni( ssl_context *ssl,
3862 int (*f_sni)(void *, ssl_context *,
3863 const unsigned char *, size_t),
3864 void *p_sni )
3865{
3866 ssl->f_sni = f_sni;
3867 ssl->p_sni = p_sni;
3868}
Paul Bakker0be444a2013-08-27 21:55:01 +02003869#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003870
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003871#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003872int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003873{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003874 size_t cur_len, tot_len;
3875 const char **p;
3876
3877 /*
3878 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3879 * truncated". Check lengths now rather than later.
3880 */
3881 tot_len = 0;
3882 for( p = protos; *p != NULL; p++ )
3883 {
3884 cur_len = strlen( *p );
3885 tot_len += cur_len;
3886
3887 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3888 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3889 }
3890
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003891 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003892
3893 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003894}
3895
3896const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3897{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003898 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003899}
3900#endif /* POLARSSL_SSL_ALPN */
3901
Paul Bakker490ecc82011-10-06 13:04:09 +00003902void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3903{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003904 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3905 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3906 {
3907 ssl->max_major_ver = major;
3908 ssl->max_minor_ver = minor;
3909 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003910}
3911
Paul Bakker1d29fb52012-09-28 13:28:45 +00003912void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3913{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003914 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3915 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3916 {
3917 ssl->min_major_ver = major;
3918 ssl->min_minor_ver = minor;
3919 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003920}
3921
Paul Bakker05decb22013-08-15 13:33:48 +02003922#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003923int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3924{
Paul Bakker77e257e2013-12-16 15:29:52 +01003925 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003926 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003927 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003928 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003929 }
3930
3931 ssl->mfl_code = mfl_code;
3932
3933 return( 0 );
3934}
Paul Bakker05decb22013-08-15 13:33:48 +02003935#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003936
Paul Bakker1f2bc622013-08-15 13:45:55 +02003937#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003938int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003939{
3940 if( ssl->endpoint != SSL_IS_CLIENT )
3941 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3942
Paul Bakker8c1ede62013-07-19 14:14:37 +02003943 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003944
3945 return( 0 );
3946}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003947#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003948
Paul Bakker48916f92012-09-16 19:57:18 +00003949void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3950{
3951 ssl->disable_renegotiation = renegotiation;
3952}
3953
3954void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3955{
3956 ssl->allow_legacy_renegotiation = allow_legacy;
3957}
3958
Paul Bakkera503a632013-08-14 13:48:06 +02003959#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003960int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3961{
3962 ssl->session_tickets = use_tickets;
3963
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003964 if( ssl->endpoint == SSL_IS_CLIENT )
3965 return( 0 );
3966
3967 if( ssl->f_rng == NULL )
3968 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3969
3970 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003971}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003972
3973void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3974{
3975 ssl->ticket_lifetime = lifetime;
3976}
Paul Bakkera503a632013-08-14 13:48:06 +02003977#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003978
Paul Bakker5121ce52009-01-03 21:22:43 +00003979/*
3980 * SSL get accessors
3981 */
Paul Bakker23986e52011-04-24 08:57:21 +00003982size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003983{
3984 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3985}
3986
Paul Bakkerff60ee62010-03-16 21:09:09 +00003987int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003988{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003989 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003990}
3991
Paul Bakkere3166ce2011-01-27 17:40:50 +00003992const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003993{
Paul Bakker926c8e42013-03-06 10:23:34 +01003994 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003995 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003996
Paul Bakkere3166ce2011-01-27 17:40:50 +00003997 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003998}
3999
Paul Bakker43ca69c2011-01-15 17:35:19 +00004000const char *ssl_get_version( const ssl_context *ssl )
4001{
4002 switch( ssl->minor_ver )
4003 {
4004 case SSL_MINOR_VERSION_0:
4005 return( "SSLv3.0" );
4006
4007 case SSL_MINOR_VERSION_1:
4008 return( "TLSv1.0" );
4009
4010 case SSL_MINOR_VERSION_2:
4011 return( "TLSv1.1" );
4012
Paul Bakker1ef83d62012-04-11 12:09:53 +00004013 case SSL_MINOR_VERSION_3:
4014 return( "TLSv1.2" );
4015
Paul Bakker43ca69c2011-01-15 17:35:19 +00004016 default:
4017 break;
4018 }
4019 return( "unknown" );
4020}
4021
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004022#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004023const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004024{
4025 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004026 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004027
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004028 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004029}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004030#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004031
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004032int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4033{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004034 if( ssl == NULL ||
4035 dst == NULL ||
4036 ssl->session == NULL ||
4037 ssl->endpoint != SSL_IS_CLIENT )
4038 {
4039 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4040 }
4041
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004042 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004043}
4044
Paul Bakker5121ce52009-01-03 21:22:43 +00004045/*
Paul Bakker1961b702013-01-25 14:49:24 +01004046 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004047 */
Paul Bakker1961b702013-01-25 14:49:24 +01004048int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004049{
Paul Bakker40e46942009-01-03 21:51:57 +00004050 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004051
Paul Bakker40e46942009-01-03 21:51:57 +00004052#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004054 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004055#endif
4056
Paul Bakker40e46942009-01-03 21:51:57 +00004057#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004058 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004059 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004060#endif
4061
Paul Bakker1961b702013-01-25 14:49:24 +01004062 return( ret );
4063}
4064
4065/*
4066 * Perform the SSL handshake
4067 */
4068int ssl_handshake( ssl_context *ssl )
4069{
4070 int ret = 0;
4071
4072 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4073
4074 while( ssl->state != SSL_HANDSHAKE_OVER )
4075 {
4076 ret = ssl_handshake_step( ssl );
4077
4078 if( ret != 0 )
4079 break;
4080 }
4081
Paul Bakker5121ce52009-01-03 21:22:43 +00004082 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4083
4084 return( ret );
4085}
4086
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004087#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004088/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004089 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004090 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004091static int ssl_write_hello_request( ssl_context *ssl )
4092{
4093 int ret;
4094
4095 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4096
4097 ssl->out_msglen = 4;
4098 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4099 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4100
4101 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4102 {
4103 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4104 return( ret );
4105 }
4106
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004107 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4108
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004109 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4110
4111 return( 0 );
4112}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004113#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004114
4115/*
4116 * Actually renegotiate current connection, triggered by either:
4117 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004118 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004119 * - receiving any handshake message on server during ssl_read() after the
4120 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004121 * If the handshake doesn't complete due to waiting for I/O, it will continue
4122 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004123 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004124static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004125{
4126 int ret;
4127
4128 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4129
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004130 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4131 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004132
4133 ssl->state = SSL_HELLO_REQUEST;
4134 ssl->renegotiation = SSL_RENEGOTIATION;
4135
Paul Bakker48916f92012-09-16 19:57:18 +00004136 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4137 {
4138 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4139 return( ret );
4140 }
4141
4142 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4143
4144 return( 0 );
4145}
4146
4147/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004148 * Renegotiate current connection on client,
4149 * or request renegotiation on server
4150 */
4151int ssl_renegotiate( ssl_context *ssl )
4152{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004153 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004154
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004155#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004156 /* On server, just send the request */
4157 if( ssl->endpoint == SSL_IS_SERVER )
4158 {
4159 if( ssl->state != SSL_HANDSHAKE_OVER )
4160 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4161
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004162 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004163 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004164#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004165
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004166#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004167 /*
4168 * On client, either start the renegotiation process or,
4169 * if already in progress, continue the handshake
4170 */
4171 if( ssl->renegotiation != SSL_RENEGOTIATION )
4172 {
4173 if( ssl->state != SSL_HANDSHAKE_OVER )
4174 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4175
4176 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4177 {
4178 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4179 return( ret );
4180 }
4181 }
4182 else
4183 {
4184 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4185 {
4186 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4187 return( ret );
4188 }
4189 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004190#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004191
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004192 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004193}
4194
4195/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004196 * Receive application data decrypted from the SSL layer
4197 */
Paul Bakker23986e52011-04-24 08:57:21 +00004198int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004199{
Paul Bakker23986e52011-04-24 08:57:21 +00004200 int ret;
4201 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004202
4203 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4204
4205 if( ssl->state != SSL_HANDSHAKE_OVER )
4206 {
4207 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4208 {
4209 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4210 return( ret );
4211 }
4212 }
4213
4214 if( ssl->in_offt == NULL )
4215 {
4216 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4217 {
Paul Bakker831a7552011-05-18 13:32:51 +00004218 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4219 return( 0 );
4220
Paul Bakker5121ce52009-01-03 21:22:43 +00004221 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4222 return( ret );
4223 }
4224
4225 if( ssl->in_msglen == 0 &&
4226 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4227 {
4228 /*
4229 * OpenSSL sends empty messages to randomize the IV
4230 */
4231 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4232 {
Paul Bakker831a7552011-05-18 13:32:51 +00004233 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4234 return( 0 );
4235
Paul Bakker5121ce52009-01-03 21:22:43 +00004236 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4237 return( ret );
4238 }
4239 }
4240
Paul Bakker48916f92012-09-16 19:57:18 +00004241 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4242 {
4243 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4244
4245 if( ssl->endpoint == SSL_IS_CLIENT &&
4246 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4247 ssl->in_hslen != 4 ) )
4248 {
4249 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4250 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4251 }
4252
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004253 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4254 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004255 ssl->allow_legacy_renegotiation ==
4256 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004257 {
4258 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4259
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004260#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004261 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004262 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004263 /*
4264 * SSLv3 does not have a "no_renegotiation" alert
4265 */
4266 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4267 return( ret );
4268 }
4269 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004270#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004271#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4272 defined(POLARSSL_SSL_PROTO_TLS1_2)
4273 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004274 {
4275 if( ( ret = ssl_send_alert_message( ssl,
4276 SSL_ALERT_LEVEL_WARNING,
4277 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4278 {
4279 return( ret );
4280 }
Paul Bakker48916f92012-09-16 19:57:18 +00004281 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004282 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004283#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4284 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004285 {
4286 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004287 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004288 }
Paul Bakker48916f92012-09-16 19:57:18 +00004289 }
4290 else
4291 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004292 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004293 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004294 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004295 return( ret );
4296 }
4297
4298 return( POLARSSL_ERR_NET_WANT_READ );
4299 }
4300 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004301 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4302 {
4303 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4304 "but not honored by client" ) );
4305 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4306 }
Paul Bakker48916f92012-09-16 19:57:18 +00004307 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 {
4309 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004310 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 }
4312
4313 ssl->in_offt = ssl->in_msg;
4314 }
4315
4316 n = ( len < ssl->in_msglen )
4317 ? len : ssl->in_msglen;
4318
4319 memcpy( buf, ssl->in_offt, n );
4320 ssl->in_msglen -= n;
4321
4322 if( ssl->in_msglen == 0 )
4323 /* all bytes consumed */
4324 ssl->in_offt = NULL;
4325 else
4326 /* more data available */
4327 ssl->in_offt += n;
4328
4329 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4330
Paul Bakker23986e52011-04-24 08:57:21 +00004331 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004332}
4333
4334/*
4335 * Send application data to be encrypted by the SSL layer
4336 */
Paul Bakker23986e52011-04-24 08:57:21 +00004337int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004338{
Paul Bakker23986e52011-04-24 08:57:21 +00004339 int ret;
4340 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004341 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004342
4343 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4344
4345 if( ssl->state != SSL_HANDSHAKE_OVER )
4346 {
4347 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4348 {
4349 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4350 return( ret );
4351 }
4352 }
4353
Paul Bakker05decb22013-08-15 13:33:48 +02004354#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004355 /*
4356 * Assume mfl_code is correct since it was checked when set
4357 */
4358 max_len = mfl_code_to_length[ssl->mfl_code];
4359
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004360 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004361 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004362 */
4363 if( ssl->session_out != NULL &&
4364 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4365 {
4366 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4367 }
Paul Bakker05decb22013-08-15 13:33:48 +02004368#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004369
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004370 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004371
Paul Bakker5121ce52009-01-03 21:22:43 +00004372 if( ssl->out_left != 0 )
4373 {
4374 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4375 {
4376 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4377 return( ret );
4378 }
4379 }
Paul Bakker887bd502011-06-08 13:10:54 +00004380 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004381 {
Paul Bakker887bd502011-06-08 13:10:54 +00004382 ssl->out_msglen = n;
4383 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4384 memcpy( ssl->out_msg, buf, n );
4385
4386 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4387 {
4388 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4389 return( ret );
4390 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004391 }
4392
4393 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4394
Paul Bakker23986e52011-04-24 08:57:21 +00004395 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004396}
4397
4398/*
4399 * Notify the peer that the connection is being closed
4400 */
4401int ssl_close_notify( ssl_context *ssl )
4402{
4403 int ret;
4404
4405 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4406
4407 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4408 {
4409 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4410 return( ret );
4411 }
4412
4413 if( ssl->state == SSL_HANDSHAKE_OVER )
4414 {
Paul Bakker48916f92012-09-16 19:57:18 +00004415 if( ( ret = ssl_send_alert_message( ssl,
4416 SSL_ALERT_LEVEL_WARNING,
4417 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004418 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004419 return( ret );
4420 }
4421 }
4422
4423 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4424
4425 return( ret );
4426}
4427
Paul Bakker48916f92012-09-16 19:57:18 +00004428void ssl_transform_free( ssl_transform *transform )
4429{
4430#if defined(POLARSSL_ZLIB_SUPPORT)
4431 deflateEnd( &transform->ctx_deflate );
4432 inflateEnd( &transform->ctx_inflate );
4433#endif
4434
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004435 cipher_free_ctx( &transform->cipher_ctx_enc );
4436 cipher_free_ctx( &transform->cipher_ctx_dec );
4437
Paul Bakker61d113b2013-07-04 11:51:43 +02004438 md_free_ctx( &transform->md_ctx_enc );
4439 md_free_ctx( &transform->md_ctx_dec );
4440
Paul Bakker34617722014-06-13 17:20:13 +02004441 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004442}
4443
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004444#if defined(POLARSSL_X509_CRT_PARSE_C)
4445static void ssl_key_cert_free( ssl_key_cert *key_cert )
4446{
4447 ssl_key_cert *cur = key_cert, *next;
4448
4449 while( cur != NULL )
4450 {
4451 next = cur->next;
4452
4453 if( cur->key_own_alloc )
4454 {
4455 pk_free( cur->key );
4456 polarssl_free( cur->key );
4457 }
4458 polarssl_free( cur );
4459
4460 cur = next;
4461 }
4462}
4463#endif /* POLARSSL_X509_CRT_PARSE_C */
4464
Paul Bakker48916f92012-09-16 19:57:18 +00004465void ssl_handshake_free( ssl_handshake_params *handshake )
4466{
4467#if defined(POLARSSL_DHM_C)
4468 dhm_free( &handshake->dhm_ctx );
4469#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004470#if defined(POLARSSL_ECDH_C)
4471 ecdh_free( &handshake->ecdh_ctx );
4472#endif
4473
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004474#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004475 /* explicit void pointer cast for buggy MS compiler */
4476 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004477#endif
4478
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004479#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4480 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4481 /*
4482 * Free only the linked list wrapper, not the keys themselves
4483 * since the belong to the SNI callback
4484 */
4485 if( handshake->sni_key_cert != NULL )
4486 {
4487 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4488
4489 while( cur != NULL )
4490 {
4491 next = cur->next;
4492 polarssl_free( cur );
4493 cur = next;
4494 }
4495 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004496#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004497
Paul Bakker34617722014-06-13 17:20:13 +02004498 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004499}
4500
4501void ssl_session_free( ssl_session *session )
4502{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004503#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004504 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004505 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004506 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004507 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004508 }
Paul Bakkered27a042013-04-18 22:46:23 +02004509#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004510
Paul Bakkera503a632013-08-14 13:48:06 +02004511#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004512 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004513#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004514
Paul Bakker34617722014-06-13 17:20:13 +02004515 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004516}
4517
Paul Bakker5121ce52009-01-03 21:22:43 +00004518/*
4519 * Free an SSL context
4520 */
4521void ssl_free( ssl_context *ssl )
4522{
4523 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4524
Paul Bakker5121ce52009-01-03 21:22:43 +00004525 if( ssl->out_ctr != NULL )
4526 {
Paul Bakker34617722014-06-13 17:20:13 +02004527 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004528 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004529 }
4530
4531 if( ssl->in_ctr != NULL )
4532 {
Paul Bakker34617722014-06-13 17:20:13 +02004533 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004534 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004535 }
4536
Paul Bakker16770332013-10-11 09:59:44 +02004537#if defined(POLARSSL_ZLIB_SUPPORT)
4538 if( ssl->compress_buf != NULL )
4539 {
Paul Bakker34617722014-06-13 17:20:13 +02004540 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004541 polarssl_free( ssl->compress_buf );
4542 }
4543#endif
4544
Paul Bakker40e46942009-01-03 21:51:57 +00004545#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004546 mpi_free( &ssl->dhm_P );
4547 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004548#endif
4549
Paul Bakker48916f92012-09-16 19:57:18 +00004550 if( ssl->transform )
4551 {
4552 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004553 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004554 }
4555
4556 if( ssl->handshake )
4557 {
4558 ssl_handshake_free( ssl->handshake );
4559 ssl_transform_free( ssl->transform_negotiate );
4560 ssl_session_free( ssl->session_negotiate );
4561
Paul Bakker6e339b52013-07-03 13:37:05 +02004562 polarssl_free( ssl->handshake );
4563 polarssl_free( ssl->transform_negotiate );
4564 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004565 }
4566
Paul Bakkerc0463502013-02-14 11:19:38 +01004567 if( ssl->session )
4568 {
4569 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004570 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004571 }
4572
Paul Bakkera503a632013-08-14 13:48:06 +02004573#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004574 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004575#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004576
Paul Bakker0be444a2013-08-27 21:55:01 +02004577#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004578 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004579 {
Paul Bakker34617722014-06-13 17:20:13 +02004580 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004581 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004582 ssl->hostname_len = 0;
4583 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004584#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004585
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004586#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004587 if( ssl->psk != NULL )
4588 {
Paul Bakker34617722014-06-13 17:20:13 +02004589 polarssl_zeroize( ssl->psk, ssl->psk_len );
4590 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004591 polarssl_free( ssl->psk );
4592 polarssl_free( ssl->psk_identity );
4593 ssl->psk_len = 0;
4594 ssl->psk_identity_len = 0;
4595 }
4596#endif
4597
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004598#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004599 ssl_key_cert_free( ssl->key_cert );
4600#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004601
Paul Bakker05ef8352012-05-08 09:17:57 +00004602#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4603 if( ssl_hw_record_finish != NULL )
4604 {
4605 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4606 ssl_hw_record_finish( ssl );
4607 }
4608#endif
4609
Paul Bakker5121ce52009-01-03 21:22:43 +00004610 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004611
Paul Bakker86f04f42013-02-14 11:20:09 +01004612 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004613 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004614}
4615
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004616#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004617/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004618 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004619 */
4620unsigned char ssl_sig_from_pk( pk_context *pk )
4621{
4622#if defined(POLARSSL_RSA_C)
4623 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4624 return( SSL_SIG_RSA );
4625#endif
4626#if defined(POLARSSL_ECDSA_C)
4627 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4628 return( SSL_SIG_ECDSA );
4629#endif
4630 return( SSL_SIG_ANON );
4631}
4632
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004633pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4634{
4635 switch( sig )
4636 {
4637#if defined(POLARSSL_RSA_C)
4638 case SSL_SIG_RSA:
4639 return( POLARSSL_PK_RSA );
4640#endif
4641#if defined(POLARSSL_ECDSA_C)
4642 case SSL_SIG_ECDSA:
4643 return( POLARSSL_PK_ECDSA );
4644#endif
4645 default:
4646 return( POLARSSL_PK_NONE );
4647 }
4648}
Paul Bakker9af723c2014-05-01 13:03:14 +02004649#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004650
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004651/*
4652 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4653 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004654md_type_t ssl_md_alg_from_hash( unsigned char hash )
4655{
4656 switch( hash )
4657 {
4658#if defined(POLARSSL_MD5_C)
4659 case SSL_HASH_MD5:
4660 return( POLARSSL_MD_MD5 );
4661#endif
4662#if defined(POLARSSL_SHA1_C)
4663 case SSL_HASH_SHA1:
4664 return( POLARSSL_MD_SHA1 );
4665#endif
4666#if defined(POLARSSL_SHA256_C)
4667 case SSL_HASH_SHA224:
4668 return( POLARSSL_MD_SHA224 );
4669 case SSL_HASH_SHA256:
4670 return( POLARSSL_MD_SHA256 );
4671#endif
4672#if defined(POLARSSL_SHA512_C)
4673 case SSL_HASH_SHA384:
4674 return( POLARSSL_MD_SHA384 );
4675 case SSL_HASH_SHA512:
4676 return( POLARSSL_MD_SHA512 );
4677#endif
4678 default:
4679 return( POLARSSL_MD_NONE );
4680 }
4681}
4682
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004683#if defined(POLARSSL_SSL_SET_CURVES)
4684/*
4685 * Check is a curve proposed by the peer is in our list.
4686 * Return 1 if we're willing to use it, 0 otherwise.
4687 */
4688int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4689{
4690 const ecp_group_id *gid;
4691
4692 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4693 if( *gid == grp_id )
4694 return( 1 );
4695
4696 return( 0 );
4697}
Paul Bakker9af723c2014-05-01 13:03:14 +02004698#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004699
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004700#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004701int ssl_check_cert_usage( const x509_crt *cert,
4702 const ssl_ciphersuite_t *ciphersuite,
4703 int cert_endpoint )
4704{
4705#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4706 int usage = 0;
4707#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004708#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4709 const char *ext_oid;
4710 size_t ext_len;
4711#endif
4712
4713#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4714 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4715 ((void) cert);
4716 ((void) cert_endpoint);
4717#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004718
4719#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4720 if( cert_endpoint == SSL_IS_SERVER )
4721 {
4722 /* Server part of the key exchange */
4723 switch( ciphersuite->key_exchange )
4724 {
4725 case POLARSSL_KEY_EXCHANGE_RSA:
4726 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4727 usage = KU_KEY_ENCIPHERMENT;
4728 break;
4729
4730 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4731 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4732 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4733 usage = KU_DIGITAL_SIGNATURE;
4734 break;
4735
4736 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4737 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4738 usage = KU_KEY_AGREEMENT;
4739 break;
4740
4741 /* Don't use default: we want warnings when adding new values */
4742 case POLARSSL_KEY_EXCHANGE_NONE:
4743 case POLARSSL_KEY_EXCHANGE_PSK:
4744 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4745 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4746 usage = 0;
4747 }
4748 }
4749 else
4750 {
4751 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4752 usage = KU_DIGITAL_SIGNATURE;
4753 }
4754
4755 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4756 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004757#else
4758 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004759#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4760
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004761#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4762 if( cert_endpoint == SSL_IS_SERVER )
4763 {
4764 ext_oid = OID_SERVER_AUTH;
4765 ext_len = OID_SIZE( OID_SERVER_AUTH );
4766 }
4767 else
4768 {
4769 ext_oid = OID_CLIENT_AUTH;
4770 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4771 }
4772
4773 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4774 return( -1 );
4775#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4776
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004777 return( 0 );
4778}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004779#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004780
4781#endif /* POLARSSL_SSL_TLS_C */