blob: f16bb53103fc6bd6f966095e0410e90d9c2c52e6 [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 {
952 if( end - p < 2 + (int) ssl->psk_len )
953 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
954
955 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
956 *(p++) = (unsigned char)( ssl->psk_len );
957 p += ssl->psk_len;
958 }
959 else
960#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200961#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
962 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
963 {
964 /*
965 * other_secret already set by the ClientKeyExchange message,
966 * and is 48 bytes long
967 */
968 *p++ = 0;
969 *p++ = 48;
970 p += 48;
971 }
972 else
973#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200974#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
975 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
976 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200977 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200978 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200979
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200980 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200981 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200982 p + 2, &len,
983 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200984 {
985 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
986 return( ret );
987 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200988 *(p++) = (unsigned char)( len >> 8 );
989 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200990 p += len;
991
992 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
993 }
994 else
995#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
996#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
997 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
998 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200999 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001000 size_t zlen;
1001
1002 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001003 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 ssl->f_rng, ssl->p_rng ) ) != 0 )
1005 {
1006 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1007 return( ret );
1008 }
1009
1010 *(p++) = (unsigned char)( zlen >> 8 );
1011 *(p++) = (unsigned char)( zlen );
1012 p += zlen;
1013
1014 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1015 }
1016 else
1017#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1018 {
1019 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001020 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001021 }
1022
1023 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001024 if( end - p < 2 + (int) ssl->psk_len )
1025 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1026
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001027 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1028 *(p++) = (unsigned char)( ssl->psk_len );
1029 memcpy( p, ssl->psk, ssl->psk_len );
1030 p += ssl->psk_len;
1031
1032 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1033
1034 return( 0 );
1035}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001036#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001037
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001038#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001039/*
1040 * SSLv3.0 MAC functions
1041 */
Paul Bakker68884e32013-01-07 18:20:04 +01001042static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1043 unsigned char *buf, size_t len,
1044 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001045{
1046 unsigned char header[11];
1047 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001048 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001049 int md_size = md_get_size( md_ctx->md_info );
1050 int md_type = md_get_type( md_ctx->md_info );
1051
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001052 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001053 if( md_type == POLARSSL_MD_MD5 )
1054 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001055 else
Paul Bakker68884e32013-01-07 18:20:04 +01001056 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
1058 memcpy( header, ctr, 8 );
1059 header[ 8] = (unsigned char) type;
1060 header[ 9] = (unsigned char)( len >> 8 );
1061 header[10] = (unsigned char)( len );
1062
Paul Bakker68884e32013-01-07 18:20:04 +01001063 memset( padding, 0x36, padlen );
1064 md_starts( md_ctx );
1065 md_update( md_ctx, secret, md_size );
1066 md_update( md_ctx, padding, padlen );
1067 md_update( md_ctx, header, 11 );
1068 md_update( md_ctx, buf, len );
1069 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Paul Bakker68884e32013-01-07 18:20:04 +01001071 memset( padding, 0x5C, padlen );
1072 md_starts( md_ctx );
1073 md_update( md_ctx, secret, md_size );
1074 md_update( md_ctx, padding, padlen );
1075 md_update( md_ctx, buf + len, md_size );
1076 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001077}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001078#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001079
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001080#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1081 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1082 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1083#define POLARSSL_SOME_MODES_USE_MAC
1084#endif
1085
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001086/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001088 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001089static int ssl_encrypt_buf( ssl_context *ssl )
1090{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001091 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001092 cipher_mode_t mode;
1093 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
1095 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1096
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001097 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1098 {
1099 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1100 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1101 }
1102
1103 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1104
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001105 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1106 ssl->out_msg, ssl->out_msglen );
1107
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001109 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001111#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001112 if( mode == POLARSSL_MODE_STREAM ||
1113 ( mode == POLARSSL_MODE_CBC
1114#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1115 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1116#endif
1117 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001119#if defined(POLARSSL_SSL_PROTO_SSL3)
1120 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1121 {
1122 ssl_mac( &ssl->transform_out->md_ctx_enc,
1123 ssl->transform_out->mac_enc,
1124 ssl->out_msg, ssl->out_msglen,
1125 ssl->out_ctr, ssl->out_msgtype );
1126 }
1127 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001128#endif
1129#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001130 defined(POLARSSL_SSL_PROTO_TLS1_2)
1131 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1132 {
1133 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1134 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1135 ssl->out_msg, ssl->out_msglen );
1136 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1137 ssl->out_msg + ssl->out_msglen );
1138 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1139 }
1140 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001141#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001142 {
1143 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001144 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001145 }
1146
1147 SSL_DEBUG_BUF( 4, "computed mac",
1148 ssl->out_msg + ssl->out_msglen,
1149 ssl->transform_out->maclen );
1150
1151 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001152 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001153 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001154#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001156 /*
1157 * Encrypt
1158 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001159#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001160 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001162 int ret;
1163 size_t olen = 0;
1164
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1166 "including %d bytes of padding",
1167 ssl->out_msglen, 0 ) );
1168
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001169 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001170 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001171 ssl->transform_out->ivlen,
1172 ssl->out_msg, ssl->out_msglen,
1173 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001174 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001175 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001176 return( ret );
1177 }
1178
1179 if( ssl->out_msglen != olen )
1180 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001181 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001182 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 }
Paul Bakker68884e32013-01-07 18:20:04 +01001185 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001186#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001187#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1188 if( mode == POLARSSL_MODE_GCM ||
1189 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001190 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001191 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001192 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001193 unsigned char *enc_msg;
1194 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001195 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1196 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001197
Paul Bakkerca4ab492012-04-18 14:23:57 +00001198 memcpy( add_data, ssl->out_ctr, 8 );
1199 add_data[8] = ssl->out_msgtype;
1200 add_data[9] = ssl->major_ver;
1201 add_data[10] = ssl->minor_ver;
1202 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1203 add_data[12] = ssl->out_msglen & 0xFF;
1204
1205 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1206 add_data, 13 );
1207
Paul Bakker68884e32013-01-07 18:20:04 +01001208 /*
1209 * Generate IV
1210 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001211#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001212 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001213 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1214 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001215 if( ret != 0 )
1216 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001217
Paul Bakker68884e32013-01-07 18:20:04 +01001218 memcpy( ssl->out_iv,
1219 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1220 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001221#else
1222 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1223 {
1224 /* Reminder if we ever add an AEAD mode with a different size */
1225 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1226 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1227 }
1228
1229 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1230 ssl->out_ctr, 8 );
1231 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1232#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001233
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001234 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001235 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001236
Paul Bakker68884e32013-01-07 18:20:04 +01001237 /*
1238 * Fix pointer positions and message length with added IV
1239 */
1240 enc_msg = ssl->out_msg;
1241 enc_msglen = ssl->out_msglen;
1242 ssl->out_msglen += ssl->transform_out->ivlen -
1243 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001244
Paul Bakker68884e32013-01-07 18:20:04 +01001245 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1246 "including %d bytes of padding",
1247 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001248
Paul Bakker68884e32013-01-07 18:20:04 +01001249 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001250 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001251 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001252 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1253 ssl->transform_out->iv_enc,
1254 ssl->transform_out->ivlen,
1255 add_data, 13,
1256 enc_msg, enc_msglen,
1257 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001258 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001259 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001260 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001261 return( ret );
1262 }
1263
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001264 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001265 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001266 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001267 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001268 }
1269
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001270 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001271 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001272
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001273 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001274 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001276#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001277#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1278 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001279 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001281 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001282 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001283 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001284
Paul Bakker48916f92012-09-16 19:57:18 +00001285 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1286 ssl->transform_out->ivlen;
1287 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 padlen = 0;
1289
1290 for( i = 0; i <= padlen; i++ )
1291 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1292
1293 ssl->out_msglen += padlen + 1;
1294
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001295 enc_msglen = ssl->out_msglen;
1296 enc_msg = ssl->out_msg;
1297
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001298#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001299 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001300 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1301 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001302 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001303 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001304 {
1305 /*
1306 * Generate IV
1307 */
Manuel Pégourié-Gonnarda67fd792015-08-27 12:02:40 +02001308 ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001309 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001310 if( ret != 0 )
1311 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001312
Paul Bakker92be97b2013-01-02 17:30:03 +01001313 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001314 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001315
1316 /*
1317 * Fix pointer positions and message length with added IV
1318 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001319 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001320 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001321 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001322 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001323#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001324
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001326 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001327 ssl->out_msglen, ssl->transform_out->ivlen,
1328 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001330 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001331 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001332 ssl->transform_out->ivlen,
1333 enc_msg, enc_msglen,
1334 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001335 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001336 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001337 return( ret );
1338 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001339
Paul Bakkercca5b812013-08-31 17:40:26 +02001340 if( enc_msglen != olen )
1341 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001342 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001343 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001344 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001345
1346#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001347 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1348 {
1349 /*
1350 * Save IV in SSL3 and TLS1
1351 */
1352 memcpy( ssl->transform_out->iv_enc,
1353 ssl->transform_out->cipher_ctx_enc.iv,
1354 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001356#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001357
1358#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001359 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001360 {
1361 /*
1362 * MAC(MAC_write_key, seq_num +
1363 * TLSCipherText.type +
1364 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001365 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001366 * IV + // except for TLS 1.0
1367 * ENC(content + padding + padding_length));
1368 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001369 unsigned char pseudo_hdr[13];
1370
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001371 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1372
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001373 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1374 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001375 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1376 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1377
1378 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001379
1380 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1381 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1382 ssl->out_iv, ssl->out_msglen );
1383 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1384 ssl->out_iv + ssl->out_msglen );
1385 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1386
1387 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001388 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001389 }
1390#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001392 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001393#endif /* POLARSSL_CIPHER_MODE_CBC &&
1394 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001395 {
1396 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001397 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001400 /* Make extra sure authentication was performed, exactly once */
1401 if( auth_done != 1 )
1402 {
1403 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1404 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1405 }
1406
Paul Bakkerca4ab492012-04-18 14:23:57 +00001407 for( i = 8; i > 0; i-- )
1408 if( ++ssl->out_ctr[i - 1] != 0 )
1409 break;
1410
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001411 /* The loops goes to its end iff the counter is wrapping */
1412 if( i == 0 )
1413 {
1414 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1415 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1416 }
1417
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1419
1420 return( 0 );
1421}
1422
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001423#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001424
Paul Bakker5121ce52009-01-03 21:22:43 +00001425static int ssl_decrypt_buf( ssl_context *ssl )
1426{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001427 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001428 cipher_mode_t mode;
1429 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001430#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001431 size_t padlen = 0, correct = 1;
1432#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
1434 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1435
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001436 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1437 {
1438 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1439 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1440 }
1441
1442 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1443
Paul Bakker48916f92012-09-16 19:57:18 +00001444 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 {
1446 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001447 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001448 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 }
1450
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001451#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001452 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001453 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001454 int ret;
1455 size_t olen = 0;
1456
Paul Bakker68884e32013-01-07 18:20:04 +01001457 padlen = 0;
1458
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001459 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001460 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001461 ssl->transform_in->ivlen,
1462 ssl->in_msg, ssl->in_msglen,
1463 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001464 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001465 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001466 return( ret );
1467 }
1468
1469 if( ssl->in_msglen != olen )
1470 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001471 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001472 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 }
Paul Bakker68884e32013-01-07 18:20:04 +01001475 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001476#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001477#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1478 if( mode == POLARSSL_MODE_GCM ||
1479 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001480 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001481 int ret;
1482 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001483 unsigned char *dec_msg;
1484 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001485 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001486 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1487 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001488 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1489 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001490
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001491 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001492 {
1493 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1494 "+ taglen (%d)", ssl->in_msglen,
1495 explicit_iv_len, taglen ) );
1496 return( POLARSSL_ERR_SSL_INVALID_MAC );
1497 }
1498 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1499
Paul Bakker68884e32013-01-07 18:20:04 +01001500 dec_msg = ssl->in_msg;
1501 dec_msg_result = ssl->in_msg;
1502 ssl->in_msglen = dec_msglen;
1503
1504 memcpy( add_data, ssl->in_ctr, 8 );
1505 add_data[8] = ssl->in_msgtype;
1506 add_data[9] = ssl->major_ver;
1507 add_data[10] = ssl->minor_ver;
1508 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1509 add_data[12] = ssl->in_msglen & 0xFF;
1510
1511 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1512 add_data, 13 );
1513
1514 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1515 ssl->in_iv,
1516 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1517
1518 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1519 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001520 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001521
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001522 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001523 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001524 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001525 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1526 ssl->transform_in->iv_dec,
1527 ssl->transform_in->ivlen,
1528 add_data, 13,
1529 dec_msg, dec_msglen,
1530 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001531 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001532 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001533 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1534
1535 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1536 return( POLARSSL_ERR_SSL_INVALID_MAC );
1537
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001538 return( ret );
1539 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001540 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001542 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001543 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001544 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001545 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001546 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001547 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001549#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001550#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1551 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001552 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001553 {
Paul Bakker45829992013-01-03 14:52:21 +01001554 /*
1555 * Decrypt and check the padding
1556 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001557 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001558 unsigned char *dec_msg;
1559 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001560 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001561 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001562 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001563
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 /*
Paul Bakker45829992013-01-03 14:52:21 +01001565 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001567#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001568 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1569 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001570#endif
Paul Bakker45829992013-01-03 14:52:21 +01001571
1572 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1573 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1574 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001575 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1576 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1577 ssl->transform_in->ivlen,
1578 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001579 return( POLARSSL_ERR_SSL_INVALID_MAC );
1580 }
1581
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001582 dec_msglen = ssl->in_msglen;
1583 dec_msg = ssl->in_msg;
1584 dec_msg_result = ssl->in_msg;
1585
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001586 /*
1587 * Authenticate before decrypt if enabled
1588 */
1589#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001590 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001591 {
1592 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001593 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001594
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001595 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1596
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001597 dec_msglen -= ssl->transform_in->maclen;
1598 ssl->in_msglen -= ssl->transform_in->maclen;
1599
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001600 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1601 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1602 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1603 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1604
1605 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1606
1607 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001608 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1609 ssl->in_iv, ssl->in_msglen );
1610 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1611 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1612
1613 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1614 ssl->transform_in->maclen );
1615 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1616 ssl->transform_in->maclen );
1617
1618 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1619 ssl->transform_in->maclen ) != 0 )
1620 {
1621 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1622
1623 return( POLARSSL_ERR_SSL_INVALID_MAC );
1624 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001625 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001626 }
1627#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1628
1629 /*
1630 * Check length sanity
1631 */
1632 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1633 {
1634 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1635 ssl->in_msglen, ssl->transform_in->ivlen ) );
1636 return( POLARSSL_ERR_SSL_INVALID_MAC );
1637 }
1638
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001639#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001640 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001641 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001642 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001643 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001644 {
Paul Bakker48916f92012-09-16 19:57:18 +00001645 dec_msglen -= ssl->transform_in->ivlen;
1646 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001647
Paul Bakker48916f92012-09-16 19:57:18 +00001648 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001649 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001650 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001651#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001652
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001653 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001654 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001655 ssl->transform_in->ivlen,
1656 dec_msg, dec_msglen,
1657 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001658 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001659 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001660 return( ret );
1661 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001662
Paul Bakkercca5b812013-08-31 17:40:26 +02001663 if( dec_msglen != olen )
1664 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001665 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001666 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001667 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001668
1669#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001670 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1671 {
1672 /*
1673 * Save IV in SSL3 and TLS1
1674 */
1675 memcpy( ssl->transform_in->iv_dec,
1676 ssl->transform_in->cipher_ctx_dec.iv,
1677 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001679#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
1681 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001682
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001683 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001684 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001685 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001686#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001687 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1688 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001689#endif
Paul Bakker45829992013-01-03 14:52:21 +01001690 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001691 correct = 0;
1692 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001693
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001694#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001695 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1696 {
Paul Bakker48916f92012-09-16 19:57:18 +00001697 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001698 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001699#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1701 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001702 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001703#endif
Paul Bakker45829992013-01-03 14:52:21 +01001704 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 }
1706 }
1707 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001708#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001709#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1710 defined(POLARSSL_SSL_PROTO_TLS1_2)
1711 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001712 {
1713 /*
Paul Bakker45829992013-01-03 14:52:21 +01001714 * TLSv1+: always check the padding up to the first failure
1715 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001716 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001717 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001718 size_t padding_idx = ssl->in_msglen - padlen - 1;
1719
Paul Bakker956c9e02013-12-19 14:42:28 +01001720 /*
1721 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001722 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001723 *
Paul Bakker61885c72014-04-25 12:59:03 +02001724 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1725 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001726 *
1727 * In both cases we reset padding_idx to a safe value (0) to
1728 * prevent out-of-buffer reads.
1729 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001730 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001731 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1732 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001733
1734 padding_idx *= correct;
1735
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001736 for( i = 1; i <= 256; i++ )
1737 {
1738 real_count &= ( i <= padlen );
1739 pad_count += real_count *
1740 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1741 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001742
1743 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001744
Paul Bakkerd66f0702013-01-31 16:57:45 +01001745#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001746 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001747 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001748#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001749 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001750 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001751 else
1752#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1753 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001754 {
1755 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001756 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001757 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001758
1759 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001760 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001761 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001762#endif /* POLARSSL_CIPHER_MODE_CBC &&
1763 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001764 {
1765 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001766 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001767 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
1769 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1770 ssl->in_msg, ssl->in_msglen );
1771
1772 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001773 * Authenticate if not done yet.
1774 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001775 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001776#if defined(POLARSSL_SOME_MODES_USE_MAC)
1777 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001778 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001779 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1780
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001781 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001783 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1784 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001786 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001788#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001789 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1790 {
1791 ssl_mac( &ssl->transform_in->md_ctx_dec,
1792 ssl->transform_in->mac_dec,
1793 ssl->in_msg, ssl->in_msglen,
1794 ssl->in_ctr, ssl->in_msgtype );
1795 }
1796 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001797#endif /* POLARSSL_SSL_PROTO_SSL3 */
1798#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001799 defined(POLARSSL_SSL_PROTO_TLS1_2)
1800 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1801 {
1802 /*
1803 * Process MAC and always update for padlen afterwards to make
1804 * total time independent of padlen
1805 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001806 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001807 *
1808 * Known timing attacks:
1809 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1810 *
1811 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1812 * correctly. (We round down instead of up, so -56 is the correct
1813 * value for our calculations instead of -55)
1814 */
1815 size_t j, extra_run = 0;
1816 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1817 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001818
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001819 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001820
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001821 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1822 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1823 ssl->in_msglen );
1824 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1825 ssl->in_msg + ssl->in_msglen );
Manuel Pégourié-Gonnard7d1e95c2015-04-29 01:35:48 +02001826 /* Call md_process at least once due to cache attacks */
1827 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001828 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001829
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1831 }
1832 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001833#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001834 POLARSSL_SSL_PROTO_TLS1_2 */
1835 {
1836 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001837 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001840 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1841 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1842 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001844 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001845 ssl->transform_in->maclen ) != 0 )
1846 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001847#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001848 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001849#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001850 correct = 0;
1851 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001852 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001854 /*
1855 * Finally check the correct flag
1856 */
1857 if( correct == 0 )
1858 return( POLARSSL_ERR_SSL_INVALID_MAC );
1859 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001860#endif /* POLARSSL_SOME_MODES_USE_MAC */
1861
1862 /* Make extra sure authentication was performed, exactly once */
1863 if( auth_done != 1 )
1864 {
1865 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1866 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1867 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001868
1869 if( ssl->in_msglen == 0 )
1870 {
1871 ssl->nb_zero++;
1872
1873 /*
1874 * Three or more empty messages may be a DoS attack
1875 * (excessive CPU consumption).
1876 */
1877 if( ssl->nb_zero > 3 )
1878 {
1879 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1880 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001881 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 }
1883 }
1884 else
1885 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001886
Paul Bakker23986e52011-04-24 08:57:21 +00001887 for( i = 8; i > 0; i-- )
1888 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 break;
1890
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001891 /* The loops goes to its end iff the counter is wrapping */
1892 if( i == 0 )
1893 {
1894 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1895 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1896 }
1897
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1899
1900 return( 0 );
1901}
1902
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001903#undef MAC_NONE
1904#undef MAC_PLAINTEXT
1905#undef MAC_CIPHERTEXT
1906
Paul Bakker2770fbd2012-07-03 13:30:23 +00001907#if defined(POLARSSL_ZLIB_SUPPORT)
1908/*
1909 * Compression/decompression functions
1910 */
1911static int ssl_compress_buf( ssl_context *ssl )
1912{
1913 int ret;
1914 unsigned char *msg_post = ssl->out_msg;
1915 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001916 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001917
1918 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1919
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001920 if( len_pre == 0 )
1921 return( 0 );
1922
Paul Bakker2770fbd2012-07-03 13:30:23 +00001923 memcpy( msg_pre, ssl->out_msg, len_pre );
1924
1925 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1926 ssl->out_msglen ) );
1927
1928 SSL_DEBUG_BUF( 4, "before compression: output payload",
1929 ssl->out_msg, ssl->out_msglen );
1930
Paul Bakker48916f92012-09-16 19:57:18 +00001931 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1932 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1933 ssl->transform_out->ctx_deflate.next_out = msg_post;
1934 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001935
Paul Bakker48916f92012-09-16 19:57:18 +00001936 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001937 if( ret != Z_OK )
1938 {
1939 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1940 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1941 }
1942
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001943 ssl->out_msglen = SSL_BUFFER_LEN -
1944 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001945
Paul Bakker2770fbd2012-07-03 13:30:23 +00001946 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1947 ssl->out_msglen ) );
1948
1949 SSL_DEBUG_BUF( 4, "after compression: output payload",
1950 ssl->out_msg, ssl->out_msglen );
1951
1952 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1953
1954 return( 0 );
1955}
1956
1957static int ssl_decompress_buf( ssl_context *ssl )
1958{
1959 int ret;
1960 unsigned char *msg_post = ssl->in_msg;
1961 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001962 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001963
1964 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1965
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001966 if( len_pre == 0 )
1967 return( 0 );
1968
Paul Bakker2770fbd2012-07-03 13:30:23 +00001969 memcpy( msg_pre, ssl->in_msg, len_pre );
1970
1971 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1972 ssl->in_msglen ) );
1973
1974 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1975 ssl->in_msg, ssl->in_msglen );
1976
Paul Bakker48916f92012-09-16 19:57:18 +00001977 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1978 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1979 ssl->transform_in->ctx_inflate.next_out = msg_post;
1980 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001981
Paul Bakker48916f92012-09-16 19:57:18 +00001982 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001983 if( ret != Z_OK )
1984 {
1985 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1986 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1987 }
1988
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001989 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1990 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001991
Paul Bakker2770fbd2012-07-03 13:30:23 +00001992 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1993 ssl->in_msglen ) );
1994
1995 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1996 ssl->in_msg, ssl->in_msglen );
1997
1998 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1999
2000 return( 0 );
2001}
2002#endif /* POLARSSL_ZLIB_SUPPORT */
2003
Paul Bakker5121ce52009-01-03 21:22:43 +00002004/*
2005 * Fill the input message buffer
2006 */
Paul Bakker23986e52011-04-24 08:57:21 +00002007int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002008{
Paul Bakker23986e52011-04-24 08:57:21 +00002009 int ret;
2010 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002011
2012 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2013
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002014 if( nb_want > SSL_BUFFER_LEN - 8 )
2015 {
2016 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2017 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2018 }
2019
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 while( ssl->in_left < nb_want )
2021 {
2022 len = nb_want - ssl->in_left;
2023 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2024
2025 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2026 ssl->in_left, nb_want ) );
2027 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2028
Paul Bakker831a7552011-05-18 13:32:51 +00002029 if( ret == 0 )
2030 return( POLARSSL_ERR_SSL_CONN_EOF );
2031
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 if( ret < 0 )
2033 return( ret );
2034
2035 ssl->in_left += ret;
2036 }
2037
2038 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2039
2040 return( 0 );
2041}
2042
2043/*
2044 * Flush any data not yet written
2045 */
2046int ssl_flush_output( ssl_context *ssl )
2047{
2048 int ret;
2049 unsigned char *buf;
2050
2051 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2052
2053 while( ssl->out_left > 0 )
2054 {
2055 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2056 5 + ssl->out_msglen, ssl->out_left ) );
2057
Paul Bakker5bd42292012-12-19 14:40:42 +01002058 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002060
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2062
2063 if( ret <= 0 )
2064 return( ret );
2065
2066 ssl->out_left -= ret;
2067 }
2068
2069 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2070
2071 return( 0 );
2072}
2073
2074/*
2075 * Record layer functions
2076 */
2077int ssl_write_record( ssl_context *ssl )
2078{
Paul Bakker05ef8352012-05-08 09:17:57 +00002079 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002080 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
2082 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2083
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2085 {
2086 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2087 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2088 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2089
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002090 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2091 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093
Paul Bakker2770fbd2012-07-03 13:30:23 +00002094#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002095 if( ssl->transform_out != NULL &&
2096 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002097 {
2098 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2099 {
2100 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2101 return( ret );
2102 }
2103
2104 len = ssl->out_msglen;
2105 }
2106#endif /*POLARSSL_ZLIB_SUPPORT */
2107
Paul Bakker05ef8352012-05-08 09:17:57 +00002108#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002109 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002111 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002112
Paul Bakker05ef8352012-05-08 09:17:57 +00002113 ret = ssl_hw_record_write( ssl );
2114 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2115 {
2116 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002117 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002118 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002119
2120 if( ret == 0 )
2121 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002122 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002123#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002124 if( !done )
2125 {
2126 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2127 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2128 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2130 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002131
Paul Bakker48916f92012-09-16 19:57:18 +00002132 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002133 {
2134 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2135 {
2136 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2137 return( ret );
2138 }
2139
2140 len = ssl->out_msglen;
2141 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2142 ssl->out_hdr[4] = (unsigned char)( len );
2143 }
2144
2145 ssl->out_left = 5 + ssl->out_msglen;
2146
2147 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2148 "version = [%d:%d], msglen = %d",
2149 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2150 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2151
2152 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002153 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 }
2155
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2157 {
2158 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2159 return( ret );
2160 }
2161
2162 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2163
2164 return( 0 );
2165}
2166
2167int ssl_read_record( ssl_context *ssl )
2168{
Paul Bakker05ef8352012-05-08 09:17:57 +00002169 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
2171 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2172
2173 if( ssl->in_hslen != 0 &&
2174 ssl->in_hslen < ssl->in_msglen )
2175 {
2176 /*
2177 * Get next Handshake message in the current record
2178 */
2179 ssl->in_msglen -= ssl->in_hslen;
2180
Paul Bakker8934a982011-08-05 11:11:53 +00002181 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002182 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
2184 ssl->in_hslen = 4;
2185 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2186
2187 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2188 " %d, type = %d, hslen = %d",
2189 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2190
2191 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2192 {
2193 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002194 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 }
2196
2197 if( ssl->in_msglen < ssl->in_hslen )
2198 {
2199 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002200 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 }
2202
Paul Bakker4224bc02014-04-08 14:36:50 +02002203 if( ssl->state != SSL_HANDSHAKE_OVER )
2204 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
2206 return( 0 );
2207 }
2208
2209 ssl->in_hslen = 0;
2210
2211 /*
2212 * Read the record header and validate it
2213 */
2214 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2215 {
2216 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2217 return( ret );
2218 }
2219
2220 ssl->in_msgtype = ssl->in_hdr[0];
2221 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2222
2223 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2224 "version = [%d:%d], msglen = %d",
2225 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2226 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2227
2228 if( ssl->in_hdr[1] != ssl->major_ver )
2229 {
2230 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002231 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 }
2233
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002234 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 {
2236 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002237 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 }
2239
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002240 /* Sanity check (outer boundaries) */
2241 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2242 {
2243 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2244 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2245 }
2246
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002248 * Make sure the message length is acceptable for the current transform
2249 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 */
Paul Bakker48916f92012-09-16 19:57:18 +00002251 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002253 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 {
2255 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002256 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 }
2258 }
2259 else
2260 {
Paul Bakker48916f92012-09-16 19:57:18 +00002261 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 {
2263 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002264 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 }
2266
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002267#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002269 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 {
2271 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002272 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002274#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002275
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002276#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2277 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 /*
2279 * TLS encrypted messages can have up to 256 bytes of padding
2280 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002281 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002282 ssl->in_msglen > ssl->transform_in->minlen +
2283 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 {
2285 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002286 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002288#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 }
2290
2291 /*
2292 * Read and optionally decrypt the message contents
2293 */
2294 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2295 {
2296 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2297 return( ret );
2298 }
2299
2300 SSL_DEBUG_BUF( 4, "input record from network",
2301 ssl->in_hdr, 5 + ssl->in_msglen );
2302
Paul Bakker05ef8352012-05-08 09:17:57 +00002303#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002304 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002305 {
2306 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2307
2308 ret = ssl_hw_record_read( ssl );
2309 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2310 {
2311 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002312 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002313 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002314
2315 if( ret == 0 )
2316 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002317 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002318#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002319 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002320 {
2321 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2322 {
Paul Bakker40865c82013-01-31 17:13:13 +01002323#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2324 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2325 {
2326 ssl_send_alert_message( ssl,
2327 SSL_ALERT_LEVEL_FATAL,
2328 SSL_ALERT_MSG_BAD_RECORD_MAC );
2329 }
2330#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002331 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2332 return( ret );
2333 }
2334
2335 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2336 ssl->in_msg, ssl->in_msglen );
2337
2338 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2339 {
2340 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002341 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 }
2343 }
2344
Paul Bakker2770fbd2012-07-03 13:30:23 +00002345#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002346 if( ssl->transform_in != NULL &&
2347 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348 {
2349 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2350 {
2351 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2352 return( ret );
2353 }
2354
2355 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2356 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2357 }
2358#endif /* POLARSSL_ZLIB_SUPPORT */
2359
Paul Bakker0a925182012-04-16 06:46:41 +00002360 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2361 ssl->in_msgtype != SSL_MSG_ALERT &&
2362 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2363 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2364 {
2365 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2366
Paul Bakker48916f92012-09-16 19:57:18 +00002367 if( ( ret = ssl_send_alert_message( ssl,
2368 SSL_ALERT_LEVEL_FATAL,
2369 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002370 {
2371 return( ret );
2372 }
2373
2374 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2375 }
2376
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2378 {
2379 ssl->in_hslen = 4;
2380 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2381
2382 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2383 " %d, type = %d, hslen = %d",
2384 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2385
2386 /*
2387 * Additional checks to validate the handshake header
2388 */
2389 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2390 {
2391 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002392 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 }
2394
2395 if( ssl->in_msglen < ssl->in_hslen )
2396 {
2397 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002398 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 }
2400
Paul Bakker48916f92012-09-16 19:57:18 +00002401 if( ssl->state != SSL_HANDSHAKE_OVER )
2402 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 }
2404
2405 if( ssl->in_msgtype == SSL_MSG_ALERT )
2406 {
2407 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2408 ssl->in_msg[0], ssl->in_msg[1] ) );
2409
2410 /*
2411 * Ignore non-fatal alerts, except close_notify
2412 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002413 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002415 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2416 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002417 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 }
2419
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002420 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2421 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 {
2423 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002424 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 }
2426 }
2427
2428 ssl->in_left = 0;
2429
2430 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2431
2432 return( 0 );
2433}
2434
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002435int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2436{
2437 int ret;
2438
2439 if( ( ret = ssl_send_alert_message( ssl,
2440 SSL_ALERT_LEVEL_FATAL,
2441 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2442 {
2443 return( ret );
2444 }
2445
2446 return( 0 );
2447}
2448
Paul Bakker0a925182012-04-16 06:46:41 +00002449int ssl_send_alert_message( ssl_context *ssl,
2450 unsigned char level,
2451 unsigned char message )
2452{
2453 int ret;
2454
2455 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2456
2457 ssl->out_msgtype = SSL_MSG_ALERT;
2458 ssl->out_msglen = 2;
2459 ssl->out_msg[0] = level;
2460 ssl->out_msg[1] = message;
2461
2462 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2463 {
2464 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2465 return( ret );
2466 }
2467
2468 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2469
2470 return( 0 );
2471}
2472
Paul Bakker5121ce52009-01-03 21:22:43 +00002473/*
2474 * Handshake functions
2475 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002476#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2477 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2478 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2479 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2480 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2481 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2482 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002483int ssl_write_certificate( ssl_context *ssl )
2484{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002485 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
2487 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2488
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002489 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002490 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2491 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002492 {
2493 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2494 ssl->state++;
2495 return( 0 );
2496 }
2497
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002498 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2499 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002500}
2501
2502int ssl_parse_certificate( ssl_context *ssl )
2503{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002504 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2505
2506 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2507
2508 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002509 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2510 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002511 {
2512 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2513 ssl->state++;
2514 return( 0 );
2515 }
2516
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002517 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2518 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002519}
2520#else
2521int ssl_write_certificate( ssl_context *ssl )
2522{
2523 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2524 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002525 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002526 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2527
2528 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2529
2530 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002531 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2532 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002533 {
2534 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2535 ssl->state++;
2536 return( 0 );
2537 }
2538
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002539#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 if( ssl->endpoint == SSL_IS_CLIENT )
2541 {
2542 if( ssl->client_auth == 0 )
2543 {
2544 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2545 ssl->state++;
2546 return( 0 );
2547 }
2548
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002549#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 /*
2551 * If using SSLv3 and got no cert, send an Alert message
2552 * (otherwise an empty Certificate message will be sent).
2553 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002554 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2556 {
2557 ssl->out_msglen = 2;
2558 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002559 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2560 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
2562 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2563 goto write_msg;
2564 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002565#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002567#endif /* POLARSSL_SSL_CLI_C */
2568#if defined(POLARSSL_SSL_SRV_C)
2569 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002571 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 {
2573 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002574 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
2576 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002577#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002579 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
2581 /*
2582 * 0 . 0 handshake type
2583 * 1 . 3 handshake length
2584 * 4 . 6 length of all certs
2585 * 7 . 9 length of cert. 1
2586 * 10 . n-1 peer certificate
2587 * n . n+2 length of cert. 2
2588 * n+3 . ... upper level cert, etc.
2589 */
2590 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002591 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592
Paul Bakker29087132010-03-21 21:03:34 +00002593 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 {
2595 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002596 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
2598 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2599 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002600 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 }
2602
2603 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2604 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2605 ssl->out_msg[i + 2] = (unsigned char)( n );
2606
2607 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2608 i += n; crt = crt->next;
2609 }
2610
2611 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2612 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2613 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2614
2615 ssl->out_msglen = i;
2616 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2617 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2618
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002619#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002620write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002621#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
2623 ssl->state++;
2624
2625 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2626 {
2627 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2628 return( ret );
2629 }
2630
2631 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2632
Paul Bakkered27a042013-04-18 22:46:23 +02002633 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634}
2635
2636int ssl_parse_certificate( ssl_context *ssl )
2637{
Paul Bakkered27a042013-04-18 22:46:23 +02002638 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002639 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002640 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
2642 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2643
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002644 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002645 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2646 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002647 {
2648 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2649 ssl->state++;
2650 return( 0 );
2651 }
2652
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002653#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002655 ( ssl->authmode == SSL_VERIFY_NONE ||
2656 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002658 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2660 ssl->state++;
2661 return( 0 );
2662 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002663#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
2665 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2666 {
2667 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2668 return( ret );
2669 }
2670
2671 ssl->state++;
2672
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002673#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002674#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 /*
2676 * Check if the client sent an empty certificate
2677 */
2678 if( ssl->endpoint == SSL_IS_SERVER &&
2679 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2680 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002681 if( ssl->in_msglen == 2 &&
2682 ssl->in_msgtype == SSL_MSG_ALERT &&
2683 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2684 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 {
2686 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2687
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002688 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2690 return( 0 );
2691 else
Paul Bakker40e46942009-01-03 21:51:57 +00002692 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 }
2694 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002695#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002697#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2698 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 if( ssl->endpoint == SSL_IS_SERVER &&
2700 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2701 {
2702 if( ssl->in_hslen == 7 &&
2703 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2704 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2705 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2706 {
2707 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2708
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002709 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002711 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 else
2713 return( 0 );
2714 }
2715 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002716#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2717 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002718#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
2720 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2721 {
2722 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002723 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 }
2725
2726 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2727 {
2728 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002729 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 }
2731
2732 /*
2733 * Same message structure as in ssl_write_certificate()
2734 */
2735 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2736
2737 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2738 {
2739 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002740 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 }
2742
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002743 /* In case we tried to reuse a session but it failed */
2744 if( ssl->session_negotiate->peer_cert != NULL )
2745 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002746 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002747 polarssl_free( ssl->session_negotiate->peer_cert );
2748 }
2749
Mansour Moufidc531b4a2015-02-15 17:35:38 -05002750 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002751 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 {
2753 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002754 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002755 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 }
2757
Paul Bakkerb6b09562013-09-18 14:17:41 +02002758 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
2760 i = 7;
2761
2762 while( i < ssl->in_hslen )
2763 {
2764 if( ssl->in_msg[i] != 0 )
2765 {
2766 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002767 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 }
2769
2770 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2771 | (unsigned int) ssl->in_msg[i + 2];
2772 i += 3;
2773
2774 if( n < 128 || i + n > ssl->in_hslen )
2775 {
2776 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002777 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 }
2779
Paul Bakkerddf26b42013-09-18 13:46:23 +02002780 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2781 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 if( ret != 0 )
2783 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002784 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 return( ret );
2786 }
2787
2788 i += n;
2789 }
2790
Paul Bakker48916f92012-09-16 19:57:18 +00002791 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002793 /*
2794 * On client, make sure the server cert doesn't change during renego to
2795 * avoid "triple handshake" attack: https://secure-resumption.com/
2796 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002797#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002798 if( ssl->endpoint == SSL_IS_CLIENT &&
2799 ssl->renegotiation == SSL_RENEGOTIATION )
2800 {
2801 if( ssl->session->peer_cert == NULL )
2802 {
2803 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2804 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2805 }
2806
2807 if( ssl->session->peer_cert->raw.len !=
2808 ssl->session_negotiate->peer_cert->raw.len ||
2809 memcmp( ssl->session->peer_cert->raw.p,
2810 ssl->session_negotiate->peer_cert->raw.p,
2811 ssl->session->peer_cert->raw.len ) != 0 )
2812 {
2813 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2814 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2815 }
2816 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002817#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002818
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 if( ssl->authmode != SSL_VERIFY_NONE )
2820 {
2821 if( ssl->ca_chain == NULL )
2822 {
2823 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002824 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 }
2826
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002827 /*
2828 * Main check: verify certificate
2829 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002830 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2831 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2832 &ssl->session_negotiate->verify_result,
2833 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
2835 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002836 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002837 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002838 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002839
2840 /*
2841 * Secondary checks: always done, but change 'ret' only if it was 0
2842 */
2843
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002844#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002845 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002846 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002847
2848 /* If certificate uses an EC key, make sure the curve is OK */
2849 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2850 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2851 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002852 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002853 if( ret == 0 )
2854 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002855 }
2856 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002857#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002859 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2860 ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02002861 ! ssl->endpoint,
2862 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002863 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002864 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002865 if( ret == 0 )
2866 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2867 }
2868
Paul Bakker5121ce52009-01-03 21:22:43 +00002869 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2870 ret = 0;
2871 }
2872
2873 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2874
2875 return( ret );
2876}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002877#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2878 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2879 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2880 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2881 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2882 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2883 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
2885int ssl_write_change_cipher_spec( ssl_context *ssl )
2886{
2887 int ret;
2888
2889 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2890
2891 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2892 ssl->out_msglen = 1;
2893 ssl->out_msg[0] = 1;
2894
Paul Bakker5121ce52009-01-03 21:22:43 +00002895 ssl->state++;
2896
2897 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2898 {
2899 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2900 return( ret );
2901 }
2902
2903 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2904
2905 return( 0 );
2906}
2907
2908int ssl_parse_change_cipher_spec( ssl_context *ssl )
2909{
2910 int ret;
2911
2912 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2913
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2915 {
2916 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2917 return( ret );
2918 }
2919
2920 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2921 {
2922 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002923 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002924 }
2925
2926 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2927 {
2928 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002929 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 }
2931
2932 ssl->state++;
2933
2934 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2935
2936 return( 0 );
2937}
2938
Paul Bakker41c83d32013-03-20 14:39:14 +01002939void ssl_optimize_checksum( ssl_context *ssl,
2940 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002941{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002942 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002943
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002944#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2945 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002946 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002947 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002948 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002949#endif
2950#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2951#if defined(POLARSSL_SHA512_C)
2952 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2953 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2954 else
2955#endif
2956#if defined(POLARSSL_SHA256_C)
2957 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002959 else
2960#endif
2961#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002962 {
2963 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002964 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002965 }
Paul Bakker380da532012-04-18 16:10:25 +00002966}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002967
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002968static void ssl_update_checksum_start( ssl_context *ssl,
2969 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002970{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002971#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2972 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002973 md5_update( &ssl->handshake->fin_md5 , buf, len );
2974 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002975#endif
2976#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2977#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002978 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002979#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002980#if defined(POLARSSL_SHA512_C)
2981 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002982#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002983#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002984}
2985
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002986#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2987 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002988static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2989 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002990{
Paul Bakker48916f92012-09-16 19:57:18 +00002991 md5_update( &ssl->handshake->fin_md5 , buf, len );
2992 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002993}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002994#endif
Paul Bakker380da532012-04-18 16:10:25 +00002995
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002996#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2997#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002998static void ssl_update_checksum_sha256( ssl_context *ssl,
2999 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003000{
Paul Bakker9e36f042013-06-30 14:34:05 +02003001 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003002}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003003#endif
Paul Bakker380da532012-04-18 16:10:25 +00003004
Paul Bakker9e36f042013-06-30 14:34:05 +02003005#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003006static void ssl_update_checksum_sha384( ssl_context *ssl,
3007 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003008{
Paul Bakker9e36f042013-06-30 14:34:05 +02003009 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003010}
Paul Bakker769075d2012-11-24 11:26:46 +01003011#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003012#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003013
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003014#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003015static void ssl_calc_finished_ssl(
3016 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003017{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003018 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003019 md5_context md5;
3020 sha1_context sha1;
3021
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 unsigned char padbuf[48];
3023 unsigned char md5sum[16];
3024 unsigned char sha1sum[20];
3025
Paul Bakker48916f92012-09-16 19:57:18 +00003026 ssl_session *session = ssl->session_negotiate;
3027 if( !session )
3028 session = ssl->session;
3029
Paul Bakker1ef83d62012-04-11 12:09:53 +00003030 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3031
Paul Bakker48916f92012-09-16 19:57:18 +00003032 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3033 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003034
3035 /*
3036 * SSLv3:
3037 * hash =
3038 * MD5( master + pad2 +
3039 * MD5( handshake + sender + master + pad1 ) )
3040 * + SHA1( master + pad2 +
3041 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003042 */
3043
Paul Bakker90995b52013-06-24 19:20:35 +02003044#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003046 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003048
Paul Bakker90995b52013-06-24 19:20:35 +02003049#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003050 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003051 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003053
Paul Bakker3c2122f2013-06-24 19:03:14 +02003054 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3055 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Paul Bakker1ef83d62012-04-11 12:09:53 +00003057 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Paul Bakker3c2122f2013-06-24 19:03:14 +02003059 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003060 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003061 md5_update( &md5, padbuf, 48 );
3062 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
Paul Bakker3c2122f2013-06-24 19:03:14 +02003064 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003065 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003066 sha1_update( &sha1, padbuf, 40 );
3067 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003068
Paul Bakker1ef83d62012-04-11 12:09:53 +00003069 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003070
Paul Bakker1ef83d62012-04-11 12:09:53 +00003071 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003072 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003073 md5_update( &md5, padbuf, 48 );
3074 md5_update( &md5, md5sum, 16 );
3075 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003076
Paul Bakker1ef83d62012-04-11 12:09:53 +00003077 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003078 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003079 sha1_update( &sha1, padbuf , 40 );
3080 sha1_update( &sha1, sha1sum, 20 );
3081 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003082
Paul Bakker1ef83d62012-04-11 12:09:53 +00003083 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
Paul Bakker5b4af392014-06-26 12:09:34 +02003085 md5_free( &md5 );
3086 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003087
Paul Bakker34617722014-06-13 17:20:13 +02003088 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3089 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3090 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
3092 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3093}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003094#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003096#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003097static void ssl_calc_finished_tls(
3098 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003099{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003100 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003101 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003104 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003105
Paul Bakker48916f92012-09-16 19:57:18 +00003106 ssl_session *session = ssl->session_negotiate;
3107 if( !session )
3108 session = ssl->session;
3109
Paul Bakker1ef83d62012-04-11 12:09:53 +00003110 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
Paul Bakker48916f92012-09-16 19:57:18 +00003112 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3113 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Paul Bakker1ef83d62012-04-11 12:09:53 +00003115 /*
3116 * TLSv1:
3117 * hash = PRF( master, finished_label,
3118 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3119 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003120
Paul Bakker90995b52013-06-24 19:20:35 +02003121#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003122 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3123 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003124#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003125
Paul Bakker90995b52013-06-24 19:20:35 +02003126#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3128 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003129#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003130
3131 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003132 ? "client finished"
3133 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003134
3135 md5_finish( &md5, padbuf );
3136 sha1_finish( &sha1, padbuf + 16 );
3137
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003138 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003139 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003140
3141 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3142
Paul Bakker5b4af392014-06-26 12:09:34 +02003143 md5_free( &md5 );
3144 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003145
Paul Bakker34617722014-06-13 17:20:13 +02003146 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003147
3148 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3149}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003150#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003151
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003152#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3153#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003154static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155 ssl_context *ssl, unsigned char *buf, int from )
3156{
3157 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003158 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003159 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003160 unsigned char padbuf[32];
3161
Paul Bakker48916f92012-09-16 19:57:18 +00003162 ssl_session *session = ssl->session_negotiate;
3163 if( !session )
3164 session = ssl->session;
3165
Paul Bakker380da532012-04-18 16:10:25 +00003166 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003167
Paul Bakker9e36f042013-06-30 14:34:05 +02003168 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003169
3170 /*
3171 * TLSv1.2:
3172 * hash = PRF( master, finished_label,
3173 * Hash( handshake ) )[0.11]
3174 */
3175
Paul Bakker9e36f042013-06-30 14:34:05 +02003176#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003177 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003178 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003179#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003180
3181 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003182 ? "client finished"
3183 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003184
Paul Bakker9e36f042013-06-30 14:34:05 +02003185 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003186
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003187 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003188 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003189
3190 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3191
Paul Bakker5b4af392014-06-26 12:09:34 +02003192 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003193
Paul Bakker34617722014-06-13 17:20:13 +02003194 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003195
3196 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3197}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003198#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003199
Paul Bakker9e36f042013-06-30 14:34:05 +02003200#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003201static void ssl_calc_finished_tls_sha384(
3202 ssl_context *ssl, unsigned char *buf, int from )
3203{
3204 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003205 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003206 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003207 unsigned char padbuf[48];
3208
Paul Bakker48916f92012-09-16 19:57:18 +00003209 ssl_session *session = ssl->session_negotiate;
3210 if( !session )
3211 session = ssl->session;
3212
Paul Bakker380da532012-04-18 16:10:25 +00003213 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003214
Paul Bakker9e36f042013-06-30 14:34:05 +02003215 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003216
3217 /*
3218 * TLSv1.2:
3219 * hash = PRF( master, finished_label,
3220 * Hash( handshake ) )[0.11]
3221 */
3222
Paul Bakker9e36f042013-06-30 14:34:05 +02003223#if !defined(POLARSSL_SHA512_ALT)
3224 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3225 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003226#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003227
3228 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003229 ? "client finished"
3230 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003231
Paul Bakker9e36f042013-06-30 14:34:05 +02003232 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003233
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003234 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003235 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003236
3237 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3238
Paul Bakker5b4af392014-06-26 12:09:34 +02003239 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003240
Paul Bakker34617722014-06-13 17:20:13 +02003241 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003242
3243 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3244}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003245#endif /* POLARSSL_SHA512_C */
3246#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003247
Paul Bakker48916f92012-09-16 19:57:18 +00003248void ssl_handshake_wrapup( ssl_context *ssl )
3249{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003250 int resume = ssl->handshake->resume;
3251
Paul Bakker48916f92012-09-16 19:57:18 +00003252 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3253
3254 /*
3255 * Free our handshake params
3256 */
3257 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003258 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003259 ssl->handshake = NULL;
3260
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003261#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003262 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003263 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003264 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003265 ssl->renego_records_seen = 0;
3266 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003267#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003268
Paul Bakker48916f92012-09-16 19:57:18 +00003269 /*
3270 * Switch in our now active transform context
3271 */
3272 if( ssl->transform )
3273 {
3274 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003275 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003276 }
3277 ssl->transform = ssl->transform_negotiate;
3278 ssl->transform_negotiate = NULL;
3279
Paul Bakker0a597072012-09-25 21:55:46 +00003280 if( ssl->session )
3281 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003282#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3283 /* RFC 7366 3.1: keep the EtM state */
3284 ssl->session_negotiate->encrypt_then_mac =
3285 ssl->session->encrypt_then_mac;
3286#endif
3287
Paul Bakker0a597072012-09-25 21:55:46 +00003288 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003289 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003290 }
3291 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003292 ssl->session_negotiate = NULL;
3293
Paul Bakker0a597072012-09-25 21:55:46 +00003294 /*
3295 * Add cache entry
3296 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003297 if( ssl->f_set_cache != NULL &&
3298 ssl->session->length != 0 &&
3299 resume == 0 )
3300 {
Paul Bakker0a597072012-09-25 21:55:46 +00003301 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3302 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003303 }
Paul Bakker0a597072012-09-25 21:55:46 +00003304
Paul Bakker48916f92012-09-16 19:57:18 +00003305 ssl->state++;
3306
3307 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3308}
3309
Paul Bakker1ef83d62012-04-11 12:09:53 +00003310int ssl_write_finished( ssl_context *ssl )
3311{
3312 int ret, hash_len;
3313
3314 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3315
Paul Bakker92be97b2013-01-02 17:30:03 +01003316 /*
3317 * Set the out_msg pointer to the correct location based on IV length
3318 */
3319 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3320 {
3321 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3322 ssl->transform_negotiate->fixed_ivlen;
3323 }
3324 else
3325 ssl->out_msg = ssl->out_iv;
3326
Paul Bakker48916f92012-09-16 19:57:18 +00003327 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003328
3329 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3331
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003332#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003333 ssl->verify_data_len = hash_len;
3334 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003335#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003336
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 ssl->out_msglen = 4 + hash_len;
3338 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3339 ssl->out_msg[0] = SSL_HS_FINISHED;
3340
3341 /*
3342 * In case of session resuming, invert the client and server
3343 * ChangeCipherSpec messages order.
3344 */
Paul Bakker0a597072012-09-25 21:55:46 +00003345 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003347#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003349 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003350#endif
3351#if defined(POLARSSL_SSL_SRV_C)
3352 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003353 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003354#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 }
3356 else
3357 ssl->state++;
3358
Paul Bakker48916f92012-09-16 19:57:18 +00003359 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003360 * Switch to our negotiated transform and session parameters for outbound
3361 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003362 */
3363 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3364 ssl->transform_out = ssl->transform_negotiate;
3365 ssl->session_out = ssl->session_negotiate;
3366 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003367
Paul Bakker07eb38b2012-12-19 14:42:06 +01003368#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003369 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003370 {
3371 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3372 {
3373 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3374 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3375 }
3376 }
3377#endif
3378
Paul Bakker5121ce52009-01-03 21:22:43 +00003379 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3380 {
3381 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3382 return( ret );
3383 }
3384
3385 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3386
3387 return( 0 );
3388}
3389
3390int ssl_parse_finished( ssl_context *ssl )
3391{
Paul Bakker23986e52011-04-24 08:57:21 +00003392 int ret;
3393 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 unsigned char buf[36];
3395
3396 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3397
Paul Bakker48916f92012-09-16 19:57:18 +00003398 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003399
Paul Bakker48916f92012-09-16 19:57:18 +00003400 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003401 * Switch to our negotiated transform and session parameters for inbound
3402 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003403 */
3404 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3405 ssl->transform_in = ssl->transform_negotiate;
3406 ssl->session_in = ssl->session_negotiate;
3407 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003408
Paul Bakker92be97b2013-01-02 17:30:03 +01003409 /*
3410 * Set the in_msg pointer to the correct location based on IV length
3411 */
3412 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3413 {
3414 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3415 ssl->transform_negotiate->fixed_ivlen;
3416 }
3417 else
3418 ssl->in_msg = ssl->in_iv;
3419
Paul Bakker07eb38b2012-12-19 14:42:06 +01003420#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003421 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003422 {
3423 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3424 {
3425 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3426 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3427 }
3428 }
3429#endif
3430
Paul Bakker5121ce52009-01-03 21:22:43 +00003431 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3432 {
3433 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3434 return( ret );
3435 }
3436
3437 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3438 {
3439 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003440 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 }
3442
Paul Bakker1ef83d62012-04-11 12:09:53 +00003443 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3445
3446 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3447 ssl->in_hslen != 4 + hash_len )
3448 {
3449 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003450 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003451 }
3452
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003453 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 {
3455 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003456 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 }
3458
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003459#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003460 ssl->verify_data_len = hash_len;
3461 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003462#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003463
Paul Bakker0a597072012-09-25 21:55:46 +00003464 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003466#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 if( ssl->endpoint == SSL_IS_CLIENT )
3468 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003469#endif
3470#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003471 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003472 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003473#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003474 }
3475 else
3476 ssl->state++;
3477
3478 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3479
3480 return( 0 );
3481}
3482
Paul Bakker968afaa2014-07-09 11:09:24 +02003483static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003484{
3485 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3486
3487#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3488 defined(POLARSSL_SSL_PROTO_TLS1_1)
3489 md5_init( &handshake->fin_md5 );
3490 sha1_init( &handshake->fin_sha1 );
3491 md5_starts( &handshake->fin_md5 );
3492 sha1_starts( &handshake->fin_sha1 );
3493#endif
3494#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3495#if defined(POLARSSL_SHA256_C)
3496 sha256_init( &handshake->fin_sha256 );
3497 sha256_starts( &handshake->fin_sha256, 0 );
3498#endif
3499#if defined(POLARSSL_SHA512_C)
3500 sha512_init( &handshake->fin_sha512 );
3501 sha512_starts( &handshake->fin_sha512, 1 );
3502#endif
3503#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3504
3505 handshake->update_checksum = ssl_update_checksum_start;
3506 handshake->sig_alg = SSL_HASH_SHA1;
3507
3508#if defined(POLARSSL_DHM_C)
3509 dhm_init( &handshake->dhm_ctx );
3510#endif
3511#if defined(POLARSSL_ECDH_C)
3512 ecdh_init( &handshake->ecdh_ctx );
3513#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003514}
3515
3516static void ssl_transform_init( ssl_transform *transform )
3517{
3518 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003519
3520 cipher_init( &transform->cipher_ctx_enc );
3521 cipher_init( &transform->cipher_ctx_dec );
3522
3523 md_init( &transform->md_ctx_enc );
3524 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003525}
3526
3527void ssl_session_init( ssl_session *session )
3528{
3529 memset( session, 0, sizeof(ssl_session) );
3530}
3531
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003532static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003533{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003534 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003535 if( ssl->transform_negotiate )
3536 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003537 if( ssl->session_negotiate )
3538 ssl_session_free( ssl->session_negotiate );
3539 if( ssl->handshake )
3540 ssl_handshake_free( ssl->handshake );
3541
3542 /*
3543 * Either the pointers are now NULL or cleared properly and can be freed.
3544 * Now allocate missing structures.
3545 */
3546 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003547 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003548 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003549 }
Paul Bakker48916f92012-09-16 19:57:18 +00003550
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003551 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003552 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003553 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003554 }
Paul Bakker48916f92012-09-16 19:57:18 +00003555
Paul Bakker82788fb2014-10-20 13:59:19 +02003556 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003557 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003558 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003559 }
Paul Bakker48916f92012-09-16 19:57:18 +00003560
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003561 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003562 if( ssl->handshake == NULL ||
3563 ssl->transform_negotiate == NULL ||
3564 ssl->session_negotiate == NULL )
3565 {
3566 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003567
3568 polarssl_free( ssl->handshake );
3569 polarssl_free( ssl->transform_negotiate );
3570 polarssl_free( ssl->session_negotiate );
3571
3572 ssl->handshake = NULL;
3573 ssl->transform_negotiate = NULL;
3574 ssl->session_negotiate = NULL;
3575
Paul Bakker48916f92012-09-16 19:57:18 +00003576 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3577 }
3578
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003579 /* Initialize structures */
3580 ssl_session_init( ssl->session_negotiate );
3581 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003582 ssl_handshake_params_init( ssl->handshake );
3583
3584#if defined(POLARSSL_X509_CRT_PARSE_C)
3585 ssl->handshake->key_cert = ssl->key_cert;
3586#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003587
Paul Bakker48916f92012-09-16 19:57:18 +00003588 return( 0 );
3589}
3590
Paul Bakker5121ce52009-01-03 21:22:43 +00003591/*
3592 * Initialize an SSL context
3593 */
3594int ssl_init( ssl_context *ssl )
3595{
Paul Bakker48916f92012-09-16 19:57:18 +00003596 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 int len = SSL_BUFFER_LEN;
3598
3599 memset( ssl, 0, sizeof( ssl_context ) );
3600
Paul Bakker62f2dee2012-09-28 07:31:51 +00003601 /*
3602 * Sane defaults
3603 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003604 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3605 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3606 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3607 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003608
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003609 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003610
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003611#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003612 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003613 memset( ssl->renego_period, 0xFF, 7 );
3614 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003615#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003616
Paul Bakker62f2dee2012-09-28 07:31:51 +00003617#if defined(POLARSSL_DHM_C)
3618 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003619 POLARSSL_DHM_RFC5114_MODP_2048_P) ) != 0 ||
Paul Bakker62f2dee2012-09-28 07:31:51 +00003620 ( ret = mpi_read_string( &ssl->dhm_G, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003621 POLARSSL_DHM_RFC5114_MODP_2048_G) ) != 0 )
Paul Bakker62f2dee2012-09-28 07:31:51 +00003622 {
3623 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3624 return( ret );
3625 }
3626#endif
3627
3628 /*
3629 * Prepare base structures
3630 */
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003631 if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL ||
3632 ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003633 {
3634 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003635 polarssl_free( ssl->in_ctr );
3636 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003637 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003638 }
3639
3640 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3641 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3642
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003643 ssl->in_hdr = ssl->in_ctr + 8;
3644 ssl->in_iv = ssl->in_ctr + 13;
3645 ssl->in_msg = ssl->in_ctr + 13;
3646
3647 ssl->out_hdr = ssl->out_ctr + 8;
3648 ssl->out_iv = ssl->out_ctr + 13;
3649 ssl->out_msg = ssl->out_ctr + 13;
3650
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003651#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3652 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3653#endif
3654
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003655#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3656 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3657#endif
3658
Paul Bakker606b4ba2013-08-14 16:52:14 +02003659#if defined(POLARSSL_SSL_SESSION_TICKETS)
3660 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3661#endif
3662
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003663#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003664 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003665#endif
3666
Paul Bakker48916f92012-09-16 19:57:18 +00003667 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3668 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003669
3670 return( 0 );
3671}
3672
3673/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003674 * Reset an initialized and used SSL context for re-use while retaining
3675 * all application-set variables, function pointers and data.
3676 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003677int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003678{
Paul Bakker48916f92012-09-16 19:57:18 +00003679 int ret;
3680
Paul Bakker7eb013f2011-10-06 12:37:39 +00003681 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003682
3683#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003684 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003685 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003686
3687 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003688 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3689 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003690#endif
3691 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003692
Paul Bakker7eb013f2011-10-06 12:37:39 +00003693 ssl->in_offt = NULL;
3694
Paul Bakker92be97b2013-01-02 17:30:03 +01003695 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003696 ssl->in_msgtype = 0;
3697 ssl->in_msglen = 0;
3698 ssl->in_left = 0;
3699
3700 ssl->in_hslen = 0;
3701 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003702 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003703
Paul Bakker92be97b2013-01-02 17:30:03 +01003704 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003705 ssl->out_msgtype = 0;
3706 ssl->out_msglen = 0;
3707 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003708#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003709 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3710 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003711#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003712
Paul Bakker48916f92012-09-16 19:57:18 +00003713 ssl->transform_in = NULL;
3714 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003715
3716 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3717 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003718
3719#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003720 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003721 {
3722 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003723 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003724 {
3725 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3726 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3727 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003728 }
3729#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003730
Paul Bakker48916f92012-09-16 19:57:18 +00003731 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003732 {
Paul Bakker48916f92012-09-16 19:57:18 +00003733 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003734 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003735 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003736 }
Paul Bakker48916f92012-09-16 19:57:18 +00003737
Paul Bakkerc0463502013-02-14 11:19:38 +01003738 if( ssl->session )
3739 {
3740 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003741 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003742 ssl->session = NULL;
3743 }
3744
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003745#if defined(POLARSSL_SSL_ALPN)
3746 ssl->alpn_chosen = NULL;
3747#endif
3748
Paul Bakker48916f92012-09-16 19:57:18 +00003749 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3750 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003751
3752 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003753}
3754
Paul Bakkera503a632013-08-14 13:48:06 +02003755#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003756static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3757{
3758 aes_free( &tkeys->enc );
3759 aes_free( &tkeys->dec );
3760
3761 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3762}
3763
Paul Bakker7eb013f2011-10-06 12:37:39 +00003764/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003765 * Allocate and initialize ticket keys
3766 */
3767static int ssl_ticket_keys_init( ssl_context *ssl )
3768{
3769 int ret;
3770 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003771 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003772
3773 if( ssl->ticket_keys != NULL )
3774 return( 0 );
3775
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003776 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003777 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003778 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3779
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003780 aes_init( &tkeys->enc );
3781 aes_init( &tkeys->dec );
3782
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003783 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003784 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003785 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003786 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003787 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003788 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003789
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003790 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3791 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3792 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3793 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003794 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003795 polarssl_free( tkeys );
3796 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003797 }
3798
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003799 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003800 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003801 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003802 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003803 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003804 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003805
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003806 ssl->ticket_keys = tkeys;
3807
3808 return( 0 );
3809}
Paul Bakkera503a632013-08-14 13:48:06 +02003810#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003811
3812/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003813 * SSL set accessors
3814 */
3815void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3816{
3817 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003818
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003819#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3820 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003821 if( endpoint == SSL_IS_CLIENT )
3822 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003823#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003824
3825#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3826 if( endpoint == SSL_IS_SERVER )
3827 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3828#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003829}
3830
3831void ssl_set_authmode( ssl_context *ssl, int authmode )
3832{
3833 ssl->authmode = authmode;
3834}
3835
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003836#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003837void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003838 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003839 void *p_vrfy )
3840{
3841 ssl->f_vrfy = f_vrfy;
3842 ssl->p_vrfy = p_vrfy;
3843}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003844#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003845
Paul Bakker5121ce52009-01-03 21:22:43 +00003846void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003847 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 void *p_rng )
3849{
3850 ssl->f_rng = f_rng;
3851 ssl->p_rng = p_rng;
3852}
3853
3854void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003855 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 void *p_dbg )
3857{
3858 ssl->f_dbg = f_dbg;
3859 ssl->p_dbg = p_dbg;
3860}
3861
3862void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003863 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003864 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003865{
3866 ssl->f_recv = f_recv;
3867 ssl->f_send = f_send;
3868 ssl->p_recv = p_recv;
3869 ssl->p_send = p_send;
3870}
3871
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003872#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003873void ssl_set_session_cache( ssl_context *ssl,
3874 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3875 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003876{
Paul Bakker0a597072012-09-25 21:55:46 +00003877 ssl->f_get_cache = f_get_cache;
3878 ssl->p_get_cache = p_get_cache;
3879 ssl->f_set_cache = f_set_cache;
3880 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003881}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003882#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003883
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003884#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003885int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003886{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003887 int ret;
3888
3889 if( ssl == NULL ||
3890 session == NULL ||
3891 ssl->session_negotiate == NULL ||
3892 ssl->endpoint != SSL_IS_CLIENT )
3893 {
3894 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3895 }
3896
3897 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3898 return( ret );
3899
Paul Bakker0a597072012-09-25 21:55:46 +00003900 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003901
3902 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003903}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003904#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003905
Paul Bakkerb68cad62012-08-23 08:34:18 +00003906void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003907{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003908 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3909 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3910 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3911 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3912}
3913
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003914void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3915 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003916 int major, int minor )
3917{
3918 if( major != SSL_MAJOR_VERSION_3 )
3919 return;
3920
3921 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3922 return;
3923
3924 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003925}
3926
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003927#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003928/* Add a new (empty) key_cert entry an return a pointer to it */
3929static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3930{
3931 ssl_key_cert *key_cert, *last;
3932
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003933 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003934 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003935 return( NULL );
3936
3937 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3938
3939 /* Append the new key_cert to the (possibly empty) current list */
3940 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003941 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003942 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003943 if( ssl->handshake != NULL )
3944 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003945 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003946 else
3947 {
3948 last = ssl->key_cert;
3949 while( last->next != NULL )
3950 last = last->next;
3951 last->next = key_cert;
3952 }
3953
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003954 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003955}
3956
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003957void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003958 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003959{
3960 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003961 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003962 ssl->peer_cn = peer_cn;
3963}
3964
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003965int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003966 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003967{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003968 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3969
3970 if( key_cert == NULL )
3971 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3972
3973 key_cert->cert = own_cert;
3974 key_cert->key = pk_key;
3975
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00003976 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003977}
3978
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01003979#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003980#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003981int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003982 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003983{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003984 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003985 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003986
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003987 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003988 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3989
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003990 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003991 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003992 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003993
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003994 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003995
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003996 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003997 if( ret != 0 )
3998 return( ret );
3999
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004000 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004001 return( ret );
4002
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004003 key_cert->cert = own_cert;
4004 key_cert->key_own_alloc = 1;
4005
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004006 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004007}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004008#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004009
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004010int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004011 void *rsa_key,
4012 rsa_decrypt_func rsa_decrypt,
4013 rsa_sign_func rsa_sign,
4014 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004015{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004016 int ret;
4017 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004018
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004019 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004020 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4021
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004022 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004023 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004024 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004025
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004026 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004027
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004028 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4029 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4030 return( ret );
4031
4032 key_cert->cert = own_cert;
4033 key_cert->key_own_alloc = 1;
4034
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004035 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004036}
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004037#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004038#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004039
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004040#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004041int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4042 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004043{
Paul Bakker6db455e2013-09-18 17:29:31 +02004044 if( psk == NULL || psk_identity == NULL )
4045 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4046
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004047 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004048 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4049
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02004050 /* Identity len will be encoded on two bytes */
4051 if( ( psk_identity_len >> 16 ) != 0 ||
4052 psk_identity_len > SSL_MAX_CONTENT_LEN )
4053 {
4054 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4055 }
4056
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004057 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004058 {
4059 polarssl_free( ssl->psk );
4060 polarssl_free( ssl->psk_identity );
4061 }
4062
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004063 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
4064 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05004065 {
4066 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004067 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004068 ssl->psk = NULL;
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004069 ssl->psk_identity = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05004070 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4071 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004072
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004073 ssl->psk_len = psk_len;
4074 ssl->psk_identity_len = psk_identity_len;
4075
Paul Bakker6db455e2013-09-18 17:29:31 +02004076 memcpy( ssl->psk, psk, ssl->psk_len );
4077 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004078
4079 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004080}
4081
4082void ssl_set_psk_cb( ssl_context *ssl,
4083 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4084 size_t),
4085 void *p_psk )
4086{
4087 ssl->f_psk = f_psk;
4088 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004089}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004090#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004091
Paul Bakker48916f92012-09-16 19:57:18 +00004092#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004093int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004094{
4095 int ret;
4096
Paul Bakker48916f92012-09-16 19:57:18 +00004097 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004098 {
4099 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4100 return( ret );
4101 }
4102
Paul Bakker48916f92012-09-16 19:57:18 +00004103 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004104 {
4105 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4106 return( ret );
4107 }
4108
4109 return( 0 );
4110}
4111
Paul Bakker1b57b062011-01-06 15:48:19 +00004112int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4113{
4114 int ret;
4115
Paul Bakker66d5d072014-06-17 16:39:18 +02004116 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004117 {
4118 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4119 return( ret );
4120 }
4121
Paul Bakker66d5d072014-06-17 16:39:18 +02004122 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004123 {
4124 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4125 return( ret );
4126 }
4127
4128 return( 0 );
4129}
Paul Bakker48916f92012-09-16 19:57:18 +00004130#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004131
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004132#if defined(POLARSSL_SSL_SET_CURVES)
4133/*
4134 * Set the allowed elliptic curves
4135 */
4136void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4137{
4138 ssl->curve_list = curve_list;
4139}
4140#endif
4141
Paul Bakker0be444a2013-08-27 21:55:01 +02004142#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004143int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004144{
4145 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004146 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004147
4148 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004149
4150 if( ssl->hostname_len + 1 == 0 )
4151 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4152
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004153 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004154
Paul Bakkerb15b8512012-01-13 13:44:06 +00004155 if( ssl->hostname == NULL )
4156 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4157
Paul Bakker3c2122f2013-06-24 19:03:14 +02004158 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004159 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004160
Paul Bakker40ea7de2009-05-03 10:18:48 +00004161 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004162
4163 return( 0 );
4164}
4165
Paul Bakker5701cdc2012-09-27 21:49:42 +00004166void ssl_set_sni( ssl_context *ssl,
4167 int (*f_sni)(void *, ssl_context *,
4168 const unsigned char *, size_t),
4169 void *p_sni )
4170{
4171 ssl->f_sni = f_sni;
4172 ssl->p_sni = p_sni;
4173}
Paul Bakker0be444a2013-08-27 21:55:01 +02004174#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004175
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004176#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004177int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004178{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004179 size_t cur_len, tot_len;
4180 const char **p;
4181
4182 /*
4183 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4184 * truncated". Check lengths now rather than later.
4185 */
4186 tot_len = 0;
4187 for( p = protos; *p != NULL; p++ )
4188 {
4189 cur_len = strlen( *p );
4190 tot_len += cur_len;
4191
4192 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4193 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4194 }
4195
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004196 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004197
4198 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004199}
4200
4201const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4202{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004203 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004204}
4205#endif /* POLARSSL_SSL_ALPN */
4206
Paul Bakker490ecc82011-10-06 13:04:09 +00004207void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4208{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004209 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4210 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4211 {
4212 ssl->max_major_ver = major;
4213 ssl->max_minor_ver = minor;
4214 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004215}
4216
Paul Bakker1d29fb52012-09-28 13:28:45 +00004217void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4218{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004219 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4220 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4221 {
4222 ssl->min_major_ver = major;
4223 ssl->min_minor_ver = minor;
4224 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004225}
4226
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004227#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4228void ssl_set_fallback( ssl_context *ssl, char fallback )
4229{
4230 ssl->fallback = fallback;
4231}
4232#endif
4233
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004234#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4235void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4236{
4237 ssl->encrypt_then_mac = etm;
4238}
4239#endif
4240
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004241#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4242void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4243{
4244 ssl->extended_ms = ems;
4245}
4246#endif
4247
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004248void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4249{
4250 ssl->arc4_disabled = arc4;
4251}
4252
Paul Bakker05decb22013-08-15 13:33:48 +02004253#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004254int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4255{
Paul Bakker77e257e2013-12-16 15:29:52 +01004256 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004257 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004258 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004259 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004260 }
4261
4262 ssl->mfl_code = mfl_code;
4263
4264 return( 0 );
4265}
Paul Bakker05decb22013-08-15 13:33:48 +02004266#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004267
Paul Bakker1f2bc622013-08-15 13:45:55 +02004268#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004269int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004270{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004271 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004272
4273 return( 0 );
4274}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004275#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004276
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004277#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4278void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4279{
4280 ssl->split_done = split;
4281}
4282#endif
4283
Paul Bakker48916f92012-09-16 19:57:18 +00004284void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4285{
4286 ssl->allow_legacy_renegotiation = allow_legacy;
4287}
4288
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004289#if defined(POLARSSL_SSL_RENEGOTIATION)
4290void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4291{
4292 ssl->disable_renegotiation = renegotiation;
4293}
4294
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004295void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4296{
4297 ssl->renego_max_records = max_records;
4298}
4299
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004300void ssl_set_renegotiation_period( ssl_context *ssl,
4301 const unsigned char period[8] )
4302{
4303 memcpy( ssl->renego_period, period, 8 );
4304}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004305#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004306
Paul Bakkera503a632013-08-14 13:48:06 +02004307#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004308int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4309{
4310 ssl->session_tickets = use_tickets;
4311
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004312#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004313 if( ssl->endpoint == SSL_IS_CLIENT )
4314 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004315#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004316
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004317 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4318 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004319
4320 if( ssl->f_rng == NULL )
4321 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4322
4323 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004324}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004325
4326void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4327{
4328 ssl->ticket_lifetime = lifetime;
4329}
Paul Bakkera503a632013-08-14 13:48:06 +02004330#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004331
Paul Bakker5121ce52009-01-03 21:22:43 +00004332/*
4333 * SSL get accessors
4334 */
4335size_t ssl_get_bytes_avail( const ssl_context *ssl )
4336{
4337 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4338}
4339
Paul Bakkerff60ee62010-03-16 21:09:09 +00004340int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004341{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004342 if( ssl->session != NULL )
4343 return( ssl->session->verify_result );
4344
4345 if( ssl->session_negotiate != NULL )
4346 return( ssl->session_negotiate->verify_result );
4347
4348 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004349}
4350
Paul Bakkere3166ce2011-01-27 17:40:50 +00004351const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004352{
Paul Bakker926c8e42013-03-06 10:23:34 +01004353 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004354 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004355
Paul Bakkere3166ce2011-01-27 17:40:50 +00004356 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004357}
4358
Paul Bakker43ca69c2011-01-15 17:35:19 +00004359const char *ssl_get_version( const ssl_context *ssl )
4360{
4361 switch( ssl->minor_ver )
4362 {
4363 case SSL_MINOR_VERSION_0:
4364 return( "SSLv3.0" );
4365
4366 case SSL_MINOR_VERSION_1:
4367 return( "TLSv1.0" );
4368
4369 case SSL_MINOR_VERSION_2:
4370 return( "TLSv1.1" );
4371
Paul Bakker1ef83d62012-04-11 12:09:53 +00004372 case SSL_MINOR_VERSION_3:
4373 return( "TLSv1.2" );
4374
Paul Bakker43ca69c2011-01-15 17:35:19 +00004375 default:
4376 break;
4377 }
4378 return( "unknown" );
4379}
4380
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004381#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004382const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004383{
4384 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004385 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004386
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004387 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004388}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004389#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004390
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004391#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004392int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4393{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004394 if( ssl == NULL ||
4395 dst == NULL ||
4396 ssl->session == NULL ||
4397 ssl->endpoint != SSL_IS_CLIENT )
4398 {
4399 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4400 }
4401
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004402 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004403}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004404#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004405
Paul Bakker5121ce52009-01-03 21:22:43 +00004406/*
Paul Bakker1961b702013-01-25 14:49:24 +01004407 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004408 */
Paul Bakker1961b702013-01-25 14:49:24 +01004409int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004410{
Paul Bakker40e46942009-01-03 21:51:57 +00004411 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004412
Paul Bakker40e46942009-01-03 21:51:57 +00004413#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004414 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004415 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004416#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004417#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004418 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004419 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004420#endif
4421
Paul Bakker1961b702013-01-25 14:49:24 +01004422 return( ret );
4423}
4424
4425/*
4426 * Perform the SSL handshake
4427 */
4428int ssl_handshake( ssl_context *ssl )
4429{
4430 int ret = 0;
4431
4432 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4433
4434 while( ssl->state != SSL_HANDSHAKE_OVER )
4435 {
4436 ret = ssl_handshake_step( ssl );
4437
4438 if( ret != 0 )
4439 break;
4440 }
4441
Paul Bakker5121ce52009-01-03 21:22:43 +00004442 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4443
4444 return( ret );
4445}
4446
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004447#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004448#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004449/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004450 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004451 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004452static int ssl_write_hello_request( ssl_context *ssl )
4453{
4454 int ret;
4455
4456 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4457
4458 ssl->out_msglen = 4;
4459 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4460 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4461
4462 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4463 {
4464 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4465 return( ret );
4466 }
4467
4468 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4469
4470 return( 0 );
4471}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004472#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004473
4474/*
4475 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004476 * - any side: calling ssl_renegotiate(),
4477 * - client: receiving a HelloRequest during ssl_read(),
4478 * - server: receiving any handshake message on server during ssl_read() after
4479 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004480 * If the handshake doesn't complete due to waiting for I/O, it will continue
4481 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004482 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004483static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004484{
4485 int ret;
4486
4487 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4488
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004489 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4490 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004491
4492 ssl->state = SSL_HELLO_REQUEST;
4493 ssl->renegotiation = SSL_RENEGOTIATION;
4494
Paul Bakker48916f92012-09-16 19:57:18 +00004495 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4496 {
4497 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4498 return( ret );
4499 }
4500
4501 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4502
4503 return( 0 );
4504}
4505
4506/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004507 * Renegotiate current connection on client,
4508 * or request renegotiation on server
4509 */
4510int ssl_renegotiate( ssl_context *ssl )
4511{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004512 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004513
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004514#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004515 /* On server, just send the request */
4516 if( ssl->endpoint == SSL_IS_SERVER )
4517 {
4518 if( ssl->state != SSL_HANDSHAKE_OVER )
4519 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4520
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004521 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4522
4523 /* Did we already try/start sending HelloRequest? */
4524 if( ssl->out_left != 0 )
4525 return( ssl_flush_output( ssl ) );
4526
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004527 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004528 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004529#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004530
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004531#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004532 /*
4533 * On client, either start the renegotiation process or,
4534 * if already in progress, continue the handshake
4535 */
4536 if( ssl->renegotiation != SSL_RENEGOTIATION )
4537 {
4538 if( ssl->state != SSL_HANDSHAKE_OVER )
4539 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4540
4541 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4542 {
4543 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4544 return( ret );
4545 }
4546 }
4547 else
4548 {
4549 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4550 {
4551 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4552 return( ret );
4553 }
4554 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004555#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004556
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004557 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004558}
4559
4560/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004561 * Check record counters and renegotiate if they're above the limit.
4562 */
4563static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4564{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004565 if( ssl->state != SSL_HANDSHAKE_OVER ||
4566 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4567 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4568 {
4569 return( 0 );
4570 }
4571
4572 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004573 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4574 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004575 {
4576 return( 0 );
4577 }
4578
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004579 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004580 return( ssl_renegotiate( ssl ) );
4581}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004582#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004583
4584/*
4585 * Receive application data decrypted from the SSL layer
4586 */
Paul Bakker23986e52011-04-24 08:57:21 +00004587int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004588{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004589 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004590 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004591
4592 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4593
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004594#if defined(POLARSSL_SSL_RENEGOTIATION)
4595 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4596 {
4597 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4598 return( ret );
4599 }
4600#endif
4601
Paul Bakker5121ce52009-01-03 21:22:43 +00004602 if( ssl->state != SSL_HANDSHAKE_OVER )
4603 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004604 ret = ssl_handshake( ssl );
4605 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4606 {
4607 record_read = 1;
4608 }
4609 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004610 {
4611 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4612 return( ret );
4613 }
4614 }
4615
4616 if( ssl->in_offt == NULL )
4617 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004618 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004619 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004620 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4621 {
4622 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4623 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004624
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004625 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4626 return( ret );
4627 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004628 }
4629
4630 if( ssl->in_msglen == 0 &&
4631 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4632 {
4633 /*
4634 * OpenSSL sends empty messages to randomize the IV
4635 */
4636 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4637 {
Paul Bakker831a7552011-05-18 13:32:51 +00004638 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4639 return( 0 );
4640
Paul Bakker5121ce52009-01-03 21:22:43 +00004641 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4642 return( ret );
4643 }
4644 }
4645
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004646#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004647 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4648 {
4649 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4650
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004651#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004652 if( ssl->endpoint == SSL_IS_CLIENT &&
4653 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4654 ssl->in_hslen != 4 ) )
4655 {
4656 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4657 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4658 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004659#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004660
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004661 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4662 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004663 ssl->allow_legacy_renegotiation ==
4664 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004665 {
4666 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4667
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004668#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004669 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004670 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004671 /*
4672 * SSLv3 does not have a "no_renegotiation" alert
4673 */
4674 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4675 return( ret );
4676 }
4677 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004678#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004679#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4680 defined(POLARSSL_SSL_PROTO_TLS1_2)
4681 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004682 {
4683 if( ( ret = ssl_send_alert_message( ssl,
4684 SSL_ALERT_LEVEL_WARNING,
4685 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4686 {
4687 return( ret );
4688 }
Paul Bakker48916f92012-09-16 19:57:18 +00004689 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004690 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004691#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4692 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004693 {
4694 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004695 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004696 }
Paul Bakker48916f92012-09-16 19:57:18 +00004697 }
4698 else
4699 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004700 ret = ssl_start_renegotiation( ssl );
4701 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4702 {
4703 record_read = 1;
4704 }
4705 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004706 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004707 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004708 return( ret );
4709 }
Paul Bakker48916f92012-09-16 19:57:18 +00004710 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004711
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004712 /* If a non-handshake record was read during renego, fallthrough,
4713 * else tell the user they should call ssl_read() again */
4714 if( ! record_read )
4715 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004716 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004717 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4718 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004719 ssl->renego_records_seen++;
4720
4721 if( ssl->renego_max_records >= 0 &&
4722 ssl->renego_records_seen > ssl->renego_max_records )
4723 {
4724 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4725 "but not honored by client" ) );
4726 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4727 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004728 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004729#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004730
4731 /* Fatal and closure alerts handled by ssl_read_record() */
4732 if( ssl->in_msgtype == SSL_MSG_ALERT )
4733 {
4734 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4735 return( POLARSSL_ERR_NET_WANT_READ );
4736 }
4737
4738 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004739 {
4740 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004741 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004742 }
4743
4744 ssl->in_offt = ssl->in_msg;
4745 }
4746
4747 n = ( len < ssl->in_msglen )
4748 ? len : ssl->in_msglen;
4749
4750 memcpy( buf, ssl->in_offt, n );
4751 ssl->in_msglen -= n;
4752
4753 if( ssl->in_msglen == 0 )
4754 /* all bytes consumed */
4755 ssl->in_offt = NULL;
4756 else
4757 /* more data available */
4758 ssl->in_offt += n;
4759
4760 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4761
Paul Bakker23986e52011-04-24 08:57:21 +00004762 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004763}
4764
4765/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004766 * Send application data to be encrypted by the SSL layer,
4767 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00004768 */
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004769static int ssl_write_real( ssl_context *ssl,
4770 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004771{
Paul Bakker23986e52011-04-24 08:57:21 +00004772 int ret;
4773 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004774 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004775
Paul Bakker05decb22013-08-15 13:33:48 +02004776#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004777 /*
4778 * Assume mfl_code is correct since it was checked when set
4779 */
4780 max_len = mfl_code_to_length[ssl->mfl_code];
4781
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004782 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004783 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004784 */
4785 if( ssl->session_out != NULL &&
4786 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4787 {
4788 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4789 }
Paul Bakker05decb22013-08-15 13:33:48 +02004790#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004791
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004792 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004793
Paul Bakker5121ce52009-01-03 21:22:43 +00004794 if( ssl->out_left != 0 )
4795 {
4796 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4797 {
4798 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4799 return( ret );
4800 }
4801 }
Paul Bakker887bd502011-06-08 13:10:54 +00004802 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004803 {
Paul Bakker887bd502011-06-08 13:10:54 +00004804 ssl->out_msglen = n;
4805 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4806 memcpy( ssl->out_msg, buf, n );
4807
4808 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4809 {
4810 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4811 return( ret );
4812 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 }
4814
Paul Bakker23986e52011-04-24 08:57:21 +00004815 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004816}
4817
4818/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004819 * Write application data, doing 1/n-1 splitting if necessary.
4820 *
4821 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004822 * then the caller will call us again with the same arguments, so
4823 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004824 */
4825#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004826static int ssl_write_split( ssl_context *ssl,
4827 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004828{
4829 int ret;
4830
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004831 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4832 len <= 1 ||
4833 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004834 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4835 != POLARSSL_MODE_CBC )
4836 {
4837 return( ssl_write_real( ssl, buf, len ) );
4838 }
4839
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004840 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004841 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004842 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004843 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004844 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004845 }
4846
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004847 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4848 return( ret );
4849 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004850
4851 return( ret + 1 );
4852}
4853#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4854
4855/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004856 * Write application data (public-facing wrapper)
4857 */
4858int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4859{
4860 int ret;
4861
4862 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4863
4864#if defined(POLARSSL_SSL_RENEGOTIATION)
4865 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4866 {
4867 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4868 return( ret );
4869 }
4870#endif
4871
4872 if( ssl->state != SSL_HANDSHAKE_OVER )
4873 {
4874 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4875 {
4876 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4877 return( ret );
4878 }
4879 }
4880
4881#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4882 ret = ssl_write_split( ssl, buf, len );
4883#else
4884 ret = ssl_write_real( ssl, buf, len );
4885#endif
4886
4887 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4888
4889 return( ret );
4890}
4891
4892/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004893 * Notify the peer that the connection is being closed
4894 */
4895int ssl_close_notify( ssl_context *ssl )
4896{
4897 int ret;
4898
4899 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4900
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004901 if( ssl->out_left != 0 )
4902 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004903
4904 if( ssl->state == SSL_HANDSHAKE_OVER )
4905 {
Paul Bakker48916f92012-09-16 19:57:18 +00004906 if( ( ret = ssl_send_alert_message( ssl,
4907 SSL_ALERT_LEVEL_WARNING,
4908 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004909 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004910 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004911 return( ret );
4912 }
4913 }
4914
4915 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4916
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004917 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004918}
4919
Paul Bakker48916f92012-09-16 19:57:18 +00004920void ssl_transform_free( ssl_transform *transform )
4921{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004922 if( transform == NULL )
4923 return;
4924
Paul Bakker48916f92012-09-16 19:57:18 +00004925#if defined(POLARSSL_ZLIB_SUPPORT)
4926 deflateEnd( &transform->ctx_deflate );
4927 inflateEnd( &transform->ctx_inflate );
4928#endif
4929
Paul Bakker84bbeb52014-07-01 14:53:22 +02004930 cipher_free( &transform->cipher_ctx_enc );
4931 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004932
Paul Bakker84bbeb52014-07-01 14:53:22 +02004933 md_free( &transform->md_ctx_enc );
4934 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004935
Paul Bakker34617722014-06-13 17:20:13 +02004936 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004937}
4938
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004939#if defined(POLARSSL_X509_CRT_PARSE_C)
4940static void ssl_key_cert_free( ssl_key_cert *key_cert )
4941{
4942 ssl_key_cert *cur = key_cert, *next;
4943
4944 while( cur != NULL )
4945 {
4946 next = cur->next;
4947
4948 if( cur->key_own_alloc )
4949 {
4950 pk_free( cur->key );
4951 polarssl_free( cur->key );
4952 }
4953 polarssl_free( cur );
4954
4955 cur = next;
4956 }
4957}
4958#endif /* POLARSSL_X509_CRT_PARSE_C */
4959
Paul Bakker48916f92012-09-16 19:57:18 +00004960void ssl_handshake_free( ssl_handshake_params *handshake )
4961{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004962 if( handshake == NULL )
4963 return;
4964
Paul Bakker48916f92012-09-16 19:57:18 +00004965#if defined(POLARSSL_DHM_C)
4966 dhm_free( &handshake->dhm_ctx );
4967#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004968#if defined(POLARSSL_ECDH_C)
4969 ecdh_free( &handshake->ecdh_ctx );
4970#endif
4971
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004972#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004973 /* explicit void pointer cast for buggy MS compiler */
4974 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004975#endif
4976
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004977#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4978 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4979 /*
4980 * Free only the linked list wrapper, not the keys themselves
4981 * since the belong to the SNI callback
4982 */
4983 if( handshake->sni_key_cert != NULL )
4984 {
4985 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4986
4987 while( cur != NULL )
4988 {
4989 next = cur->next;
4990 polarssl_free( cur );
4991 cur = next;
4992 }
4993 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004994#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004995
Paul Bakker34617722014-06-13 17:20:13 +02004996 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004997}
4998
4999void ssl_session_free( ssl_session *session )
5000{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005001 if( session == NULL )
5002 return;
5003
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005004#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005005 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005006 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005007 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005008 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005009 }
Paul Bakkered27a042013-04-18 22:46:23 +02005010#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005011
Paul Bakkera503a632013-08-14 13:48:06 +02005012#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005013 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005014#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005015
Paul Bakker34617722014-06-13 17:20:13 +02005016 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005017}
5018
Paul Bakker5121ce52009-01-03 21:22:43 +00005019/*
5020 * Free an SSL context
5021 */
5022void ssl_free( ssl_context *ssl )
5023{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005024 if( ssl == NULL )
5025 return;
5026
Paul Bakker5121ce52009-01-03 21:22:43 +00005027 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5028
Paul Bakker5121ce52009-01-03 21:22:43 +00005029 if( ssl->out_ctr != NULL )
5030 {
Paul Bakker34617722014-06-13 17:20:13 +02005031 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005032 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005033 }
5034
5035 if( ssl->in_ctr != NULL )
5036 {
Paul Bakker34617722014-06-13 17:20:13 +02005037 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005038 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005039 }
5040
Paul Bakker16770332013-10-11 09:59:44 +02005041#if defined(POLARSSL_ZLIB_SUPPORT)
5042 if( ssl->compress_buf != NULL )
5043 {
Paul Bakker34617722014-06-13 17:20:13 +02005044 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005045 polarssl_free( ssl->compress_buf );
5046 }
5047#endif
5048
Paul Bakker40e46942009-01-03 21:51:57 +00005049#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005050 mpi_free( &ssl->dhm_P );
5051 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005052#endif
5053
Paul Bakker48916f92012-09-16 19:57:18 +00005054 if( ssl->transform )
5055 {
5056 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005057 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005058 }
5059
5060 if( ssl->handshake )
5061 {
5062 ssl_handshake_free( ssl->handshake );
5063 ssl_transform_free( ssl->transform_negotiate );
5064 ssl_session_free( ssl->session_negotiate );
5065
Paul Bakker6e339b52013-07-03 13:37:05 +02005066 polarssl_free( ssl->handshake );
5067 polarssl_free( ssl->transform_negotiate );
5068 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005069 }
5070
Paul Bakkerc0463502013-02-14 11:19:38 +01005071 if( ssl->session )
5072 {
5073 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005074 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005075 }
5076
Paul Bakkera503a632013-08-14 13:48:06 +02005077#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005078 if( ssl->ticket_keys )
5079 {
5080 ssl_ticket_keys_free( ssl->ticket_keys );
5081 polarssl_free( ssl->ticket_keys );
5082 }
Paul Bakkera503a632013-08-14 13:48:06 +02005083#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005084
Paul Bakker0be444a2013-08-27 21:55:01 +02005085#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005086 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005087 {
Paul Bakker34617722014-06-13 17:20:13 +02005088 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005089 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005090 ssl->hostname_len = 0;
5091 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005092#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005093
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005094#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005095 if( ssl->psk != NULL )
5096 {
Paul Bakker34617722014-06-13 17:20:13 +02005097 polarssl_zeroize( ssl->psk, ssl->psk_len );
5098 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005099 polarssl_free( ssl->psk );
5100 polarssl_free( ssl->psk_identity );
5101 ssl->psk_len = 0;
5102 ssl->psk_identity_len = 0;
5103 }
5104#endif
5105
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005106#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005107 ssl_key_cert_free( ssl->key_cert );
5108#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005109
Paul Bakker05ef8352012-05-08 09:17:57 +00005110#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5111 if( ssl_hw_record_finish != NULL )
5112 {
5113 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5114 ssl_hw_record_finish( ssl );
5115 }
5116#endif
5117
Paul Bakker5121ce52009-01-03 21:22:43 +00005118 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005119
Paul Bakker86f04f42013-02-14 11:20:09 +01005120 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005121 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005122}
5123
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005124#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005125/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005126 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005127 */
5128unsigned char ssl_sig_from_pk( pk_context *pk )
5129{
5130#if defined(POLARSSL_RSA_C)
5131 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5132 return( SSL_SIG_RSA );
5133#endif
5134#if defined(POLARSSL_ECDSA_C)
5135 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5136 return( SSL_SIG_ECDSA );
5137#endif
5138 return( SSL_SIG_ANON );
5139}
5140
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005141pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5142{
5143 switch( sig )
5144 {
5145#if defined(POLARSSL_RSA_C)
5146 case SSL_SIG_RSA:
5147 return( POLARSSL_PK_RSA );
5148#endif
5149#if defined(POLARSSL_ECDSA_C)
5150 case SSL_SIG_ECDSA:
5151 return( POLARSSL_PK_ECDSA );
5152#endif
5153 default:
5154 return( POLARSSL_PK_NONE );
5155 }
5156}
Paul Bakker9af723c2014-05-01 13:03:14 +02005157#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005158
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005159/*
5160 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5161 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005162md_type_t ssl_md_alg_from_hash( unsigned char hash )
5163{
5164 switch( hash )
5165 {
5166#if defined(POLARSSL_MD5_C)
5167 case SSL_HASH_MD5:
5168 return( POLARSSL_MD_MD5 );
5169#endif
5170#if defined(POLARSSL_SHA1_C)
5171 case SSL_HASH_SHA1:
5172 return( POLARSSL_MD_SHA1 );
5173#endif
5174#if defined(POLARSSL_SHA256_C)
5175 case SSL_HASH_SHA224:
5176 return( POLARSSL_MD_SHA224 );
5177 case SSL_HASH_SHA256:
5178 return( POLARSSL_MD_SHA256 );
5179#endif
5180#if defined(POLARSSL_SHA512_C)
5181 case SSL_HASH_SHA384:
5182 return( POLARSSL_MD_SHA384 );
5183 case SSL_HASH_SHA512:
5184 return( POLARSSL_MD_SHA512 );
5185#endif
5186 default:
5187 return( POLARSSL_MD_NONE );
5188 }
5189}
5190
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005191#if defined(POLARSSL_SSL_SET_CURVES)
5192/*
5193 * Check is a curve proposed by the peer is in our list.
5194 * Return 1 if we're willing to use it, 0 otherwise.
5195 */
5196int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5197{
5198 const ecp_group_id *gid;
5199
5200 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5201 if( *gid == grp_id )
5202 return( 1 );
5203
5204 return( 0 );
5205}
Paul Bakker9af723c2014-05-01 13:03:14 +02005206#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005207
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005208#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005209int ssl_check_cert_usage( const x509_crt *cert,
5210 const ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005211 int cert_endpoint,
5212 int *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005213{
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005214 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005215#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5216 int usage = 0;
5217#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005218#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5219 const char *ext_oid;
5220 size_t ext_len;
5221#endif
5222
5223#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5224 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5225 ((void) cert);
5226 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005227 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005228#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005229
5230#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5231 if( cert_endpoint == SSL_IS_SERVER )
5232 {
5233 /* Server part of the key exchange */
5234 switch( ciphersuite->key_exchange )
5235 {
5236 case POLARSSL_KEY_EXCHANGE_RSA:
5237 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5238 usage = KU_KEY_ENCIPHERMENT;
5239 break;
5240
5241 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5242 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5243 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5244 usage = KU_DIGITAL_SIGNATURE;
5245 break;
5246
5247 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5248 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5249 usage = KU_KEY_AGREEMENT;
5250 break;
5251
5252 /* Don't use default: we want warnings when adding new values */
5253 case POLARSSL_KEY_EXCHANGE_NONE:
5254 case POLARSSL_KEY_EXCHANGE_PSK:
5255 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5256 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5257 usage = 0;
5258 }
5259 }
5260 else
5261 {
5262 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5263 usage = KU_DIGITAL_SIGNATURE;
5264 }
5265
5266 if( x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005267 {
5268 *flags |= BADCERT_KEY_USAGE;
5269 ret = -1;
5270 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005271#else
5272 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005273#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5274
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005275#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5276 if( cert_endpoint == SSL_IS_SERVER )
5277 {
5278 ext_oid = OID_SERVER_AUTH;
5279 ext_len = OID_SIZE( OID_SERVER_AUTH );
5280 }
5281 else
5282 {
5283 ext_oid = OID_CLIENT_AUTH;
5284 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5285 }
5286
5287 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005288 {
5289 *flags |= BADCERT_EXT_KEY_USAGE;
5290 ret = -1;
5291 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005292#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5293
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005294 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005295}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005296#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005297
5298#endif /* POLARSSL_SSL_TLS_C */