blob: 373a1f5bdc3ba2073049038ca18b8d52cf27ff72 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
159 /*
160 * SSLv3:
161 * block =
162 * MD5( secret + SHA1( 'A' + secret + random ) ) +
163 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
164 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
165 * ...
166 */
167 for( i = 0; i < dlen / 16; i++ )
168 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200169 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000170
171 sha1_starts( &sha1 );
172 sha1_update( &sha1, padding, 1 + i );
173 sha1_update( &sha1, secret, slen );
174 sha1_update( &sha1, random, rlen );
175 sha1_finish( &sha1, sha1sum );
176
177 md5_starts( &md5 );
178 md5_update( &md5, secret, slen );
179 md5_update( &md5, sha1sum, 20 );
180 md5_finish( &md5, dstbuf + i * 16 );
181 }
182
Paul Bakker34617722014-06-13 17:20:13 +0200183 polarssl_zeroize( &md5, sizeof( md5 ) );
184 polarssl_zeroize( &sha1, sizeof( sha1 ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000185
Paul Bakker34617722014-06-13 17:20:13 +0200186 polarssl_zeroize( padding, sizeof( padding ) );
187 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
189 return( 0 );
190}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200191#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000192
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200193#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200194static int tls1_prf( const unsigned char *secret, size_t slen,
195 const char *label,
196 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000197 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
Paul Bakker23986e52011-04-24 08:57:21 +0000199 size_t nb, hs;
200 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200201 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 unsigned char tmp[128];
203 unsigned char h_i[20];
204
205 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000206 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 hs = ( slen + 1 ) / 2;
209 S1 = secret;
210 S2 = secret + slen - hs;
211
212 nb = strlen( label );
213 memcpy( tmp + 20, label, nb );
214 memcpy( tmp + 20 + nb, random, rlen );
215 nb += rlen;
216
217 /*
218 * First compute P_md5(secret,label+random)[0..dlen]
219 */
220 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
221
222 for( i = 0; i < dlen; i += 16 )
223 {
224 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
225 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
226
227 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
228
229 for( j = 0; j < k; j++ )
230 dstbuf[i + j] = h_i[j];
231 }
232
233 /*
234 * XOR out with P_sha1(secret,label+random)[0..dlen]
235 */
236 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
237
238 for( i = 0; i < dlen; i += 20 )
239 {
240 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
241 sha1_hmac( S2, hs, tmp, 20, tmp );
242
243 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
244
245 for( j = 0; j < k; j++ )
246 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
247 }
248
Paul Bakker34617722014-06-13 17:20:13 +0200249 polarssl_zeroize( tmp, sizeof( tmp ) );
250 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252 return( 0 );
253}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200254#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200256#if defined(POLARSSL_SSL_PROTO_TLS1_2)
257#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200258static int tls_prf_sha256( const unsigned char *secret, size_t slen,
259 const char *label,
260 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000261 unsigned char *dstbuf, size_t dlen )
262{
263 size_t nb;
264 size_t i, j, k;
265 unsigned char tmp[128];
266 unsigned char h_i[32];
267
268 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
269 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
270
271 nb = strlen( label );
272 memcpy( tmp + 32, label, nb );
273 memcpy( tmp + 32 + nb, random, rlen );
274 nb += rlen;
275
276 /*
277 * Compute P_<hash>(secret, label + random)[0..dlen]
278 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200279 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000280
281 for( i = 0; i < dlen; i += 32 )
282 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200283 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
284 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000285
286 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
287
288 for( j = 0; j < k; j++ )
289 dstbuf[i + j] = h_i[j];
290 }
291
Paul Bakker34617722014-06-13 17:20:13 +0200292 polarssl_zeroize( tmp, sizeof( tmp ) );
293 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000294
295 return( 0 );
296}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200297#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000298
Paul Bakker9e36f042013-06-30 14:34:05 +0200299#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200300static int tls_prf_sha384( const unsigned char *secret, size_t slen,
301 const char *label,
302 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000303 unsigned char *dstbuf, size_t dlen )
304{
305 size_t nb;
306 size_t i, j, k;
307 unsigned char tmp[128];
308 unsigned char h_i[48];
309
310 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
311 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
312
313 nb = strlen( label );
314 memcpy( tmp + 48, label, nb );
315 memcpy( tmp + 48 + nb, random, rlen );
316 nb += rlen;
317
318 /*
319 * Compute P_<hash>(secret, label + random)[0..dlen]
320 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200321 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000322
323 for( i = 0; i < dlen; i += 48 )
324 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200325 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
326 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000327
328 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
329
330 for( j = 0; j < k; j++ )
331 dstbuf[i + j] = h_i[j];
332 }
333
Paul Bakker34617722014-06-13 17:20:13 +0200334 polarssl_zeroize( tmp, sizeof( tmp ) );
335 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000336
337 return( 0 );
338}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200339#endif /* POLARSSL_SHA512_C */
340#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000341
Paul Bakker66d5d072014-06-17 16:39:18 +0200342static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343
344#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
345 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200346static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200347#endif
Paul Bakker380da532012-04-18 16:10:25 +0000348
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200349#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200350static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
351static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif
353
354#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200355static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
356static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200357#endif
358
359#if defined(POLARSSL_SSL_PROTO_TLS1_2)
360#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200361static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
362static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
363static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100365
Paul Bakker9e36f042013-06-30 14:34:05 +0200366#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200367static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
368static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
369static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100370#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200371#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000372
Paul Bakker5121ce52009-01-03 21:22:43 +0000373int ssl_derive_keys( ssl_context *ssl )
374{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200375 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char keyblk[256];
378 unsigned char *key1;
379 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100380 unsigned char *mac_enc;
381 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200382 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 const cipher_info_t *cipher_info;
384 const md_info_t *md_info;
385
Paul Bakker48916f92012-09-16 19:57:18 +0000386 ssl_session *session = ssl->session_negotiate;
387 ssl_transform *transform = ssl->transform_negotiate;
388 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
391
Paul Bakker68884e32013-01-07 18:20:04 +0100392 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
393 if( cipher_info == NULL )
394 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200395 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100396 transform->ciphersuite_info->cipher ) );
397 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
398 }
399
400 md_info = md_info_from_type( transform->ciphersuite_info->mac );
401 if( md_info == NULL )
402 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200403 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100404 transform->ciphersuite_info->mac ) );
405 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
406 }
407
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000409 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000410 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200411#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000412 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413 {
Paul Bakker48916f92012-09-16 19:57:18 +0000414 handshake->tls_prf = ssl3_prf;
415 handshake->calc_verify = ssl_calc_verify_ssl;
416 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200418 else
419#endif
420#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
421 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000422 {
Paul Bakker48916f92012-09-16 19:57:18 +0000423 handshake->tls_prf = tls1_prf;
424 handshake->calc_verify = ssl_calc_verify_tls;
425 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000426 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200427 else
428#endif
429#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200430#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
432 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000433 {
Paul Bakker48916f92012-09-16 19:57:18 +0000434 handshake->tls_prf = tls_prf_sha384;
435 handshake->calc_verify = ssl_calc_verify_tls_sha384;
436 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000438 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif
440#if defined(POLARSSL_SHA256_C)
441 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000442 {
Paul Bakker48916f92012-09-16 19:57:18 +0000443 handshake->tls_prf = tls_prf_sha256;
444 handshake->calc_verify = ssl_calc_verify_tls_sha256;
445 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000446 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447 else
448#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200449#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200450 {
451 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200452 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200453 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000454
455 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 * SSLv3:
457 * master =
458 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
459 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200461 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200462 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 * master = PRF( premaster, "master secret", randbytes )[0..47]
464 */
Paul Bakker0a597072012-09-25 21:55:46 +0000465 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 {
Paul Bakker48916f92012-09-16 19:57:18 +0000467 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
468 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
Paul Bakker48916f92012-09-16 19:57:18 +0000470 handshake->tls_prf( handshake->premaster, handshake->pmslen,
471 "master secret",
472 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Paul Bakker34617722014-06-13 17:20:13 +0200474 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 }
476 else
477 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
478
479 /*
480 * Swap the client and server random values.
481 */
Paul Bakker48916f92012-09-16 19:57:18 +0000482 memcpy( tmp, handshake->randbytes, 64 );
483 memcpy( handshake->randbytes, tmp + 32, 32 );
484 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200485 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487 /*
488 * SSLv3:
489 * key block =
490 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
491 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
492 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
493 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
494 * ...
495 *
496 * TLSv1:
497 * key block = PRF( master, "key expansion", randbytes )
498 */
Paul Bakker48916f92012-09-16 19:57:18 +0000499 handshake->tls_prf( session->master, 48, "key expansion",
500 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
Paul Bakker48916f92012-09-16 19:57:18 +0000502 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
503 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
504 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
505 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
507
Paul Bakker34617722014-06-13 17:20:13 +0200508 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 /*
511 * Determine the appropriate key, IV and MAC length.
512 */
Paul Bakker68884e32013-01-07 18:20:04 +0100513
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200514 transform->keylen = cipher_info->key_length / 8;
515
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200516 if( cipher_info->mode == POLARSSL_MODE_GCM ||
517 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200519 transform->maclen = 0;
520
Paul Bakker68884e32013-01-07 18:20:04 +0100521 transform->ivlen = 12;
522 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200523
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200524 /* Minimum length is expicit IV + tag */
525 transform->minlen = transform->ivlen - transform->fixed_ivlen
526 + ( transform->ciphersuite_info->flags &
527 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100528 }
529 else
530 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200531 int ret;
532
533 /* Initialize HMAC contexts */
534 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
535 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100536 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200537 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
538 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100539 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200541 /* Get MAC length */
542 transform->maclen = md_get_size( md_info );
543
544#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
545 /*
546 * If HMAC is to be truncated, we shall keep the leftmost bytes,
547 * (rfc 6066 page 13 or rfc 2104 section 4),
548 * so we only need to adjust the length here.
549 */
550 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
551 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
552#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
553
554 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100555 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200557 /* Minimum length */
558 if( cipher_info->mode == POLARSSL_MODE_STREAM )
559 transform->minlen = transform->maclen;
560 else
Paul Bakker68884e32013-01-07 18:20:04 +0100561 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200562 /*
563 * GenericBlockCipher:
564 * first multiple of blocklen greater than maclen
565 * + IV except for SSL3 and TLS 1.0
566 */
567 transform->minlen = transform->maclen
568 + cipher_info->block_size
569 - transform->maclen % cipher_info->block_size;
570
571#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
572 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
573 ssl->minor_ver == SSL_MINOR_VERSION_1 )
574 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100575 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200576#endif
577#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
578 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
579 ssl->minor_ver == SSL_MINOR_VERSION_3 )
580 {
581 transform->minlen += transform->ivlen;
582 }
583 else
584#endif
585 {
586 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
587 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
588 }
Paul Bakker68884e32013-01-07 18:20:04 +0100589 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 }
591
592 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000593 transform->keylen, transform->minlen, transform->ivlen,
594 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
596 /*
597 * Finally setup the cipher contexts, IVs and MAC secrets.
598 */
599 if( ssl->endpoint == SSL_IS_CLIENT )
600 {
Paul Bakker48916f92012-09-16 19:57:18 +0000601 key1 = keyblk + transform->maclen * 2;
602 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
Paul Bakker68884e32013-01-07 18:20:04 +0100604 mac_enc = keyblk;
605 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000607 /*
608 * This is not used in TLS v1.1.
609 */
Paul Bakker48916f92012-09-16 19:57:18 +0000610 iv_copy_len = ( transform->fixed_ivlen ) ?
611 transform->fixed_ivlen : transform->ivlen;
612 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
613 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000614 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 }
616 else
617 {
Paul Bakker48916f92012-09-16 19:57:18 +0000618 key1 = keyblk + transform->maclen * 2 + transform->keylen;
619 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Paul Bakker68884e32013-01-07 18:20:04 +0100621 mac_enc = keyblk + transform->maclen;
622 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000624 /*
625 * This is not used in TLS v1.1.
626 */
Paul Bakker48916f92012-09-16 19:57:18 +0000627 iv_copy_len = ( transform->fixed_ivlen ) ?
628 transform->fixed_ivlen : transform->ivlen;
629 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
630 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000631 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 }
633
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200634#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100635 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
636 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100637 if( transform->maclen > sizeof transform->mac_enc )
638 {
639 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200640 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100641 }
642
Paul Bakker68884e32013-01-07 18:20:04 +0100643 memcpy( transform->mac_enc, mac_enc, transform->maclen );
644 memcpy( transform->mac_dec, mac_dec, transform->maclen );
645 }
646 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200647#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200648#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
649 defined(POLARSSL_SSL_PROTO_TLS1_2)
650 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100651 {
652 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
653 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
654 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200655 else
656#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200657 {
658 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200659 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200660 }
Paul Bakker68884e32013-01-07 18:20:04 +0100661
Paul Bakker05ef8352012-05-08 09:17:57 +0000662#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200663 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000664 {
665 int ret = 0;
666
667 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
668
Paul Bakker07eb38b2012-12-19 14:42:06 +0100669 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
670 transform->iv_enc, transform->iv_dec,
671 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100672 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100673 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000674 {
675 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200676 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000677 }
678 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200679#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000680
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200681 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
682 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200684 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
685 return( ret );
686 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200687
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200688 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
689 cipher_info ) ) != 0 )
690 {
691 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
692 return( ret );
693 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200694
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200695 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
696 cipher_info->key_length,
697 POLARSSL_ENCRYPT ) ) != 0 )
698 {
699 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
700 return( ret );
701 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200702
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200703 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
704 cipher_info->key_length,
705 POLARSSL_DECRYPT ) ) != 0 )
706 {
707 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
708 return( ret );
709 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200710
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200711#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200712 if( cipher_info->mode == POLARSSL_MODE_CBC )
713 {
714 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
715 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200716 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200717 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
718 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200719 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200720
721 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
722 POLARSSL_PADDING_NONE ) ) != 0 )
723 {
724 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
725 return( ret );
726 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200728#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Paul Bakker34617722014-06-13 17:20:13 +0200730 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Paul Bakker2770fbd2012-07-03 13:30:23 +0000732#if defined(POLARSSL_ZLIB_SUPPORT)
733 // Initialize compression
734 //
Paul Bakker48916f92012-09-16 19:57:18 +0000735 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000736 {
Paul Bakker16770332013-10-11 09:59:44 +0200737 if( ssl->compress_buf == NULL )
738 {
739 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
740 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
741 if( ssl->compress_buf == NULL )
742 {
743 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
744 SSL_BUFFER_LEN ) );
745 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
746 }
747 }
748
Paul Bakker2770fbd2012-07-03 13:30:23 +0000749 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
750
Paul Bakker48916f92012-09-16 19:57:18 +0000751 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
752 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000753
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200754 if( deflateInit( &transform->ctx_deflate,
755 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000756 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000757 {
758 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
759 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
760 }
761 }
762#endif /* POLARSSL_ZLIB_SUPPORT */
763
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
765
766 return( 0 );
767}
768
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200769#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000770void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000771{
772 md5_context md5;
773 sha1_context sha1;
774 unsigned char pad_1[48];
775 unsigned char pad_2[48];
776
Paul Bakker380da532012-04-18 16:10:25 +0000777 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Paul Bakker48916f92012-09-16 19:57:18 +0000779 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
780 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
Paul Bakker380da532012-04-18 16:10:25 +0000782 memset( pad_1, 0x36, 48 );
783 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Paul Bakker48916f92012-09-16 19:57:18 +0000785 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000786 md5_update( &md5, pad_1, 48 );
787 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker380da532012-04-18 16:10:25 +0000789 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000790 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000791 md5_update( &md5, pad_2, 48 );
792 md5_update( &md5, hash, 16 );
793 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Paul Bakker48916f92012-09-16 19:57:18 +0000795 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000796 sha1_update( &sha1, pad_1, 40 );
797 sha1_finish( &sha1, hash + 16 );
798
799 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000800 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000801 sha1_update( &sha1, pad_2, 40 );
802 sha1_update( &sha1, hash + 16, 20 );
803 sha1_finish( &sha1, hash + 16 );
804
805 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
806 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
807
808 return;
809}
Paul Bakker9af723c2014-05-01 13:03:14 +0200810#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000811
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200812#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000813void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
814{
815 md5_context md5;
816 sha1_context sha1;
817
818 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
819
Paul Bakker48916f92012-09-16 19:57:18 +0000820 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
821 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000822
Paul Bakker48916f92012-09-16 19:57:18 +0000823 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000824 sha1_finish( &sha1, hash + 16 );
825
826 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
827 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
828
829 return;
830}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200831#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000832
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200833#if defined(POLARSSL_SSL_PROTO_TLS1_2)
834#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000835void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
836{
Paul Bakker9e36f042013-06-30 14:34:05 +0200837 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000838
839 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
840
Paul Bakker9e36f042013-06-30 14:34:05 +0200841 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
842 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000843
844 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
845 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
846
847 return;
848}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200849#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000850
Paul Bakker9e36f042013-06-30 14:34:05 +0200851#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000852void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
853{
Paul Bakker9e36f042013-06-30 14:34:05 +0200854 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000855
856 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
857
Paul Bakker9e36f042013-06-30 14:34:05 +0200858 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
859 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakkerca4ab492012-04-18 14:23:57 +0000861 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
863
864 return;
865}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200866#endif /* POLARSSL_SHA512_C */
867#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000868
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200869#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200870int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
871{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200872 unsigned char *p = ssl->handshake->premaster;
873 unsigned char *end = p + sizeof( ssl->handshake->premaster );
874
875 /*
876 * PMS = struct {
877 * opaque other_secret<0..2^16-1>;
878 * opaque psk<0..2^16-1>;
879 * };
880 * with "other_secret" depending on the particular key exchange
881 */
882#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
883 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
884 {
885 if( end - p < 2 + (int) ssl->psk_len )
886 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
887
888 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
889 *(p++) = (unsigned char)( ssl->psk_len );
890 p += ssl->psk_len;
891 }
892 else
893#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200894#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
895 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
896 {
897 /*
898 * other_secret already set by the ClientKeyExchange message,
899 * and is 48 bytes long
900 */
901 *p++ = 0;
902 *p++ = 48;
903 p += 48;
904 }
905 else
906#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200907#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
908 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
909 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200910 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200911 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200912
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200913 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200914 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200915 p + 2, &len,
916 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200917 {
918 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
919 return( ret );
920 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200921 *(p++) = (unsigned char)( len >> 8 );
922 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200923 p += len;
924
925 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
926 }
927 else
928#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
929#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
930 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
931 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200932 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200933 size_t zlen;
934
935 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200936 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937 ssl->f_rng, ssl->p_rng ) ) != 0 )
938 {
939 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
940 return( ret );
941 }
942
943 *(p++) = (unsigned char)( zlen >> 8 );
944 *(p++) = (unsigned char)( zlen );
945 p += zlen;
946
947 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
948 }
949 else
950#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
951 {
952 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200953 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200954 }
955
956 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100957 if( end - p < 2 + (int) ssl->psk_len )
958 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
959
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200960 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
961 *(p++) = (unsigned char)( ssl->psk_len );
962 memcpy( p, ssl->psk, ssl->psk_len );
963 p += ssl->psk_len;
964
965 ssl->handshake->pmslen = p - ssl->handshake->premaster;
966
967 return( 0 );
968}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200969#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200970
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200971#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000972/*
973 * SSLv3.0 MAC functions
974 */
Paul Bakker68884e32013-01-07 18:20:04 +0100975static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
976 unsigned char *buf, size_t len,
977 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000978{
979 unsigned char header[11];
980 unsigned char padding[48];
Paul Bakker68884e32013-01-07 18:20:04 +0100981 int padlen = 0;
982 int md_size = md_get_size( md_ctx->md_info );
983 int md_type = md_get_type( md_ctx->md_info );
984
985 if( md_type == POLARSSL_MD_MD5 )
986 padlen = 48;
987 else if( md_type == POLARSSL_MD_SHA1 )
988 padlen = 40;
989 else if( md_type == POLARSSL_MD_SHA256 )
990 padlen = 32;
Manuel Pégourié-Gonnardc72ac7c2013-12-17 10:17:08 +0100991 else if( md_type == POLARSSL_MD_SHA384 )
992 padlen = 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
994 memcpy( header, ctr, 8 );
995 header[ 8] = (unsigned char) type;
996 header[ 9] = (unsigned char)( len >> 8 );
997 header[10] = (unsigned char)( len );
998
Paul Bakker68884e32013-01-07 18:20:04 +0100999 memset( padding, 0x36, padlen );
1000 md_starts( md_ctx );
1001 md_update( md_ctx, secret, md_size );
1002 md_update( md_ctx, padding, padlen );
1003 md_update( md_ctx, header, 11 );
1004 md_update( md_ctx, buf, len );
1005 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
Paul Bakker68884e32013-01-07 18:20:04 +01001007 memset( padding, 0x5C, padlen );
1008 md_starts( md_ctx );
1009 md_update( md_ctx, secret, md_size );
1010 md_update( md_ctx, padding, padlen );
1011 md_update( md_ctx, buf + len, md_size );
1012 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001013}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001014#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001015
Paul Bakker5121ce52009-01-03 21:22:43 +00001016/*
1017 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001018 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001019static int ssl_encrypt_buf( ssl_context *ssl )
1020{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001021 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001022 const cipher_mode_t mode = cipher_get_cipher_mode(
1023 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
1025 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1026
1027 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001028 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001030#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1031 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1032 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001033 if( mode != POLARSSL_MODE_GCM &&
1034 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001036#if defined(POLARSSL_SSL_PROTO_SSL3)
1037 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1038 {
1039 ssl_mac( &ssl->transform_out->md_ctx_enc,
1040 ssl->transform_out->mac_enc,
1041 ssl->out_msg, ssl->out_msglen,
1042 ssl->out_ctr, ssl->out_msgtype );
1043 }
1044 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001045#endif
1046#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001047 defined(POLARSSL_SSL_PROTO_TLS1_2)
1048 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1049 {
1050 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1051 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1052 ssl->out_msg, ssl->out_msglen );
1053 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1054 ssl->out_msg + ssl->out_msglen );
1055 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1056 }
1057 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001058#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001059 {
1060 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001061 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001062 }
1063
1064 SSL_DEBUG_BUF( 4, "computed mac",
1065 ssl->out_msg + ssl->out_msglen,
1066 ssl->transform_out->maclen );
1067
1068 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001069 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001070#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001072 /*
1073 * Encrypt
1074 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001075#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001076 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001078 int ret;
1079 size_t olen = 0;
1080
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1082 "including %d bytes of padding",
1083 ssl->out_msglen, 0 ) );
1084
1085 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1086 ssl->out_msg, ssl->out_msglen );
1087
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001088 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001089 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001090 ssl->transform_out->ivlen,
1091 ssl->out_msg, ssl->out_msglen,
1092 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001093 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001094 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001095 return( ret );
1096 }
1097
1098 if( ssl->out_msglen != olen )
1099 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001100 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001101 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001102 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001103 }
Paul Bakker68884e32013-01-07 18:20:04 +01001104 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001105#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001106#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1107 if( mode == POLARSSL_MODE_GCM ||
1108 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001109 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001110 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001111 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001112 unsigned char *enc_msg;
1113 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001114 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1115 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001116
Paul Bakkerca4ab492012-04-18 14:23:57 +00001117 memcpy( add_data, ssl->out_ctr, 8 );
1118 add_data[8] = ssl->out_msgtype;
1119 add_data[9] = ssl->major_ver;
1120 add_data[10] = ssl->minor_ver;
1121 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1122 add_data[12] = ssl->out_msglen & 0xFF;
1123
1124 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1125 add_data, 13 );
1126
Paul Bakker68884e32013-01-07 18:20:04 +01001127 /*
1128 * Generate IV
1129 */
1130 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001131 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1132 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001133 if( ret != 0 )
1134 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001135
Paul Bakker68884e32013-01-07 18:20:04 +01001136 memcpy( ssl->out_iv,
1137 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1138 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001139
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001140 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001141 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001142
Paul Bakker68884e32013-01-07 18:20:04 +01001143 /*
1144 * Fix pointer positions and message length with added IV
1145 */
1146 enc_msg = ssl->out_msg;
1147 enc_msglen = ssl->out_msglen;
1148 ssl->out_msglen += ssl->transform_out->ivlen -
1149 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001150
Paul Bakker68884e32013-01-07 18:20:04 +01001151 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1152 "including %d bytes of padding",
1153 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001154
Paul Bakker68884e32013-01-07 18:20:04 +01001155 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1156 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001157
Paul Bakker68884e32013-01-07 18:20:04 +01001158 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001159 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001160 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001161 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1162 ssl->transform_out->iv_enc,
1163 ssl->transform_out->ivlen,
1164 add_data, 13,
1165 enc_msg, enc_msglen,
1166 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001167 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001168 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001169 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001170 return( ret );
1171 }
1172
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001173 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001174 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001175 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001176 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001177 }
1178
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001179 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001180
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001181 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001184#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001185#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1186 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001187 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001189 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001190 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001191 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001192
Paul Bakker48916f92012-09-16 19:57:18 +00001193 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1194 ssl->transform_out->ivlen;
1195 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 padlen = 0;
1197
1198 for( i = 0; i <= padlen; i++ )
1199 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1200
1201 ssl->out_msglen += padlen + 1;
1202
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001203 enc_msglen = ssl->out_msglen;
1204 enc_msg = ssl->out_msg;
1205
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001206#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001207 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001208 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1209 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001210 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001211 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001212 {
1213 /*
1214 * Generate IV
1215 */
Paul Bakker48916f92012-09-16 19:57:18 +00001216 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1217 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001218 if( ret != 0 )
1219 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220
Paul Bakker92be97b2013-01-02 17:30:03 +01001221 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001222 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001223
1224 /*
1225 * Fix pointer positions and message length with added IV
1226 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001227 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001228 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001229 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001231#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001232
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001234 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001235 ssl->out_msglen, ssl->transform_out->ivlen,
1236 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
1238 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001239 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001241 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001242 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001243 ssl->transform_out->ivlen,
1244 enc_msg, enc_msglen,
1245 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001246 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001247 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001248 return( ret );
1249 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001250
Paul Bakkercca5b812013-08-31 17:40:26 +02001251 if( enc_msglen != olen )
1252 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001253 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001254 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001255 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001256
1257#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001258 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1259 {
1260 /*
1261 * Save IV in SSL3 and TLS1
1262 */
1263 memcpy( ssl->transform_out->iv_enc,
1264 ssl->transform_out->cipher_ctx_enc.iv,
1265 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001267#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001269 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001270#endif /* POLARSSL_CIPHER_MODE_CBC &&
1271 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001272 {
1273 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001274 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001276
Paul Bakkerca4ab492012-04-18 14:23:57 +00001277 for( i = 8; i > 0; i-- )
1278 if( ++ssl->out_ctr[i - 1] != 0 )
1279 break;
1280
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001281 /* The loops goes to its end iff the counter is wrapping */
1282 if( i == 0 )
1283 {
1284 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1285 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1286 }
1287
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1289
1290 return( 0 );
1291}
1292
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001293#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001294
Paul Bakker5121ce52009-01-03 21:22:43 +00001295static int ssl_decrypt_buf( ssl_context *ssl )
1296{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001297 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001298 const cipher_mode_t mode = cipher_get_cipher_mode(
1299 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001300#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1301 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1302 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1303 size_t padlen = 0, correct = 1;
1304#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001305
1306 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1307
Paul Bakker48916f92012-09-16 19:57:18 +00001308 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 {
1310 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001311 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001312 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 }
1314
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001315#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001316 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001317 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001318 int ret;
1319 size_t olen = 0;
1320
Paul Bakker68884e32013-01-07 18:20:04 +01001321 padlen = 0;
1322
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001323 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001324 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001325 ssl->transform_in->ivlen,
1326 ssl->in_msg, ssl->in_msglen,
1327 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001328 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001329 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001330 return( ret );
1331 }
1332
1333 if( ssl->in_msglen != olen )
1334 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001335 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001336 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 }
Paul Bakker68884e32013-01-07 18:20:04 +01001339 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001340#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001341#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1342 if( mode == POLARSSL_MODE_GCM ||
1343 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001344 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001345 int ret;
1346 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001347 unsigned char *dec_msg;
1348 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001349 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001350 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1351 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001352 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1353 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001354
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001355 if( ssl->in_msglen < explicit_iv_len + taglen )
1356 {
1357 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1358 "+ taglen (%d)", ssl->in_msglen,
1359 explicit_iv_len, taglen ) );
1360 return( POLARSSL_ERR_SSL_INVALID_MAC );
1361 }
1362 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1363
Paul Bakker68884e32013-01-07 18:20:04 +01001364 dec_msg = ssl->in_msg;
1365 dec_msg_result = ssl->in_msg;
1366 ssl->in_msglen = dec_msglen;
1367
1368 memcpy( add_data, ssl->in_ctr, 8 );
1369 add_data[8] = ssl->in_msgtype;
1370 add_data[9] = ssl->major_ver;
1371 add_data[10] = ssl->minor_ver;
1372 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1373 add_data[12] = ssl->in_msglen & 0xFF;
1374
1375 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1376 add_data, 13 );
1377
1378 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1379 ssl->in_iv,
1380 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1381
1382 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1383 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001384 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001385
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001386 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001387 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001388 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001389 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1390 ssl->transform_in->iv_dec,
1391 ssl->transform_in->ivlen,
1392 add_data, 13,
1393 dec_msg, dec_msglen,
1394 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001395 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001396 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001397 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1398
1399 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1400 return( POLARSSL_ERR_SSL_INVALID_MAC );
1401
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001402 return( ret );
1403 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001404
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001405 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001406 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001407 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001408 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001409 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001412#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001413#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1414 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001415 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 {
Paul Bakker45829992013-01-03 14:52:21 +01001417 /*
1418 * Decrypt and check the padding
1419 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001420 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001421 unsigned char *dec_msg;
1422 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001423 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001424 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001425 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001426
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 /*
Paul Bakker45829992013-01-03 14:52:21 +01001428 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 */
Paul Bakker48916f92012-09-16 19:57:18 +00001430 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 {
1432 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001433 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001434 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 }
1436
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001437#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001438 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1439 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001440#endif
Paul Bakker45829992013-01-03 14:52:21 +01001441
1442 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1443 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1444 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001445 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1446 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1447 ssl->transform_in->ivlen,
1448 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001449 return( POLARSSL_ERR_SSL_INVALID_MAC );
1450 }
1451
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001452 dec_msglen = ssl->in_msglen;
1453 dec_msg = ssl->in_msg;
1454 dec_msg_result = ssl->in_msg;
1455
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001456#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001457 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001458 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001459 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001460 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001461 {
Paul Bakker48916f92012-09-16 19:57:18 +00001462 dec_msglen -= ssl->transform_in->ivlen;
1463 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001464
Paul Bakker48916f92012-09-16 19:57:18 +00001465 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001466 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001467 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001468#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001469
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001470 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001471 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001472 ssl->transform_in->ivlen,
1473 dec_msg, dec_msglen,
1474 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001475 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001476 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001477 return( ret );
1478 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001479
Paul Bakkercca5b812013-08-31 17:40:26 +02001480 if( dec_msglen != olen )
1481 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001482 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001483 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001484 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001485
1486#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001487 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1488 {
1489 /*
1490 * Save IV in SSL3 and TLS1
1491 */
1492 memcpy( ssl->transform_in->iv_dec,
1493 ssl->transform_in->cipher_ctx_dec.iv,
1494 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001495 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001496#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001497
1498 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001499
1500 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1501 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001502#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001503 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1504 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001505#endif
Paul Bakker45829992013-01-03 14:52:21 +01001506 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001507 correct = 0;
1508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001510#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1512 {
Paul Bakker48916f92012-09-16 19:57:18 +00001513 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001515#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1517 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001518 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001519#endif
Paul Bakker45829992013-01-03 14:52:21 +01001520 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 }
1522 }
1523 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001524#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001525#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1526 defined(POLARSSL_SSL_PROTO_TLS1_2)
1527 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 {
1529 /*
Paul Bakker45829992013-01-03 14:52:21 +01001530 * TLSv1+: always check the padding up to the first failure
1531 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001533 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001534 size_t padding_idx = ssl->in_msglen - padlen - 1;
1535
Paul Bakker956c9e02013-12-19 14:42:28 +01001536 /*
1537 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001538 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001539 *
Paul Bakker61885c72014-04-25 12:59:03 +02001540 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1541 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001542 *
1543 * In both cases we reset padding_idx to a safe value (0) to
1544 * prevent out-of-buffer reads.
1545 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001546 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001547 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1548 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001549
1550 padding_idx *= correct;
1551
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001552 for( i = 1; i <= 256; i++ )
1553 {
1554 real_count &= ( i <= padlen );
1555 pad_count += real_count *
1556 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1557 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001558
1559 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001560
Paul Bakkerd66f0702013-01-31 16:57:45 +01001561#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001562 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001563 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001564#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001565 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001567 else
1568#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1569 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001570 {
1571 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001572 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001573 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001574 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001575 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001576#endif /* POLARSSL_CIPHER_MODE_CBC &&
1577 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001578 {
1579 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001580 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001582
1583 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1584 ssl->in_msg, ssl->in_msglen );
1585
1586 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001587 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001589#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1590 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1591 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001592 if( mode != POLARSSL_MODE_GCM &&
1593 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001594 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001595 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1596
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001597 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001598
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001599 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1600 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001602 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001604#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001605 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1606 {
1607 ssl_mac( &ssl->transform_in->md_ctx_dec,
1608 ssl->transform_in->mac_dec,
1609 ssl->in_msg, ssl->in_msglen,
1610 ssl->in_ctr, ssl->in_msgtype );
1611 }
1612 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001613#endif /* POLARSSL_SSL_PROTO_SSL3 */
1614#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001615 defined(POLARSSL_SSL_PROTO_TLS1_2)
1616 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1617 {
1618 /*
1619 * Process MAC and always update for padlen afterwards to make
1620 * total time independent of padlen
1621 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001622 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001623 *
1624 * Known timing attacks:
1625 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1626 *
1627 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1628 * correctly. (We round down instead of up, so -56 is the correct
1629 * value for our calculations instead of -55)
1630 */
1631 size_t j, extra_run = 0;
1632 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1633 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001634
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001635 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001636
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001637 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1638 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1639 ssl->in_msglen );
1640 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1641 ssl->in_msg + ssl->in_msglen );
1642 for( j = 0; j < extra_run; j++ )
1643 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001644
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001645 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1646 }
1647 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001648#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001649 POLARSSL_SSL_PROTO_TLS1_2 */
1650 {
1651 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001652 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001654
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001655 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1656 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1657 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001659 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001660 ssl->transform_in->maclen ) != 0 )
1661 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001662#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001664#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001665 correct = 0;
1666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 /*
1669 * Finally check the correct flag
1670 */
1671 if( correct == 0 )
1672 return( POLARSSL_ERR_SSL_INVALID_MAC );
1673 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001674#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
1676 if( ssl->in_msglen == 0 )
1677 {
1678 ssl->nb_zero++;
1679
1680 /*
1681 * Three or more empty messages may be a DoS attack
1682 * (excessive CPU consumption).
1683 */
1684 if( ssl->nb_zero > 3 )
1685 {
1686 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1687 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001688 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001689 }
1690 }
1691 else
1692 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001693
Paul Bakker23986e52011-04-24 08:57:21 +00001694 for( i = 8; i > 0; i-- )
1695 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001696 break;
1697
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001698 /* The loops goes to its end iff the counter is wrapping */
1699 if( i == 0 )
1700 {
1701 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1702 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1703 }
1704
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1706
1707 return( 0 );
1708}
1709
Paul Bakker2770fbd2012-07-03 13:30:23 +00001710#if defined(POLARSSL_ZLIB_SUPPORT)
1711/*
1712 * Compression/decompression functions
1713 */
1714static int ssl_compress_buf( ssl_context *ssl )
1715{
1716 int ret;
1717 unsigned char *msg_post = ssl->out_msg;
1718 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001719 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001720
1721 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1722
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001723 if( len_pre == 0 )
1724 return( 0 );
1725
Paul Bakker2770fbd2012-07-03 13:30:23 +00001726 memcpy( msg_pre, ssl->out_msg, len_pre );
1727
1728 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1729 ssl->out_msglen ) );
1730
1731 SSL_DEBUG_BUF( 4, "before compression: output payload",
1732 ssl->out_msg, ssl->out_msglen );
1733
Paul Bakker48916f92012-09-16 19:57:18 +00001734 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1735 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1736 ssl->transform_out->ctx_deflate.next_out = msg_post;
1737 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001738
Paul Bakker48916f92012-09-16 19:57:18 +00001739 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001740 if( ret != Z_OK )
1741 {
1742 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1743 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1744 }
1745
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001746 ssl->out_msglen = SSL_BUFFER_LEN -
1747 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001748
Paul Bakker2770fbd2012-07-03 13:30:23 +00001749 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1750 ssl->out_msglen ) );
1751
1752 SSL_DEBUG_BUF( 4, "after compression: output payload",
1753 ssl->out_msg, ssl->out_msglen );
1754
1755 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1756
1757 return( 0 );
1758}
1759
1760static int ssl_decompress_buf( ssl_context *ssl )
1761{
1762 int ret;
1763 unsigned char *msg_post = ssl->in_msg;
1764 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001765 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001766
1767 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1768
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001769 if( len_pre == 0 )
1770 return( 0 );
1771
Paul Bakker2770fbd2012-07-03 13:30:23 +00001772 memcpy( msg_pre, ssl->in_msg, len_pre );
1773
1774 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1775 ssl->in_msglen ) );
1776
1777 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1778 ssl->in_msg, ssl->in_msglen );
1779
Paul Bakker48916f92012-09-16 19:57:18 +00001780 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1781 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1782 ssl->transform_in->ctx_inflate.next_out = msg_post;
1783 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001784
Paul Bakker48916f92012-09-16 19:57:18 +00001785 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001786 if( ret != Z_OK )
1787 {
1788 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1789 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1790 }
1791
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001792 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1793 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001794
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1796 ssl->in_msglen ) );
1797
1798 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1799 ssl->in_msg, ssl->in_msglen );
1800
1801 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1802
1803 return( 0 );
1804}
1805#endif /* POLARSSL_ZLIB_SUPPORT */
1806
Paul Bakker5121ce52009-01-03 21:22:43 +00001807/*
1808 * Fill the input message buffer
1809 */
Paul Bakker23986e52011-04-24 08:57:21 +00001810int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001811{
Paul Bakker23986e52011-04-24 08:57:21 +00001812 int ret;
1813 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001814
1815 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1816
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001817 if( nb_want > SSL_BUFFER_LEN - 8 )
1818 {
1819 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1820 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1821 }
1822
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 while( ssl->in_left < nb_want )
1824 {
1825 len = nb_want - ssl->in_left;
1826 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1827
1828 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1829 ssl->in_left, nb_want ) );
1830 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1831
Paul Bakker831a7552011-05-18 13:32:51 +00001832 if( ret == 0 )
1833 return( POLARSSL_ERR_SSL_CONN_EOF );
1834
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 if( ret < 0 )
1836 return( ret );
1837
1838 ssl->in_left += ret;
1839 }
1840
1841 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1842
1843 return( 0 );
1844}
1845
1846/*
1847 * Flush any data not yet written
1848 */
1849int ssl_flush_output( ssl_context *ssl )
1850{
1851 int ret;
1852 unsigned char *buf;
1853
1854 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1855
1856 while( ssl->out_left > 0 )
1857 {
1858 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1859 5 + ssl->out_msglen, ssl->out_left ) );
1860
Paul Bakker5bd42292012-12-19 14:40:42 +01001861 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001863
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1865
1866 if( ret <= 0 )
1867 return( ret );
1868
1869 ssl->out_left -= ret;
1870 }
1871
1872 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1873
1874 return( 0 );
1875}
1876
1877/*
1878 * Record layer functions
1879 */
1880int ssl_write_record( ssl_context *ssl )
1881{
Paul Bakker05ef8352012-05-08 09:17:57 +00001882 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001883 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
1885 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1886
Paul Bakker5121ce52009-01-03 21:22:43 +00001887 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1888 {
1889 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1890 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1891 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1892
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001893 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1894 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 }
1896
Paul Bakker2770fbd2012-07-03 13:30:23 +00001897#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001898 if( ssl->transform_out != NULL &&
1899 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001900 {
1901 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1902 {
1903 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1904 return( ret );
1905 }
1906
1907 len = ssl->out_msglen;
1908 }
1909#endif /*POLARSSL_ZLIB_SUPPORT */
1910
Paul Bakker05ef8352012-05-08 09:17:57 +00001911#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001912 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001914 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
Paul Bakker05ef8352012-05-08 09:17:57 +00001916 ret = ssl_hw_record_write( ssl );
1917 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1918 {
1919 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001920 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001921 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001922
1923 if( ret == 0 )
1924 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001925 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001926#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001927 if( !done )
1928 {
1929 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1930 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1931 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001932 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1933 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001934
Paul Bakker48916f92012-09-16 19:57:18 +00001935 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001936 {
1937 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1938 {
1939 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1940 return( ret );
1941 }
1942
1943 len = ssl->out_msglen;
1944 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1945 ssl->out_hdr[4] = (unsigned char)( len );
1946 }
1947
1948 ssl->out_left = 5 + ssl->out_msglen;
1949
1950 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1951 "version = [%d:%d], msglen = %d",
1952 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1953 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1954
1955 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001956 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 }
1958
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1960 {
1961 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1962 return( ret );
1963 }
1964
1965 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1966
1967 return( 0 );
1968}
1969
1970int ssl_read_record( ssl_context *ssl )
1971{
Paul Bakker05ef8352012-05-08 09:17:57 +00001972 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001973
1974 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1975
1976 if( ssl->in_hslen != 0 &&
1977 ssl->in_hslen < ssl->in_msglen )
1978 {
1979 /*
1980 * Get next Handshake message in the current record
1981 */
1982 ssl->in_msglen -= ssl->in_hslen;
1983
Paul Bakker8934a982011-08-05 11:11:53 +00001984 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001985 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
1987 ssl->in_hslen = 4;
1988 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
1989
1990 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
1991 " %d, type = %d, hslen = %d",
1992 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
1993
1994 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
1995 {
1996 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001997 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998 }
1999
2000 if( ssl->in_msglen < ssl->in_hslen )
2001 {
2002 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002003 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 }
2005
Paul Bakker4224bc02014-04-08 14:36:50 +02002006 if( ssl->state != SSL_HANDSHAKE_OVER )
2007 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
2009 return( 0 );
2010 }
2011
2012 ssl->in_hslen = 0;
2013
2014 /*
2015 * Read the record header and validate it
2016 */
2017 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2018 {
2019 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2020 return( ret );
2021 }
2022
2023 ssl->in_msgtype = ssl->in_hdr[0];
2024 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2025
2026 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2027 "version = [%d:%d], msglen = %d",
2028 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2029 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2030
2031 if( ssl->in_hdr[1] != ssl->major_ver )
2032 {
2033 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002034 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 }
2036
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002037 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 {
2039 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002040 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 }
2042
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002043 /* Sanity check (outer boundaries) */
2044 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2045 {
2046 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2047 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2048 }
2049
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002051 * Make sure the message length is acceptable for the current transform
2052 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 */
Paul Bakker48916f92012-09-16 19:57:18 +00002054 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002056 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 {
2058 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002059 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 }
2061 }
2062 else
2063 {
Paul Bakker48916f92012-09-16 19:57:18 +00002064 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 {
2066 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002067 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
2069
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002070#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002072 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 {
2074 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002075 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002078
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002079#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2080 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 /*
2082 * TLS encrypted messages can have up to 256 bytes of padding
2083 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002084 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002085 ssl->in_msglen > ssl->transform_in->minlen +
2086 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 {
2088 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002089 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002091#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093
2094 /*
2095 * Read and optionally decrypt the message contents
2096 */
2097 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2098 {
2099 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2100 return( ret );
2101 }
2102
2103 SSL_DEBUG_BUF( 4, "input record from network",
2104 ssl->in_hdr, 5 + ssl->in_msglen );
2105
Paul Bakker05ef8352012-05-08 09:17:57 +00002106#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002107 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002108 {
2109 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2110
2111 ret = ssl_hw_record_read( ssl );
2112 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2113 {
2114 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002115 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002116 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002117
2118 if( ret == 0 )
2119 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002120 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002121#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002122 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 {
2124 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2125 {
Paul Bakker40865c82013-01-31 17:13:13 +01002126#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2127 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2128 {
2129 ssl_send_alert_message( ssl,
2130 SSL_ALERT_LEVEL_FATAL,
2131 SSL_ALERT_MSG_BAD_RECORD_MAC );
2132 }
2133#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2135 return( ret );
2136 }
2137
2138 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2139 ssl->in_msg, ssl->in_msglen );
2140
2141 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2142 {
2143 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002144 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 }
2146 }
2147
Paul Bakker2770fbd2012-07-03 13:30:23 +00002148#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002149 if( ssl->transform_in != NULL &&
2150 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002151 {
2152 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2153 {
2154 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2155 return( ret );
2156 }
2157
2158 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2159 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2160 }
2161#endif /* POLARSSL_ZLIB_SUPPORT */
2162
Paul Bakker0a925182012-04-16 06:46:41 +00002163 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2164 ssl->in_msgtype != SSL_MSG_ALERT &&
2165 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2166 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2167 {
2168 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2169
Paul Bakker48916f92012-09-16 19:57:18 +00002170 if( ( ret = ssl_send_alert_message( ssl,
2171 SSL_ALERT_LEVEL_FATAL,
2172 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002173 {
2174 return( ret );
2175 }
2176
2177 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2178 }
2179
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2181 {
2182 ssl->in_hslen = 4;
2183 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2184
2185 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2186 " %d, type = %d, hslen = %d",
2187 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2188
2189 /*
2190 * Additional checks to validate the handshake header
2191 */
2192 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2193 {
2194 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002195 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 }
2197
2198 if( ssl->in_msglen < ssl->in_hslen )
2199 {
2200 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002201 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 }
2203
Paul Bakker48916f92012-09-16 19:57:18 +00002204 if( ssl->state != SSL_HANDSHAKE_OVER )
2205 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
2207
2208 if( ssl->in_msgtype == SSL_MSG_ALERT )
2209 {
2210 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2211 ssl->in_msg[0], ssl->in_msg[1] ) );
2212
2213 /*
2214 * Ignore non-fatal alerts, except close_notify
2215 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002216 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002218 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2219 ssl->in_msg[1] ) );
Paul Bakker9d781402011-05-09 16:17:09 +00002220 /**
2221 * Subtract from error code as ssl->in_msg[1] is 7-bit positive
2222 * error identifier.
2223 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002224 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 }
2226
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002227 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2228 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 {
2230 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002231 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 }
2233 }
2234
2235 ssl->in_left = 0;
2236
2237 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2238
2239 return( 0 );
2240}
2241
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002242int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2243{
2244 int ret;
2245
2246 if( ( ret = ssl_send_alert_message( ssl,
2247 SSL_ALERT_LEVEL_FATAL,
2248 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2249 {
2250 return( ret );
2251 }
2252
2253 return( 0 );
2254}
2255
Paul Bakker0a925182012-04-16 06:46:41 +00002256int ssl_send_alert_message( ssl_context *ssl,
2257 unsigned char level,
2258 unsigned char message )
2259{
2260 int ret;
2261
2262 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2263
2264 ssl->out_msgtype = SSL_MSG_ALERT;
2265 ssl->out_msglen = 2;
2266 ssl->out_msg[0] = level;
2267 ssl->out_msg[1] = message;
2268
2269 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2270 {
2271 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2272 return( ret );
2273 }
2274
2275 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2276
2277 return( 0 );
2278}
2279
Paul Bakker5121ce52009-01-03 21:22:43 +00002280/*
2281 * Handshake functions
2282 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002283#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2284 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2285 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2286 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2287 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2288 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2289 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002290int ssl_write_certificate( ssl_context *ssl )
2291{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002292 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
2294 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2295
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002296 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002297 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2298 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002299 {
2300 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2301 ssl->state++;
2302 return( 0 );
2303 }
2304
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002305 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2306 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307}
2308
2309int ssl_parse_certificate( ssl_context *ssl )
2310{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002311 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2312
2313 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2314
2315 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002316 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2317 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002318 {
2319 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2320 ssl->state++;
2321 return( 0 );
2322 }
2323
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002324 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2325 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002326}
2327#else
2328int ssl_write_certificate( ssl_context *ssl )
2329{
2330 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2331 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002332 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002333 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2334
2335 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2336
2337 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002338 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2339 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002340 {
2341 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2342 ssl->state++;
2343 return( 0 );
2344 }
2345
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 if( ssl->endpoint == SSL_IS_CLIENT )
2347 {
2348 if( ssl->client_auth == 0 )
2349 {
2350 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2351 ssl->state++;
2352 return( 0 );
2353 }
2354
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002355#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002356 /*
2357 * If using SSLv3 and got no cert, send an Alert message
2358 * (otherwise an empty Certificate message will be sent).
2359 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002360 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2362 {
2363 ssl->out_msglen = 2;
2364 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002365 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2366 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
2368 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2369 goto write_msg;
2370 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002371#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 }
2373 else /* SSL_IS_SERVER */
2374 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002375 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 {
2377 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002378 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 }
2380 }
2381
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002382 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002383
2384 /*
2385 * 0 . 0 handshake type
2386 * 1 . 3 handshake length
2387 * 4 . 6 length of all certs
2388 * 7 . 9 length of cert. 1
2389 * 10 . n-1 peer certificate
2390 * n . n+2 length of cert. 2
2391 * n+3 . ... upper level cert, etc.
2392 */
2393 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002394 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
Paul Bakker29087132010-03-21 21:03:34 +00002396 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 {
2398 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002399 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 {
2401 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2402 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002403 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 }
2405
2406 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2407 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2408 ssl->out_msg[i + 2] = (unsigned char)( n );
2409
2410 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2411 i += n; crt = crt->next;
2412 }
2413
2414 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2415 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2416 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2417
2418 ssl->out_msglen = i;
2419 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2420 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2421
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002422#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002423write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002424#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
2426 ssl->state++;
2427
2428 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2429 {
2430 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2431 return( ret );
2432 }
2433
2434 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2435
Paul Bakkered27a042013-04-18 22:46:23 +02002436 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437}
2438
2439int ssl_parse_certificate( ssl_context *ssl )
2440{
Paul Bakkered27a042013-04-18 22:46:23 +02002441 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002442 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002443 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2446
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002447 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002448 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2449 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002450 {
2451 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2452 ssl->state++;
2453 return( 0 );
2454 }
2455
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002457 ( ssl->authmode == SSL_VERIFY_NONE ||
2458 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002460 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2462 ssl->state++;
2463 return( 0 );
2464 }
2465
2466 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2467 {
2468 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2469 return( ret );
2470 }
2471
2472 ssl->state++;
2473
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002474#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 /*
2476 * Check if the client sent an empty certificate
2477 */
2478 if( ssl->endpoint == SSL_IS_SERVER &&
2479 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2480 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002481 if( ssl->in_msglen == 2 &&
2482 ssl->in_msgtype == SSL_MSG_ALERT &&
2483 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2484 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 {
2486 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2487
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002488 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2490 return( 0 );
2491 else
Paul Bakker40e46942009-01-03 21:51:57 +00002492 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 }
2494 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002495#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002497#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2498 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 if( ssl->endpoint == SSL_IS_SERVER &&
2500 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2501 {
2502 if( ssl->in_hslen == 7 &&
2503 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2504 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2505 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2506 {
2507 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2508
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002509 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002511 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 else
2513 return( 0 );
2514 }
2515 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002516#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2517 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
2519 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2520 {
2521 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002522 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 }
2524
2525 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2526 {
2527 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002528 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 }
2530
2531 /*
2532 * Same message structure as in ssl_write_certificate()
2533 */
2534 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2535
2536 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2537 {
2538 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002539 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 }
2541
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002542 /* In case we tried to reuse a session but it failed */
2543 if( ssl->session_negotiate->peer_cert != NULL )
2544 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002545 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002546 polarssl_free( ssl->session_negotiate->peer_cert );
2547 }
2548
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002549 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2550 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 {
2552 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002553 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002554 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 }
2556
Paul Bakkerb6b09562013-09-18 14:17:41 +02002557 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
2559 i = 7;
2560
2561 while( i < ssl->in_hslen )
2562 {
2563 if( ssl->in_msg[i] != 0 )
2564 {
2565 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002566 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 }
2568
2569 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2570 | (unsigned int) ssl->in_msg[i + 2];
2571 i += 3;
2572
2573 if( n < 128 || i + n > ssl->in_hslen )
2574 {
2575 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002576 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 }
2578
Paul Bakkerddf26b42013-09-18 13:46:23 +02002579 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2580 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 if( ret != 0 )
2582 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002583 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 return( ret );
2585 }
2586
2587 i += n;
2588 }
2589
Paul Bakker48916f92012-09-16 19:57:18 +00002590 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002592 /*
2593 * On client, make sure the server cert doesn't change during renego to
2594 * avoid "triple handshake" attack: https://secure-resumption.com/
2595 */
2596 if( ssl->endpoint == SSL_IS_CLIENT &&
2597 ssl->renegotiation == SSL_RENEGOTIATION )
2598 {
2599 if( ssl->session->peer_cert == NULL )
2600 {
2601 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2602 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2603 }
2604
2605 if( ssl->session->peer_cert->raw.len !=
2606 ssl->session_negotiate->peer_cert->raw.len ||
2607 memcmp( ssl->session->peer_cert->raw.p,
2608 ssl->session_negotiate->peer_cert->raw.p,
2609 ssl->session->peer_cert->raw.len ) != 0 )
2610 {
2611 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2612 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2613 }
2614 }
2615
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 if( ssl->authmode != SSL_VERIFY_NONE )
2617 {
2618 if( ssl->ca_chain == NULL )
2619 {
2620 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002621 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 }
2623
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002624 /*
2625 * Main check: verify certificate
2626 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002627 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2628 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2629 &ssl->session_negotiate->verify_result,
2630 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
2632 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002633 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002635 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002636
2637 /*
2638 * Secondary checks: always done, but change 'ret' only if it was 0
2639 */
2640
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002641#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002642 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002643 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002644
2645 /* If certificate uses an EC key, make sure the curve is OK */
2646 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2647 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2648 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002649 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002650 if( ret == 0 )
2651 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002652 }
2653 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002654#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002656 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2657 ciphersuite_info,
2658 ! ssl->endpoint ) != 0 )
2659 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002660 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002661 if( ret == 0 )
2662 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2663 }
2664
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2666 ret = 0;
2667 }
2668
2669 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2670
2671 return( ret );
2672}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002673#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2674 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2675 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2676 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2677 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2678 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2679 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
2681int ssl_write_change_cipher_spec( ssl_context *ssl )
2682{
2683 int ret;
2684
2685 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2686
2687 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2688 ssl->out_msglen = 1;
2689 ssl->out_msg[0] = 1;
2690
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 ssl->state++;
2692
2693 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2694 {
2695 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2696 return( ret );
2697 }
2698
2699 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2700
2701 return( 0 );
2702}
2703
2704int ssl_parse_change_cipher_spec( ssl_context *ssl )
2705{
2706 int ret;
2707
2708 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2709
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2711 {
2712 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2713 return( ret );
2714 }
2715
2716 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2717 {
2718 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002719 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 }
2721
2722 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2723 {
2724 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002725 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
2728 ssl->state++;
2729
2730 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2731
2732 return( 0 );
2733}
2734
Paul Bakker41c83d32013-03-20 14:39:14 +01002735void ssl_optimize_checksum( ssl_context *ssl,
2736 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002737{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002738 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002739
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002740#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2741 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002742 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002743 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002744 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002745#endif
2746#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2747#if defined(POLARSSL_SHA512_C)
2748 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2749 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2750 else
2751#endif
2752#if defined(POLARSSL_SHA256_C)
2753 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002754 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002755 else
2756#endif
2757#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002758 {
2759 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002760 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002761 }
Paul Bakker380da532012-04-18 16:10:25 +00002762}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002763
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002764static void ssl_update_checksum_start( ssl_context *ssl,
2765 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002766{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002767#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2768 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002769 md5_update( &ssl->handshake->fin_md5 , buf, len );
2770 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002771#endif
2772#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2773#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002774 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002775#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002776#if defined(POLARSSL_SHA512_C)
2777 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002778#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002779#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002780}
2781
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002782#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2783 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002784static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2785 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002786{
Paul Bakker48916f92012-09-16 19:57:18 +00002787 md5_update( &ssl->handshake->fin_md5 , buf, len );
2788 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002789}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002790#endif
Paul Bakker380da532012-04-18 16:10:25 +00002791
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002792#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2793#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002794static void ssl_update_checksum_sha256( ssl_context *ssl,
2795 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002796{
Paul Bakker9e36f042013-06-30 14:34:05 +02002797 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002798}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002799#endif
Paul Bakker380da532012-04-18 16:10:25 +00002800
Paul Bakker9e36f042013-06-30 14:34:05 +02002801#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002802static void ssl_update_checksum_sha384( ssl_context *ssl,
2803 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002804{
Paul Bakker9e36f042013-06-30 14:34:05 +02002805 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002806}
Paul Bakker769075d2012-11-24 11:26:46 +01002807#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002808#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002809
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002810#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002811static void ssl_calc_finished_ssl(
2812 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002813{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002814 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002815 md5_context md5;
2816 sha1_context sha1;
2817
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 unsigned char padbuf[48];
2819 unsigned char md5sum[16];
2820 unsigned char sha1sum[20];
2821
Paul Bakker48916f92012-09-16 19:57:18 +00002822 ssl_session *session = ssl->session_negotiate;
2823 if( !session )
2824 session = ssl->session;
2825
Paul Bakker1ef83d62012-04-11 12:09:53 +00002826 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2827
Paul Bakker48916f92012-09-16 19:57:18 +00002828 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2829 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
2831 /*
2832 * SSLv3:
2833 * hash =
2834 * MD5( master + pad2 +
2835 * MD5( handshake + sender + master + pad1 ) )
2836 * + SHA1( master + pad2 +
2837 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002838 */
2839
Paul Bakker90995b52013-06-24 19:20:35 +02002840#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002842 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002843#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Paul Bakker90995b52013-06-24 19:20:35 +02002845#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002846 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002847 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002848#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Paul Bakker3c2122f2013-06-24 19:03:14 +02002850 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2851 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
Paul Bakker1ef83d62012-04-11 12:09:53 +00002853 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
Paul Bakker3c2122f2013-06-24 19:03:14 +02002855 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002856 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002857 md5_update( &md5, padbuf, 48 );
2858 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Paul Bakker3c2122f2013-06-24 19:03:14 +02002860 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002861 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002862 sha1_update( &sha1, padbuf, 40 );
2863 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Paul Bakker1ef83d62012-04-11 12:09:53 +00002865 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
Paul Bakker1ef83d62012-04-11 12:09:53 +00002867 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002868 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 md5_update( &md5, padbuf, 48 );
2870 md5_update( &md5, md5sum, 16 );
2871 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Paul Bakker1ef83d62012-04-11 12:09:53 +00002873 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002874 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002875 sha1_update( &sha1, padbuf , 40 );
2876 sha1_update( &sha1, sha1sum, 20 );
2877 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002878
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
Paul Bakker34617722014-06-13 17:20:13 +02002881 polarssl_zeroize( &md5, sizeof( md5_context ) );
2882 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Paul Bakker34617722014-06-13 17:20:13 +02002884 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2885 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2886 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
2888 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2889}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002890#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002892#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002893static void ssl_calc_finished_tls(
2894 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002895{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002897 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002898 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
Paul Bakker48916f92012-09-16 19:57:18 +00002902 ssl_session *session = ssl->session_negotiate;
2903 if( !session )
2904 session = ssl->session;
2905
Paul Bakker1ef83d62012-04-11 12:09:53 +00002906 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
Paul Bakker48916f92012-09-16 19:57:18 +00002908 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2909 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911 /*
2912 * TLSv1:
2913 * hash = PRF( master, finished_label,
2914 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2915 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Paul Bakker90995b52013-06-24 19:20:35 +02002917#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002918 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2919 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002920#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002921
Paul Bakker90995b52013-06-24 19:20:35 +02002922#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002923 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2924 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002925#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002926
2927 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002928 ? "client finished"
2929 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002930
2931 md5_finish( &md5, padbuf );
2932 sha1_finish( &sha1, padbuf + 16 );
2933
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002934 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002935 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936
2937 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2938
Paul Bakker34617722014-06-13 17:20:13 +02002939 polarssl_zeroize( &md5, sizeof( md5_context ) );
2940 polarssl_zeroize( &sha1, sizeof( sha1_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002941
Paul Bakker34617722014-06-13 17:20:13 +02002942 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002943
2944 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2945}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002947
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002948#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2949#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002950static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951 ssl_context *ssl, unsigned char *buf, int from )
2952{
2953 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002954 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002955 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956 unsigned char padbuf[32];
2957
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ssl_session *session = ssl->session_negotiate;
2959 if( !session )
2960 session = ssl->session;
2961
Paul Bakker380da532012-04-18 16:10:25 +00002962 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963
Paul Bakker9e36f042013-06-30 14:34:05 +02002964 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002965
2966 /*
2967 * TLSv1.2:
2968 * hash = PRF( master, finished_label,
2969 * Hash( handshake ) )[0.11]
2970 */
2971
Paul Bakker9e36f042013-06-30 14:34:05 +02002972#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002973 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002974 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002975#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976
2977 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002978 ? "client finished"
2979 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002980
Paul Bakker9e36f042013-06-30 14:34:05 +02002981 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002983 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002984 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002985
2986 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2987
Paul Bakker34617722014-06-13 17:20:13 +02002988 polarssl_zeroize( &sha256, sizeof( sha256_context ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989
Paul Bakker34617722014-06-13 17:20:13 +02002990 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002991
2992 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2993}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002994#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995
Paul Bakker9e36f042013-06-30 14:34:05 +02002996#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002997static void ssl_calc_finished_tls_sha384(
2998 ssl_context *ssl, unsigned char *buf, int from )
2999{
3000 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003001 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003002 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003003 unsigned char padbuf[48];
3004
Paul Bakker48916f92012-09-16 19:57:18 +00003005 ssl_session *session = ssl->session_negotiate;
3006 if( !session )
3007 session = ssl->session;
3008
Paul Bakker380da532012-04-18 16:10:25 +00003009 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003010
Paul Bakker9e36f042013-06-30 14:34:05 +02003011 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003012
3013 /*
3014 * TLSv1.2:
3015 * hash = PRF( master, finished_label,
3016 * Hash( handshake ) )[0.11]
3017 */
3018
Paul Bakker9e36f042013-06-30 14:34:05 +02003019#if !defined(POLARSSL_SHA512_ALT)
3020 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3021 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003022#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003023
3024 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003025 ? "client finished"
3026 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003027
Paul Bakker9e36f042013-06-30 14:34:05 +02003028 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003029
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003030 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003031 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003032
3033 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3034
Paul Bakker34617722014-06-13 17:20:13 +02003035 polarssl_zeroize( &sha512, sizeof( sha512_context ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003036
Paul Bakker34617722014-06-13 17:20:13 +02003037 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003038
3039 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3040}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003041#endif /* POLARSSL_SHA512_C */
3042#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003043
Paul Bakker48916f92012-09-16 19:57:18 +00003044void ssl_handshake_wrapup( ssl_context *ssl )
3045{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003046 int resume = ssl->handshake->resume;
3047
Paul Bakker48916f92012-09-16 19:57:18 +00003048 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3049
3050 /*
3051 * Free our handshake params
3052 */
3053 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003054 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003055 ssl->handshake = NULL;
3056
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003057 if( ssl->renegotiation == SSL_RENEGOTIATION )
3058 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3059
Paul Bakker48916f92012-09-16 19:57:18 +00003060 /*
3061 * Switch in our now active transform context
3062 */
3063 if( ssl->transform )
3064 {
3065 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003066 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003067 }
3068 ssl->transform = ssl->transform_negotiate;
3069 ssl->transform_negotiate = NULL;
3070
Paul Bakker0a597072012-09-25 21:55:46 +00003071 if( ssl->session )
3072 {
3073 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003074 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003075 }
3076 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003077 ssl->session_negotiate = NULL;
3078
Paul Bakker0a597072012-09-25 21:55:46 +00003079 /*
3080 * Add cache entry
3081 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003082 if( ssl->f_set_cache != NULL &&
3083 ssl->session->length != 0 &&
3084 resume == 0 )
3085 {
Paul Bakker0a597072012-09-25 21:55:46 +00003086 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3087 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003088 }
Paul Bakker0a597072012-09-25 21:55:46 +00003089
Paul Bakker48916f92012-09-16 19:57:18 +00003090 ssl->state++;
3091
3092 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3093}
3094
Paul Bakker1ef83d62012-04-11 12:09:53 +00003095int ssl_write_finished( ssl_context *ssl )
3096{
3097 int ret, hash_len;
3098
3099 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3100
Paul Bakker92be97b2013-01-02 17:30:03 +01003101 /*
3102 * Set the out_msg pointer to the correct location based on IV length
3103 */
3104 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3105 {
3106 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3107 ssl->transform_negotiate->fixed_ivlen;
3108 }
3109 else
3110 ssl->out_msg = ssl->out_iv;
3111
Paul Bakker48916f92012-09-16 19:57:18 +00003112 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003113
3114 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3116
Paul Bakker48916f92012-09-16 19:57:18 +00003117 ssl->verify_data_len = hash_len;
3118 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3119
Paul Bakker5121ce52009-01-03 21:22:43 +00003120 ssl->out_msglen = 4 + hash_len;
3121 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3122 ssl->out_msg[0] = SSL_HS_FINISHED;
3123
3124 /*
3125 * In case of session resuming, invert the client and server
3126 * ChangeCipherSpec messages order.
3127 */
Paul Bakker0a597072012-09-25 21:55:46 +00003128 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003129 {
3130 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003131 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 else
3133 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3134 }
3135 else
3136 ssl->state++;
3137
Paul Bakker48916f92012-09-16 19:57:18 +00003138 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003139 * Switch to our negotiated transform and session parameters for outbound
3140 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003141 */
3142 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3143 ssl->transform_out = ssl->transform_negotiate;
3144 ssl->session_out = ssl->session_negotiate;
3145 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003146
Paul Bakker07eb38b2012-12-19 14:42:06 +01003147#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003148 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003149 {
3150 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3151 {
3152 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3153 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3154 }
3155 }
3156#endif
3157
Paul Bakker5121ce52009-01-03 21:22:43 +00003158 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3159 {
3160 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3161 return( ret );
3162 }
3163
3164 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3165
3166 return( 0 );
3167}
3168
3169int ssl_parse_finished( ssl_context *ssl )
3170{
Paul Bakker23986e52011-04-24 08:57:21 +00003171 int ret;
3172 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003173 unsigned char buf[36];
3174
3175 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3176
Paul Bakker48916f92012-09-16 19:57:18 +00003177 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003178
Paul Bakker48916f92012-09-16 19:57:18 +00003179 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003180 * Switch to our negotiated transform and session parameters for inbound
3181 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003182 */
3183 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3184 ssl->transform_in = ssl->transform_negotiate;
3185 ssl->session_in = ssl->session_negotiate;
3186 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003187
Paul Bakker92be97b2013-01-02 17:30:03 +01003188 /*
3189 * Set the in_msg pointer to the correct location based on IV length
3190 */
3191 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3192 {
3193 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3194 ssl->transform_negotiate->fixed_ivlen;
3195 }
3196 else
3197 ssl->in_msg = ssl->in_iv;
3198
Paul Bakker07eb38b2012-12-19 14:42:06 +01003199#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003200 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003201 {
3202 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3203 {
3204 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3205 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3206 }
3207 }
3208#endif
3209
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3211 {
3212 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3213 return( ret );
3214 }
3215
3216 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3217 {
3218 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003219 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 }
3221
Paul Bakker1ef83d62012-04-11 12:09:53 +00003222 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3224
3225 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3226 ssl->in_hslen != 4 + hash_len )
3227 {
3228 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003229 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230 }
3231
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003232 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 {
3234 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003235 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 }
3237
Paul Bakker48916f92012-09-16 19:57:18 +00003238 ssl->verify_data_len = hash_len;
3239 memcpy( ssl->peer_verify_data, buf, hash_len );
3240
Paul Bakker0a597072012-09-25 21:55:46 +00003241 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003242 {
3243 if( ssl->endpoint == SSL_IS_CLIENT )
3244 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3245
3246 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003247 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 }
3249 else
3250 ssl->state++;
3251
3252 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3253
3254 return( 0 );
3255}
3256
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003257static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003258{
3259 if( ssl->transform_negotiate )
3260 ssl_transform_free( ssl->transform_negotiate );
3261 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003262 {
3263 ssl->transform_negotiate =
3264 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003265
3266 if( ssl->transform_negotiate != NULL )
3267 memset( ssl->transform_negotiate, 0, sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003268 }
Paul Bakker48916f92012-09-16 19:57:18 +00003269
3270 if( ssl->session_negotiate )
3271 ssl_session_free( ssl->session_negotiate );
3272 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003273 {
3274 ssl->session_negotiate =
3275 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003276
3277 if( ssl->session_negotiate != NULL )
3278 memset( ssl->session_negotiate, 0, sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003279 }
Paul Bakker48916f92012-09-16 19:57:18 +00003280
3281 if( ssl->handshake )
3282 ssl_handshake_free( ssl->handshake );
3283 else
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003284 {
3285 ssl->handshake = (ssl_handshake_params *)
3286 polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakker77f4f392014-03-26 15:28:55 +01003287
3288 if( ssl->handshake != NULL )
3289 memset( ssl->handshake, 0, sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003290 }
Paul Bakker48916f92012-09-16 19:57:18 +00003291
3292 if( ssl->handshake == NULL ||
3293 ssl->transform_negotiate == NULL ||
3294 ssl->session_negotiate == NULL )
3295 {
3296 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
3297 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3298 }
3299
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003300#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3301 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003302 md5_starts( &ssl->handshake->fin_md5 );
3303 sha1_starts( &ssl->handshake->fin_sha1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003304#endif
3305#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3306#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003307 sha256_starts( &ssl->handshake->fin_sha256, 0 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003308#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003309#if defined(POLARSSL_SHA512_C)
3310 sha512_starts( &ssl->handshake->fin_sha512, 1 );
Paul Bakker769075d2012-11-24 11:26:46 +01003311#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003312#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00003313
3314 ssl->handshake->update_checksum = ssl_update_checksum_start;
Paul Bakker23f36802012-09-28 14:15:14 +00003315 ssl->handshake->sig_alg = SSL_HASH_SHA1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02003316
Paul Bakker61d113b2013-07-04 11:51:43 +02003317#if defined(POLARSSL_ECDH_C)
3318 ecdh_init( &ssl->handshake->ecdh_ctx );
3319#endif
3320
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003321#if defined(POLARSSL_X509_CRT_PARSE_C)
3322 ssl->handshake->key_cert = ssl->key_cert;
3323#endif
3324
Paul Bakker48916f92012-09-16 19:57:18 +00003325 return( 0 );
3326}
3327
Paul Bakker5121ce52009-01-03 21:22:43 +00003328/*
3329 * Initialize an SSL context
3330 */
3331int ssl_init( ssl_context *ssl )
3332{
Paul Bakker48916f92012-09-16 19:57:18 +00003333 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 int len = SSL_BUFFER_LEN;
3335
3336 memset( ssl, 0, sizeof( ssl_context ) );
3337
Paul Bakker62f2dee2012-09-28 07:31:51 +00003338 /*
3339 * Sane defaults
3340 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003341 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3342 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3343 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3344 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003345
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003346 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003347
Paul Bakker62f2dee2012-09-28 07:31:51 +00003348#if defined(POLARSSL_DHM_C)
3349 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3350 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3351 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3352 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3353 {
3354 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3355 return( ret );
3356 }
3357#endif
3358
3359 /*
3360 * Prepare base structures
3361 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003362 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003364 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003365 ssl->in_msg = ssl->in_ctr + 13;
3366
3367 if( ssl->in_ctr == NULL )
3368 {
3369 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003370 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003371 }
3372
Paul Bakker6e339b52013-07-03 13:37:05 +02003373 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003375 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003376 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003377
3378 if( ssl->out_ctr == NULL )
3379 {
3380 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003381 polarssl_free( ssl->in_ctr );
3382 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003383 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003384 }
3385
3386 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3387 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3388
Paul Bakker606b4ba2013-08-14 16:52:14 +02003389#if defined(POLARSSL_SSL_SESSION_TICKETS)
3390 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3391#endif
3392
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003393#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003394 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003395#endif
3396
Paul Bakker48916f92012-09-16 19:57:18 +00003397 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3398 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003399
3400 return( 0 );
3401}
3402
3403/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003404 * Reset an initialized and used SSL context for re-use while retaining
3405 * all application-set variables, function pointers and data.
3406 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003407int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003408{
Paul Bakker48916f92012-09-16 19:57:18 +00003409 int ret;
3410
Paul Bakker7eb013f2011-10-06 12:37:39 +00003411 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003412 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3413 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3414
3415 ssl->verify_data_len = 0;
3416 memset( ssl->own_verify_data, 0, 36 );
3417 memset( ssl->peer_verify_data, 0, 36 );
3418
Paul Bakker7eb013f2011-10-06 12:37:39 +00003419 ssl->in_offt = NULL;
3420
Paul Bakker92be97b2013-01-02 17:30:03 +01003421 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003422 ssl->in_msgtype = 0;
3423 ssl->in_msglen = 0;
3424 ssl->in_left = 0;
3425
3426 ssl->in_hslen = 0;
3427 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003428 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003429
Paul Bakker92be97b2013-01-02 17:30:03 +01003430 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003431 ssl->out_msgtype = 0;
3432 ssl->out_msglen = 0;
3433 ssl->out_left = 0;
3434
Paul Bakker48916f92012-09-16 19:57:18 +00003435 ssl->transform_in = NULL;
3436 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003437
3438 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3439 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003440
3441#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003442 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003443 {
3444 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003445 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003446 {
3447 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3448 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3449 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003450 }
3451#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003452
Paul Bakker48916f92012-09-16 19:57:18 +00003453 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003454 {
Paul Bakker48916f92012-09-16 19:57:18 +00003455 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003456 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003457 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003458 }
Paul Bakker48916f92012-09-16 19:57:18 +00003459
Paul Bakkerc0463502013-02-14 11:19:38 +01003460 if( ssl->session )
3461 {
3462 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003463 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003464 ssl->session = NULL;
3465 }
3466
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003467#if defined(POLARSSL_SSL_ALPN)
3468 ssl->alpn_chosen = NULL;
3469#endif
3470
Paul Bakker48916f92012-09-16 19:57:18 +00003471 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3472 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003473
3474 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003475}
3476
Paul Bakkera503a632013-08-14 13:48:06 +02003477#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker7eb013f2011-10-06 12:37:39 +00003478/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003479 * Allocate and initialize ticket keys
3480 */
3481static int ssl_ticket_keys_init( ssl_context *ssl )
3482{
3483 int ret;
3484 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003485 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003486
3487 if( ssl->ticket_keys != NULL )
3488 return( 0 );
3489
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003490 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3491 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003492 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3493
3494 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003495 {
3496 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003497 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003498 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003499
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003500 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3501 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3502 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3503 {
Paul Bakker6f0636a2013-12-16 15:24:05 +01003504 polarssl_free( tkeys );
3505 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003506 }
3507
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003508 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003509 {
3510 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003511 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003512 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003513
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003514 ssl->ticket_keys = tkeys;
3515
3516 return( 0 );
3517}
Paul Bakkera503a632013-08-14 13:48:06 +02003518#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003519
3520/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003521 * SSL set accessors
3522 */
3523void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3524{
3525 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003526
Paul Bakker606b4ba2013-08-14 16:52:14 +02003527#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003528 if( endpoint == SSL_IS_CLIENT )
3529 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003530#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003531}
3532
3533void ssl_set_authmode( ssl_context *ssl, int authmode )
3534{
3535 ssl->authmode = authmode;
3536}
3537
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003538#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003539void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003540 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003541 void *p_vrfy )
3542{
3543 ssl->f_vrfy = f_vrfy;
3544 ssl->p_vrfy = p_vrfy;
3545}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003546#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003547
Paul Bakker5121ce52009-01-03 21:22:43 +00003548void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003549 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003550 void *p_rng )
3551{
3552 ssl->f_rng = f_rng;
3553 ssl->p_rng = p_rng;
3554}
3555
3556void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003557 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 void *p_dbg )
3559{
3560 ssl->f_dbg = f_dbg;
3561 ssl->p_dbg = p_dbg;
3562}
3563
3564void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003565 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003566 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003567{
3568 ssl->f_recv = f_recv;
3569 ssl->f_send = f_send;
3570 ssl->p_recv = p_recv;
3571 ssl->p_send = p_send;
3572}
3573
Paul Bakker0a597072012-09-25 21:55:46 +00003574void ssl_set_session_cache( ssl_context *ssl,
3575 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3576 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003577{
Paul Bakker0a597072012-09-25 21:55:46 +00003578 ssl->f_get_cache = f_get_cache;
3579 ssl->p_get_cache = p_get_cache;
3580 ssl->f_set_cache = f_set_cache;
3581 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003582}
3583
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003584int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003585{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003586 int ret;
3587
3588 if( ssl == NULL ||
3589 session == NULL ||
3590 ssl->session_negotiate == NULL ||
3591 ssl->endpoint != SSL_IS_CLIENT )
3592 {
3593 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3594 }
3595
3596 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3597 return( ret );
3598
Paul Bakker0a597072012-09-25 21:55:46 +00003599 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003600
3601 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003602}
3603
Paul Bakkerb68cad62012-08-23 08:34:18 +00003604void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003605{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003606 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3607 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3608 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3609 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3610}
3611
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003612void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3613 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003614 int major, int minor )
3615{
3616 if( major != SSL_MAJOR_VERSION_3 )
3617 return;
3618
3619 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3620 return;
3621
3622 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003623}
3624
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003625#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003626/* Add a new (empty) key_cert entry an return a pointer to it */
3627static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3628{
3629 ssl_key_cert *key_cert, *last;
3630
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003631 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3632 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003633 return( NULL );
3634
3635 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3636
3637 /* Append the new key_cert to the (possibly empty) current list */
3638 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003639 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003640 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003641 if( ssl->handshake != NULL )
3642 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003643 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003644 else
3645 {
3646 last = ssl->key_cert;
3647 while( last->next != NULL )
3648 last = last->next;
3649 last->next = key_cert;
3650 }
3651
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003652 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003653}
3654
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003655void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003656 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003657{
3658 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003659 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003660 ssl->peer_cn = peer_cn;
3661}
3662
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003663int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003664 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003665{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003666 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3667
3668 if( key_cert == NULL )
3669 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3670
3671 key_cert->cert = own_cert;
3672 key_cert->key = pk_key;
3673
3674 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003675}
3676
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003677#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003678int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003679 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003680{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003681 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003682 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003683
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003684 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003685 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3686
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003687 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3688 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003689 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003690
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003691 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003692
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003693 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003694 if( ret != 0 )
3695 return( ret );
3696
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003697 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003698 return( ret );
3699
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003700 key_cert->cert = own_cert;
3701 key_cert->key_own_alloc = 1;
3702
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003703 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003704}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003705#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003706
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003707int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003708 void *rsa_key,
3709 rsa_decrypt_func rsa_decrypt,
3710 rsa_sign_func rsa_sign,
3711 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003712{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003713 int ret;
3714 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003715
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003716 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003717 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3718
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003719 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3720 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003721 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003722
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003723 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003724
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003725 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3726 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3727 return( ret );
3728
3729 key_cert->cert = own_cert;
3730 key_cert->key_own_alloc = 1;
3731
3732 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003733}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003734#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003735
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003736#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003737int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3738 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003739{
Paul Bakker6db455e2013-09-18 17:29:31 +02003740 if( psk == NULL || psk_identity == NULL )
3741 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3742
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003743 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003744 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3745
Paul Bakker6db455e2013-09-18 17:29:31 +02003746 if( ssl->psk != NULL )
3747 {
3748 polarssl_free( ssl->psk );
3749 polarssl_free( ssl->psk_identity );
3750 }
3751
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003752 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003753 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003754
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003755 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003756 ssl->psk_identity = (unsigned char *)
3757 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003758
3759 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3760 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3761
3762 memcpy( ssl->psk, psk, ssl->psk_len );
3763 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003764
3765 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003766}
3767
3768void ssl_set_psk_cb( ssl_context *ssl,
3769 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3770 size_t),
3771 void *p_psk )
3772{
3773 ssl->f_psk = f_psk;
3774 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003775}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003776#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003777
Paul Bakker48916f92012-09-16 19:57:18 +00003778#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003779int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003780{
3781 int ret;
3782
Paul Bakker48916f92012-09-16 19:57:18 +00003783 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003784 {
3785 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3786 return( ret );
3787 }
3788
Paul Bakker48916f92012-09-16 19:57:18 +00003789 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003790 {
3791 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3792 return( ret );
3793 }
3794
3795 return( 0 );
3796}
3797
Paul Bakker1b57b062011-01-06 15:48:19 +00003798int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3799{
3800 int ret;
3801
Paul Bakker66d5d072014-06-17 16:39:18 +02003802 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003803 {
3804 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3805 return( ret );
3806 }
3807
Paul Bakker66d5d072014-06-17 16:39:18 +02003808 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003809 {
3810 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3811 return( ret );
3812 }
3813
3814 return( 0 );
3815}
Paul Bakker48916f92012-09-16 19:57:18 +00003816#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003817
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003818#if defined(POLARSSL_SSL_SET_CURVES)
3819/*
3820 * Set the allowed elliptic curves
3821 */
3822void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3823{
3824 ssl->curve_list = curve_list;
3825}
3826#endif
3827
Paul Bakker0be444a2013-08-27 21:55:01 +02003828#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003829int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003830{
3831 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003832 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003833
3834 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003835
3836 if( ssl->hostname_len + 1 == 0 )
3837 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3838
Paul Bakker6e339b52013-07-03 13:37:05 +02003839 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003840
Paul Bakkerb15b8512012-01-13 13:44:06 +00003841 if( ssl->hostname == NULL )
3842 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3843
Paul Bakker3c2122f2013-06-24 19:03:14 +02003844 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003845 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003846
Paul Bakker40ea7de2009-05-03 10:18:48 +00003847 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003848
3849 return( 0 );
3850}
3851
Paul Bakker5701cdc2012-09-27 21:49:42 +00003852void ssl_set_sni( ssl_context *ssl,
3853 int (*f_sni)(void *, ssl_context *,
3854 const unsigned char *, size_t),
3855 void *p_sni )
3856{
3857 ssl->f_sni = f_sni;
3858 ssl->p_sni = p_sni;
3859}
Paul Bakker0be444a2013-08-27 21:55:01 +02003860#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003861
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003862#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003863int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003864{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003865 size_t cur_len, tot_len;
3866 const char **p;
3867
3868 /*
3869 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3870 * truncated". Check lengths now rather than later.
3871 */
3872 tot_len = 0;
3873 for( p = protos; *p != NULL; p++ )
3874 {
3875 cur_len = strlen( *p );
3876 tot_len += cur_len;
3877
3878 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3879 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3880 }
3881
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003882 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003883
3884 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003885}
3886
3887const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3888{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003889 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003890}
3891#endif /* POLARSSL_SSL_ALPN */
3892
Paul Bakker490ecc82011-10-06 13:04:09 +00003893void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3894{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003895 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3896 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3897 {
3898 ssl->max_major_ver = major;
3899 ssl->max_minor_ver = minor;
3900 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003901}
3902
Paul Bakker1d29fb52012-09-28 13:28:45 +00003903void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3904{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003905 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3906 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3907 {
3908 ssl->min_major_ver = major;
3909 ssl->min_minor_ver = minor;
3910 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003911}
3912
Paul Bakker05decb22013-08-15 13:33:48 +02003913#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003914int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3915{
Paul Bakker77e257e2013-12-16 15:29:52 +01003916 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003917 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003918 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003919 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003920 }
3921
3922 ssl->mfl_code = mfl_code;
3923
3924 return( 0 );
3925}
Paul Bakker05decb22013-08-15 13:33:48 +02003926#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003927
Paul Bakker1f2bc622013-08-15 13:45:55 +02003928#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02003929int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003930{
3931 if( ssl->endpoint != SSL_IS_CLIENT )
3932 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3933
Paul Bakker8c1ede62013-07-19 14:14:37 +02003934 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003935
3936 return( 0 );
3937}
Paul Bakker1f2bc622013-08-15 13:45:55 +02003938#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02003939
Paul Bakker48916f92012-09-16 19:57:18 +00003940void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
3941{
3942 ssl->disable_renegotiation = renegotiation;
3943}
3944
3945void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
3946{
3947 ssl->allow_legacy_renegotiation = allow_legacy;
3948}
3949
Paul Bakkera503a632013-08-14 13:48:06 +02003950#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003951int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
3952{
3953 ssl->session_tickets = use_tickets;
3954
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003955 if( ssl->endpoint == SSL_IS_CLIENT )
3956 return( 0 );
3957
3958 if( ssl->f_rng == NULL )
3959 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3960
3961 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003962}
Paul Bakker606b4ba2013-08-14 16:52:14 +02003963
3964void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
3965{
3966 ssl->ticket_lifetime = lifetime;
3967}
Paul Bakkera503a632013-08-14 13:48:06 +02003968#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003969
Paul Bakker5121ce52009-01-03 21:22:43 +00003970/*
3971 * SSL get accessors
3972 */
Paul Bakker23986e52011-04-24 08:57:21 +00003973size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003974{
3975 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
3976}
3977
Paul Bakkerff60ee62010-03-16 21:09:09 +00003978int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003979{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003980 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00003981}
3982
Paul Bakkere3166ce2011-01-27 17:40:50 +00003983const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00003984{
Paul Bakker926c8e42013-03-06 10:23:34 +01003985 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003986 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01003987
Paul Bakkere3166ce2011-01-27 17:40:50 +00003988 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00003989}
3990
Paul Bakker43ca69c2011-01-15 17:35:19 +00003991const char *ssl_get_version( const ssl_context *ssl )
3992{
3993 switch( ssl->minor_ver )
3994 {
3995 case SSL_MINOR_VERSION_0:
3996 return( "SSLv3.0" );
3997
3998 case SSL_MINOR_VERSION_1:
3999 return( "TLSv1.0" );
4000
4001 case SSL_MINOR_VERSION_2:
4002 return( "TLSv1.1" );
4003
Paul Bakker1ef83d62012-04-11 12:09:53 +00004004 case SSL_MINOR_VERSION_3:
4005 return( "TLSv1.2" );
4006
Paul Bakker43ca69c2011-01-15 17:35:19 +00004007 default:
4008 break;
4009 }
4010 return( "unknown" );
4011}
4012
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004013#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004014const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004015{
4016 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004017 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004018
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004019 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004020}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004021#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004022
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004023int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4024{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004025 if( ssl == NULL ||
4026 dst == NULL ||
4027 ssl->session == NULL ||
4028 ssl->endpoint != SSL_IS_CLIENT )
4029 {
4030 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4031 }
4032
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004033 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004034}
4035
Paul Bakker5121ce52009-01-03 21:22:43 +00004036/*
Paul Bakker1961b702013-01-25 14:49:24 +01004037 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004038 */
Paul Bakker1961b702013-01-25 14:49:24 +01004039int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004040{
Paul Bakker40e46942009-01-03 21:51:57 +00004041 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004042
Paul Bakker40e46942009-01-03 21:51:57 +00004043#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004045 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004046#endif
4047
Paul Bakker40e46942009-01-03 21:51:57 +00004048#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004049 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004050 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004051#endif
4052
Paul Bakker1961b702013-01-25 14:49:24 +01004053 return( ret );
4054}
4055
4056/*
4057 * Perform the SSL handshake
4058 */
4059int ssl_handshake( ssl_context *ssl )
4060{
4061 int ret = 0;
4062
4063 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4064
4065 while( ssl->state != SSL_HANDSHAKE_OVER )
4066 {
4067 ret = ssl_handshake_step( ssl );
4068
4069 if( ret != 0 )
4070 break;
4071 }
4072
Paul Bakker5121ce52009-01-03 21:22:43 +00004073 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4074
4075 return( ret );
4076}
4077
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004078#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004079/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004080 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004081 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004082static int ssl_write_hello_request( ssl_context *ssl )
4083{
4084 int ret;
4085
4086 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4087
4088 ssl->out_msglen = 4;
4089 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4090 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4091
4092 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4093 {
4094 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4095 return( ret );
4096 }
4097
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004098 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4099
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004100 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4101
4102 return( 0 );
4103}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004104#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004105
4106/*
4107 * Actually renegotiate current connection, triggered by either:
4108 * - calling ssl_renegotiate() on client,
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004109 * - receiving a HelloRequest on client during ssl_read(),
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004110 * - receiving any handshake message on server during ssl_read() after the
4111 * initial handshake is completed
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004112 * If the handshake doesn't complete due to waiting for I/O, it will continue
4113 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004114 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004115static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004116{
4117 int ret;
4118
4119 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4120
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004121 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4122 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004123
4124 ssl->state = SSL_HELLO_REQUEST;
4125 ssl->renegotiation = SSL_RENEGOTIATION;
4126
Paul Bakker48916f92012-09-16 19:57:18 +00004127 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4128 {
4129 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4130 return( ret );
4131 }
4132
4133 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4134
4135 return( 0 );
4136}
4137
4138/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004139 * Renegotiate current connection on client,
4140 * or request renegotiation on server
4141 */
4142int ssl_renegotiate( ssl_context *ssl )
4143{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004144 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004145
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004146#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004147 /* On server, just send the request */
4148 if( ssl->endpoint == SSL_IS_SERVER )
4149 {
4150 if( ssl->state != SSL_HANDSHAKE_OVER )
4151 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4152
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004153 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004154 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004155#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004156
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004157#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004158 /*
4159 * On client, either start the renegotiation process or,
4160 * if already in progress, continue the handshake
4161 */
4162 if( ssl->renegotiation != SSL_RENEGOTIATION )
4163 {
4164 if( ssl->state != SSL_HANDSHAKE_OVER )
4165 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4166
4167 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4168 {
4169 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4170 return( ret );
4171 }
4172 }
4173 else
4174 {
4175 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4176 {
4177 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4178 return( ret );
4179 }
4180 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004181#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004182
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004183 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004184}
4185
4186/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004187 * Receive application data decrypted from the SSL layer
4188 */
Paul Bakker23986e52011-04-24 08:57:21 +00004189int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004190{
Paul Bakker23986e52011-04-24 08:57:21 +00004191 int ret;
4192 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004193
4194 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4195
4196 if( ssl->state != SSL_HANDSHAKE_OVER )
4197 {
4198 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4199 {
4200 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4201 return( ret );
4202 }
4203 }
4204
4205 if( ssl->in_offt == NULL )
4206 {
4207 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4208 {
Paul Bakker831a7552011-05-18 13:32:51 +00004209 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4210 return( 0 );
4211
Paul Bakker5121ce52009-01-03 21:22:43 +00004212 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4213 return( ret );
4214 }
4215
4216 if( ssl->in_msglen == 0 &&
4217 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4218 {
4219 /*
4220 * OpenSSL sends empty messages to randomize the IV
4221 */
4222 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4223 {
Paul Bakker831a7552011-05-18 13:32:51 +00004224 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4225 return( 0 );
4226
Paul Bakker5121ce52009-01-03 21:22:43 +00004227 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4228 return( ret );
4229 }
4230 }
4231
Paul Bakker48916f92012-09-16 19:57:18 +00004232 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4233 {
4234 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4235
4236 if( ssl->endpoint == SSL_IS_CLIENT &&
4237 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4238 ssl->in_hslen != 4 ) )
4239 {
4240 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4241 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4242 }
4243
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004244 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4245 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004246 ssl->allow_legacy_renegotiation ==
4247 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004248 {
4249 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4250
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004251#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004252 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004253 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004254 /*
4255 * SSLv3 does not have a "no_renegotiation" alert
4256 */
4257 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4258 return( ret );
4259 }
4260 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004261#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004262#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4263 defined(POLARSSL_SSL_PROTO_TLS1_2)
4264 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004265 {
4266 if( ( ret = ssl_send_alert_message( ssl,
4267 SSL_ALERT_LEVEL_WARNING,
4268 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4269 {
4270 return( ret );
4271 }
Paul Bakker48916f92012-09-16 19:57:18 +00004272 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004273 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004274#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4275 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004276 {
4277 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004278 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004279 }
Paul Bakker48916f92012-09-16 19:57:18 +00004280 }
4281 else
4282 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004283 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004284 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004285 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004286 return( ret );
4287 }
4288
4289 return( POLARSSL_ERR_NET_WANT_READ );
4290 }
4291 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004292 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4293 {
4294 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4295 "but not honored by client" ) );
4296 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4297 }
Paul Bakker48916f92012-09-16 19:57:18 +00004298 else if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004299 {
4300 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004301 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004302 }
4303
4304 ssl->in_offt = ssl->in_msg;
4305 }
4306
4307 n = ( len < ssl->in_msglen )
4308 ? len : ssl->in_msglen;
4309
4310 memcpy( buf, ssl->in_offt, n );
4311 ssl->in_msglen -= n;
4312
4313 if( ssl->in_msglen == 0 )
4314 /* all bytes consumed */
4315 ssl->in_offt = NULL;
4316 else
4317 /* more data available */
4318 ssl->in_offt += n;
4319
4320 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4321
Paul Bakker23986e52011-04-24 08:57:21 +00004322 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004323}
4324
4325/*
4326 * Send application data to be encrypted by the SSL layer
4327 */
Paul Bakker23986e52011-04-24 08:57:21 +00004328int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004329{
Paul Bakker23986e52011-04-24 08:57:21 +00004330 int ret;
4331 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004332 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
4334 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4335
4336 if( ssl->state != SSL_HANDSHAKE_OVER )
4337 {
4338 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4339 {
4340 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4341 return( ret );
4342 }
4343 }
4344
Paul Bakker05decb22013-08-15 13:33:48 +02004345#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004346 /*
4347 * Assume mfl_code is correct since it was checked when set
4348 */
4349 max_len = mfl_code_to_length[ssl->mfl_code];
4350
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004351 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004352 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004353 */
4354 if( ssl->session_out != NULL &&
4355 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4356 {
4357 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4358 }
Paul Bakker05decb22013-08-15 13:33:48 +02004359#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004360
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004361 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004362
Paul Bakker5121ce52009-01-03 21:22:43 +00004363 if( ssl->out_left != 0 )
4364 {
4365 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4366 {
4367 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4368 return( ret );
4369 }
4370 }
Paul Bakker887bd502011-06-08 13:10:54 +00004371 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004372 {
Paul Bakker887bd502011-06-08 13:10:54 +00004373 ssl->out_msglen = n;
4374 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4375 memcpy( ssl->out_msg, buf, n );
4376
4377 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4378 {
4379 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4380 return( ret );
4381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004382 }
4383
4384 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4385
Paul Bakker23986e52011-04-24 08:57:21 +00004386 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004387}
4388
4389/*
4390 * Notify the peer that the connection is being closed
4391 */
4392int ssl_close_notify( ssl_context *ssl )
4393{
4394 int ret;
4395
4396 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4397
4398 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4399 {
4400 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4401 return( ret );
4402 }
4403
4404 if( ssl->state == SSL_HANDSHAKE_OVER )
4405 {
Paul Bakker48916f92012-09-16 19:57:18 +00004406 if( ( ret = ssl_send_alert_message( ssl,
4407 SSL_ALERT_LEVEL_WARNING,
4408 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004409 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004410 return( ret );
4411 }
4412 }
4413
4414 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4415
4416 return( ret );
4417}
4418
Paul Bakker48916f92012-09-16 19:57:18 +00004419void ssl_transform_free( ssl_transform *transform )
4420{
4421#if defined(POLARSSL_ZLIB_SUPPORT)
4422 deflateEnd( &transform->ctx_deflate );
4423 inflateEnd( &transform->ctx_inflate );
4424#endif
4425
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004426 cipher_free_ctx( &transform->cipher_ctx_enc );
4427 cipher_free_ctx( &transform->cipher_ctx_dec );
4428
Paul Bakker61d113b2013-07-04 11:51:43 +02004429 md_free_ctx( &transform->md_ctx_enc );
4430 md_free_ctx( &transform->md_ctx_dec );
4431
Paul Bakker34617722014-06-13 17:20:13 +02004432 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004433}
4434
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004435#if defined(POLARSSL_X509_CRT_PARSE_C)
4436static void ssl_key_cert_free( ssl_key_cert *key_cert )
4437{
4438 ssl_key_cert *cur = key_cert, *next;
4439
4440 while( cur != NULL )
4441 {
4442 next = cur->next;
4443
4444 if( cur->key_own_alloc )
4445 {
4446 pk_free( cur->key );
4447 polarssl_free( cur->key );
4448 }
4449 polarssl_free( cur );
4450
4451 cur = next;
4452 }
4453}
4454#endif /* POLARSSL_X509_CRT_PARSE_C */
4455
Paul Bakker48916f92012-09-16 19:57:18 +00004456void ssl_handshake_free( ssl_handshake_params *handshake )
4457{
4458#if defined(POLARSSL_DHM_C)
4459 dhm_free( &handshake->dhm_ctx );
4460#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004461#if defined(POLARSSL_ECDH_C)
4462 ecdh_free( &handshake->ecdh_ctx );
4463#endif
4464
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004465#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004466 /* explicit void pointer cast for buggy MS compiler */
4467 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004468#endif
4469
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004470#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4471 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4472 /*
4473 * Free only the linked list wrapper, not the keys themselves
4474 * since the belong to the SNI callback
4475 */
4476 if( handshake->sni_key_cert != NULL )
4477 {
4478 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4479
4480 while( cur != NULL )
4481 {
4482 next = cur->next;
4483 polarssl_free( cur );
4484 cur = next;
4485 }
4486 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004487#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004488
Paul Bakker34617722014-06-13 17:20:13 +02004489 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004490}
4491
4492void ssl_session_free( ssl_session *session )
4493{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004494#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004495 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004496 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004497 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004498 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004499 }
Paul Bakkered27a042013-04-18 22:46:23 +02004500#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004501
Paul Bakkera503a632013-08-14 13:48:06 +02004502#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004503 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004504#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004505
Paul Bakker34617722014-06-13 17:20:13 +02004506 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004507}
4508
Paul Bakker5121ce52009-01-03 21:22:43 +00004509/*
4510 * Free an SSL context
4511 */
4512void ssl_free( ssl_context *ssl )
4513{
4514 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4515
Paul Bakker5121ce52009-01-03 21:22:43 +00004516 if( ssl->out_ctr != NULL )
4517 {
Paul Bakker34617722014-06-13 17:20:13 +02004518 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004519 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004520 }
4521
4522 if( ssl->in_ctr != NULL )
4523 {
Paul Bakker34617722014-06-13 17:20:13 +02004524 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004525 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004526 }
4527
Paul Bakker16770332013-10-11 09:59:44 +02004528#if defined(POLARSSL_ZLIB_SUPPORT)
4529 if( ssl->compress_buf != NULL )
4530 {
Paul Bakker34617722014-06-13 17:20:13 +02004531 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004532 polarssl_free( ssl->compress_buf );
4533 }
4534#endif
4535
Paul Bakker40e46942009-01-03 21:51:57 +00004536#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004537 mpi_free( &ssl->dhm_P );
4538 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004539#endif
4540
Paul Bakker48916f92012-09-16 19:57:18 +00004541 if( ssl->transform )
4542 {
4543 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004544 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004545 }
4546
4547 if( ssl->handshake )
4548 {
4549 ssl_handshake_free( ssl->handshake );
4550 ssl_transform_free( ssl->transform_negotiate );
4551 ssl_session_free( ssl->session_negotiate );
4552
Paul Bakker6e339b52013-07-03 13:37:05 +02004553 polarssl_free( ssl->handshake );
4554 polarssl_free( ssl->transform_negotiate );
4555 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004556 }
4557
Paul Bakkerc0463502013-02-14 11:19:38 +01004558 if( ssl->session )
4559 {
4560 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004561 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004562 }
4563
Paul Bakkera503a632013-08-14 13:48:06 +02004564#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004565 polarssl_free( ssl->ticket_keys );
Paul Bakkera503a632013-08-14 13:48:06 +02004566#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004567
Paul Bakker0be444a2013-08-27 21:55:01 +02004568#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004569 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004570 {
Paul Bakker34617722014-06-13 17:20:13 +02004571 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004572 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004573 ssl->hostname_len = 0;
4574 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004575#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004576
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004577#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004578 if( ssl->psk != NULL )
4579 {
Paul Bakker34617722014-06-13 17:20:13 +02004580 polarssl_zeroize( ssl->psk, ssl->psk_len );
4581 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004582 polarssl_free( ssl->psk );
4583 polarssl_free( ssl->psk_identity );
4584 ssl->psk_len = 0;
4585 ssl->psk_identity_len = 0;
4586 }
4587#endif
4588
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004589#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004590 ssl_key_cert_free( ssl->key_cert );
4591#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004592
Paul Bakker05ef8352012-05-08 09:17:57 +00004593#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4594 if( ssl_hw_record_finish != NULL )
4595 {
4596 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4597 ssl_hw_record_finish( ssl );
4598 }
4599#endif
4600
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004602
Paul Bakker86f04f42013-02-14 11:20:09 +01004603 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004604 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004605}
4606
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004607#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004608/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004609 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004610 */
4611unsigned char ssl_sig_from_pk( pk_context *pk )
4612{
4613#if defined(POLARSSL_RSA_C)
4614 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4615 return( SSL_SIG_RSA );
4616#endif
4617#if defined(POLARSSL_ECDSA_C)
4618 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4619 return( SSL_SIG_ECDSA );
4620#endif
4621 return( SSL_SIG_ANON );
4622}
4623
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004624pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4625{
4626 switch( sig )
4627 {
4628#if defined(POLARSSL_RSA_C)
4629 case SSL_SIG_RSA:
4630 return( POLARSSL_PK_RSA );
4631#endif
4632#if defined(POLARSSL_ECDSA_C)
4633 case SSL_SIG_ECDSA:
4634 return( POLARSSL_PK_ECDSA );
4635#endif
4636 default:
4637 return( POLARSSL_PK_NONE );
4638 }
4639}
Paul Bakker9af723c2014-05-01 13:03:14 +02004640#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004641
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004642/*
4643 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4644 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004645md_type_t ssl_md_alg_from_hash( unsigned char hash )
4646{
4647 switch( hash )
4648 {
4649#if defined(POLARSSL_MD5_C)
4650 case SSL_HASH_MD5:
4651 return( POLARSSL_MD_MD5 );
4652#endif
4653#if defined(POLARSSL_SHA1_C)
4654 case SSL_HASH_SHA1:
4655 return( POLARSSL_MD_SHA1 );
4656#endif
4657#if defined(POLARSSL_SHA256_C)
4658 case SSL_HASH_SHA224:
4659 return( POLARSSL_MD_SHA224 );
4660 case SSL_HASH_SHA256:
4661 return( POLARSSL_MD_SHA256 );
4662#endif
4663#if defined(POLARSSL_SHA512_C)
4664 case SSL_HASH_SHA384:
4665 return( POLARSSL_MD_SHA384 );
4666 case SSL_HASH_SHA512:
4667 return( POLARSSL_MD_SHA512 );
4668#endif
4669 default:
4670 return( POLARSSL_MD_NONE );
4671 }
4672}
4673
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004674#if defined(POLARSSL_SSL_SET_CURVES)
4675/*
4676 * Check is a curve proposed by the peer is in our list.
4677 * Return 1 if we're willing to use it, 0 otherwise.
4678 */
4679int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4680{
4681 const ecp_group_id *gid;
4682
4683 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4684 if( *gid == grp_id )
4685 return( 1 );
4686
4687 return( 0 );
4688}
Paul Bakker9af723c2014-05-01 13:03:14 +02004689#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004690
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004691#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004692int ssl_check_cert_usage( const x509_crt *cert,
4693 const ssl_ciphersuite_t *ciphersuite,
4694 int cert_endpoint )
4695{
4696#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4697 int usage = 0;
4698#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004699#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4700 const char *ext_oid;
4701 size_t ext_len;
4702#endif
4703
4704#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4705 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4706 ((void) cert);
4707 ((void) cert_endpoint);
4708#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004709
4710#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4711 if( cert_endpoint == SSL_IS_SERVER )
4712 {
4713 /* Server part of the key exchange */
4714 switch( ciphersuite->key_exchange )
4715 {
4716 case POLARSSL_KEY_EXCHANGE_RSA:
4717 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4718 usage = KU_KEY_ENCIPHERMENT;
4719 break;
4720
4721 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4722 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4723 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4724 usage = KU_DIGITAL_SIGNATURE;
4725 break;
4726
4727 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4728 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4729 usage = KU_KEY_AGREEMENT;
4730 break;
4731
4732 /* Don't use default: we want warnings when adding new values */
4733 case POLARSSL_KEY_EXCHANGE_NONE:
4734 case POLARSSL_KEY_EXCHANGE_PSK:
4735 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4736 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4737 usage = 0;
4738 }
4739 }
4740 else
4741 {
4742 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4743 usage = KU_DIGITAL_SIGNATURE;
4744 }
4745
4746 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4747 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004748#else
4749 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004750#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4751
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004752#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4753 if( cert_endpoint == SSL_IS_SERVER )
4754 {
4755 ext_oid = OID_SERVER_AUTH;
4756 ext_len = OID_SIZE( OID_SERVER_AUTH );
4757 }
4758 else
4759 {
4760 ext_oid = OID_CLIENT_AUTH;
4761 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4762 }
4763
4764 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4765 return( -1 );
4766#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4767
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004768 return( 0 );
4769}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004770#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004771
4772#endif /* POLARSSL_SSL_TLS_C */