blob: d9eb0a995ec7f3920b241bb0382257230da8b1dc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The SSL 3.0 specification was drafted by Netscape in 1996,
24 * and became an IETF standard in 1999.
25 *
26 * http://wp.netscape.com/eng/ssl3/
27 * http://www.ietf.org/rfc/rfc2246.txt
28 * http://www.ietf.org/rfc/rfc4346.txt
29 */
30
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker0be444a2013-08-27 21:55:01 +020039#include "polarssl/debug.h"
40#include "polarssl/ssl.h"
41
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020044#if defined(POLARSSL_X509_CRT_PARSE_C) && \
45 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
46#include "polarssl/oid.h"
47#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020053#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker6edcd412013-10-29 15:22:54 +010057#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
58 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000059#define strcasecmp _stricmp
60#endif
61
Paul Bakker34617722014-06-13 17:20:13 +020062/* Implementation that should never be optimized out by the compiler */
63static void polarssl_zeroize( void *v, size_t n ) {
64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
65}
66
Paul Bakker05decb22013-08-15 13:33:48 +020067#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020068/*
69 * Convert max_fragment_length codes to length.
70 * RFC 6066 says:
71 * enum{
72 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
73 * } MaxFragmentLength;
74 * and we add 0 -> extension unused
75 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020076static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020077{
78 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
79 512, /* SSL_MAX_FRAG_LEN_512 */
80 1024, /* SSL_MAX_FRAG_LEN_1024 */
81 2048, /* SSL_MAX_FRAG_LEN_2048 */
82 4096, /* SSL_MAX_FRAG_LEN_4096 */
83};
Paul Bakker05decb22013-08-15 13:33:48 +020084#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020085
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020086static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
87{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088 ssl_session_free( dst );
89 memcpy( dst, src, sizeof( ssl_session ) );
90
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020092 if( src->peer_cert != NULL )
93 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020094 int ret;
95
Mansour Moufidc531b4a2015-02-15 17:35:38 -050096 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020097 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
99
Paul Bakkerb6b09562013-09-18 14:17:41 +0200100 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200102 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
103 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200104 {
105 polarssl_free( dst->peer_cert );
106 dst->peer_cert = NULL;
107 return( ret );
108 }
109 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200111
Paul Bakkera503a632013-08-14 13:48:06 +0200112#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113 if( src->ticket != NULL )
114 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500115 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200116 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200117 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
118
119 memcpy( dst->ticket, src->ticket, src->ticket_len );
120 }
Paul Bakkera503a632013-08-14 13:48:06 +0200121#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200122
123 return( 0 );
124}
125
Paul Bakker05ef8352012-05-08 09:17:57 +0000126#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200127int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200128 const unsigned char *key_enc, const unsigned char *key_dec,
129 size_t keylen,
130 const unsigned char *iv_enc, const unsigned char *iv_dec,
131 size_t ivlen,
132 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200133 size_t maclen ) = NULL;
134int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
135int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
136int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
137int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200139#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141/*
142 * Key material generation
143 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200144#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200145static int ssl3_prf( const unsigned char *secret, size_t slen,
146 const char *label,
147 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000148 unsigned char *dstbuf, size_t dlen )
149{
150 size_t i;
151 md5_context md5;
152 sha1_context sha1;
153 unsigned char padding[16];
154 unsigned char sha1sum[20];
155 ((void)label);
156
Paul Bakker5b4af392014-06-26 12:09:34 +0200157 md5_init( &md5 );
158 sha1_init( &sha1 );
159
Paul Bakker5f70b252012-09-13 14:23:06 +0000160 /*
161 * SSLv3:
162 * block =
163 * MD5( secret + SHA1( 'A' + secret + random ) ) +
164 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
165 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
166 * ...
167 */
168 for( i = 0; i < dlen / 16; i++ )
169 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200170 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000171
172 sha1_starts( &sha1 );
173 sha1_update( &sha1, padding, 1 + i );
174 sha1_update( &sha1, secret, slen );
175 sha1_update( &sha1, random, rlen );
176 sha1_finish( &sha1, sha1sum );
177
178 md5_starts( &md5 );
179 md5_update( &md5, secret, slen );
180 md5_update( &md5, sha1sum, 20 );
181 md5_finish( &md5, dstbuf + i * 16 );
182 }
183
Paul Bakker5b4af392014-06-26 12:09:34 +0200184 md5_free( &md5 );
185 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000186
Paul Bakker34617722014-06-13 17:20:13 +0200187 polarssl_zeroize( padding, sizeof( padding ) );
188 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000189
190 return( 0 );
191}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200192#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000193
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200195static int tls1_prf( const unsigned char *secret, size_t slen,
196 const char *label,
197 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000198 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199{
Paul Bakker23986e52011-04-24 08:57:21 +0000200 size_t nb, hs;
201 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200202 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 unsigned char tmp[128];
204 unsigned char h_i[20];
205
206 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000207 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209 hs = ( slen + 1 ) / 2;
210 S1 = secret;
211 S2 = secret + slen - hs;
212
213 nb = strlen( label );
214 memcpy( tmp + 20, label, nb );
215 memcpy( tmp + 20 + nb, random, rlen );
216 nb += rlen;
217
218 /*
219 * First compute P_md5(secret,label+random)[0..dlen]
220 */
221 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
222
223 for( i = 0; i < dlen; i += 16 )
224 {
225 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
226 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
227
228 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
229
230 for( j = 0; j < k; j++ )
231 dstbuf[i + j] = h_i[j];
232 }
233
234 /*
235 * XOR out with P_sha1(secret,label+random)[0..dlen]
236 */
237 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
238
239 for( i = 0; i < dlen; i += 20 )
240 {
241 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
242 sha1_hmac( S2, hs, tmp, 20, tmp );
243
244 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
245
246 for( j = 0; j < k; j++ )
247 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
248 }
249
Paul Bakker34617722014-06-13 17:20:13 +0200250 polarssl_zeroize( tmp, sizeof( tmp ) );
251 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
253 return( 0 );
254}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200255#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#if defined(POLARSSL_SSL_PROTO_TLS1_2)
258#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200259static int tls_prf_sha256( const unsigned char *secret, size_t slen,
260 const char *label,
261 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000262 unsigned char *dstbuf, size_t dlen )
263{
264 size_t nb;
265 size_t i, j, k;
266 unsigned char tmp[128];
267 unsigned char h_i[32];
268
269 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
272 nb = strlen( label );
273 memcpy( tmp + 32, label, nb );
274 memcpy( tmp + 32 + nb, random, rlen );
275 nb += rlen;
276
277 /*
278 * Compute P_<hash>(secret, label + random)[0..dlen]
279 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200280 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000281
282 for( i = 0; i < dlen; i += 32 )
283 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200284 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
285 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000286
287 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
288
289 for( j = 0; j < k; j++ )
290 dstbuf[i + j] = h_i[j];
291 }
292
Paul Bakker34617722014-06-13 17:20:13 +0200293 polarssl_zeroize( tmp, sizeof( tmp ) );
294 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000295
296 return( 0 );
297}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200298#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000299
Paul Bakker9e36f042013-06-30 14:34:05 +0200300#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200301static int tls_prf_sha384( const unsigned char *secret, size_t slen,
302 const char *label,
303 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000304 unsigned char *dstbuf, size_t dlen )
305{
306 size_t nb;
307 size_t i, j, k;
308 unsigned char tmp[128];
309 unsigned char h_i[48];
310
311 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
312 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
313
314 nb = strlen( label );
315 memcpy( tmp + 48, label, nb );
316 memcpy( tmp + 48 + nb, random, rlen );
317 nb += rlen;
318
319 /*
320 * Compute P_<hash>(secret, label + random)[0..dlen]
321 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200322 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000323
324 for( i = 0; i < dlen; i += 48 )
325 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200326 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
327 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000328
329 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
330
331 for( j = 0; j < k; j++ )
332 dstbuf[i + j] = h_i[j];
333 }
334
Paul Bakker34617722014-06-13 17:20:13 +0200335 polarssl_zeroize( tmp, sizeof( tmp ) );
336 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337
338 return( 0 );
339}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200340#endif /* POLARSSL_SHA512_C */
341#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342
Paul Bakker66d5d072014-06-17 16:39:18 +0200343static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200344
345#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
346 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200347static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200348#endif
Paul Bakker380da532012-04-18 16:10:25 +0000349
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200351static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
352static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200353#endif
354
355#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200356static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
357static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200358#endif
359
360#if defined(POLARSSL_SSL_PROTO_TLS1_2)
361#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200362static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
363static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
364static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200365#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100366
Paul Bakker9e36f042013-06-30 14:34:05 +0200367#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200368static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
369static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
370static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100371#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200372#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000373
Paul Bakker5121ce52009-01-03 21:22:43 +0000374int ssl_derive_keys( ssl_context *ssl )
375{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200376 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 unsigned char keyblk[256];
379 unsigned char *key1;
380 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100381 unsigned char *mac_enc;
382 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200383 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100384 const cipher_info_t *cipher_info;
385 const md_info_t *md_info;
386
Paul Bakker48916f92012-09-16 19:57:18 +0000387 ssl_session *session = ssl->session_negotiate;
388 ssl_transform *transform = ssl->transform_negotiate;
389 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
392
Paul Bakker68884e32013-01-07 18:20:04 +0100393 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
394 if( cipher_info == NULL )
395 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200396 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100397 transform->ciphersuite_info->cipher ) );
398 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
399 }
400
401 md_info = md_info_from_type( transform->ciphersuite_info->mac );
402 if( md_info == NULL )
403 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200404 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100405 transform->ciphersuite_info->mac ) );
406 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
407 }
408
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000410 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000411 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200412#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000414 {
Paul Bakker48916f92012-09-16 19:57:18 +0000415 handshake->tls_prf = ssl3_prf;
416 handshake->calc_verify = ssl_calc_verify_ssl;
417 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000418 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200419 else
420#endif
421#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
422 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000423 {
Paul Bakker48916f92012-09-16 19:57:18 +0000424 handshake->tls_prf = tls1_prf;
425 handshake->calc_verify = ssl_calc_verify_tls;
426 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000427 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200428 else
429#endif
430#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200431#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200432 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
433 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000434 {
Paul Bakker48916f92012-09-16 19:57:18 +0000435 handshake->tls_prf = tls_prf_sha384;
436 handshake->calc_verify = ssl_calc_verify_tls_sha384;
437 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000438 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000439 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200440#endif
441#if defined(POLARSSL_SHA256_C)
442 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000443 {
Paul Bakker48916f92012-09-16 19:57:18 +0000444 handshake->tls_prf = tls_prf_sha256;
445 handshake->calc_verify = ssl_calc_verify_tls_sha256;
446 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000447 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200448 else
449#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200450#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200451 {
452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200453 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200454 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000455
456 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 * SSLv3:
458 * master =
459 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
461 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200462 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200463 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 * master = PRF( premaster, "master secret", randbytes )[0..47]
465 */
Paul Bakker0a597072012-09-25 21:55:46 +0000466 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 {
Paul Bakker48916f92012-09-16 19:57:18 +0000468 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
469 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200471#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
472 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200473 {
474 unsigned char session_hash[48];
475 size_t hash_len;
476
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200477 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200478
479 ssl->handshake->calc_verify( ssl, session_hash );
480
481#if defined(POLARSSL_SSL_PROTO_TLS1_2)
482 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
483 {
484#if defined(POLARSSL_SHA512_C)
485 if( ssl->transform_negotiate->ciphersuite_info->mac ==
486 POLARSSL_MD_SHA384 )
487 {
488 hash_len = 48;
489 }
490 else
491#endif
492 hash_len = 32;
493 }
494 else
495#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
496 hash_len = 36;
497
498 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
499
500 handshake->tls_prf( handshake->premaster, handshake->pmslen,
501 "extended master secret",
502 session_hash, hash_len, session->master, 48 );
503
504 }
505 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200506#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000507 handshake->tls_prf( handshake->premaster, handshake->pmslen,
508 "master secret",
509 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200511
Paul Bakker34617722014-06-13 17:20:13 +0200512 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 }
514 else
515 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
516
517 /*
518 * Swap the client and server random values.
519 */
Paul Bakker48916f92012-09-16 19:57:18 +0000520 memcpy( tmp, handshake->randbytes, 64 );
521 memcpy( handshake->randbytes, tmp + 32, 32 );
522 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200523 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 /*
526 * SSLv3:
527 * key block =
528 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
529 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
530 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
532 * ...
533 *
534 * TLSv1:
535 * key block = PRF( master, "key expansion", randbytes )
536 */
Paul Bakker48916f92012-09-16 19:57:18 +0000537 handshake->tls_prf( session->master, 48, "key expansion",
538 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker48916f92012-09-16 19:57:18 +0000540 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
541 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
542 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
543 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
545
Paul Bakker34617722014-06-13 17:20:13 +0200546 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 /*
549 * Determine the appropriate key, IV and MAC length.
550 */
Paul Bakker68884e32013-01-07 18:20:04 +0100551
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200552 transform->keylen = cipher_info->key_length / 8;
553
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200554 if( cipher_info->mode == POLARSSL_MODE_GCM ||
555 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200557 transform->maclen = 0;
558
Paul Bakker68884e32013-01-07 18:20:04 +0100559 transform->ivlen = 12;
560 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200561
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200562 /* Minimum length is expicit IV + tag */
563 transform->minlen = transform->ivlen - transform->fixed_ivlen
564 + ( transform->ciphersuite_info->flags &
565 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100566 }
567 else
568 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200569 /* Initialize HMAC contexts */
570 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
571 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100572 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200573 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
574 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100575 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 /* Get MAC length */
578 transform->maclen = md_get_size( md_info );
579
580#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
581 /*
582 * If HMAC is to be truncated, we shall keep the leftmost bytes,
583 * (rfc 6066 page 13 or rfc 2104 section 4),
584 * so we only need to adjust the length here.
585 */
586 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
587 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
588#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
589
590 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100591 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200593 /* Minimum length */
594 if( cipher_info->mode == POLARSSL_MODE_STREAM )
595 transform->minlen = transform->maclen;
596 else
Paul Bakker68884e32013-01-07 18:20:04 +0100597 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200598 /*
599 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100600 * 1. if EtM is in use: one block plus MAC
601 * otherwise: * first multiple of blocklen greater than maclen
602 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200603 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100604#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
605 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
606 {
607 transform->minlen = transform->maclen
608 + cipher_info->block_size;
609 }
610 else
611#endif
612 {
613 transform->minlen = transform->maclen
614 + cipher_info->block_size
615 - transform->maclen % cipher_info->block_size;
616 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200617
618#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
619 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
620 ssl->minor_ver == SSL_MINOR_VERSION_1 )
621 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100622 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200623#endif
624#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
625 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
626 ssl->minor_ver == SSL_MINOR_VERSION_3 )
627 {
628 transform->minlen += transform->ivlen;
629 }
630 else
631#endif
632 {
633 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
634 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
635 }
Paul Bakker68884e32013-01-07 18:20:04 +0100636 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 }
638
639 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000640 transform->keylen, transform->minlen, transform->ivlen,
641 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
643 /*
644 * Finally setup the cipher contexts, IVs and MAC secrets.
645 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100646#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 if( ssl->endpoint == SSL_IS_CLIENT )
648 {
Paul Bakker48916f92012-09-16 19:57:18 +0000649 key1 = keyblk + transform->maclen * 2;
650 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Paul Bakker68884e32013-01-07 18:20:04 +0100652 mac_enc = keyblk;
653 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000655 /*
656 * This is not used in TLS v1.1.
657 */
Paul Bakker48916f92012-09-16 19:57:18 +0000658 iv_copy_len = ( transform->fixed_ivlen ) ?
659 transform->fixed_ivlen : transform->ivlen;
660 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
661 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000662 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 }
664 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100665#endif /* POLARSSL_SSL_CLI_C */
666#if defined(POLARSSL_SSL_SRV_C)
667 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 {
Paul Bakker48916f92012-09-16 19:57:18 +0000669 key1 = keyblk + transform->maclen * 2 + transform->keylen;
670 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Paul Bakker68884e32013-01-07 18:20:04 +0100672 mac_enc = keyblk + transform->maclen;
673 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000675 /*
676 * This is not used in TLS v1.1.
677 */
Paul Bakker48916f92012-09-16 19:57:18 +0000678 iv_copy_len = ( transform->fixed_ivlen ) ?
679 transform->fixed_ivlen : transform->ivlen;
680 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
681 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000682 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100684 else
685#endif /* POLARSSL_SSL_SRV_C */
686 {
687 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
688 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
689 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200691#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100692 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
693 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100694 if( transform->maclen > sizeof transform->mac_enc )
695 {
696 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200697 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100698 }
699
Paul Bakker68884e32013-01-07 18:20:04 +0100700 memcpy( transform->mac_enc, mac_enc, transform->maclen );
701 memcpy( transform->mac_dec, mac_dec, transform->maclen );
702 }
703 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200704#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200705#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
706 defined(POLARSSL_SSL_PROTO_TLS1_2)
707 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100708 {
709 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
710 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
711 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200712 else
713#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200714 {
715 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200716 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200717 }
Paul Bakker68884e32013-01-07 18:20:04 +0100718
Paul Bakker05ef8352012-05-08 09:17:57 +0000719#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200720 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000721 {
722 int ret = 0;
723
724 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
725
Paul Bakker07eb38b2012-12-19 14:42:06 +0100726 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
727 transform->iv_enc, transform->iv_dec,
728 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100729 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100730 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000731 {
732 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200733 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000734 }
735 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200736#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000737
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200738 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
739 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200741 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
742 return( ret );
743 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200744
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200745 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
746 cipher_info ) ) != 0 )
747 {
748 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
749 return( ret );
750 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200751
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200752 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
753 cipher_info->key_length,
754 POLARSSL_ENCRYPT ) ) != 0 )
755 {
756 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
757 return( ret );
758 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200759
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200760 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
761 cipher_info->key_length,
762 POLARSSL_DECRYPT ) ) != 0 )
763 {
764 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
765 return( ret );
766 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200767
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200768#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200769 if( cipher_info->mode == POLARSSL_MODE_CBC )
770 {
771 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
772 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200773 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200774 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
775 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200776 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200777
778 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
779 POLARSSL_PADDING_NONE ) ) != 0 )
780 {
781 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
782 return( ret );
783 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000784 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200785#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Paul Bakker34617722014-06-13 17:20:13 +0200787 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker2770fbd2012-07-03 13:30:23 +0000789#if defined(POLARSSL_ZLIB_SUPPORT)
790 // Initialize compression
791 //
Paul Bakker48916f92012-09-16 19:57:18 +0000792 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000793 {
Paul Bakker16770332013-10-11 09:59:44 +0200794 if( ssl->compress_buf == NULL )
795 {
796 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
797 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
798 if( ssl->compress_buf == NULL )
799 {
800 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
801 SSL_BUFFER_LEN ) );
802 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
803 }
804 }
805
Paul Bakker2770fbd2012-07-03 13:30:23 +0000806 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
807
Paul Bakker48916f92012-09-16 19:57:18 +0000808 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
809 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000810
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200811 if( deflateInit( &transform->ctx_deflate,
812 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000813 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000814 {
815 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
816 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
817 }
818 }
819#endif /* POLARSSL_ZLIB_SUPPORT */
820
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
822
823 return( 0 );
824}
825
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200826#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000827void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000828{
829 md5_context md5;
830 sha1_context sha1;
831 unsigned char pad_1[48];
832 unsigned char pad_2[48];
833
Paul Bakker380da532012-04-18 16:10:25 +0000834 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Paul Bakker48916f92012-09-16 19:57:18 +0000836 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
837 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Paul Bakker380da532012-04-18 16:10:25 +0000839 memset( pad_1, 0x36, 48 );
840 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Paul Bakker48916f92012-09-16 19:57:18 +0000842 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000843 md5_update( &md5, pad_1, 48 );
844 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Paul Bakker380da532012-04-18 16:10:25 +0000846 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000847 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000848 md5_update( &md5, pad_2, 48 );
849 md5_update( &md5, hash, 16 );
850 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Paul Bakker48916f92012-09-16 19:57:18 +0000852 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000853 sha1_update( &sha1, pad_1, 40 );
854 sha1_finish( &sha1, hash + 16 );
855
856 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000857 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000858 sha1_update( &sha1, pad_2, 40 );
859 sha1_update( &sha1, hash + 16, 20 );
860 sha1_finish( &sha1, hash + 16 );
861
862 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
863 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
864
Paul Bakker5b4af392014-06-26 12:09:34 +0200865 md5_free( &md5 );
866 sha1_free( &sha1 );
867
Paul Bakker380da532012-04-18 16:10:25 +0000868 return;
869}
Paul Bakker9af723c2014-05-01 13:03:14 +0200870#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000871
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000873void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
874{
875 md5_context md5;
876 sha1_context sha1;
877
878 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
879
Paul Bakker48916f92012-09-16 19:57:18 +0000880 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
881 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000882
Paul Bakker48916f92012-09-16 19:57:18 +0000883 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000884 sha1_finish( &sha1, hash + 16 );
885
886 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
887 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
888
Paul Bakker5b4af392014-06-26 12:09:34 +0200889 md5_free( &md5 );
890 sha1_free( &sha1 );
891
Paul Bakker380da532012-04-18 16:10:25 +0000892 return;
893}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200894#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000895
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200896#if defined(POLARSSL_SSL_PROTO_TLS1_2)
897#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000898void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
899{
Paul Bakker9e36f042013-06-30 14:34:05 +0200900 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000901
902 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
903
Paul Bakker9e36f042013-06-30 14:34:05 +0200904 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
905 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000906
907 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
908 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
909
Paul Bakker5b4af392014-06-26 12:09:34 +0200910 sha256_free( &sha256 );
911
Paul Bakker380da532012-04-18 16:10:25 +0000912 return;
913}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200914#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000915
Paul Bakker9e36f042013-06-30 14:34:05 +0200916#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000917void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
918{
Paul Bakker9e36f042013-06-30 14:34:05 +0200919 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000920
921 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
922
Paul Bakker9e36f042013-06-30 14:34:05 +0200923 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
924 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
Paul Bakkerca4ab492012-04-18 14:23:57 +0000926 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
928
Paul Bakker5b4af392014-06-26 12:09:34 +0200929 sha512_free( &sha512 );
930
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 return;
932}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200933#endif /* POLARSSL_SHA512_C */
934#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200936#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
938{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200939 unsigned char *p = ssl->handshake->premaster;
940 unsigned char *end = p + sizeof( ssl->handshake->premaster );
941
942 /*
943 * PMS = struct {
944 * opaque other_secret<0..2^16-1>;
945 * opaque psk<0..2^16-1>;
946 * };
947 * with "other_secret" depending on the particular key exchange
948 */
949#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
950 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
951 {
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200952 if( end - p < 2 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200953 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
954
955 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
956 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200957
958 if( end < p || (size_t)( end - p ) < ssl->psk_len )
959 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
960
961 memset( p, 0, ssl->psk_len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962 p += ssl->psk_len;
963 }
964 else
965#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200966#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
967 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
968 {
969 /*
970 * other_secret already set by the ClientKeyExchange message,
971 * and is 48 bytes long
972 */
973 *p++ = 0;
974 *p++ = 48;
975 p += 48;
976 }
977 else
978#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200979#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
980 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
981 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200982 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200983 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200984
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200985 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200986 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200987 p + 2, &len,
988 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200989 {
990 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
991 return( ret );
992 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200993 *(p++) = (unsigned char)( len >> 8 );
994 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200995 p += len;
996
997 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
998 }
999 else
1000#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1001#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1002 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1003 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001004 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001005 size_t zlen;
1006
1007 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001008 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001009 ssl->f_rng, ssl->p_rng ) ) != 0 )
1010 {
1011 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1012 return( ret );
1013 }
1014
1015 *(p++) = (unsigned char)( zlen >> 8 );
1016 *(p++) = (unsigned char)( zlen );
1017 p += zlen;
1018
1019 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1020 }
1021 else
1022#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1023 {
1024 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001025 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001026 }
1027
1028 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001029 if( end - p < 2 )
1030 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001031
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001032 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1033 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001034
1035 if( end < p || (size_t)( end - p ) < ssl->psk_len )
1036 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1037
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001038 memcpy( p, ssl->psk, ssl->psk_len );
1039 p += ssl->psk_len;
1040
1041 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1042
1043 return( 0 );
1044}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001045#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001046
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001047#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001048/*
1049 * SSLv3.0 MAC functions
1050 */
Paul Bakker68884e32013-01-07 18:20:04 +01001051static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1052 unsigned char *buf, size_t len,
1053 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001054{
1055 unsigned char header[11];
1056 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001057 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001058 int md_size = md_get_size( md_ctx->md_info );
1059 int md_type = md_get_type( md_ctx->md_info );
1060
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001061 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001062 if( md_type == POLARSSL_MD_MD5 )
1063 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001064 else
Paul Bakker68884e32013-01-07 18:20:04 +01001065 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001066
1067 memcpy( header, ctr, 8 );
1068 header[ 8] = (unsigned char) type;
1069 header[ 9] = (unsigned char)( len >> 8 );
1070 header[10] = (unsigned char)( len );
1071
Paul Bakker68884e32013-01-07 18:20:04 +01001072 memset( padding, 0x36, padlen );
1073 md_starts( md_ctx );
1074 md_update( md_ctx, secret, md_size );
1075 md_update( md_ctx, padding, padlen );
1076 md_update( md_ctx, header, 11 );
1077 md_update( md_ctx, buf, len );
1078 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Paul Bakker68884e32013-01-07 18:20:04 +01001080 memset( padding, 0x5C, padlen );
1081 md_starts( md_ctx );
1082 md_update( md_ctx, secret, md_size );
1083 md_update( md_ctx, padding, padlen );
1084 md_update( md_ctx, buf + len, md_size );
1085 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001086}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001087#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001088
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001089#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1090 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1091 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1092#define POLARSSL_SOME_MODES_USE_MAC
1093#endif
1094
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001095/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001097 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001098static int ssl_encrypt_buf( ssl_context *ssl )
1099{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001100 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001101 cipher_mode_t mode;
1102 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
1104 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1105
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001106 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1107 {
1108 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1109 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1110 }
1111
1112 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1113
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001114 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1115 ssl->out_msg, ssl->out_msglen );
1116
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001118 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001120#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001121 if( mode == POLARSSL_MODE_STREAM ||
1122 ( mode == POLARSSL_MODE_CBC
1123#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1124 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1125#endif
1126 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001128#if defined(POLARSSL_SSL_PROTO_SSL3)
1129 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1130 {
1131 ssl_mac( &ssl->transform_out->md_ctx_enc,
1132 ssl->transform_out->mac_enc,
1133 ssl->out_msg, ssl->out_msglen,
1134 ssl->out_ctr, ssl->out_msgtype );
1135 }
1136 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001137#endif
1138#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001139 defined(POLARSSL_SSL_PROTO_TLS1_2)
1140 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1141 {
1142 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1143 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1144 ssl->out_msg, ssl->out_msglen );
1145 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1146 ssl->out_msg + ssl->out_msglen );
1147 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1148 }
1149 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001150#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001151 {
1152 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001153 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001154 }
1155
1156 SSL_DEBUG_BUF( 4, "computed mac",
1157 ssl->out_msg + ssl->out_msglen,
1158 ssl->transform_out->maclen );
1159
1160 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001161 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001162 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001163#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001165 /*
1166 * Encrypt
1167 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001168#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001169 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001171 int ret;
1172 size_t olen = 0;
1173
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1175 "including %d bytes of padding",
1176 ssl->out_msglen, 0 ) );
1177
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001178 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001179 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001180 ssl->transform_out->ivlen,
1181 ssl->out_msg, ssl->out_msglen,
1182 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001183 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001184 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001185 return( ret );
1186 }
1187
1188 if( ssl->out_msglen != olen )
1189 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001190 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001191 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 }
Paul Bakker68884e32013-01-07 18:20:04 +01001194 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001195#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001196#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1197 if( mode == POLARSSL_MODE_GCM ||
1198 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001199 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001200 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001201 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202 unsigned char *enc_msg;
1203 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001204 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1205 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001206
Paul Bakkerca4ab492012-04-18 14:23:57 +00001207 memcpy( add_data, ssl->out_ctr, 8 );
1208 add_data[8] = ssl->out_msgtype;
1209 add_data[9] = ssl->major_ver;
1210 add_data[10] = ssl->minor_ver;
1211 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1212 add_data[12] = ssl->out_msglen & 0xFF;
1213
1214 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1215 add_data, 13 );
1216
Paul Bakker68884e32013-01-07 18:20:04 +01001217 /*
1218 * Generate IV
1219 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001220#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001221 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001222 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1223 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001224 if( ret != 0 )
1225 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001226
Paul Bakker68884e32013-01-07 18:20:04 +01001227 memcpy( ssl->out_iv,
1228 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1229 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001230#else
1231 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1232 {
1233 /* Reminder if we ever add an AEAD mode with a different size */
1234 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1235 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1236 }
1237
1238 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1239 ssl->out_ctr, 8 );
1240 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1241#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001242
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001243 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001244 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001245
Paul Bakker68884e32013-01-07 18:20:04 +01001246 /*
1247 * Fix pointer positions and message length with added IV
1248 */
1249 enc_msg = ssl->out_msg;
1250 enc_msglen = ssl->out_msglen;
1251 ssl->out_msglen += ssl->transform_out->ivlen -
1252 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001253
Paul Bakker68884e32013-01-07 18:20:04 +01001254 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1255 "including %d bytes of padding",
1256 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001257
Paul Bakker68884e32013-01-07 18:20:04 +01001258 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001259 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001260 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001261 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1262 ssl->transform_out->iv_enc,
1263 ssl->transform_out->ivlen,
1264 add_data, 13,
1265 enc_msg, enc_msglen,
1266 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001267 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001268 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001269 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001270 return( ret );
1271 }
1272
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001273 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001274 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001275 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001276 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001277 }
1278
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001279 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001280 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001281
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001282 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001283 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001285#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001286#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1287 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001288 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001290 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001291 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001292 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001293
Paul Bakker48916f92012-09-16 19:57:18 +00001294 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1295 ssl->transform_out->ivlen;
1296 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 padlen = 0;
1298
1299 for( i = 0; i <= padlen; i++ )
1300 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1301
1302 ssl->out_msglen += padlen + 1;
1303
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001304 enc_msglen = ssl->out_msglen;
1305 enc_msg = ssl->out_msg;
1306
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001307#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001308 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001309 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1310 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001311 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001312 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001313 {
1314 /*
1315 * Generate IV
1316 */
Manuel Pégourié-Gonnarda67fd792015-08-27 12:02:40 +02001317 ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001318 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001319 if( ret != 0 )
1320 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001321
Paul Bakker92be97b2013-01-02 17:30:03 +01001322 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001323 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001324
1325 /*
1326 * Fix pointer positions and message length with added IV
1327 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001328 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001329 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001330 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001331 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001332#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001333
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001335 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001336 ssl->out_msglen, ssl->transform_out->ivlen,
1337 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001339 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001340 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001341 ssl->transform_out->ivlen,
1342 enc_msg, enc_msglen,
1343 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001344 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001345 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001346 return( ret );
1347 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001348
Paul Bakkercca5b812013-08-31 17:40:26 +02001349 if( enc_msglen != olen )
1350 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001351 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001352 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001353 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001354
1355#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001356 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1357 {
1358 /*
1359 * Save IV in SSL3 and TLS1
1360 */
1361 memcpy( ssl->transform_out->iv_enc,
1362 ssl->transform_out->cipher_ctx_enc.iv,
1363 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001365#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001366
1367#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001368 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001369 {
1370 /*
1371 * MAC(MAC_write_key, seq_num +
1372 * TLSCipherText.type +
1373 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001374 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001375 * IV + // except for TLS 1.0
1376 * ENC(content + padding + padding_length));
1377 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001378 unsigned char pseudo_hdr[13];
1379
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001380 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1381
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001382 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1383 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001384 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1385 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1386
1387 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001388
1389 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1390 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1391 ssl->out_iv, ssl->out_msglen );
1392 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1393 ssl->out_iv + ssl->out_msglen );
1394 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1395
1396 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001397 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001398 }
1399#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001401 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001402#endif /* POLARSSL_CIPHER_MODE_CBC &&
1403 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001404 {
1405 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001406 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001407 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001409 /* Make extra sure authentication was performed, exactly once */
1410 if( auth_done != 1 )
1411 {
1412 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1413 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1414 }
1415
Paul Bakkerca4ab492012-04-18 14:23:57 +00001416 for( i = 8; i > 0; i-- )
1417 if( ++ssl->out_ctr[i - 1] != 0 )
1418 break;
1419
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001420 /* The loops goes to its end iff the counter is wrapping */
1421 if( i == 0 )
1422 {
1423 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1424 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1425 }
1426
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1428
1429 return( 0 );
1430}
1431
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001432#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001433
Paul Bakker5121ce52009-01-03 21:22:43 +00001434static int ssl_decrypt_buf( ssl_context *ssl )
1435{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001436 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001437 cipher_mode_t mode;
1438 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001439#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001440 size_t padlen = 0, correct = 1;
1441#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
1443 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1444
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001445 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1446 {
1447 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1448 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1449 }
1450
1451 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1452
Paul Bakker48916f92012-09-16 19:57:18 +00001453 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 {
1455 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001456 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001457 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 }
1459
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001460#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001461 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001462 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001463 int ret;
1464 size_t olen = 0;
1465
Paul Bakker68884e32013-01-07 18:20:04 +01001466 padlen = 0;
1467
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001468 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001469 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001470 ssl->transform_in->ivlen,
1471 ssl->in_msg, ssl->in_msglen,
1472 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001473 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001474 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001475 return( ret );
1476 }
1477
1478 if( ssl->in_msglen != olen )
1479 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001480 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001481 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 }
Paul Bakker68884e32013-01-07 18:20:04 +01001484 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001485#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001486#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1487 if( mode == POLARSSL_MODE_GCM ||
1488 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001489 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001490 int ret;
1491 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001492 unsigned char *dec_msg;
1493 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001494 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001495 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1496 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001497 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1498 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001499
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001500 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001501 {
1502 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1503 "+ taglen (%d)", ssl->in_msglen,
1504 explicit_iv_len, taglen ) );
1505 return( POLARSSL_ERR_SSL_INVALID_MAC );
1506 }
1507 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1508
Paul Bakker68884e32013-01-07 18:20:04 +01001509 dec_msg = ssl->in_msg;
1510 dec_msg_result = ssl->in_msg;
1511 ssl->in_msglen = dec_msglen;
1512
1513 memcpy( add_data, ssl->in_ctr, 8 );
1514 add_data[8] = ssl->in_msgtype;
1515 add_data[9] = ssl->major_ver;
1516 add_data[10] = ssl->minor_ver;
1517 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1518 add_data[12] = ssl->in_msglen & 0xFF;
1519
1520 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1521 add_data, 13 );
1522
1523 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1524 ssl->in_iv,
1525 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1526
1527 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1528 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001529 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001530
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001531 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001532 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001533 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001534 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1535 ssl->transform_in->iv_dec,
1536 ssl->transform_in->ivlen,
1537 add_data, 13,
1538 dec_msg, dec_msglen,
1539 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001540 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001542 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1543
1544 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1545 return( POLARSSL_ERR_SSL_INVALID_MAC );
1546
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001547 return( ret );
1548 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001549 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001550
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001551 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001552 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001553 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001554 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001555 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001556 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001558#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001559#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1560 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001561 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 {
Paul Bakker45829992013-01-03 14:52:21 +01001563 /*
1564 * Decrypt and check the padding
1565 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001567 unsigned char *dec_msg;
1568 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001569 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001570 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001571 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001572
Paul Bakker5121ce52009-01-03 21:22:43 +00001573 /*
Paul Bakker45829992013-01-03 14:52:21 +01001574 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001576#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001577 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1578 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001579#endif
Paul Bakker45829992013-01-03 14:52:21 +01001580
1581 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1582 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1583 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001584 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1585 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1586 ssl->transform_in->ivlen,
1587 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001588 return( POLARSSL_ERR_SSL_INVALID_MAC );
1589 }
1590
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001591 dec_msglen = ssl->in_msglen;
1592 dec_msg = ssl->in_msg;
1593 dec_msg_result = ssl->in_msg;
1594
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001595 /*
1596 * Authenticate before decrypt if enabled
1597 */
1598#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001599 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001600 {
1601 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001602 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001603
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001604 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1605
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001606 dec_msglen -= ssl->transform_in->maclen;
1607 ssl->in_msglen -= ssl->transform_in->maclen;
1608
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001609 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1610 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1611 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1612 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1613
1614 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1615
1616 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001617 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1618 ssl->in_iv, ssl->in_msglen );
1619 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1620 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1621
1622 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1623 ssl->transform_in->maclen );
1624 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1625 ssl->transform_in->maclen );
1626
1627 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1628 ssl->transform_in->maclen ) != 0 )
1629 {
1630 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1631
1632 return( POLARSSL_ERR_SSL_INVALID_MAC );
1633 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001634 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001635 }
1636#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1637
1638 /*
1639 * Check length sanity
1640 */
1641 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1642 {
1643 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1644 ssl->in_msglen, ssl->transform_in->ivlen ) );
1645 return( POLARSSL_ERR_SSL_INVALID_MAC );
1646 }
1647
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001648#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001649 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001650 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001652 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001653 {
Paul Bakker48916f92012-09-16 19:57:18 +00001654 dec_msglen -= ssl->transform_in->ivlen;
1655 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001656
Paul Bakker48916f92012-09-16 19:57:18 +00001657 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001658 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001659 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001660#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001661
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001662 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001663 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001664 ssl->transform_in->ivlen,
1665 dec_msg, dec_msglen,
1666 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001667 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001668 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001669 return( ret );
1670 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001671
Paul Bakkercca5b812013-08-31 17:40:26 +02001672 if( dec_msglen != olen )
1673 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001674 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001675 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001676 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001677
1678#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001679 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1680 {
1681 /*
1682 * Save IV in SSL3 and TLS1
1683 */
1684 memcpy( ssl->transform_in->iv_dec,
1685 ssl->transform_in->cipher_ctx_dec.iv,
1686 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001688#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
1690 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001691
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001692 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001693 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001694 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001695#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001696 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1697 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001698#endif
Paul Bakker45829992013-01-03 14:52:21 +01001699 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001700 correct = 0;
1701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001702
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001703#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1705 {
Paul Bakker48916f92012-09-16 19:57:18 +00001706 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001708#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1710 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001711 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001712#endif
Paul Bakker45829992013-01-03 14:52:21 +01001713 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001714 }
1715 }
1716 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001717#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001718#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1719 defined(POLARSSL_SSL_PROTO_TLS1_2)
1720 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 {
1722 /*
Paul Bakker45829992013-01-03 14:52:21 +01001723 * TLSv1+: always check the padding up to the first failure
1724 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001725 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001726 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001727 size_t padding_idx = ssl->in_msglen - padlen - 1;
1728
Paul Bakker956c9e02013-12-19 14:42:28 +01001729 /*
1730 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001731 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001732 *
Paul Bakker61885c72014-04-25 12:59:03 +02001733 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1734 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001735 *
1736 * In both cases we reset padding_idx to a safe value (0) to
1737 * prevent out-of-buffer reads.
1738 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001739 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001740 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1741 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001742
1743 padding_idx *= correct;
1744
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001745 for( i = 1; i <= 256; i++ )
1746 {
1747 real_count &= ( i <= padlen );
1748 pad_count += real_count *
1749 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1750 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001751
1752 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001753
Paul Bakkerd66f0702013-01-31 16:57:45 +01001754#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001755 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001756 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001757#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001758 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001760 else
1761#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1762 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001763 {
1764 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001765 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001766 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001767
1768 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001770 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001771#endif /* POLARSSL_CIPHER_MODE_CBC &&
1772 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001773 {
1774 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001775 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001776 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001777
1778 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1779 ssl->in_msg, ssl->in_msglen );
1780
1781 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001782 * Authenticate if not done yet.
1783 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001785#if defined(POLARSSL_SOME_MODES_USE_MAC)
1786 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001787 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001788 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1789
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001790 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001792 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1793 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001795 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001796
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001797#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001798 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1799 {
1800 ssl_mac( &ssl->transform_in->md_ctx_dec,
1801 ssl->transform_in->mac_dec,
1802 ssl->in_msg, ssl->in_msglen,
1803 ssl->in_ctr, ssl->in_msgtype );
1804 }
1805 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001806#endif /* POLARSSL_SSL_PROTO_SSL3 */
1807#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001808 defined(POLARSSL_SSL_PROTO_TLS1_2)
1809 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1810 {
1811 /*
1812 * Process MAC and always update for padlen afterwards to make
1813 * total time independent of padlen
1814 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001815 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001816 *
1817 * Known timing attacks:
1818 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1819 *
1820 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1821 * correctly. (We round down instead of up, so -56 is the correct
1822 * value for our calculations instead of -55)
1823 */
1824 size_t j, extra_run = 0;
1825 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1826 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001827
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001828 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001829
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1831 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1832 ssl->in_msglen );
1833 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1834 ssl->in_msg + ssl->in_msglen );
Manuel Pégourié-Gonnard7d1e95c2015-04-29 01:35:48 +02001835 /* Call md_process at least once due to cache attacks */
1836 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001837 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001838
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001839 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1840 }
1841 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001842#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001843 POLARSSL_SSL_PROTO_TLS1_2 */
1844 {
1845 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001846 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001847 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001849 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1850 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1851 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001853 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001854 ssl->transform_in->maclen ) != 0 )
1855 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001856#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001857 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001858#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001859 correct = 0;
1860 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001861 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001863 /*
1864 * Finally check the correct flag
1865 */
1866 if( correct == 0 )
1867 return( POLARSSL_ERR_SSL_INVALID_MAC );
1868 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001869#endif /* POLARSSL_SOME_MODES_USE_MAC */
1870
1871 /* Make extra sure authentication was performed, exactly once */
1872 if( auth_done != 1 )
1873 {
1874 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1875 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1876 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
1878 if( ssl->in_msglen == 0 )
1879 {
1880 ssl->nb_zero++;
1881
1882 /*
1883 * Three or more empty messages may be a DoS attack
1884 * (excessive CPU consumption).
1885 */
1886 if( ssl->nb_zero > 3 )
1887 {
1888 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1889 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001890 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 }
1892 }
1893 else
1894 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001895
Paul Bakker23986e52011-04-24 08:57:21 +00001896 for( i = 8; i > 0; i-- )
1897 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 break;
1899
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001900 /* The loops goes to its end iff the counter is wrapping */
1901 if( i == 0 )
1902 {
1903 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1904 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1905 }
1906
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1908
1909 return( 0 );
1910}
1911
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001912#undef MAC_NONE
1913#undef MAC_PLAINTEXT
1914#undef MAC_CIPHERTEXT
1915
Paul Bakker2770fbd2012-07-03 13:30:23 +00001916#if defined(POLARSSL_ZLIB_SUPPORT)
1917/*
1918 * Compression/decompression functions
1919 */
1920static int ssl_compress_buf( ssl_context *ssl )
1921{
1922 int ret;
1923 unsigned char *msg_post = ssl->out_msg;
1924 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001925 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001926
1927 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1928
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001929 if( len_pre == 0 )
1930 return( 0 );
1931
Paul Bakker2770fbd2012-07-03 13:30:23 +00001932 memcpy( msg_pre, ssl->out_msg, len_pre );
1933
1934 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1935 ssl->out_msglen ) );
1936
1937 SSL_DEBUG_BUF( 4, "before compression: output payload",
1938 ssl->out_msg, ssl->out_msglen );
1939
Paul Bakker48916f92012-09-16 19:57:18 +00001940 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1941 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1942 ssl->transform_out->ctx_deflate.next_out = msg_post;
1943 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001944
Paul Bakker48916f92012-09-16 19:57:18 +00001945 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001946 if( ret != Z_OK )
1947 {
1948 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1949 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1950 }
1951
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001952 ssl->out_msglen = SSL_BUFFER_LEN -
1953 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001954
Paul Bakker2770fbd2012-07-03 13:30:23 +00001955 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1956 ssl->out_msglen ) );
1957
1958 SSL_DEBUG_BUF( 4, "after compression: output payload",
1959 ssl->out_msg, ssl->out_msglen );
1960
1961 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1962
1963 return( 0 );
1964}
1965
1966static int ssl_decompress_buf( ssl_context *ssl )
1967{
1968 int ret;
1969 unsigned char *msg_post = ssl->in_msg;
1970 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001971 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001972
1973 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1974
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001975 if( len_pre == 0 )
1976 return( 0 );
1977
Paul Bakker2770fbd2012-07-03 13:30:23 +00001978 memcpy( msg_pre, ssl->in_msg, len_pre );
1979
1980 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1981 ssl->in_msglen ) );
1982
1983 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1984 ssl->in_msg, ssl->in_msglen );
1985
Paul Bakker48916f92012-09-16 19:57:18 +00001986 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1987 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1988 ssl->transform_in->ctx_inflate.next_out = msg_post;
1989 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001990
Paul Bakker48916f92012-09-16 19:57:18 +00001991 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001992 if( ret != Z_OK )
1993 {
1994 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1995 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1996 }
1997
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001998 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1999 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002000
Paul Bakker2770fbd2012-07-03 13:30:23 +00002001 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2002 ssl->in_msglen ) );
2003
2004 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2005 ssl->in_msg, ssl->in_msglen );
2006
2007 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2008
2009 return( 0 );
2010}
2011#endif /* POLARSSL_ZLIB_SUPPORT */
2012
Paul Bakker5121ce52009-01-03 21:22:43 +00002013/*
2014 * Fill the input message buffer
2015 */
Paul Bakker23986e52011-04-24 08:57:21 +00002016int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002017{
Paul Bakker23986e52011-04-24 08:57:21 +00002018 int ret;
2019 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
2021 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2022
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002023 if( nb_want > SSL_BUFFER_LEN - 8 )
2024 {
2025 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2026 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2027 }
2028
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 while( ssl->in_left < nb_want )
2030 {
2031 len = nb_want - ssl->in_left;
2032 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2033
2034 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2035 ssl->in_left, nb_want ) );
2036 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2037
Paul Bakker831a7552011-05-18 13:32:51 +00002038 if( ret == 0 )
2039 return( POLARSSL_ERR_SSL_CONN_EOF );
2040
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 if( ret < 0 )
2042 return( ret );
2043
2044 ssl->in_left += ret;
2045 }
2046
2047 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2048
2049 return( 0 );
2050}
2051
2052/*
2053 * Flush any data not yet written
2054 */
2055int ssl_flush_output( ssl_context *ssl )
2056{
2057 int ret;
2058 unsigned char *buf;
2059
2060 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2061
2062 while( ssl->out_left > 0 )
2063 {
2064 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2065 5 + ssl->out_msglen, ssl->out_left ) );
2066
Paul Bakker5bd42292012-12-19 14:40:42 +01002067 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002069
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2071
2072 if( ret <= 0 )
2073 return( ret );
2074
2075 ssl->out_left -= ret;
2076 }
2077
2078 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2079
2080 return( 0 );
2081}
2082
2083/*
2084 * Record layer functions
2085 */
2086int ssl_write_record( ssl_context *ssl )
2087{
Paul Bakker05ef8352012-05-08 09:17:57 +00002088 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002089 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002090
2091 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2092
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2094 {
2095 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2096 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2097 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2098
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002099 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2100 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 }
2102
Paul Bakker2770fbd2012-07-03 13:30:23 +00002103#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002104 if( ssl->transform_out != NULL &&
2105 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002106 {
2107 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2108 {
2109 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2110 return( ret );
2111 }
2112
2113 len = ssl->out_msglen;
2114 }
2115#endif /*POLARSSL_ZLIB_SUPPORT */
2116
Paul Bakker05ef8352012-05-08 09:17:57 +00002117#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002118 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002120 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Paul Bakker05ef8352012-05-08 09:17:57 +00002122 ret = ssl_hw_record_write( ssl );
2123 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2124 {
2125 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002126 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002127 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002128
2129 if( ret == 0 )
2130 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002131 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002132#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002133 if( !done )
2134 {
2135 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2136 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2137 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2139 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002140
Paul Bakker48916f92012-09-16 19:57:18 +00002141 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002142 {
2143 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2144 {
2145 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2146 return( ret );
2147 }
2148
2149 len = ssl->out_msglen;
2150 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2151 ssl->out_hdr[4] = (unsigned char)( len );
2152 }
2153
2154 ssl->out_left = 5 + ssl->out_msglen;
2155
2156 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2157 "version = [%d:%d], msglen = %d",
2158 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2159 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2160
2161 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002162 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 }
2164
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2166 {
2167 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2168 return( ret );
2169 }
2170
2171 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2172
2173 return( 0 );
2174}
2175
2176int ssl_read_record( ssl_context *ssl )
2177{
Paul Bakker05ef8352012-05-08 09:17:57 +00002178 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002179
2180 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2181
2182 if( ssl->in_hslen != 0 &&
2183 ssl->in_hslen < ssl->in_msglen )
2184 {
2185 /*
2186 * Get next Handshake message in the current record
2187 */
2188 ssl->in_msglen -= ssl->in_hslen;
2189
Paul Bakker8934a982011-08-05 11:11:53 +00002190 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002191 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
2193 ssl->in_hslen = 4;
2194 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2195
2196 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2197 " %d, type = %d, hslen = %d",
2198 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2199
2200 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2201 {
2202 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002203 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 }
2205
2206 if( ssl->in_msglen < ssl->in_hslen )
2207 {
2208 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002209 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 }
2211
Paul Bakker4224bc02014-04-08 14:36:50 +02002212 if( ssl->state != SSL_HANDSHAKE_OVER )
2213 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002214
2215 return( 0 );
2216 }
2217
2218 ssl->in_hslen = 0;
2219
2220 /*
2221 * Read the record header and validate it
2222 */
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002223read_record_header:
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2225 {
2226 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2227 return( ret );
2228 }
2229
2230 ssl->in_msgtype = ssl->in_hdr[0];
2231 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2232
2233 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2234 "version = [%d:%d], msglen = %d",
2235 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2236 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2237
2238 if( ssl->in_hdr[1] != ssl->major_ver )
2239 {
2240 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002241 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 }
2243
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002244 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 {
2246 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002247 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 }
2249
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002250 /* Sanity check (outer boundaries) */
2251 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2252 {
2253 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2254 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2255 }
2256
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002258 * Make sure the message length is acceptable for the current transform
2259 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 */
Paul Bakker48916f92012-09-16 19:57:18 +00002261 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002263 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 {
2265 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002266 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 }
2268 }
2269 else
2270 {
Paul Bakker48916f92012-09-16 19:57:18 +00002271 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 {
2273 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002274 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 }
2276
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002277#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002279 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 {
2281 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002282 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002284#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002286#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2287 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 /*
2289 * TLS encrypted messages can have up to 256 bytes of padding
2290 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002291 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002292 ssl->in_msglen > ssl->transform_in->minlen +
2293 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 {
2295 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002296 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002297 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002298#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002299 }
2300
2301 /*
2302 * Read and optionally decrypt the message contents
2303 */
2304 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2305 {
2306 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2307 return( ret );
2308 }
2309
2310 SSL_DEBUG_BUF( 4, "input record from network",
2311 ssl->in_hdr, 5 + ssl->in_msglen );
2312
Paul Bakker05ef8352012-05-08 09:17:57 +00002313#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002314 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002315 {
2316 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2317
2318 ret = ssl_hw_record_read( ssl );
2319 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2320 {
2321 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002322 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002323 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002324
2325 if( ret == 0 )
2326 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002327 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002328#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002329 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002330 {
2331 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2332 {
Paul Bakker40865c82013-01-31 17:13:13 +01002333#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2334 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2335 {
2336 ssl_send_alert_message( ssl,
2337 SSL_ALERT_LEVEL_FATAL,
2338 SSL_ALERT_MSG_BAD_RECORD_MAC );
2339 }
2340#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2342 return( ret );
2343 }
2344
2345 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2346 ssl->in_msg, ssl->in_msglen );
2347
2348 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2349 {
2350 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002351 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 }
2353 }
2354
Paul Bakker2770fbd2012-07-03 13:30:23 +00002355#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002356 if( ssl->transform_in != NULL &&
2357 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002358 {
2359 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2360 {
2361 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2362 return( ret );
2363 }
2364
2365 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2366 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2367 }
2368#endif /* POLARSSL_ZLIB_SUPPORT */
2369
Paul Bakker0a925182012-04-16 06:46:41 +00002370 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2371 ssl->in_msgtype != SSL_MSG_ALERT &&
2372 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2373 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2374 {
2375 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2376
Paul Bakker48916f92012-09-16 19:57:18 +00002377 if( ( ret = ssl_send_alert_message( ssl,
2378 SSL_ALERT_LEVEL_FATAL,
2379 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002380 {
2381 return( ret );
2382 }
2383
2384 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2385 }
2386
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2388 {
2389 ssl->in_hslen = 4;
2390 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2391
2392 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2393 " %d, type = %d, hslen = %d",
2394 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2395
2396 /*
2397 * Additional checks to validate the handshake header
2398 */
2399 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2400 {
2401 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002402 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 }
2404
2405 if( ssl->in_msglen < ssl->in_hslen )
2406 {
2407 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002408 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 }
2410
Paul Bakker48916f92012-09-16 19:57:18 +00002411 if( ssl->state != SSL_HANDSHAKE_OVER )
2412 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 }
2414
2415 if( ssl->in_msgtype == SSL_MSG_ALERT )
2416 {
2417 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2418 ssl->in_msg[0], ssl->in_msg[1] ) );
2419
2420 /*
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002421 * Ignore non-fatal alerts, except close_notify and no_renego
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002423 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002425 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2426 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002427 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 }
2429
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002430 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2431 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 {
2433 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002434 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 }
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002436
2437 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2438 ssl->in_msg[1] == SSL_ALERT_MSG_NO_RENEGOTIATION )
2439 {
2440 SSL_DEBUG_MSG( 2, ( "is a no_renegotiation" ) );
2441 /* Will be handled when trying to parse ServerHello */
2442 ssl->in_left = 0;
2443 return( 0 );
2444 }
2445
2446 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
2447 ssl->endpoint == SSL_IS_SERVER &&
2448 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2449 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
2450 {
2451 SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
2452 /* Will be handled in ssl_parse_certificate() */
2453 ssl->in_left = 0;
2454 return( 0 );
2455 }
2456
2457 /* Silently discard: fetch new message */
2458 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 }
2460
2461 ssl->in_left = 0;
2462
2463 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2464
2465 return( 0 );
2466}
2467
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002468int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2469{
2470 int ret;
2471
2472 if( ( ret = ssl_send_alert_message( ssl,
2473 SSL_ALERT_LEVEL_FATAL,
2474 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2475 {
2476 return( ret );
2477 }
2478
2479 return( 0 );
2480}
2481
Paul Bakker0a925182012-04-16 06:46:41 +00002482int ssl_send_alert_message( ssl_context *ssl,
2483 unsigned char level,
2484 unsigned char message )
2485{
2486 int ret;
2487
2488 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2489
2490 ssl->out_msgtype = SSL_MSG_ALERT;
2491 ssl->out_msglen = 2;
2492 ssl->out_msg[0] = level;
2493 ssl->out_msg[1] = message;
2494
2495 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2496 {
2497 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2498 return( ret );
2499 }
2500
2501 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2502
2503 return( 0 );
2504}
2505
Paul Bakker5121ce52009-01-03 21:22:43 +00002506/*
2507 * Handshake functions
2508 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002509#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2510 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2511 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2512 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2513 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2514 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2515 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002516int ssl_write_certificate( ssl_context *ssl )
2517{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002518 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
2520 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2521
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002522 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002523 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2524 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002525 {
2526 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2527 ssl->state++;
2528 return( 0 );
2529 }
2530
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002531 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2532 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002533}
2534
2535int ssl_parse_certificate( ssl_context *ssl )
2536{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002537 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2538
2539 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2540
2541 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002542 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2543 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002544 {
2545 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2546 ssl->state++;
2547 return( 0 );
2548 }
2549
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002550 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2551 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002552}
2553#else
2554int ssl_write_certificate( ssl_context *ssl )
2555{
2556 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2557 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002558 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002559 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2560
2561 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2562
2563 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002564 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2565 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002566 {
2567 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2568 ssl->state++;
2569 return( 0 );
2570 }
2571
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002572#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 if( ssl->endpoint == SSL_IS_CLIENT )
2574 {
2575 if( ssl->client_auth == 0 )
2576 {
2577 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2578 ssl->state++;
2579 return( 0 );
2580 }
2581
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002582#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 /*
2584 * If using SSLv3 and got no cert, send an Alert message
2585 * (otherwise an empty Certificate message will be sent).
2586 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002587 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2589 {
2590 ssl->out_msglen = 2;
2591 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002592 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2593 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
2595 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2596 goto write_msg;
2597 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002598#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002600#endif /* POLARSSL_SSL_CLI_C */
2601#if defined(POLARSSL_SSL_SRV_C)
2602 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002604 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 {
2606 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002607 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 }
2609 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002610#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002612 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002613
2614 /*
2615 * 0 . 0 handshake type
2616 * 1 . 3 handshake length
2617 * 4 . 6 length of all certs
2618 * 7 . 9 length of cert. 1
2619 * 10 . n-1 peer certificate
2620 * n . n+2 length of cert. 2
2621 * n+3 . ... upper level cert, etc.
2622 */
2623 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002624 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
Paul Bakker29087132010-03-21 21:03:34 +00002626 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 {
2628 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002629 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002630 {
2631 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2632 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002633 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 }
2635
2636 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2637 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2638 ssl->out_msg[i + 2] = (unsigned char)( n );
2639
2640 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2641 i += n; crt = crt->next;
2642 }
2643
2644 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2645 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2646 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2647
2648 ssl->out_msglen = i;
2649 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2650 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2651
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002652#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002653write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002654#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
2656 ssl->state++;
2657
2658 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2659 {
2660 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2661 return( ret );
2662 }
2663
2664 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2665
Paul Bakkered27a042013-04-18 22:46:23 +02002666 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002667}
2668
2669int ssl_parse_certificate( ssl_context *ssl )
2670{
Paul Bakkered27a042013-04-18 22:46:23 +02002671 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002672 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002673 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
2675 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2676
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002677 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002678 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2679 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002680 {
2681 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2682 ssl->state++;
2683 return( 0 );
2684 }
2685
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002686#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002688 ( ssl->authmode == SSL_VERIFY_NONE ||
2689 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002691 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2693 ssl->state++;
2694 return( 0 );
2695 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002696#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
2698 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2699 {
2700 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2701 return( ret );
2702 }
2703
2704 ssl->state++;
2705
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002706#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002707#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 /*
2709 * Check if the client sent an empty certificate
2710 */
2711 if( ssl->endpoint == SSL_IS_SERVER &&
2712 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2713 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002714 if( ssl->in_msglen == 2 &&
2715 ssl->in_msgtype == SSL_MSG_ALERT &&
2716 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2717 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 {
2719 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2720
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002721 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2723 return( 0 );
2724 else
Paul Bakker40e46942009-01-03 21:51:57 +00002725 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002728#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002730#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2731 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 if( ssl->endpoint == SSL_IS_SERVER &&
2733 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2734 {
2735 if( ssl->in_hslen == 7 &&
2736 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2737 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2738 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2739 {
2740 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2741
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002742 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002744 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 else
2746 return( 0 );
2747 }
2748 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002749#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2750 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002751#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
2753 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2754 {
2755 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002756 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 }
2758
2759 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2760 {
2761 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002762 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 }
2764
2765 /*
2766 * Same message structure as in ssl_write_certificate()
2767 */
2768 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2769
2770 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2771 {
2772 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002773 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002774 }
2775
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002776 /* In case we tried to reuse a session but it failed */
2777 if( ssl->session_negotiate->peer_cert != NULL )
2778 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002779 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002780 polarssl_free( ssl->session_negotiate->peer_cert );
2781 }
2782
Mansour Moufidc531b4a2015-02-15 17:35:38 -05002783 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002784 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 {
2786 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002787 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002788 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 }
2790
Paul Bakkerb6b09562013-09-18 14:17:41 +02002791 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
2793 i = 7;
2794
2795 while( i < ssl->in_hslen )
2796 {
2797 if( ssl->in_msg[i] != 0 )
2798 {
2799 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002800 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 }
2802
2803 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2804 | (unsigned int) ssl->in_msg[i + 2];
2805 i += 3;
2806
2807 if( n < 128 || i + n > ssl->in_hslen )
2808 {
2809 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002810 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 }
2812
Paul Bakkerddf26b42013-09-18 13:46:23 +02002813 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2814 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 if( ret != 0 )
2816 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002817 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 return( ret );
2819 }
2820
2821 i += n;
2822 }
2823
Paul Bakker48916f92012-09-16 19:57:18 +00002824 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002826 /*
2827 * On client, make sure the server cert doesn't change during renego to
2828 * avoid "triple handshake" attack: https://secure-resumption.com/
2829 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002830#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002831 if( ssl->endpoint == SSL_IS_CLIENT &&
2832 ssl->renegotiation == SSL_RENEGOTIATION )
2833 {
2834 if( ssl->session->peer_cert == NULL )
2835 {
2836 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2837 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2838 }
2839
2840 if( ssl->session->peer_cert->raw.len !=
2841 ssl->session_negotiate->peer_cert->raw.len ||
2842 memcmp( ssl->session->peer_cert->raw.p,
2843 ssl->session_negotiate->peer_cert->raw.p,
2844 ssl->session->peer_cert->raw.len ) != 0 )
2845 {
2846 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2847 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2848 }
2849 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002850#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002851
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 if( ssl->authmode != SSL_VERIFY_NONE )
2853 {
2854 if( ssl->ca_chain == NULL )
2855 {
2856 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002857 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002858 }
2859
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002860 /*
2861 * Main check: verify certificate
2862 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002863 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2864 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2865 &ssl->session_negotiate->verify_result,
2866 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
2868 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002869 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002871 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002872
2873 /*
2874 * Secondary checks: always done, but change 'ret' only if it was 0
2875 */
2876
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002877#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002878 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002879 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002880
2881 /* If certificate uses an EC key, make sure the curve is OK */
2882 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2883 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2884 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002885 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002886 if( ret == 0 )
2887 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002888 }
2889 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002890#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002892 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2893 ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02002894 ! ssl->endpoint,
2895 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002896 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002897 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002898 if( ret == 0 )
2899 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2900 }
2901
Paul Bakker5121ce52009-01-03 21:22:43 +00002902 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2903 ret = 0;
2904 }
2905
2906 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2907
2908 return( ret );
2909}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002910#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2911 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2912 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2913 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2914 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2915 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2916 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002917
2918int ssl_write_change_cipher_spec( ssl_context *ssl )
2919{
2920 int ret;
2921
2922 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2923
2924 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2925 ssl->out_msglen = 1;
2926 ssl->out_msg[0] = 1;
2927
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 ssl->state++;
2929
2930 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2931 {
2932 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2933 return( ret );
2934 }
2935
2936 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2937
2938 return( 0 );
2939}
2940
2941int ssl_parse_change_cipher_spec( ssl_context *ssl )
2942{
2943 int ret;
2944
2945 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2946
Paul Bakker5121ce52009-01-03 21:22:43 +00002947 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2948 {
2949 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2950 return( ret );
2951 }
2952
2953 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2954 {
2955 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002956 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002957 }
2958
2959 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2960 {
2961 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002962 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002963 }
2964
2965 ssl->state++;
2966
2967 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2968
2969 return( 0 );
2970}
2971
Paul Bakker41c83d32013-03-20 14:39:14 +01002972void ssl_optimize_checksum( ssl_context *ssl,
2973 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002974{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002975 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002976
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002977#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2978 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002979 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002980 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002981 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002982#endif
2983#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2984#if defined(POLARSSL_SHA512_C)
2985 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2986 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2987 else
2988#endif
2989#if defined(POLARSSL_SHA256_C)
2990 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002991 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002992 else
2993#endif
2994#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002995 {
2996 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002997 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002998 }
Paul Bakker380da532012-04-18 16:10:25 +00002999}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003000
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003001static void ssl_update_checksum_start( ssl_context *ssl,
3002 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003003{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003004#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3005 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003006 md5_update( &ssl->handshake->fin_md5 , buf, len );
3007 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003008#endif
3009#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3010#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003011 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003012#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003013#if defined(POLARSSL_SHA512_C)
3014 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003015#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003016#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003017}
3018
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003019#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3020 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003021static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3022 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003023{
Paul Bakker48916f92012-09-16 19:57:18 +00003024 md5_update( &ssl->handshake->fin_md5 , buf, len );
3025 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003026}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003027#endif
Paul Bakker380da532012-04-18 16:10:25 +00003028
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003029#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3030#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003031static void ssl_update_checksum_sha256( ssl_context *ssl,
3032 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003033{
Paul Bakker9e36f042013-06-30 14:34:05 +02003034 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003035}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003036#endif
Paul Bakker380da532012-04-18 16:10:25 +00003037
Paul Bakker9e36f042013-06-30 14:34:05 +02003038#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003039static void ssl_update_checksum_sha384( ssl_context *ssl,
3040 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003041{
Paul Bakker9e36f042013-06-30 14:34:05 +02003042 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003043}
Paul Bakker769075d2012-11-24 11:26:46 +01003044#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003045#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003046
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003047#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048static void ssl_calc_finished_ssl(
3049 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003050{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003051 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003052 md5_context md5;
3053 sha1_context sha1;
3054
Paul Bakker5121ce52009-01-03 21:22:43 +00003055 unsigned char padbuf[48];
3056 unsigned char md5sum[16];
3057 unsigned char sha1sum[20];
3058
Paul Bakker48916f92012-09-16 19:57:18 +00003059 ssl_session *session = ssl->session_negotiate;
3060 if( !session )
3061 session = ssl->session;
3062
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3064
Paul Bakker48916f92012-09-16 19:57:18 +00003065 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3066 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003067
3068 /*
3069 * SSLv3:
3070 * hash =
3071 * MD5( master + pad2 +
3072 * MD5( handshake + sender + master + pad1 ) )
3073 * + SHA1( master + pad2 +
3074 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003075 */
3076
Paul Bakker90995b52013-06-24 19:20:35 +02003077#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003079 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003080#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003081
Paul Bakker90995b52013-06-24 19:20:35 +02003082#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003084 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003085#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Paul Bakker3c2122f2013-06-24 19:03:14 +02003087 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3088 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
Paul Bakker1ef83d62012-04-11 12:09:53 +00003090 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
Paul Bakker3c2122f2013-06-24 19:03:14 +02003092 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003093 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003094 md5_update( &md5, padbuf, 48 );
3095 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Paul Bakker3c2122f2013-06-24 19:03:14 +02003097 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003098 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003099 sha1_update( &sha1, padbuf, 40 );
3100 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003101
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003103
Paul Bakker1ef83d62012-04-11 12:09:53 +00003104 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003105 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003106 md5_update( &md5, padbuf, 48 );
3107 md5_update( &md5, md5sum, 16 );
3108 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003109
Paul Bakker1ef83d62012-04-11 12:09:53 +00003110 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003111 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003112 sha1_update( &sha1, padbuf , 40 );
3113 sha1_update( &sha1, sha1sum, 20 );
3114 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003115
Paul Bakker1ef83d62012-04-11 12:09:53 +00003116 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003117
Paul Bakker5b4af392014-06-26 12:09:34 +02003118 md5_free( &md5 );
3119 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003120
Paul Bakker34617722014-06-13 17:20:13 +02003121 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3122 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3123 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003124
3125 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3126}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003127#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003128
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003129#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003130static void ssl_calc_finished_tls(
3131 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003132{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003133 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003134 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003135 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003136 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003137 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Paul Bakker48916f92012-09-16 19:57:18 +00003139 ssl_session *session = ssl->session_negotiate;
3140 if( !session )
3141 session = ssl->session;
3142
Paul Bakker1ef83d62012-04-11 12:09:53 +00003143 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003144
Paul Bakker48916f92012-09-16 19:57:18 +00003145 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3146 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003147
Paul Bakker1ef83d62012-04-11 12:09:53 +00003148 /*
3149 * TLSv1:
3150 * hash = PRF( master, finished_label,
3151 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3152 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003153
Paul Bakker90995b52013-06-24 19:20:35 +02003154#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3156 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003157#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003158
Paul Bakker90995b52013-06-24 19:20:35 +02003159#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003160 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3161 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003162#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003163
3164 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003165 ? "client finished"
3166 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003167
3168 md5_finish( &md5, padbuf );
3169 sha1_finish( &sha1, padbuf + 16 );
3170
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003171 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003172 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003173
3174 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3175
Paul Bakker5b4af392014-06-26 12:09:34 +02003176 md5_free( &md5 );
3177 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003178
Paul Bakker34617722014-06-13 17:20:13 +02003179 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003180
3181 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3182}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003183#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003184
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003185#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3186#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003187static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003188 ssl_context *ssl, unsigned char *buf, int from )
3189{
3190 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003191 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003192 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003193 unsigned char padbuf[32];
3194
Paul Bakker48916f92012-09-16 19:57:18 +00003195 ssl_session *session = ssl->session_negotiate;
3196 if( !session )
3197 session = ssl->session;
3198
Paul Bakker380da532012-04-18 16:10:25 +00003199 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003200
Paul Bakker9e36f042013-06-30 14:34:05 +02003201 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003202
3203 /*
3204 * TLSv1.2:
3205 * hash = PRF( master, finished_label,
3206 * Hash( handshake ) )[0.11]
3207 */
3208
Paul Bakker9e36f042013-06-30 14:34:05 +02003209#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003210 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003211 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003212#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003213
3214 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003215 ? "client finished"
3216 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003217
Paul Bakker9e36f042013-06-30 14:34:05 +02003218 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003219
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003220 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003221 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003222
3223 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3224
Paul Bakker5b4af392014-06-26 12:09:34 +02003225 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003226
Paul Bakker34617722014-06-13 17:20:13 +02003227 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003228
3229 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3230}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003231#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003232
Paul Bakker9e36f042013-06-30 14:34:05 +02003233#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003234static void ssl_calc_finished_tls_sha384(
3235 ssl_context *ssl, unsigned char *buf, int from )
3236{
3237 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003238 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003239 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003240 unsigned char padbuf[48];
3241
Paul Bakker48916f92012-09-16 19:57:18 +00003242 ssl_session *session = ssl->session_negotiate;
3243 if( !session )
3244 session = ssl->session;
3245
Paul Bakker380da532012-04-18 16:10:25 +00003246 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003247
Paul Bakker9e36f042013-06-30 14:34:05 +02003248 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003249
3250 /*
3251 * TLSv1.2:
3252 * hash = PRF( master, finished_label,
3253 * Hash( handshake ) )[0.11]
3254 */
3255
Paul Bakker9e36f042013-06-30 14:34:05 +02003256#if !defined(POLARSSL_SHA512_ALT)
3257 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3258 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003259#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003260
3261 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003262 ? "client finished"
3263 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003264
Paul Bakker9e36f042013-06-30 14:34:05 +02003265 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003266
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003267 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003268 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003269
3270 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3271
Paul Bakker5b4af392014-06-26 12:09:34 +02003272 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003273
Paul Bakker34617722014-06-13 17:20:13 +02003274 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003275
3276 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3277}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003278#endif /* POLARSSL_SHA512_C */
3279#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003280
Paul Bakker48916f92012-09-16 19:57:18 +00003281void ssl_handshake_wrapup( ssl_context *ssl )
3282{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003283 int resume = ssl->handshake->resume;
3284
Paul Bakker48916f92012-09-16 19:57:18 +00003285 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3286
3287 /*
3288 * Free our handshake params
3289 */
3290 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003291 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003292 ssl->handshake = NULL;
3293
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003294#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003295 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003296 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003297 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003298 ssl->renego_records_seen = 0;
3299 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003300#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003301
Paul Bakker48916f92012-09-16 19:57:18 +00003302 /*
3303 * Switch in our now active transform context
3304 */
3305 if( ssl->transform )
3306 {
3307 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003308 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003309 }
3310 ssl->transform = ssl->transform_negotiate;
3311 ssl->transform_negotiate = NULL;
3312
Paul Bakker0a597072012-09-25 21:55:46 +00003313 if( ssl->session )
3314 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003315#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3316 /* RFC 7366 3.1: keep the EtM state */
3317 ssl->session_negotiate->encrypt_then_mac =
3318 ssl->session->encrypt_then_mac;
3319#endif
3320
Paul Bakker0a597072012-09-25 21:55:46 +00003321 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003322 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003323 }
3324 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003325 ssl->session_negotiate = NULL;
3326
Paul Bakker0a597072012-09-25 21:55:46 +00003327 /*
3328 * Add cache entry
3329 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003330 if( ssl->f_set_cache != NULL &&
3331 ssl->session->length != 0 &&
3332 resume == 0 )
3333 {
Paul Bakker0a597072012-09-25 21:55:46 +00003334 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3335 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003336 }
Paul Bakker0a597072012-09-25 21:55:46 +00003337
Paul Bakker48916f92012-09-16 19:57:18 +00003338 ssl->state++;
3339
3340 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3341}
3342
Paul Bakker1ef83d62012-04-11 12:09:53 +00003343int ssl_write_finished( ssl_context *ssl )
3344{
3345 int ret, hash_len;
3346
3347 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3348
Paul Bakker92be97b2013-01-02 17:30:03 +01003349 /*
3350 * Set the out_msg pointer to the correct location based on IV length
3351 */
3352 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3353 {
3354 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3355 ssl->transform_negotiate->fixed_ivlen;
3356 }
3357 else
3358 ssl->out_msg = ssl->out_iv;
3359
Paul Bakker48916f92012-09-16 19:57:18 +00003360 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003361
3362 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3364
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003365#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003366 ssl->verify_data_len = hash_len;
3367 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003368#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003369
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 ssl->out_msglen = 4 + hash_len;
3371 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3372 ssl->out_msg[0] = SSL_HS_FINISHED;
3373
3374 /*
3375 * In case of session resuming, invert the client and server
3376 * ChangeCipherSpec messages order.
3377 */
Paul Bakker0a597072012-09-25 21:55:46 +00003378 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003379 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003380#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003382 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003383#endif
3384#if defined(POLARSSL_SSL_SRV_C)
3385 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003387#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 }
3389 else
3390 ssl->state++;
3391
Paul Bakker48916f92012-09-16 19:57:18 +00003392 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003393 * Switch to our negotiated transform and session parameters for outbound
3394 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003395 */
3396 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3397 ssl->transform_out = ssl->transform_negotiate;
3398 ssl->session_out = ssl->session_negotiate;
3399 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003400
Paul Bakker07eb38b2012-12-19 14:42:06 +01003401#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003402 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003403 {
3404 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3405 {
3406 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3407 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3408 }
3409 }
3410#endif
3411
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3413 {
3414 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3415 return( ret );
3416 }
3417
3418 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3419
3420 return( 0 );
3421}
3422
3423int ssl_parse_finished( ssl_context *ssl )
3424{
Paul Bakker23986e52011-04-24 08:57:21 +00003425 int ret;
3426 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003427 unsigned char buf[36];
3428
3429 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3430
Paul Bakker48916f92012-09-16 19:57:18 +00003431 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003432
Paul Bakker48916f92012-09-16 19:57:18 +00003433 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003434 * Switch to our negotiated transform and session parameters for inbound
3435 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003436 */
3437 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3438 ssl->transform_in = ssl->transform_negotiate;
3439 ssl->session_in = ssl->session_negotiate;
3440 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003441
Paul Bakker92be97b2013-01-02 17:30:03 +01003442 /*
3443 * Set the in_msg pointer to the correct location based on IV length
3444 */
3445 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3446 {
3447 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3448 ssl->transform_negotiate->fixed_ivlen;
3449 }
3450 else
3451 ssl->in_msg = ssl->in_iv;
3452
Paul Bakker07eb38b2012-12-19 14:42:06 +01003453#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003454 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003455 {
3456 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3457 {
3458 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3459 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3460 }
3461 }
3462#endif
3463
Paul Bakker5121ce52009-01-03 21:22:43 +00003464 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3465 {
3466 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3467 return( ret );
3468 }
3469
3470 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3471 {
3472 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003473 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003474 }
3475
Paul Bakker1ef83d62012-04-11 12:09:53 +00003476 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003477 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3478
3479 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3480 ssl->in_hslen != 4 + hash_len )
3481 {
3482 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003483 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 }
3485
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003486 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 {
3488 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003489 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003490 }
3491
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003492#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003493 ssl->verify_data_len = hash_len;
3494 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003495#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003496
Paul Bakker0a597072012-09-25 21:55:46 +00003497 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003498 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003499#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003500 if( ssl->endpoint == SSL_IS_CLIENT )
3501 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003502#endif
3503#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003504 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003505 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003507 }
3508 else
3509 ssl->state++;
3510
3511 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3512
3513 return( 0 );
3514}
3515
Paul Bakker968afaa2014-07-09 11:09:24 +02003516static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003517{
3518 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3519
3520#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3521 defined(POLARSSL_SSL_PROTO_TLS1_1)
3522 md5_init( &handshake->fin_md5 );
3523 sha1_init( &handshake->fin_sha1 );
3524 md5_starts( &handshake->fin_md5 );
3525 sha1_starts( &handshake->fin_sha1 );
3526#endif
3527#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3528#if defined(POLARSSL_SHA256_C)
3529 sha256_init( &handshake->fin_sha256 );
3530 sha256_starts( &handshake->fin_sha256, 0 );
3531#endif
3532#if defined(POLARSSL_SHA512_C)
3533 sha512_init( &handshake->fin_sha512 );
3534 sha512_starts( &handshake->fin_sha512, 1 );
3535#endif
3536#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3537
3538 handshake->update_checksum = ssl_update_checksum_start;
3539 handshake->sig_alg = SSL_HASH_SHA1;
3540
3541#if defined(POLARSSL_DHM_C)
3542 dhm_init( &handshake->dhm_ctx );
3543#endif
3544#if defined(POLARSSL_ECDH_C)
3545 ecdh_init( &handshake->ecdh_ctx );
3546#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003547}
3548
3549static void ssl_transform_init( ssl_transform *transform )
3550{
3551 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003552
3553 cipher_init( &transform->cipher_ctx_enc );
3554 cipher_init( &transform->cipher_ctx_dec );
3555
3556 md_init( &transform->md_ctx_enc );
3557 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003558}
3559
3560void ssl_session_init( ssl_session *session )
3561{
3562 memset( session, 0, sizeof(ssl_session) );
3563}
3564
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003565static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003566{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003567 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003568 if( ssl->transform_negotiate )
3569 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003570 if( ssl->session_negotiate )
3571 ssl_session_free( ssl->session_negotiate );
3572 if( ssl->handshake )
3573 ssl_handshake_free( ssl->handshake );
3574
3575 /*
3576 * Either the pointers are now NULL or cleared properly and can be freed.
3577 * Now allocate missing structures.
3578 */
3579 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003580 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003581 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003582 }
Paul Bakker48916f92012-09-16 19:57:18 +00003583
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003584 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003585 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003586 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003587 }
Paul Bakker48916f92012-09-16 19:57:18 +00003588
Paul Bakker82788fb2014-10-20 13:59:19 +02003589 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003590 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003591 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003592 }
Paul Bakker48916f92012-09-16 19:57:18 +00003593
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003594 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003595 if( ssl->handshake == NULL ||
3596 ssl->transform_negotiate == NULL ||
3597 ssl->session_negotiate == NULL )
3598 {
3599 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003600
3601 polarssl_free( ssl->handshake );
3602 polarssl_free( ssl->transform_negotiate );
3603 polarssl_free( ssl->session_negotiate );
3604
3605 ssl->handshake = NULL;
3606 ssl->transform_negotiate = NULL;
3607 ssl->session_negotiate = NULL;
3608
Paul Bakker48916f92012-09-16 19:57:18 +00003609 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3610 }
3611
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003612 /* Initialize structures */
3613 ssl_session_init( ssl->session_negotiate );
3614 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003615 ssl_handshake_params_init( ssl->handshake );
3616
3617#if defined(POLARSSL_X509_CRT_PARSE_C)
3618 ssl->handshake->key_cert = ssl->key_cert;
3619#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003620
Paul Bakker48916f92012-09-16 19:57:18 +00003621 return( 0 );
3622}
3623
Paul Bakker5121ce52009-01-03 21:22:43 +00003624/*
3625 * Initialize an SSL context
3626 */
3627int ssl_init( ssl_context *ssl )
3628{
Paul Bakker48916f92012-09-16 19:57:18 +00003629 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003630 int len = SSL_BUFFER_LEN;
3631
3632 memset( ssl, 0, sizeof( ssl_context ) );
3633
Paul Bakker62f2dee2012-09-28 07:31:51 +00003634 /*
3635 * Sane defaults
3636 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003637 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3638 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3639 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3640 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003641
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003642 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003643
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003644#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003645 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003646 memset( ssl->renego_period, 0xFF, 7 );
3647 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003648#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003649
Paul Bakker62f2dee2012-09-28 07:31:51 +00003650#if defined(POLARSSL_DHM_C)
3651 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003652 POLARSSL_DHM_RFC5114_MODP_2048_P) ) != 0 ||
Paul Bakker62f2dee2012-09-28 07:31:51 +00003653 ( ret = mpi_read_string( &ssl->dhm_G, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003654 POLARSSL_DHM_RFC5114_MODP_2048_G) ) != 0 )
Paul Bakker62f2dee2012-09-28 07:31:51 +00003655 {
3656 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3657 return( ret );
3658 }
3659#endif
3660
3661 /*
3662 * Prepare base structures
3663 */
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003664 if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL ||
3665 ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 {
3667 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003668 polarssl_free( ssl->in_ctr );
3669 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003670 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003671 }
3672
3673 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3674 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3675
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003676 ssl->in_hdr = ssl->in_ctr + 8;
3677 ssl->in_iv = ssl->in_ctr + 13;
3678 ssl->in_msg = ssl->in_ctr + 13;
3679
3680 ssl->out_hdr = ssl->out_ctr + 8;
3681 ssl->out_iv = ssl->out_ctr + 13;
3682 ssl->out_msg = ssl->out_ctr + 13;
3683
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003684#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3685 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3686#endif
3687
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003688#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3689 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3690#endif
3691
Paul Bakker606b4ba2013-08-14 16:52:14 +02003692#if defined(POLARSSL_SSL_SESSION_TICKETS)
3693 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3694#endif
3695
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003696#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003697 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003698#endif
3699
Paul Bakker48916f92012-09-16 19:57:18 +00003700 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3701 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003702
3703 return( 0 );
3704}
3705
3706/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003707 * Reset an initialized and used SSL context for re-use while retaining
3708 * all application-set variables, function pointers and data.
3709 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003710int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003711{
Paul Bakker48916f92012-09-16 19:57:18 +00003712 int ret;
3713
Paul Bakker7eb013f2011-10-06 12:37:39 +00003714 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003715
3716#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003717 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003718 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003719
3720 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003721 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3722 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003723#endif
3724 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003725
Paul Bakker7eb013f2011-10-06 12:37:39 +00003726 ssl->in_offt = NULL;
3727
Paul Bakker92be97b2013-01-02 17:30:03 +01003728 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003729 ssl->in_msgtype = 0;
3730 ssl->in_msglen = 0;
3731 ssl->in_left = 0;
3732
3733 ssl->in_hslen = 0;
3734 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003735 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003736
Paul Bakker92be97b2013-01-02 17:30:03 +01003737 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003738 ssl->out_msgtype = 0;
3739 ssl->out_msglen = 0;
3740 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003741#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003742 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3743 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003744#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003745
Paul Bakker48916f92012-09-16 19:57:18 +00003746 ssl->transform_in = NULL;
3747 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003748
3749 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3750 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003751
3752#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003753 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003754 {
3755 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003756 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003757 {
3758 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3759 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3760 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003761 }
3762#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003763
Paul Bakker48916f92012-09-16 19:57:18 +00003764 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003765 {
Paul Bakker48916f92012-09-16 19:57:18 +00003766 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003767 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003768 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003769 }
Paul Bakker48916f92012-09-16 19:57:18 +00003770
Paul Bakkerc0463502013-02-14 11:19:38 +01003771 if( ssl->session )
3772 {
3773 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003774 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003775 ssl->session = NULL;
3776 }
3777
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003778#if defined(POLARSSL_SSL_ALPN)
3779 ssl->alpn_chosen = NULL;
3780#endif
3781
Paul Bakker48916f92012-09-16 19:57:18 +00003782 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3783 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003784
3785 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003786}
3787
Paul Bakkera503a632013-08-14 13:48:06 +02003788#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003789static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3790{
3791 aes_free( &tkeys->enc );
3792 aes_free( &tkeys->dec );
3793
3794 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3795}
3796
Paul Bakker7eb013f2011-10-06 12:37:39 +00003797/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003798 * Allocate and initialize ticket keys
3799 */
3800static int ssl_ticket_keys_init( ssl_context *ssl )
3801{
3802 int ret;
3803 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003804 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003805
3806 if( ssl->ticket_keys != NULL )
3807 return( 0 );
3808
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003809 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003810 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003811 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3812
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003813 aes_init( &tkeys->enc );
3814 aes_init( &tkeys->dec );
3815
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003816 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003817 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003818 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003819 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003820 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003821 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003822
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003823 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3824 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3825 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3826 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003827 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003828 polarssl_free( tkeys );
3829 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003830 }
3831
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003832 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003833 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003834 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003835 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003836 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003837 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003838
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003839 ssl->ticket_keys = tkeys;
3840
3841 return( 0 );
3842}
Paul Bakkera503a632013-08-14 13:48:06 +02003843#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003844
3845/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003846 * SSL set accessors
3847 */
3848void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3849{
3850 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003851
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003852#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3853 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003854 if( endpoint == SSL_IS_CLIENT )
3855 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003856#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003857
3858#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3859 if( endpoint == SSL_IS_SERVER )
3860 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3861#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003862}
3863
3864void ssl_set_authmode( ssl_context *ssl, int authmode )
3865{
3866 ssl->authmode = authmode;
3867}
3868
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003869#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003870void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003871 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003872 void *p_vrfy )
3873{
3874 ssl->f_vrfy = f_vrfy;
3875 ssl->p_vrfy = p_vrfy;
3876}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003877#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003878
Paul Bakker5121ce52009-01-03 21:22:43 +00003879void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003880 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003881 void *p_rng )
3882{
3883 ssl->f_rng = f_rng;
3884 ssl->p_rng = p_rng;
3885}
3886
3887void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003888 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003889 void *p_dbg )
3890{
3891 ssl->f_dbg = f_dbg;
3892 ssl->p_dbg = p_dbg;
3893}
3894
3895void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003896 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003897 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003898{
3899 ssl->f_recv = f_recv;
3900 ssl->f_send = f_send;
3901 ssl->p_recv = p_recv;
3902 ssl->p_send = p_send;
3903}
3904
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003905#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003906void ssl_set_session_cache( ssl_context *ssl,
3907 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3908 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003909{
Paul Bakker0a597072012-09-25 21:55:46 +00003910 ssl->f_get_cache = f_get_cache;
3911 ssl->p_get_cache = p_get_cache;
3912 ssl->f_set_cache = f_set_cache;
3913 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003914}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003915#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003916
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003917#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003918int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003919{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003920 int ret;
3921
3922 if( ssl == NULL ||
3923 session == NULL ||
3924 ssl->session_negotiate == NULL ||
3925 ssl->endpoint != SSL_IS_CLIENT )
3926 {
3927 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3928 }
3929
3930 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3931 return( ret );
3932
Paul Bakker0a597072012-09-25 21:55:46 +00003933 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003934
3935 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003936}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003937#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003938
Paul Bakkerb68cad62012-08-23 08:34:18 +00003939void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003940{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003941 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3942 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3943 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3944 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3945}
3946
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003947void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3948 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003949 int major, int minor )
3950{
3951 if( major != SSL_MAJOR_VERSION_3 )
3952 return;
3953
3954 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3955 return;
3956
3957 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003958}
3959
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003960#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003961/* Add a new (empty) key_cert entry an return a pointer to it */
3962static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3963{
3964 ssl_key_cert *key_cert, *last;
3965
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003966 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003967 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003968 return( NULL );
3969
3970 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3971
3972 /* Append the new key_cert to the (possibly empty) current list */
3973 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003974 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003975 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003976 if( ssl->handshake != NULL )
3977 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003978 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003979 else
3980 {
3981 last = ssl->key_cert;
3982 while( last->next != NULL )
3983 last = last->next;
3984 last->next = key_cert;
3985 }
3986
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003987 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003988}
3989
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003990void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003991 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003992{
3993 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003994 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003995 ssl->peer_cn = peer_cn;
3996}
3997
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003998int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003999 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00004000{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004001 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
4002
4003 if( key_cert == NULL )
4004 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4005
4006 key_cert->cert = own_cert;
4007 key_cert->key = pk_key;
4008
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004009 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010}
4011
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004012#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004013#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004014int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004015 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004016{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004017 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004018 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004019
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004020 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004021 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4022
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004023 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004024 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004025 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004026
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004027 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004028
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004029 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004030 if( ret != 0 )
4031 return( ret );
4032
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004033 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004034 return( ret );
4035
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004036 key_cert->cert = own_cert;
4037 key_cert->key_own_alloc = 1;
4038
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004039 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004040}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004041#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004042
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004043int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004044 void *rsa_key,
4045 rsa_decrypt_func rsa_decrypt,
4046 rsa_sign_func rsa_sign,
4047 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004048{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004049 int ret;
4050 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004051
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004052 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004053 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4054
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004055 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004056 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004057 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004058
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004059 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004060
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004061 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4062 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4063 return( ret );
4064
4065 key_cert->cert = own_cert;
4066 key_cert->key_own_alloc = 1;
4067
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004068 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004069}
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004070#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004071#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004072
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004073#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004074int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4075 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004076{
Paul Bakker6db455e2013-09-18 17:29:31 +02004077 if( psk == NULL || psk_identity == NULL )
4078 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4079
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004080 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004081 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4082
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02004083 /* Identity len will be encoded on two bytes */
4084 if( ( psk_identity_len >> 16 ) != 0 ||
4085 psk_identity_len > SSL_MAX_CONTENT_LEN )
4086 {
4087 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4088 }
4089
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004090 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004091 {
4092 polarssl_free( ssl->psk );
4093 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnard9c521762015-10-27 10:54:53 +01004094 ssl->psk = NULL;
4095 ssl->psk_identity = NULL;
Paul Bakker6db455e2013-09-18 17:29:31 +02004096 }
4097
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004098 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
4099 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05004100 {
4101 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004102 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004103 ssl->psk = NULL;
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004104 ssl->psk_identity = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05004105 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4106 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004107
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004108 ssl->psk_len = psk_len;
4109 ssl->psk_identity_len = psk_identity_len;
4110
Paul Bakker6db455e2013-09-18 17:29:31 +02004111 memcpy( ssl->psk, psk, ssl->psk_len );
4112 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004113
4114 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004115}
4116
4117void ssl_set_psk_cb( ssl_context *ssl,
4118 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4119 size_t),
4120 void *p_psk )
4121{
4122 ssl->f_psk = f_psk;
4123 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004124}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004125#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004126
Paul Bakker48916f92012-09-16 19:57:18 +00004127#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004128int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004129{
4130 int ret;
4131
Paul Bakker48916f92012-09-16 19:57:18 +00004132 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004133 {
4134 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4135 return( ret );
4136 }
4137
Paul Bakker48916f92012-09-16 19:57:18 +00004138 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004139 {
4140 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4141 return( ret );
4142 }
4143
4144 return( 0 );
4145}
4146
Paul Bakker1b57b062011-01-06 15:48:19 +00004147int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4148{
4149 int ret;
4150
Paul Bakker66d5d072014-06-17 16:39:18 +02004151 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004152 {
4153 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4154 return( ret );
4155 }
4156
Paul Bakker66d5d072014-06-17 16:39:18 +02004157 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004158 {
4159 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4160 return( ret );
4161 }
4162
4163 return( 0 );
4164}
Paul Bakker48916f92012-09-16 19:57:18 +00004165#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004166
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004167#if defined(POLARSSL_SSL_SET_CURVES)
4168/*
4169 * Set the allowed elliptic curves
4170 */
4171void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4172{
4173 ssl->curve_list = curve_list;
4174}
4175#endif
4176
Paul Bakker0be444a2013-08-27 21:55:01 +02004177#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004178int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004179{
4180 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004181 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004182
4183 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004184
4185 if( ssl->hostname_len + 1 == 0 )
4186 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4187
Simon Butcherc988f322015-09-29 23:27:20 +01004188 if( ssl->hostname_len > SSL_MAX_HOST_NAME_LEN )
4189 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4190
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004191 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004192
Paul Bakkerb15b8512012-01-13 13:44:06 +00004193 if( ssl->hostname == NULL )
4194 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4195
Paul Bakker3c2122f2013-06-24 19:03:14 +02004196 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004197 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004198
Paul Bakker40ea7de2009-05-03 10:18:48 +00004199 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004200
4201 return( 0 );
4202}
4203
Paul Bakker5701cdc2012-09-27 21:49:42 +00004204void ssl_set_sni( ssl_context *ssl,
4205 int (*f_sni)(void *, ssl_context *,
4206 const unsigned char *, size_t),
4207 void *p_sni )
4208{
4209 ssl->f_sni = f_sni;
4210 ssl->p_sni = p_sni;
4211}
Paul Bakker0be444a2013-08-27 21:55:01 +02004212#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004213
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004214#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004215int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004216{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004217 size_t cur_len, tot_len;
4218 const char **p;
4219
4220 /*
4221 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4222 * truncated". Check lengths now rather than later.
4223 */
4224 tot_len = 0;
4225 for( p = protos; *p != NULL; p++ )
4226 {
4227 cur_len = strlen( *p );
4228 tot_len += cur_len;
4229
4230 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4231 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4232 }
4233
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004234 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004235
4236 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004237}
4238
4239const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4240{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004241 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004242}
4243#endif /* POLARSSL_SSL_ALPN */
4244
Paul Bakker490ecc82011-10-06 13:04:09 +00004245void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4246{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004247 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4248 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4249 {
4250 ssl->max_major_ver = major;
4251 ssl->max_minor_ver = minor;
4252 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004253}
4254
Paul Bakker1d29fb52012-09-28 13:28:45 +00004255void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4256{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004257 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4258 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4259 {
4260 ssl->min_major_ver = major;
4261 ssl->min_minor_ver = minor;
4262 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004263}
4264
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004265#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4266void ssl_set_fallback( ssl_context *ssl, char fallback )
4267{
4268 ssl->fallback = fallback;
4269}
4270#endif
4271
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004272#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4273void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4274{
4275 ssl->encrypt_then_mac = etm;
4276}
4277#endif
4278
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004279#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4280void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4281{
4282 ssl->extended_ms = ems;
4283}
4284#endif
4285
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004286void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4287{
4288 ssl->arc4_disabled = arc4;
4289}
4290
Paul Bakker05decb22013-08-15 13:33:48 +02004291#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004292int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4293{
Paul Bakker77e257e2013-12-16 15:29:52 +01004294 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004295 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004296 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004297 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004298 }
4299
4300 ssl->mfl_code = mfl_code;
4301
4302 return( 0 );
4303}
Paul Bakker05decb22013-08-15 13:33:48 +02004304#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004305
Paul Bakker1f2bc622013-08-15 13:45:55 +02004306#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004307int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004308{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004309 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004310
4311 return( 0 );
4312}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004313#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004314
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004315#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4316void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4317{
4318 ssl->split_done = split;
4319}
4320#endif
4321
Paul Bakker48916f92012-09-16 19:57:18 +00004322void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4323{
4324 ssl->allow_legacy_renegotiation = allow_legacy;
4325}
4326
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004327#if defined(POLARSSL_SSL_RENEGOTIATION)
4328void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4329{
4330 ssl->disable_renegotiation = renegotiation;
4331}
4332
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004333void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4334{
4335 ssl->renego_max_records = max_records;
4336}
4337
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004338void ssl_set_renegotiation_period( ssl_context *ssl,
4339 const unsigned char period[8] )
4340{
4341 memcpy( ssl->renego_period, period, 8 );
4342}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004343#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004344
Paul Bakkera503a632013-08-14 13:48:06 +02004345#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004346int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4347{
4348 ssl->session_tickets = use_tickets;
4349
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004350#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004351 if( ssl->endpoint == SSL_IS_CLIENT )
4352 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004353#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004354
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004355 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4356 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004357
4358 if( ssl->f_rng == NULL )
4359 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4360
4361 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004362}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004363
4364void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4365{
4366 ssl->ticket_lifetime = lifetime;
4367}
Paul Bakkera503a632013-08-14 13:48:06 +02004368#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004369
Paul Bakker5121ce52009-01-03 21:22:43 +00004370/*
4371 * SSL get accessors
4372 */
4373size_t ssl_get_bytes_avail( const ssl_context *ssl )
4374{
4375 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4376}
4377
Paul Bakkerff60ee62010-03-16 21:09:09 +00004378int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004379{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004380 if( ssl->session != NULL )
4381 return( ssl->session->verify_result );
4382
4383 if( ssl->session_negotiate != NULL )
4384 return( ssl->session_negotiate->verify_result );
4385
4386 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004387}
4388
Paul Bakkere3166ce2011-01-27 17:40:50 +00004389const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004390{
Paul Bakker926c8e42013-03-06 10:23:34 +01004391 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004392 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004393
Paul Bakkere3166ce2011-01-27 17:40:50 +00004394 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004395}
4396
Paul Bakker43ca69c2011-01-15 17:35:19 +00004397const char *ssl_get_version( const ssl_context *ssl )
4398{
4399 switch( ssl->minor_ver )
4400 {
4401 case SSL_MINOR_VERSION_0:
4402 return( "SSLv3.0" );
4403
4404 case SSL_MINOR_VERSION_1:
4405 return( "TLSv1.0" );
4406
4407 case SSL_MINOR_VERSION_2:
4408 return( "TLSv1.1" );
4409
Paul Bakker1ef83d62012-04-11 12:09:53 +00004410 case SSL_MINOR_VERSION_3:
4411 return( "TLSv1.2" );
4412
Paul Bakker43ca69c2011-01-15 17:35:19 +00004413 default:
4414 break;
4415 }
4416 return( "unknown" );
4417}
4418
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004419#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004420const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004421{
4422 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004423 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004424
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004425 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004426}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004427#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004428
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004429#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004430int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4431{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004432 if( ssl == NULL ||
4433 dst == NULL ||
4434 ssl->session == NULL ||
4435 ssl->endpoint != SSL_IS_CLIENT )
4436 {
4437 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4438 }
4439
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004440 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004441}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004442#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004443
Paul Bakker5121ce52009-01-03 21:22:43 +00004444/*
Paul Bakker1961b702013-01-25 14:49:24 +01004445 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004446 */
Paul Bakker1961b702013-01-25 14:49:24 +01004447int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004448{
Paul Bakker40e46942009-01-03 21:51:57 +00004449 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004450
Paul Bakker40e46942009-01-03 21:51:57 +00004451#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004452 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004453 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004454#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004455#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004456 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004457 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004458#endif
4459
Paul Bakker1961b702013-01-25 14:49:24 +01004460 return( ret );
4461}
4462
4463/*
4464 * Perform the SSL handshake
4465 */
4466int ssl_handshake( ssl_context *ssl )
4467{
4468 int ret = 0;
4469
4470 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4471
4472 while( ssl->state != SSL_HANDSHAKE_OVER )
4473 {
4474 ret = ssl_handshake_step( ssl );
4475
4476 if( ret != 0 )
4477 break;
4478 }
4479
Paul Bakker5121ce52009-01-03 21:22:43 +00004480 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4481
4482 return( ret );
4483}
4484
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004485#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004486#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004487/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004488 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004489 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004490static int ssl_write_hello_request( ssl_context *ssl )
4491{
4492 int ret;
4493
4494 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4495
4496 ssl->out_msglen = 4;
4497 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4498 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4499
4500 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4501 {
4502 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4503 return( ret );
4504 }
4505
4506 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4507
4508 return( 0 );
4509}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004510#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004511
4512/*
4513 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004514 * - any side: calling ssl_renegotiate(),
4515 * - client: receiving a HelloRequest during ssl_read(),
4516 * - server: receiving any handshake message on server during ssl_read() after
4517 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004518 * If the handshake doesn't complete due to waiting for I/O, it will continue
4519 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004520 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004521static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004522{
4523 int ret;
4524
4525 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4526
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004527 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4528 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004529
4530 ssl->state = SSL_HELLO_REQUEST;
4531 ssl->renegotiation = SSL_RENEGOTIATION;
4532
Paul Bakker48916f92012-09-16 19:57:18 +00004533 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4534 {
4535 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4536 return( ret );
4537 }
4538
4539 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4540
4541 return( 0 );
4542}
4543
4544/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004545 * Renegotiate current connection on client,
4546 * or request renegotiation on server
4547 */
4548int ssl_renegotiate( ssl_context *ssl )
4549{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004550 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004551
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004552#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004553 /* On server, just send the request */
4554 if( ssl->endpoint == SSL_IS_SERVER )
4555 {
4556 if( ssl->state != SSL_HANDSHAKE_OVER )
4557 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4558
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004559 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4560
4561 /* Did we already try/start sending HelloRequest? */
4562 if( ssl->out_left != 0 )
4563 return( ssl_flush_output( ssl ) );
4564
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004565 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004566 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004567#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004568
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004569#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004570 /*
4571 * On client, either start the renegotiation process or,
4572 * if already in progress, continue the handshake
4573 */
4574 if( ssl->renegotiation != SSL_RENEGOTIATION )
4575 {
4576 if( ssl->state != SSL_HANDSHAKE_OVER )
4577 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4578
4579 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4580 {
4581 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4582 return( ret );
4583 }
4584 }
4585 else
4586 {
4587 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4588 {
4589 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4590 return( ret );
4591 }
4592 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004593#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004594
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004595 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004596}
4597
4598/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004599 * Check record counters and renegotiate if they're above the limit.
4600 */
4601static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4602{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004603 if( ssl->state != SSL_HANDSHAKE_OVER ||
4604 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4605 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4606 {
4607 return( 0 );
4608 }
4609
4610 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004611 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4612 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004613 {
4614 return( 0 );
4615 }
4616
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004617 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004618 return( ssl_renegotiate( ssl ) );
4619}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004620#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004621
4622/*
4623 * Receive application data decrypted from the SSL layer
4624 */
Paul Bakker23986e52011-04-24 08:57:21 +00004625int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004626{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004627 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004628 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004629
4630 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4631
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004632#if defined(POLARSSL_SSL_RENEGOTIATION)
4633 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4634 {
4635 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4636 return( ret );
4637 }
4638#endif
4639
Paul Bakker5121ce52009-01-03 21:22:43 +00004640 if( ssl->state != SSL_HANDSHAKE_OVER )
4641 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004642 ret = ssl_handshake( ssl );
4643 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4644 {
4645 record_read = 1;
4646 }
4647 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 {
4649 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4650 return( ret );
4651 }
4652 }
4653
4654 if( ssl->in_offt == NULL )
4655 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004656 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004658 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4659 {
4660 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4661 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004662
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004663 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4664 return( ret );
4665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004666 }
4667
4668 if( ssl->in_msglen == 0 &&
4669 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4670 {
4671 /*
4672 * OpenSSL sends empty messages to randomize the IV
4673 */
4674 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4675 {
Paul Bakker831a7552011-05-18 13:32:51 +00004676 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4677 return( 0 );
4678
Paul Bakker5121ce52009-01-03 21:22:43 +00004679 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4680 return( ret );
4681 }
4682 }
4683
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004684#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004685 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4686 {
4687 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4688
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004689#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004690 if( ssl->endpoint == SSL_IS_CLIENT &&
4691 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4692 ssl->in_hslen != 4 ) )
4693 {
4694 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4695 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4696 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004697#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004698
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004699 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4700 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004701 ssl->allow_legacy_renegotiation ==
4702 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004703 {
4704 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4705
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004706#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004707 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004708 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004709 /*
4710 * SSLv3 does not have a "no_renegotiation" alert
4711 */
4712 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4713 return( ret );
4714 }
4715 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004716#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004717#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4718 defined(POLARSSL_SSL_PROTO_TLS1_2)
4719 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004720 {
4721 if( ( ret = ssl_send_alert_message( ssl,
4722 SSL_ALERT_LEVEL_WARNING,
4723 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4724 {
4725 return( ret );
4726 }
Paul Bakker48916f92012-09-16 19:57:18 +00004727 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004728 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004729#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4730 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004731 {
4732 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004733 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004734 }
Paul Bakker48916f92012-09-16 19:57:18 +00004735 }
4736 else
4737 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004738 ret = ssl_start_renegotiation( ssl );
4739 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4740 {
4741 record_read = 1;
4742 }
4743 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004744 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004745 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004746 return( ret );
4747 }
Paul Bakker48916f92012-09-16 19:57:18 +00004748 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004749
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004750 /* If a non-handshake record was read during renego, fallthrough,
4751 * else tell the user they should call ssl_read() again */
4752 if( ! record_read )
4753 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004754 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004755 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4756 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004757 ssl->renego_records_seen++;
4758
4759 if( ssl->renego_max_records >= 0 &&
4760 ssl->renego_records_seen > ssl->renego_max_records )
4761 {
4762 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4763 "but not honored by client" ) );
4764 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4765 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004766 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004767#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004768
4769 /* Fatal and closure alerts handled by ssl_read_record() */
4770 if( ssl->in_msgtype == SSL_MSG_ALERT )
4771 {
4772 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4773 return( POLARSSL_ERR_NET_WANT_READ );
4774 }
4775
4776 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004777 {
4778 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004779 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004780 }
4781
4782 ssl->in_offt = ssl->in_msg;
4783 }
4784
4785 n = ( len < ssl->in_msglen )
4786 ? len : ssl->in_msglen;
4787
4788 memcpy( buf, ssl->in_offt, n );
4789 ssl->in_msglen -= n;
4790
4791 if( ssl->in_msglen == 0 )
4792 /* all bytes consumed */
4793 ssl->in_offt = NULL;
4794 else
4795 /* more data available */
4796 ssl->in_offt += n;
4797
4798 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4799
Paul Bakker23986e52011-04-24 08:57:21 +00004800 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004801}
4802
4803/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004804 * Send application data to be encrypted by the SSL layer,
4805 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00004806 */
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004807static int ssl_write_real( ssl_context *ssl,
4808 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004809{
Paul Bakker23986e52011-04-24 08:57:21 +00004810 int ret;
4811 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004812 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004813
Paul Bakker05decb22013-08-15 13:33:48 +02004814#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004815 /*
4816 * Assume mfl_code is correct since it was checked when set
4817 */
4818 max_len = mfl_code_to_length[ssl->mfl_code];
4819
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004820 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004821 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004822 */
4823 if( ssl->session_out != NULL &&
4824 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4825 {
4826 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4827 }
Paul Bakker05decb22013-08-15 13:33:48 +02004828#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004829
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004830 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004831
Paul Bakker5121ce52009-01-03 21:22:43 +00004832 if( ssl->out_left != 0 )
4833 {
4834 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4835 {
4836 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4837 return( ret );
4838 }
4839 }
Paul Bakker887bd502011-06-08 13:10:54 +00004840 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004841 {
Paul Bakker887bd502011-06-08 13:10:54 +00004842 ssl->out_msglen = n;
4843 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4844 memcpy( ssl->out_msg, buf, n );
4845
4846 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4847 {
4848 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4849 return( ret );
4850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004851 }
4852
Paul Bakker23986e52011-04-24 08:57:21 +00004853 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004854}
4855
4856/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004857 * Write application data, doing 1/n-1 splitting if necessary.
4858 *
4859 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004860 * then the caller will call us again with the same arguments, so
4861 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004862 */
4863#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004864static int ssl_write_split( ssl_context *ssl,
4865 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004866{
4867 int ret;
4868
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004869 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4870 len <= 1 ||
4871 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004872 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4873 != POLARSSL_MODE_CBC )
4874 {
4875 return( ssl_write_real( ssl, buf, len ) );
4876 }
4877
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004878 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004879 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004880 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004881 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004882 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004883 }
4884
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004885 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4886 return( ret );
4887 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004888
4889 return( ret + 1 );
4890}
4891#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4892
4893/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004894 * Write application data (public-facing wrapper)
4895 */
4896int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4897{
4898 int ret;
4899
4900 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4901
4902#if defined(POLARSSL_SSL_RENEGOTIATION)
4903 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4904 {
4905 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4906 return( ret );
4907 }
4908#endif
4909
4910 if( ssl->state != SSL_HANDSHAKE_OVER )
4911 {
4912 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4913 {
4914 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4915 return( ret );
4916 }
4917 }
4918
4919#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4920 ret = ssl_write_split( ssl, buf, len );
4921#else
4922 ret = ssl_write_real( ssl, buf, len );
4923#endif
4924
4925 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4926
4927 return( ret );
4928}
4929
4930/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004931 * Notify the peer that the connection is being closed
4932 */
4933int ssl_close_notify( ssl_context *ssl )
4934{
4935 int ret;
4936
4937 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4938
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004939 if( ssl->out_left != 0 )
4940 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004941
4942 if( ssl->state == SSL_HANDSHAKE_OVER )
4943 {
Paul Bakker48916f92012-09-16 19:57:18 +00004944 if( ( ret = ssl_send_alert_message( ssl,
4945 SSL_ALERT_LEVEL_WARNING,
4946 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004947 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004948 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004949 return( ret );
4950 }
4951 }
4952
4953 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4954
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004955 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004956}
4957
Paul Bakker48916f92012-09-16 19:57:18 +00004958void ssl_transform_free( ssl_transform *transform )
4959{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004960 if( transform == NULL )
4961 return;
4962
Paul Bakker48916f92012-09-16 19:57:18 +00004963#if defined(POLARSSL_ZLIB_SUPPORT)
4964 deflateEnd( &transform->ctx_deflate );
4965 inflateEnd( &transform->ctx_inflate );
4966#endif
4967
Paul Bakker84bbeb52014-07-01 14:53:22 +02004968 cipher_free( &transform->cipher_ctx_enc );
4969 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004970
Paul Bakker84bbeb52014-07-01 14:53:22 +02004971 md_free( &transform->md_ctx_enc );
4972 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004973
Paul Bakker34617722014-06-13 17:20:13 +02004974 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004975}
4976
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004977#if defined(POLARSSL_X509_CRT_PARSE_C)
4978static void ssl_key_cert_free( ssl_key_cert *key_cert )
4979{
4980 ssl_key_cert *cur = key_cert, *next;
4981
4982 while( cur != NULL )
4983 {
4984 next = cur->next;
4985
4986 if( cur->key_own_alloc )
4987 {
4988 pk_free( cur->key );
4989 polarssl_free( cur->key );
4990 }
4991 polarssl_free( cur );
4992
4993 cur = next;
4994 }
4995}
4996#endif /* POLARSSL_X509_CRT_PARSE_C */
4997
Paul Bakker48916f92012-09-16 19:57:18 +00004998void ssl_handshake_free( ssl_handshake_params *handshake )
4999{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005000 if( handshake == NULL )
5001 return;
5002
Paul Bakker48916f92012-09-16 19:57:18 +00005003#if defined(POLARSSL_DHM_C)
5004 dhm_free( &handshake->dhm_ctx );
5005#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005006#if defined(POLARSSL_ECDH_C)
5007 ecdh_free( &handshake->ecdh_ctx );
5008#endif
5009
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005010#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005011 /* explicit void pointer cast for buggy MS compiler */
5012 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005013#endif
5014
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005015#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5016 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5017 /*
5018 * Free only the linked list wrapper, not the keys themselves
5019 * since the belong to the SNI callback
5020 */
5021 if( handshake->sni_key_cert != NULL )
5022 {
5023 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5024
5025 while( cur != NULL )
5026 {
5027 next = cur->next;
5028 polarssl_free( cur );
5029 cur = next;
5030 }
5031 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005032#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005033
Paul Bakker34617722014-06-13 17:20:13 +02005034 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005035}
5036
5037void ssl_session_free( ssl_session *session )
5038{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005039 if( session == NULL )
5040 return;
5041
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005042#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005043 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005044 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005045 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005046 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005047 }
Paul Bakkered27a042013-04-18 22:46:23 +02005048#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005049
Paul Bakkera503a632013-08-14 13:48:06 +02005050#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005051 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005052#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005053
Paul Bakker34617722014-06-13 17:20:13 +02005054 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005055}
5056
Paul Bakker5121ce52009-01-03 21:22:43 +00005057/*
5058 * Free an SSL context
5059 */
5060void ssl_free( ssl_context *ssl )
5061{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005062 if( ssl == NULL )
5063 return;
5064
Paul Bakker5121ce52009-01-03 21:22:43 +00005065 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5066
Paul Bakker5121ce52009-01-03 21:22:43 +00005067 if( ssl->out_ctr != NULL )
5068 {
Paul Bakker34617722014-06-13 17:20:13 +02005069 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005070 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005071 }
5072
5073 if( ssl->in_ctr != NULL )
5074 {
Paul Bakker34617722014-06-13 17:20:13 +02005075 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005076 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005077 }
5078
Paul Bakker16770332013-10-11 09:59:44 +02005079#if defined(POLARSSL_ZLIB_SUPPORT)
5080 if( ssl->compress_buf != NULL )
5081 {
Paul Bakker34617722014-06-13 17:20:13 +02005082 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005083 polarssl_free( ssl->compress_buf );
5084 }
5085#endif
5086
Paul Bakker40e46942009-01-03 21:51:57 +00005087#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005088 mpi_free( &ssl->dhm_P );
5089 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005090#endif
5091
Paul Bakker48916f92012-09-16 19:57:18 +00005092 if( ssl->transform )
5093 {
5094 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005095 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005096 }
5097
5098 if( ssl->handshake )
5099 {
5100 ssl_handshake_free( ssl->handshake );
5101 ssl_transform_free( ssl->transform_negotiate );
5102 ssl_session_free( ssl->session_negotiate );
5103
Paul Bakker6e339b52013-07-03 13:37:05 +02005104 polarssl_free( ssl->handshake );
5105 polarssl_free( ssl->transform_negotiate );
5106 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005107 }
5108
Paul Bakkerc0463502013-02-14 11:19:38 +01005109 if( ssl->session )
5110 {
5111 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005112 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005113 }
5114
Paul Bakkera503a632013-08-14 13:48:06 +02005115#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005116 if( ssl->ticket_keys )
5117 {
5118 ssl_ticket_keys_free( ssl->ticket_keys );
5119 polarssl_free( ssl->ticket_keys );
5120 }
Paul Bakkera503a632013-08-14 13:48:06 +02005121#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005122
Paul Bakker0be444a2013-08-27 21:55:01 +02005123#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005124 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005125 {
Paul Bakker34617722014-06-13 17:20:13 +02005126 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005127 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005128 ssl->hostname_len = 0;
5129 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005130#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005131
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005132#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005133 if( ssl->psk != NULL )
5134 {
Paul Bakker34617722014-06-13 17:20:13 +02005135 polarssl_zeroize( ssl->psk, ssl->psk_len );
5136 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005137 polarssl_free( ssl->psk );
5138 polarssl_free( ssl->psk_identity );
5139 ssl->psk_len = 0;
5140 ssl->psk_identity_len = 0;
5141 }
5142#endif
5143
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005144#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005145 ssl_key_cert_free( ssl->key_cert );
5146#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005147
Paul Bakker05ef8352012-05-08 09:17:57 +00005148#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5149 if( ssl_hw_record_finish != NULL )
5150 {
5151 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5152 ssl_hw_record_finish( ssl );
5153 }
5154#endif
5155
Paul Bakker5121ce52009-01-03 21:22:43 +00005156 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005157
Paul Bakker86f04f42013-02-14 11:20:09 +01005158 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005159 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005160}
5161
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005162#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005163/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005164 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005165 */
5166unsigned char ssl_sig_from_pk( pk_context *pk )
5167{
5168#if defined(POLARSSL_RSA_C)
5169 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5170 return( SSL_SIG_RSA );
5171#endif
5172#if defined(POLARSSL_ECDSA_C)
5173 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5174 return( SSL_SIG_ECDSA );
5175#endif
5176 return( SSL_SIG_ANON );
5177}
5178
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005179pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5180{
5181 switch( sig )
5182 {
5183#if defined(POLARSSL_RSA_C)
5184 case SSL_SIG_RSA:
5185 return( POLARSSL_PK_RSA );
5186#endif
5187#if defined(POLARSSL_ECDSA_C)
5188 case SSL_SIG_ECDSA:
5189 return( POLARSSL_PK_ECDSA );
5190#endif
5191 default:
5192 return( POLARSSL_PK_NONE );
5193 }
5194}
Paul Bakker9af723c2014-05-01 13:03:14 +02005195#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005196
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005197/*
5198 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5199 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005200md_type_t ssl_md_alg_from_hash( unsigned char hash )
5201{
5202 switch( hash )
5203 {
5204#if defined(POLARSSL_MD5_C)
5205 case SSL_HASH_MD5:
5206 return( POLARSSL_MD_MD5 );
5207#endif
5208#if defined(POLARSSL_SHA1_C)
5209 case SSL_HASH_SHA1:
5210 return( POLARSSL_MD_SHA1 );
5211#endif
5212#if defined(POLARSSL_SHA256_C)
5213 case SSL_HASH_SHA224:
5214 return( POLARSSL_MD_SHA224 );
5215 case SSL_HASH_SHA256:
5216 return( POLARSSL_MD_SHA256 );
5217#endif
5218#if defined(POLARSSL_SHA512_C)
5219 case SSL_HASH_SHA384:
5220 return( POLARSSL_MD_SHA384 );
5221 case SSL_HASH_SHA512:
5222 return( POLARSSL_MD_SHA512 );
5223#endif
5224 default:
5225 return( POLARSSL_MD_NONE );
5226 }
5227}
5228
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005229#if defined(POLARSSL_SSL_SET_CURVES)
5230/*
5231 * Check is a curve proposed by the peer is in our list.
5232 * Return 1 if we're willing to use it, 0 otherwise.
5233 */
5234int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5235{
5236 const ecp_group_id *gid;
5237
5238 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5239 if( *gid == grp_id )
5240 return( 1 );
5241
5242 return( 0 );
5243}
Paul Bakker9af723c2014-05-01 13:03:14 +02005244#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005245
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005246#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005247int ssl_check_cert_usage( const x509_crt *cert,
5248 const ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005249 int cert_endpoint,
5250 int *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005251{
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005252 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005253#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5254 int usage = 0;
5255#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005256#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5257 const char *ext_oid;
5258 size_t ext_len;
5259#endif
5260
5261#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5262 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5263 ((void) cert);
5264 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005265 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005266#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005267
5268#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5269 if( cert_endpoint == SSL_IS_SERVER )
5270 {
5271 /* Server part of the key exchange */
5272 switch( ciphersuite->key_exchange )
5273 {
5274 case POLARSSL_KEY_EXCHANGE_RSA:
5275 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5276 usage = KU_KEY_ENCIPHERMENT;
5277 break;
5278
5279 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5280 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5281 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5282 usage = KU_DIGITAL_SIGNATURE;
5283 break;
5284
5285 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5286 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5287 usage = KU_KEY_AGREEMENT;
5288 break;
5289
5290 /* Don't use default: we want warnings when adding new values */
5291 case POLARSSL_KEY_EXCHANGE_NONE:
5292 case POLARSSL_KEY_EXCHANGE_PSK:
5293 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5294 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5295 usage = 0;
5296 }
5297 }
5298 else
5299 {
5300 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5301 usage = KU_DIGITAL_SIGNATURE;
5302 }
5303
5304 if( x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005305 {
5306 *flags |= BADCERT_KEY_USAGE;
5307 ret = -1;
5308 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005309#else
5310 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005311#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5312
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005313#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5314 if( cert_endpoint == SSL_IS_SERVER )
5315 {
5316 ext_oid = OID_SERVER_AUTH;
5317 ext_len = OID_SIZE( OID_SERVER_AUTH );
5318 }
5319 else
5320 {
5321 ext_oid = OID_CLIENT_AUTH;
5322 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5323 }
5324
5325 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005326 {
5327 *flags |= BADCERT_EXT_KEY_USAGE;
5328 ret = -1;
5329 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005330#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5331
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005332 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005333}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005334#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005335
5336#endif /* POLARSSL_SSL_TLS_C */