blob: 2eb24b259e2d36be5583715e360b0c208672f29e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The SSL 3.0 specification was drafted by Netscape in 1996,
24 * and became an IETF standard in 1999.
25 *
26 * http://wp.netscape.com/eng/ssl3/
27 * http://www.ietf.org/rfc/rfc2246.txt
28 * http://www.ietf.org/rfc/rfc4346.txt
29 */
30
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker0be444a2013-08-27 21:55:01 +020039#include "polarssl/debug.h"
40#include "polarssl/ssl.h"
41
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020044#if defined(POLARSSL_X509_CRT_PARSE_C) && \
45 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
46#include "polarssl/oid.h"
47#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020053#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker6edcd412013-10-29 15:22:54 +010057#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
58 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000059#define strcasecmp _stricmp
60#endif
61
Paul Bakker34617722014-06-13 17:20:13 +020062/* Implementation that should never be optimized out by the compiler */
63static void polarssl_zeroize( void *v, size_t n ) {
64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
65}
66
Paul Bakker05decb22013-08-15 13:33:48 +020067#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020068/*
69 * Convert max_fragment_length codes to length.
70 * RFC 6066 says:
71 * enum{
72 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
73 * } MaxFragmentLength;
74 * and we add 0 -> extension unused
75 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020076static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020077{
78 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
79 512, /* SSL_MAX_FRAG_LEN_512 */
80 1024, /* SSL_MAX_FRAG_LEN_1024 */
81 2048, /* SSL_MAX_FRAG_LEN_2048 */
82 4096, /* SSL_MAX_FRAG_LEN_4096 */
83};
Paul Bakker05decb22013-08-15 13:33:48 +020084#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020085
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020086static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
87{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088 ssl_session_free( dst );
89 memcpy( dst, src, sizeof( ssl_session ) );
90
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020092 if( src->peer_cert != NULL )
93 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020094 int ret;
95
Mansour Moufidc531b4a2015-02-15 17:35:38 -050096 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020097 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
99
Paul Bakkerb6b09562013-09-18 14:17:41 +0200100 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200102 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
103 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200104 {
105 polarssl_free( dst->peer_cert );
106 dst->peer_cert = NULL;
107 return( ret );
108 }
109 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200111
Paul Bakkera503a632013-08-14 13:48:06 +0200112#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113 if( src->ticket != NULL )
114 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500115 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200116 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200117 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
118
119 memcpy( dst->ticket, src->ticket, src->ticket_len );
120 }
Paul Bakkera503a632013-08-14 13:48:06 +0200121#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200122
123 return( 0 );
124}
125
Paul Bakker05ef8352012-05-08 09:17:57 +0000126#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200127int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200128 const unsigned char *key_enc, const unsigned char *key_dec,
129 size_t keylen,
130 const unsigned char *iv_enc, const unsigned char *iv_dec,
131 size_t ivlen,
132 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200133 size_t maclen ) = NULL;
134int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
135int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
136int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
137int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200139#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141/*
142 * Key material generation
143 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200144#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200145static int ssl3_prf( const unsigned char *secret, size_t slen,
146 const char *label,
147 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000148 unsigned char *dstbuf, size_t dlen )
149{
150 size_t i;
151 md5_context md5;
152 sha1_context sha1;
153 unsigned char padding[16];
154 unsigned char sha1sum[20];
155 ((void)label);
156
Paul Bakker5b4af392014-06-26 12:09:34 +0200157 md5_init( &md5 );
158 sha1_init( &sha1 );
159
Paul Bakker5f70b252012-09-13 14:23:06 +0000160 /*
161 * SSLv3:
162 * block =
163 * MD5( secret + SHA1( 'A' + secret + random ) ) +
164 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
165 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
166 * ...
167 */
168 for( i = 0; i < dlen / 16; i++ )
169 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200170 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000171
172 sha1_starts( &sha1 );
173 sha1_update( &sha1, padding, 1 + i );
174 sha1_update( &sha1, secret, slen );
175 sha1_update( &sha1, random, rlen );
176 sha1_finish( &sha1, sha1sum );
177
178 md5_starts( &md5 );
179 md5_update( &md5, secret, slen );
180 md5_update( &md5, sha1sum, 20 );
181 md5_finish( &md5, dstbuf + i * 16 );
182 }
183
Paul Bakker5b4af392014-06-26 12:09:34 +0200184 md5_free( &md5 );
185 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000186
Paul Bakker34617722014-06-13 17:20:13 +0200187 polarssl_zeroize( padding, sizeof( padding ) );
188 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000189
190 return( 0 );
191}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200192#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000193
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200195static int tls1_prf( const unsigned char *secret, size_t slen,
196 const char *label,
197 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000198 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199{
Paul Bakker23986e52011-04-24 08:57:21 +0000200 size_t nb, hs;
201 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200202 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 unsigned char tmp[128];
204 unsigned char h_i[20];
205
206 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000207 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209 hs = ( slen + 1 ) / 2;
210 S1 = secret;
211 S2 = secret + slen - hs;
212
213 nb = strlen( label );
214 memcpy( tmp + 20, label, nb );
215 memcpy( tmp + 20 + nb, random, rlen );
216 nb += rlen;
217
218 /*
219 * First compute P_md5(secret,label+random)[0..dlen]
220 */
221 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
222
223 for( i = 0; i < dlen; i += 16 )
224 {
225 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
226 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
227
228 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
229
230 for( j = 0; j < k; j++ )
231 dstbuf[i + j] = h_i[j];
232 }
233
234 /*
235 * XOR out with P_sha1(secret,label+random)[0..dlen]
236 */
237 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
238
239 for( i = 0; i < dlen; i += 20 )
240 {
241 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
242 sha1_hmac( S2, hs, tmp, 20, tmp );
243
244 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
245
246 for( j = 0; j < k; j++ )
247 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
248 }
249
Paul Bakker34617722014-06-13 17:20:13 +0200250 polarssl_zeroize( tmp, sizeof( tmp ) );
251 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
253 return( 0 );
254}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200255#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#if defined(POLARSSL_SSL_PROTO_TLS1_2)
258#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200259static int tls_prf_sha256( const unsigned char *secret, size_t slen,
260 const char *label,
261 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000262 unsigned char *dstbuf, size_t dlen )
263{
264 size_t nb;
265 size_t i, j, k;
266 unsigned char tmp[128];
267 unsigned char h_i[32];
268
269 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
272 nb = strlen( label );
273 memcpy( tmp + 32, label, nb );
274 memcpy( tmp + 32 + nb, random, rlen );
275 nb += rlen;
276
277 /*
278 * Compute P_<hash>(secret, label + random)[0..dlen]
279 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200280 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000281
282 for( i = 0; i < dlen; i += 32 )
283 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200284 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
285 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000286
287 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
288
289 for( j = 0; j < k; j++ )
290 dstbuf[i + j] = h_i[j];
291 }
292
Paul Bakker34617722014-06-13 17:20:13 +0200293 polarssl_zeroize( tmp, sizeof( tmp ) );
294 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000295
296 return( 0 );
297}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200298#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000299
Paul Bakker9e36f042013-06-30 14:34:05 +0200300#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200301static int tls_prf_sha384( const unsigned char *secret, size_t slen,
302 const char *label,
303 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000304 unsigned char *dstbuf, size_t dlen )
305{
306 size_t nb;
307 size_t i, j, k;
308 unsigned char tmp[128];
309 unsigned char h_i[48];
310
311 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
312 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
313
314 nb = strlen( label );
315 memcpy( tmp + 48, label, nb );
316 memcpy( tmp + 48 + nb, random, rlen );
317 nb += rlen;
318
319 /*
320 * Compute P_<hash>(secret, label + random)[0..dlen]
321 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200322 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000323
324 for( i = 0; i < dlen; i += 48 )
325 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200326 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
327 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000328
329 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
330
331 for( j = 0; j < k; j++ )
332 dstbuf[i + j] = h_i[j];
333 }
334
Paul Bakker34617722014-06-13 17:20:13 +0200335 polarssl_zeroize( tmp, sizeof( tmp ) );
336 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337
338 return( 0 );
339}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200340#endif /* POLARSSL_SHA512_C */
341#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342
Paul Bakker66d5d072014-06-17 16:39:18 +0200343static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200344
345#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
346 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200347static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200348#endif
Paul Bakker380da532012-04-18 16:10:25 +0000349
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200351static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
352static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200353#endif
354
355#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200356static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
357static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200358#endif
359
360#if defined(POLARSSL_SSL_PROTO_TLS1_2)
361#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200362static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
363static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
364static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200365#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100366
Paul Bakker9e36f042013-06-30 14:34:05 +0200367#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200368static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
369static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
370static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100371#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200372#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000373
Paul Bakker5121ce52009-01-03 21:22:43 +0000374int ssl_derive_keys( ssl_context *ssl )
375{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200376 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 unsigned char keyblk[256];
379 unsigned char *key1;
380 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100381 unsigned char *mac_enc;
382 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200383 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100384 const cipher_info_t *cipher_info;
385 const md_info_t *md_info;
386
Paul Bakker48916f92012-09-16 19:57:18 +0000387 ssl_session *session = ssl->session_negotiate;
388 ssl_transform *transform = ssl->transform_negotiate;
389 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
392
Paul Bakker68884e32013-01-07 18:20:04 +0100393 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
394 if( cipher_info == NULL )
395 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200396 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100397 transform->ciphersuite_info->cipher ) );
398 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
399 }
400
401 md_info = md_info_from_type( transform->ciphersuite_info->mac );
402 if( md_info == NULL )
403 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200404 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100405 transform->ciphersuite_info->mac ) );
406 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
407 }
408
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000410 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000411 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200412#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000414 {
Paul Bakker48916f92012-09-16 19:57:18 +0000415 handshake->tls_prf = ssl3_prf;
416 handshake->calc_verify = ssl_calc_verify_ssl;
417 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000418 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200419 else
420#endif
421#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
422 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000423 {
Paul Bakker48916f92012-09-16 19:57:18 +0000424 handshake->tls_prf = tls1_prf;
425 handshake->calc_verify = ssl_calc_verify_tls;
426 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000427 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200428 else
429#endif
430#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200431#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200432 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
433 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000434 {
Paul Bakker48916f92012-09-16 19:57:18 +0000435 handshake->tls_prf = tls_prf_sha384;
436 handshake->calc_verify = ssl_calc_verify_tls_sha384;
437 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000438 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000439 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200440#endif
441#if defined(POLARSSL_SHA256_C)
442 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000443 {
Paul Bakker48916f92012-09-16 19:57:18 +0000444 handshake->tls_prf = tls_prf_sha256;
445 handshake->calc_verify = ssl_calc_verify_tls_sha256;
446 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000447 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200448 else
449#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200450#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200451 {
452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200453 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200454 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000455
456 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 * SSLv3:
458 * master =
459 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
461 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200462 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200463 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 * master = PRF( premaster, "master secret", randbytes )[0..47]
465 */
Paul Bakker0a597072012-09-25 21:55:46 +0000466 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 {
Paul Bakker48916f92012-09-16 19:57:18 +0000468 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
469 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200471#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
472 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200473 {
474 unsigned char session_hash[48];
475 size_t hash_len;
476
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200477 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200478
479 ssl->handshake->calc_verify( ssl, session_hash );
480
481#if defined(POLARSSL_SSL_PROTO_TLS1_2)
482 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
483 {
484#if defined(POLARSSL_SHA512_C)
485 if( ssl->transform_negotiate->ciphersuite_info->mac ==
486 POLARSSL_MD_SHA384 )
487 {
488 hash_len = 48;
489 }
490 else
491#endif
492 hash_len = 32;
493 }
494 else
495#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
496 hash_len = 36;
497
498 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
499
500 handshake->tls_prf( handshake->premaster, handshake->pmslen,
501 "extended master secret",
502 session_hash, hash_len, session->master, 48 );
503
504 }
505 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200506#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000507 handshake->tls_prf( handshake->premaster, handshake->pmslen,
508 "master secret",
509 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200511
Paul Bakker34617722014-06-13 17:20:13 +0200512 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 }
514 else
515 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
516
517 /*
518 * Swap the client and server random values.
519 */
Paul Bakker48916f92012-09-16 19:57:18 +0000520 memcpy( tmp, handshake->randbytes, 64 );
521 memcpy( handshake->randbytes, tmp + 32, 32 );
522 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200523 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 /*
526 * SSLv3:
527 * key block =
528 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
529 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
530 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
532 * ...
533 *
534 * TLSv1:
535 * key block = PRF( master, "key expansion", randbytes )
536 */
Paul Bakker48916f92012-09-16 19:57:18 +0000537 handshake->tls_prf( session->master, 48, "key expansion",
538 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker48916f92012-09-16 19:57:18 +0000540 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
541 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
542 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
543 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
545
Paul Bakker34617722014-06-13 17:20:13 +0200546 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 /*
549 * Determine the appropriate key, IV and MAC length.
550 */
Paul Bakker68884e32013-01-07 18:20:04 +0100551
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200552 transform->keylen = cipher_info->key_length / 8;
553
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200554 if( cipher_info->mode == POLARSSL_MODE_GCM ||
555 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200557 transform->maclen = 0;
558
Paul Bakker68884e32013-01-07 18:20:04 +0100559 transform->ivlen = 12;
560 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200561
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200562 /* Minimum length is expicit IV + tag */
563 transform->minlen = transform->ivlen - transform->fixed_ivlen
564 + ( transform->ciphersuite_info->flags &
565 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100566 }
567 else
568 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200569 /* Initialize HMAC contexts */
570 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
571 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100572 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200573 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
574 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100575 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 /* Get MAC length */
578 transform->maclen = md_get_size( md_info );
579
580#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
581 /*
582 * If HMAC is to be truncated, we shall keep the leftmost bytes,
583 * (rfc 6066 page 13 or rfc 2104 section 4),
584 * so we only need to adjust the length here.
585 */
586 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
587 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
588#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
589
590 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100591 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200593 /* Minimum length */
594 if( cipher_info->mode == POLARSSL_MODE_STREAM )
595 transform->minlen = transform->maclen;
596 else
Paul Bakker68884e32013-01-07 18:20:04 +0100597 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200598 /*
599 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100600 * 1. if EtM is in use: one block plus MAC
601 * otherwise: * first multiple of blocklen greater than maclen
602 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200603 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100604#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
605 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
606 {
607 transform->minlen = transform->maclen
608 + cipher_info->block_size;
609 }
610 else
611#endif
612 {
613 transform->minlen = transform->maclen
614 + cipher_info->block_size
615 - transform->maclen % cipher_info->block_size;
616 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200617
618#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
619 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
620 ssl->minor_ver == SSL_MINOR_VERSION_1 )
621 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100622 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200623#endif
624#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
625 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
626 ssl->minor_ver == SSL_MINOR_VERSION_3 )
627 {
628 transform->minlen += transform->ivlen;
629 }
630 else
631#endif
632 {
633 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
634 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
635 }
Paul Bakker68884e32013-01-07 18:20:04 +0100636 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 }
638
639 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000640 transform->keylen, transform->minlen, transform->ivlen,
641 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
643 /*
644 * Finally setup the cipher contexts, IVs and MAC secrets.
645 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100646#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 if( ssl->endpoint == SSL_IS_CLIENT )
648 {
Paul Bakker48916f92012-09-16 19:57:18 +0000649 key1 = keyblk + transform->maclen * 2;
650 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Paul Bakker68884e32013-01-07 18:20:04 +0100652 mac_enc = keyblk;
653 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000655 /*
656 * This is not used in TLS v1.1.
657 */
Paul Bakker48916f92012-09-16 19:57:18 +0000658 iv_copy_len = ( transform->fixed_ivlen ) ?
659 transform->fixed_ivlen : transform->ivlen;
660 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
661 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000662 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 }
664 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100665#endif /* POLARSSL_SSL_CLI_C */
666#if defined(POLARSSL_SSL_SRV_C)
667 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 {
Paul Bakker48916f92012-09-16 19:57:18 +0000669 key1 = keyblk + transform->maclen * 2 + transform->keylen;
670 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Paul Bakker68884e32013-01-07 18:20:04 +0100672 mac_enc = keyblk + transform->maclen;
673 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000675 /*
676 * This is not used in TLS v1.1.
677 */
Paul Bakker48916f92012-09-16 19:57:18 +0000678 iv_copy_len = ( transform->fixed_ivlen ) ?
679 transform->fixed_ivlen : transform->ivlen;
680 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
681 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000682 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100684 else
685#endif /* POLARSSL_SSL_SRV_C */
686 {
687 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
688 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
689 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200691#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100692 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
693 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100694 if( transform->maclen > sizeof transform->mac_enc )
695 {
696 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200697 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100698 }
699
Paul Bakker68884e32013-01-07 18:20:04 +0100700 memcpy( transform->mac_enc, mac_enc, transform->maclen );
701 memcpy( transform->mac_dec, mac_dec, transform->maclen );
702 }
703 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200704#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200705#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
706 defined(POLARSSL_SSL_PROTO_TLS1_2)
707 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100708 {
709 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
710 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
711 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200712 else
713#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200714 {
715 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200716 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200717 }
Paul Bakker68884e32013-01-07 18:20:04 +0100718
Paul Bakker05ef8352012-05-08 09:17:57 +0000719#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200720 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000721 {
722 int ret = 0;
723
724 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
725
Paul Bakker07eb38b2012-12-19 14:42:06 +0100726 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
727 transform->iv_enc, transform->iv_dec,
728 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100729 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100730 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000731 {
732 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200733 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000734 }
735 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200736#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000737
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200738 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
739 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200741 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
742 return( ret );
743 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200744
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200745 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
746 cipher_info ) ) != 0 )
747 {
748 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
749 return( ret );
750 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200751
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200752 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
753 cipher_info->key_length,
754 POLARSSL_ENCRYPT ) ) != 0 )
755 {
756 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
757 return( ret );
758 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200759
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200760 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
761 cipher_info->key_length,
762 POLARSSL_DECRYPT ) ) != 0 )
763 {
764 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
765 return( ret );
766 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200767
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200768#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200769 if( cipher_info->mode == POLARSSL_MODE_CBC )
770 {
771 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
772 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200773 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200774 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
775 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200776 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200777
778 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
779 POLARSSL_PADDING_NONE ) ) != 0 )
780 {
781 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
782 return( ret );
783 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000784 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200785#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Paul Bakker34617722014-06-13 17:20:13 +0200787 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker2770fbd2012-07-03 13:30:23 +0000789#if defined(POLARSSL_ZLIB_SUPPORT)
790 // Initialize compression
791 //
Paul Bakker48916f92012-09-16 19:57:18 +0000792 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000793 {
Paul Bakker16770332013-10-11 09:59:44 +0200794 if( ssl->compress_buf == NULL )
795 {
796 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
797 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
798 if( ssl->compress_buf == NULL )
799 {
800 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
801 SSL_BUFFER_LEN ) );
802 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
803 }
804 }
805
Paul Bakker2770fbd2012-07-03 13:30:23 +0000806 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
807
Paul Bakker48916f92012-09-16 19:57:18 +0000808 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
809 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000810
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200811 if( deflateInit( &transform->ctx_deflate,
812 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000813 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000814 {
815 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
816 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
817 }
818 }
819#endif /* POLARSSL_ZLIB_SUPPORT */
820
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
822
823 return( 0 );
824}
825
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200826#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000827void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000828{
829 md5_context md5;
830 sha1_context sha1;
831 unsigned char pad_1[48];
832 unsigned char pad_2[48];
833
Paul Bakker380da532012-04-18 16:10:25 +0000834 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Paul Bakker48916f92012-09-16 19:57:18 +0000836 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
837 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Paul Bakker380da532012-04-18 16:10:25 +0000839 memset( pad_1, 0x36, 48 );
840 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Paul Bakker48916f92012-09-16 19:57:18 +0000842 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000843 md5_update( &md5, pad_1, 48 );
844 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Paul Bakker380da532012-04-18 16:10:25 +0000846 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000847 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000848 md5_update( &md5, pad_2, 48 );
849 md5_update( &md5, hash, 16 );
850 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Paul Bakker48916f92012-09-16 19:57:18 +0000852 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000853 sha1_update( &sha1, pad_1, 40 );
854 sha1_finish( &sha1, hash + 16 );
855
856 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000857 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000858 sha1_update( &sha1, pad_2, 40 );
859 sha1_update( &sha1, hash + 16, 20 );
860 sha1_finish( &sha1, hash + 16 );
861
862 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
863 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
864
Paul Bakker5b4af392014-06-26 12:09:34 +0200865 md5_free( &md5 );
866 sha1_free( &sha1 );
867
Paul Bakker380da532012-04-18 16:10:25 +0000868 return;
869}
Paul Bakker9af723c2014-05-01 13:03:14 +0200870#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000871
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000873void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
874{
875 md5_context md5;
876 sha1_context sha1;
877
878 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
879
Paul Bakker48916f92012-09-16 19:57:18 +0000880 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
881 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000882
Paul Bakker48916f92012-09-16 19:57:18 +0000883 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000884 sha1_finish( &sha1, hash + 16 );
885
886 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
887 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
888
Paul Bakker5b4af392014-06-26 12:09:34 +0200889 md5_free( &md5 );
890 sha1_free( &sha1 );
891
Paul Bakker380da532012-04-18 16:10:25 +0000892 return;
893}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200894#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000895
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200896#if defined(POLARSSL_SSL_PROTO_TLS1_2)
897#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000898void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
899{
Paul Bakker9e36f042013-06-30 14:34:05 +0200900 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000901
902 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
903
Paul Bakker9e36f042013-06-30 14:34:05 +0200904 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
905 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000906
907 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
908 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
909
Paul Bakker5b4af392014-06-26 12:09:34 +0200910 sha256_free( &sha256 );
911
Paul Bakker380da532012-04-18 16:10:25 +0000912 return;
913}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200914#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000915
Paul Bakker9e36f042013-06-30 14:34:05 +0200916#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000917void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
918{
Paul Bakker9e36f042013-06-30 14:34:05 +0200919 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000920
921 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
922
Paul Bakker9e36f042013-06-30 14:34:05 +0200923 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
924 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000925
Paul Bakkerca4ab492012-04-18 14:23:57 +0000926 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
928
Paul Bakker5b4af392014-06-26 12:09:34 +0200929 sha512_free( &sha512 );
930
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 return;
932}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200933#endif /* POLARSSL_SHA512_C */
934#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200936#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
938{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200939 unsigned char *p = ssl->handshake->premaster;
940 unsigned char *end = p + sizeof( ssl->handshake->premaster );
941
942 /*
943 * PMS = struct {
944 * opaque other_secret<0..2^16-1>;
945 * opaque psk<0..2^16-1>;
946 * };
947 * with "other_secret" depending on the particular key exchange
948 */
949#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
950 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
951 {
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200952 if( end - p < 2 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200953 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
954
955 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
956 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +0200957
958 if( end < p || (size_t)( end - p ) < ssl->psk_len )
959 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
960
961 memset( p, 0, ssl->psk_len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962 p += ssl->psk_len;
963 }
964 else
965#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200966#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
967 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
968 {
969 /*
970 * other_secret already set by the ClientKeyExchange message,
971 * and is 48 bytes long
972 */
973 *p++ = 0;
974 *p++ = 48;
975 p += 48;
976 }
977 else
978#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200979#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
980 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
981 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200982 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200983 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200984
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200985 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200986 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200987 p + 2, &len,
988 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200989 {
990 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
991 return( ret );
992 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200993 *(p++) = (unsigned char)( len >> 8 );
994 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200995 p += len;
996
997 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
998 }
999 else
1000#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1001#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1002 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1003 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001004 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001005 size_t zlen;
1006
1007 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001008 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001009 ssl->f_rng, ssl->p_rng ) ) != 0 )
1010 {
1011 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1012 return( ret );
1013 }
1014
1015 *(p++) = (unsigned char)( zlen >> 8 );
1016 *(p++) = (unsigned char)( zlen );
1017 p += zlen;
1018
1019 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1020 }
1021 else
1022#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1023 {
1024 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001025 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001026 }
1027
1028 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001029 if( end - p < 2 )
1030 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001031
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001032 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1033 *(p++) = (unsigned char)( ssl->psk_len );
Manuel Pégourié-Gonnard5ca36402015-10-21 12:35:29 +02001034
1035 if( end < p || (size_t)( end - p ) < ssl->psk_len )
1036 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1037
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001038 memcpy( p, ssl->psk, ssl->psk_len );
1039 p += ssl->psk_len;
1040
1041 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1042
1043 return( 0 );
1044}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001045#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001046
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001047#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001048/*
1049 * SSLv3.0 MAC functions
1050 */
Paul Bakker68884e32013-01-07 18:20:04 +01001051static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1052 unsigned char *buf, size_t len,
1053 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001054{
1055 unsigned char header[11];
1056 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001057 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001058 int md_size = md_get_size( md_ctx->md_info );
1059 int md_type = md_get_type( md_ctx->md_info );
1060
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001061 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001062 if( md_type == POLARSSL_MD_MD5 )
1063 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001064 else
Paul Bakker68884e32013-01-07 18:20:04 +01001065 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001066
1067 memcpy( header, ctr, 8 );
1068 header[ 8] = (unsigned char) type;
1069 header[ 9] = (unsigned char)( len >> 8 );
1070 header[10] = (unsigned char)( len );
1071
Paul Bakker68884e32013-01-07 18:20:04 +01001072 memset( padding, 0x36, padlen );
1073 md_starts( md_ctx );
1074 md_update( md_ctx, secret, md_size );
1075 md_update( md_ctx, padding, padlen );
1076 md_update( md_ctx, header, 11 );
1077 md_update( md_ctx, buf, len );
1078 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Paul Bakker68884e32013-01-07 18:20:04 +01001080 memset( padding, 0x5C, padlen );
1081 md_starts( md_ctx );
1082 md_update( md_ctx, secret, md_size );
1083 md_update( md_ctx, padding, padlen );
1084 md_update( md_ctx, buf + len, md_size );
1085 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001086}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001087#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001088
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001089#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1090 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1091 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1092#define POLARSSL_SOME_MODES_USE_MAC
1093#endif
1094
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001095/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001097 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001098static int ssl_encrypt_buf( ssl_context *ssl )
1099{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001100 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001101 cipher_mode_t mode;
1102 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
1104 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1105
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001106 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1107 {
1108 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1109 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1110 }
1111
1112 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1113
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001114 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1115 ssl->out_msg, ssl->out_msglen );
1116
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001118 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001120#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001121 if( mode == POLARSSL_MODE_STREAM ||
1122 ( mode == POLARSSL_MODE_CBC
1123#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1124 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1125#endif
1126 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001128#if defined(POLARSSL_SSL_PROTO_SSL3)
1129 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1130 {
1131 ssl_mac( &ssl->transform_out->md_ctx_enc,
1132 ssl->transform_out->mac_enc,
1133 ssl->out_msg, ssl->out_msglen,
1134 ssl->out_ctr, ssl->out_msgtype );
1135 }
1136 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001137#endif
1138#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001139 defined(POLARSSL_SSL_PROTO_TLS1_2)
1140 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1141 {
1142 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1143 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1144 ssl->out_msg, ssl->out_msglen );
1145 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1146 ssl->out_msg + ssl->out_msglen );
1147 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1148 }
1149 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001150#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001151 {
1152 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001153 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001154 }
1155
1156 SSL_DEBUG_BUF( 4, "computed mac",
1157 ssl->out_msg + ssl->out_msglen,
1158 ssl->transform_out->maclen );
1159
1160 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001161 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001162 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001163#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001165 /*
1166 * Encrypt
1167 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001168#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001169 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001171 int ret;
1172 size_t olen = 0;
1173
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1175 "including %d bytes of padding",
1176 ssl->out_msglen, 0 ) );
1177
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001178 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001179 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001180 ssl->transform_out->ivlen,
1181 ssl->out_msg, ssl->out_msglen,
1182 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001183 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001184 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001185 return( ret );
1186 }
1187
1188 if( ssl->out_msglen != olen )
1189 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001190 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001191 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 }
Paul Bakker68884e32013-01-07 18:20:04 +01001194 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001195#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001196#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1197 if( mode == POLARSSL_MODE_GCM ||
1198 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001199 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001200 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001201 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202 unsigned char *enc_msg;
1203 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001204 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1205 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001206
Paul Bakkerca4ab492012-04-18 14:23:57 +00001207 memcpy( add_data, ssl->out_ctr, 8 );
1208 add_data[8] = ssl->out_msgtype;
1209 add_data[9] = ssl->major_ver;
1210 add_data[10] = ssl->minor_ver;
1211 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1212 add_data[12] = ssl->out_msglen & 0xFF;
1213
1214 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1215 add_data, 13 );
1216
Paul Bakker68884e32013-01-07 18:20:04 +01001217 /*
1218 * Generate IV
1219 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001220 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1221 {
1222 /* Reminder if we ever add an AEAD mode with a different size */
1223 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1224 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1225 }
1226
1227 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1228 ssl->out_ctr, 8 );
1229 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001230
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001231 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001232 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001233
Paul Bakker68884e32013-01-07 18:20:04 +01001234 /*
1235 * Fix pointer positions and message length with added IV
1236 */
1237 enc_msg = ssl->out_msg;
1238 enc_msglen = ssl->out_msglen;
1239 ssl->out_msglen += ssl->transform_out->ivlen -
1240 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001241
Paul Bakker68884e32013-01-07 18:20:04 +01001242 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1243 "including %d bytes of padding",
1244 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001245
Paul Bakker68884e32013-01-07 18:20:04 +01001246 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001247 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001248 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001249 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1250 ssl->transform_out->iv_enc,
1251 ssl->transform_out->ivlen,
1252 add_data, 13,
1253 enc_msg, enc_msglen,
1254 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001255 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001256 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001257 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001258 return( ret );
1259 }
1260
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001261 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001262 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001263 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001264 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001265 }
1266
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001267 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001268 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001269
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001270 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001271 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001273#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001274#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1275 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001276 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001278 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001279 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001280 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001281
Paul Bakker48916f92012-09-16 19:57:18 +00001282 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1283 ssl->transform_out->ivlen;
1284 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 padlen = 0;
1286
1287 for( i = 0; i <= padlen; i++ )
1288 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1289
1290 ssl->out_msglen += padlen + 1;
1291
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001292 enc_msglen = ssl->out_msglen;
1293 enc_msg = ssl->out_msg;
1294
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001295#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001296 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001297 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1298 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001299 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001300 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001301 {
1302 /*
1303 * Generate IV
1304 */
Manuel Pégourié-Gonnarda67fd792015-08-27 12:02:40 +02001305 ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001306 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001307 if( ret != 0 )
1308 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001309
Paul Bakker92be97b2013-01-02 17:30:03 +01001310 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001311 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001312
1313 /*
1314 * Fix pointer positions and message length with added IV
1315 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001316 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001317 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001318 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001319 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001320#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001321
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001323 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001324 ssl->out_msglen, ssl->transform_out->ivlen,
1325 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001327 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001328 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001329 ssl->transform_out->ivlen,
1330 enc_msg, enc_msglen,
1331 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001332 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001333 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001334 return( ret );
1335 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001336
Paul Bakkercca5b812013-08-31 17:40:26 +02001337 if( enc_msglen != olen )
1338 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001339 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001340 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001341 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001342
1343#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001344 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1345 {
1346 /*
1347 * Save IV in SSL3 and TLS1
1348 */
1349 memcpy( ssl->transform_out->iv_enc,
1350 ssl->transform_out->cipher_ctx_enc.iv,
1351 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001353#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001354
1355#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001356 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001357 {
1358 /*
1359 * MAC(MAC_write_key, seq_num +
1360 * TLSCipherText.type +
1361 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001362 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001363 * IV + // except for TLS 1.0
1364 * ENC(content + padding + padding_length));
1365 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001366 unsigned char pseudo_hdr[13];
1367
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001368 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1369
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001370 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1371 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001372 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1373 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1374
1375 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001376
1377 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1378 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1379 ssl->out_iv, ssl->out_msglen );
1380 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1381 ssl->out_iv + ssl->out_msglen );
1382 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1383
1384 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001385 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001386 }
1387#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001389 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001390#endif /* POLARSSL_CIPHER_MODE_CBC &&
1391 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001392 {
1393 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001394 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001397 /* Make extra sure authentication was performed, exactly once */
1398 if( auth_done != 1 )
1399 {
1400 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1401 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1402 }
1403
Paul Bakkerca4ab492012-04-18 14:23:57 +00001404 for( i = 8; i > 0; i-- )
1405 if( ++ssl->out_ctr[i - 1] != 0 )
1406 break;
1407
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001408 /* The loops goes to its end iff the counter is wrapping */
1409 if( i == 0 )
1410 {
1411 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1412 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1413 }
1414
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1416
1417 return( 0 );
1418}
1419
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001420#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001421
Paul Bakker5121ce52009-01-03 21:22:43 +00001422static int ssl_decrypt_buf( ssl_context *ssl )
1423{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001424 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001425 cipher_mode_t mode;
1426 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001427#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001428 size_t padlen = 0, correct = 1;
1429#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
1431 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1432
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001433 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1434 {
1435 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1436 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1437 }
1438
1439 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1440
Paul Bakker48916f92012-09-16 19:57:18 +00001441 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 {
1443 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001444 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001445 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 }
1447
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001448#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001449 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001450 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001451 int ret;
1452 size_t olen = 0;
1453
Paul Bakker68884e32013-01-07 18:20:04 +01001454 padlen = 0;
1455
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001456 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001457 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001458 ssl->transform_in->ivlen,
1459 ssl->in_msg, ssl->in_msglen,
1460 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001461 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001462 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001463 return( ret );
1464 }
1465
1466 if( ssl->in_msglen != olen )
1467 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001468 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001469 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001470 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 }
Paul Bakker68884e32013-01-07 18:20:04 +01001472 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001473#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001474#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1475 if( mode == POLARSSL_MODE_GCM ||
1476 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001477 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001478 int ret;
1479 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001480 unsigned char *dec_msg;
1481 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001482 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001483 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1484 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001485 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1486 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001487
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001488 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001489 {
1490 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1491 "+ taglen (%d)", ssl->in_msglen,
1492 explicit_iv_len, taglen ) );
1493 return( POLARSSL_ERR_SSL_INVALID_MAC );
1494 }
1495 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1496
Paul Bakker68884e32013-01-07 18:20:04 +01001497 dec_msg = ssl->in_msg;
1498 dec_msg_result = ssl->in_msg;
1499 ssl->in_msglen = dec_msglen;
1500
1501 memcpy( add_data, ssl->in_ctr, 8 );
1502 add_data[8] = ssl->in_msgtype;
1503 add_data[9] = ssl->major_ver;
1504 add_data[10] = ssl->minor_ver;
1505 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1506 add_data[12] = ssl->in_msglen & 0xFF;
1507
1508 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1509 add_data, 13 );
1510
1511 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1512 ssl->in_iv,
1513 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1514
1515 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1516 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001517 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001518
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001519 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001520 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001521 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001522 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1523 ssl->transform_in->iv_dec,
1524 ssl->transform_in->ivlen,
1525 add_data, 13,
1526 dec_msg, dec_msglen,
1527 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001528 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001529 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001530 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1531
1532 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1533 return( POLARSSL_ERR_SSL_INVALID_MAC );
1534
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001535 return( ret );
1536 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001537 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001538
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001539 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001540 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001541 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001542 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001543 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001546#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001547#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1548 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001549 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001550 {
Paul Bakker45829992013-01-03 14:52:21 +01001551 /*
1552 * Decrypt and check the padding
1553 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001554 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001555 unsigned char *dec_msg;
1556 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001557 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001558 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001559 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001560
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 /*
Paul Bakker45829992013-01-03 14:52:21 +01001562 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001564#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001565 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1566 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001567#endif
Paul Bakker45829992013-01-03 14:52:21 +01001568
1569 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1570 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1571 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001572 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1573 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1574 ssl->transform_in->ivlen,
1575 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001576 return( POLARSSL_ERR_SSL_INVALID_MAC );
1577 }
1578
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001579 dec_msglen = ssl->in_msglen;
1580 dec_msg = ssl->in_msg;
1581 dec_msg_result = ssl->in_msg;
1582
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001583 /*
1584 * Authenticate before decrypt if enabled
1585 */
1586#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001587 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001588 {
1589 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001590 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001591
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001592 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1593
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001594 dec_msglen -= ssl->transform_in->maclen;
1595 ssl->in_msglen -= ssl->transform_in->maclen;
1596
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001597 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1598 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1599 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1600 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1601
1602 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1603
1604 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001605 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1606 ssl->in_iv, ssl->in_msglen );
1607 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1608 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1609
1610 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1611 ssl->transform_in->maclen );
1612 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1613 ssl->transform_in->maclen );
1614
1615 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1616 ssl->transform_in->maclen ) != 0 )
1617 {
1618 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1619
1620 return( POLARSSL_ERR_SSL_INVALID_MAC );
1621 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001622 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001623 }
1624#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1625
1626 /*
1627 * Check length sanity
1628 */
1629 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1630 {
1631 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1632 ssl->in_msglen, ssl->transform_in->ivlen ) );
1633 return( POLARSSL_ERR_SSL_INVALID_MAC );
1634 }
1635
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001636#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001637 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001638 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001639 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001640 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001641 {
Paul Bakker48916f92012-09-16 19:57:18 +00001642 dec_msglen -= ssl->transform_in->ivlen;
1643 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001644
Paul Bakker48916f92012-09-16 19:57:18 +00001645 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001646 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001647 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001648#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001649
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001650 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001651 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001652 ssl->transform_in->ivlen,
1653 dec_msg, dec_msglen,
1654 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001655 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001656 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001657 return( ret );
1658 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001659
Paul Bakkercca5b812013-08-31 17:40:26 +02001660 if( dec_msglen != olen )
1661 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001662 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001663 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001664 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001665
1666#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001667 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1668 {
1669 /*
1670 * Save IV in SSL3 and TLS1
1671 */
1672 memcpy( ssl->transform_in->iv_dec,
1673 ssl->transform_in->cipher_ctx_dec.iv,
1674 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001676#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
1678 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001679
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001680 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001681 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001682 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001683#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001684 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1685 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001686#endif
Paul Bakker45829992013-01-03 14:52:21 +01001687 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001688 correct = 0;
1689 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001691#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001692 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1693 {
Paul Bakker48916f92012-09-16 19:57:18 +00001694 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001695 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001696#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1698 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001699 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001700#endif
Paul Bakker45829992013-01-03 14:52:21 +01001701 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 }
1703 }
1704 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001705#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001706#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1707 defined(POLARSSL_SSL_PROTO_TLS1_2)
1708 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 {
1710 /*
Paul Bakker45829992013-01-03 14:52:21 +01001711 * TLSv1+: always check the padding up to the first failure
1712 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001713 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001714 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001715 size_t padding_idx = ssl->in_msglen - padlen - 1;
1716
Paul Bakker956c9e02013-12-19 14:42:28 +01001717 /*
1718 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001719 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001720 *
Paul Bakker61885c72014-04-25 12:59:03 +02001721 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1722 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001723 *
1724 * In both cases we reset padding_idx to a safe value (0) to
1725 * prevent out-of-buffer reads.
1726 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001727 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001728 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1729 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001730
1731 padding_idx *= correct;
1732
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001733 for( i = 1; i <= 256; i++ )
1734 {
1735 real_count &= ( i <= padlen );
1736 pad_count += real_count *
1737 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1738 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001739
1740 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001741
Paul Bakkerd66f0702013-01-31 16:57:45 +01001742#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001743 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001744 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001745#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001746 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001747 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001748 else
1749#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1750 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001751 {
1752 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001753 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001754 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001755
1756 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001758 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001759#endif /* POLARSSL_CIPHER_MODE_CBC &&
1760 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001761 {
1762 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001763 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001764 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
1766 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1767 ssl->in_msg, ssl->in_msglen );
1768
1769 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001770 * Authenticate if not done yet.
1771 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001773#if defined(POLARSSL_SOME_MODES_USE_MAC)
1774 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001775 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001776 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1777
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001778 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001780 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1781 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001783 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001785#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001786 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1787 {
1788 ssl_mac( &ssl->transform_in->md_ctx_dec,
1789 ssl->transform_in->mac_dec,
1790 ssl->in_msg, ssl->in_msglen,
1791 ssl->in_ctr, ssl->in_msgtype );
1792 }
1793 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001794#endif /* POLARSSL_SSL_PROTO_SSL3 */
1795#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001796 defined(POLARSSL_SSL_PROTO_TLS1_2)
1797 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1798 {
1799 /*
1800 * Process MAC and always update for padlen afterwards to make
1801 * total time independent of padlen
1802 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001803 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001804 *
1805 * Known timing attacks:
1806 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1807 *
1808 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1809 * correctly. (We round down instead of up, so -56 is the correct
1810 * value for our calculations instead of -55)
1811 */
1812 size_t j, extra_run = 0;
1813 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1814 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001815
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001816 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001817
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001818 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1819 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1820 ssl->in_msglen );
1821 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1822 ssl->in_msg + ssl->in_msglen );
Manuel Pégourié-Gonnard7d1e95c2015-04-29 01:35:48 +02001823 /* Call md_process at least once due to cache attacks */
1824 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001825 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001826
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001827 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1828 }
1829 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001830#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001831 POLARSSL_SSL_PROTO_TLS1_2 */
1832 {
1833 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001834 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001837 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1838 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1839 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001840
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001841 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001842 ssl->transform_in->maclen ) != 0 )
1843 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001844#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001845 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001846#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001847 correct = 0;
1848 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001849 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001851 /*
1852 * Finally check the correct flag
1853 */
1854 if( correct == 0 )
1855 return( POLARSSL_ERR_SSL_INVALID_MAC );
1856 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001857#endif /* POLARSSL_SOME_MODES_USE_MAC */
1858
1859 /* Make extra sure authentication was performed, exactly once */
1860 if( auth_done != 1 )
1861 {
1862 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1863 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1864 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001865
1866 if( ssl->in_msglen == 0 )
1867 {
1868 ssl->nb_zero++;
1869
1870 /*
1871 * Three or more empty messages may be a DoS attack
1872 * (excessive CPU consumption).
1873 */
1874 if( ssl->nb_zero > 3 )
1875 {
1876 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1877 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001878 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 }
1880 }
1881 else
1882 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001883
Paul Bakker23986e52011-04-24 08:57:21 +00001884 for( i = 8; i > 0; i-- )
1885 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 break;
1887
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001888 /* The loops goes to its end iff the counter is wrapping */
1889 if( i == 0 )
1890 {
1891 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1892 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1893 }
1894
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1896
1897 return( 0 );
1898}
1899
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001900#undef MAC_NONE
1901#undef MAC_PLAINTEXT
1902#undef MAC_CIPHERTEXT
1903
Paul Bakker2770fbd2012-07-03 13:30:23 +00001904#if defined(POLARSSL_ZLIB_SUPPORT)
1905/*
1906 * Compression/decompression functions
1907 */
1908static int ssl_compress_buf( ssl_context *ssl )
1909{
1910 int ret;
1911 unsigned char *msg_post = ssl->out_msg;
1912 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001913 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001914
1915 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1916
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001917 if( len_pre == 0 )
1918 return( 0 );
1919
Paul Bakker2770fbd2012-07-03 13:30:23 +00001920 memcpy( msg_pre, ssl->out_msg, len_pre );
1921
1922 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1923 ssl->out_msglen ) );
1924
1925 SSL_DEBUG_BUF( 4, "before compression: output payload",
1926 ssl->out_msg, ssl->out_msglen );
1927
Paul Bakker48916f92012-09-16 19:57:18 +00001928 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1929 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1930 ssl->transform_out->ctx_deflate.next_out = msg_post;
1931 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001932
Paul Bakker48916f92012-09-16 19:57:18 +00001933 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001934 if( ret != Z_OK )
1935 {
1936 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1937 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1938 }
1939
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001940 ssl->out_msglen = SSL_BUFFER_LEN -
1941 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001942
Paul Bakker2770fbd2012-07-03 13:30:23 +00001943 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1944 ssl->out_msglen ) );
1945
1946 SSL_DEBUG_BUF( 4, "after compression: output payload",
1947 ssl->out_msg, ssl->out_msglen );
1948
1949 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1950
1951 return( 0 );
1952}
1953
1954static int ssl_decompress_buf( ssl_context *ssl )
1955{
1956 int ret;
1957 unsigned char *msg_post = ssl->in_msg;
1958 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001959 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001960
1961 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1962
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001963 if( len_pre == 0 )
1964 return( 0 );
1965
Paul Bakker2770fbd2012-07-03 13:30:23 +00001966 memcpy( msg_pre, ssl->in_msg, len_pre );
1967
1968 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1969 ssl->in_msglen ) );
1970
1971 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1972 ssl->in_msg, ssl->in_msglen );
1973
Paul Bakker48916f92012-09-16 19:57:18 +00001974 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1975 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1976 ssl->transform_in->ctx_inflate.next_out = msg_post;
1977 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001978
Paul Bakker48916f92012-09-16 19:57:18 +00001979 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001980 if( ret != Z_OK )
1981 {
1982 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1983 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1984 }
1985
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001986 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1987 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001988
Paul Bakker2770fbd2012-07-03 13:30:23 +00001989 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1990 ssl->in_msglen ) );
1991
1992 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1993 ssl->in_msg, ssl->in_msglen );
1994
1995 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1996
1997 return( 0 );
1998}
1999#endif /* POLARSSL_ZLIB_SUPPORT */
2000
Paul Bakker5121ce52009-01-03 21:22:43 +00002001/*
2002 * Fill the input message buffer
2003 */
Paul Bakker23986e52011-04-24 08:57:21 +00002004int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002005{
Paul Bakker23986e52011-04-24 08:57:21 +00002006 int ret;
2007 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
2009 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2010
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002011 if( nb_want > SSL_BUFFER_LEN - 8 )
2012 {
2013 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2014 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2015 }
2016
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 while( ssl->in_left < nb_want )
2018 {
2019 len = nb_want - ssl->in_left;
2020 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2021
2022 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2023 ssl->in_left, nb_want ) );
2024 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2025
Paul Bakker831a7552011-05-18 13:32:51 +00002026 if( ret == 0 )
2027 return( POLARSSL_ERR_SSL_CONN_EOF );
2028
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 if( ret < 0 )
2030 return( ret );
2031
2032 ssl->in_left += ret;
2033 }
2034
2035 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2036
2037 return( 0 );
2038}
2039
2040/*
2041 * Flush any data not yet written
2042 */
2043int ssl_flush_output( ssl_context *ssl )
2044{
2045 int ret;
2046 unsigned char *buf;
2047
2048 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2049
2050 while( ssl->out_left > 0 )
2051 {
2052 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2053 5 + ssl->out_msglen, ssl->out_left ) );
2054
Paul Bakker5bd42292012-12-19 14:40:42 +01002055 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002057
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2059
2060 if( ret <= 0 )
2061 return( ret );
2062
2063 ssl->out_left -= ret;
2064 }
2065
2066 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2067
2068 return( 0 );
2069}
2070
2071/*
2072 * Record layer functions
2073 */
2074int ssl_write_record( ssl_context *ssl )
2075{
Paul Bakker05ef8352012-05-08 09:17:57 +00002076 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002077 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002078
2079 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2080
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2082 {
2083 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2084 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2085 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2086
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002087 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2088 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 }
2090
Paul Bakker2770fbd2012-07-03 13:30:23 +00002091#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002092 if( ssl->transform_out != NULL &&
2093 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002094 {
2095 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2096 {
2097 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2098 return( ret );
2099 }
2100
2101 len = ssl->out_msglen;
2102 }
2103#endif /*POLARSSL_ZLIB_SUPPORT */
2104
Paul Bakker05ef8352012-05-08 09:17:57 +00002105#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002106 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002108 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002109
Paul Bakker05ef8352012-05-08 09:17:57 +00002110 ret = ssl_hw_record_write( ssl );
2111 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2112 {
2113 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002114 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002115 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002116
2117 if( ret == 0 )
2118 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002119 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002120#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002121 if( !done )
2122 {
2123 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2124 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2125 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2127 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002128
Paul Bakker48916f92012-09-16 19:57:18 +00002129 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002130 {
2131 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2132 {
2133 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2134 return( ret );
2135 }
2136
2137 len = ssl->out_msglen;
2138 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2139 ssl->out_hdr[4] = (unsigned char)( len );
2140 }
2141
2142 ssl->out_left = 5 + ssl->out_msglen;
2143
2144 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2145 "version = [%d:%d], msglen = %d",
2146 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2147 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2148
2149 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002150 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 }
2152
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2154 {
2155 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2156 return( ret );
2157 }
2158
2159 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2160
2161 return( 0 );
2162}
2163
2164int ssl_read_record( ssl_context *ssl )
2165{
Paul Bakker05ef8352012-05-08 09:17:57 +00002166 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002167
2168 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2169
2170 if( ssl->in_hslen != 0 &&
2171 ssl->in_hslen < ssl->in_msglen )
2172 {
2173 /*
2174 * Get next Handshake message in the current record
2175 */
2176 ssl->in_msglen -= ssl->in_hslen;
2177
Paul Bakker8934a982011-08-05 11:11:53 +00002178 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002179 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002180
2181 ssl->in_hslen = 4;
2182 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2183
2184 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2185 " %d, type = %d, hslen = %d",
2186 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2187
2188 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2189 {
2190 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002191 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 }
2193
2194 if( ssl->in_msglen < ssl->in_hslen )
2195 {
2196 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002197 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
2199
Paul Bakker4224bc02014-04-08 14:36:50 +02002200 if( ssl->state != SSL_HANDSHAKE_OVER )
2201 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
2203 return( 0 );
2204 }
2205
2206 ssl->in_hslen = 0;
2207
2208 /*
2209 * Read the record header and validate it
2210 */
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002211read_record_header:
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2213 {
2214 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2215 return( ret );
2216 }
2217
2218 ssl->in_msgtype = ssl->in_hdr[0];
2219 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2220
2221 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2222 "version = [%d:%d], msglen = %d",
2223 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2224 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2225
2226 if( ssl->in_hdr[1] != ssl->major_ver )
2227 {
2228 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002229 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 }
2231
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002232 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 {
2234 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002235 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 }
2237
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002238 /* Sanity check (outer boundaries) */
2239 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2240 {
2241 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2242 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2243 }
2244
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002246 * Make sure the message length is acceptable for the current transform
2247 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 */
Paul Bakker48916f92012-09-16 19:57:18 +00002249 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002251 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 {
2253 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002254 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
2256 }
2257 else
2258 {
Paul Bakker48916f92012-09-16 19:57:18 +00002259 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 {
2261 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002262 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 }
2264
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002265#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002267 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 {
2269 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002270 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002274#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2275 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 /*
2277 * TLS encrypted messages can have up to 256 bytes of padding
2278 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002279 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002280 ssl->in_msglen > ssl->transform_in->minlen +
2281 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 {
2283 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002284 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002286#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
2288
2289 /*
2290 * Read and optionally decrypt the message contents
2291 */
2292 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2293 {
2294 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2295 return( ret );
2296 }
2297
2298 SSL_DEBUG_BUF( 4, "input record from network",
2299 ssl->in_hdr, 5 + ssl->in_msglen );
2300
Paul Bakker05ef8352012-05-08 09:17:57 +00002301#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002302 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002303 {
2304 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2305
2306 ret = ssl_hw_record_read( ssl );
2307 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2308 {
2309 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002310 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002311 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002312
2313 if( ret == 0 )
2314 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002315 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002316#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002317 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 {
2319 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2320 {
Paul Bakker40865c82013-01-31 17:13:13 +01002321#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2322 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2323 {
2324 ssl_send_alert_message( ssl,
2325 SSL_ALERT_LEVEL_FATAL,
2326 SSL_ALERT_MSG_BAD_RECORD_MAC );
2327 }
2328#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2330 return( ret );
2331 }
2332
2333 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2334 ssl->in_msg, ssl->in_msglen );
2335
2336 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2337 {
2338 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002339 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 }
2341 }
2342
Paul Bakker2770fbd2012-07-03 13:30:23 +00002343#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002344 if( ssl->transform_in != NULL &&
2345 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346 {
2347 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2348 {
2349 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2350 return( ret );
2351 }
2352
2353 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2354 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2355 }
2356#endif /* POLARSSL_ZLIB_SUPPORT */
2357
Paul Bakker0a925182012-04-16 06:46:41 +00002358 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2359 ssl->in_msgtype != SSL_MSG_ALERT &&
2360 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2361 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2362 {
2363 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2364
Paul Bakker48916f92012-09-16 19:57:18 +00002365 if( ( ret = ssl_send_alert_message( ssl,
2366 SSL_ALERT_LEVEL_FATAL,
2367 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002368 {
2369 return( ret );
2370 }
2371
2372 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2373 }
2374
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2376 {
2377 ssl->in_hslen = 4;
2378 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2379
2380 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2381 " %d, type = %d, hslen = %d",
2382 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2383
2384 /*
2385 * Additional checks to validate the handshake header
2386 */
2387 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2388 {
2389 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002390 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 }
2392
2393 if( ssl->in_msglen < ssl->in_hslen )
2394 {
2395 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002396 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 }
2398
Paul Bakker48916f92012-09-16 19:57:18 +00002399 if( ssl->state != SSL_HANDSHAKE_OVER )
2400 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 }
2402
2403 if( ssl->in_msgtype == SSL_MSG_ALERT )
2404 {
2405 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2406 ssl->in_msg[0], ssl->in_msg[1] ) );
2407
2408 /*
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002409 * Ignore non-fatal alerts, except close_notify and no_renego
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002411 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002413 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2414 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002415 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 }
2417
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002418 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2419 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 {
2421 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002422 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 }
Manuel Pégourié-Gonnard0aaefce2015-10-27 13:42:11 +01002424
2425 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2426 ssl->in_msg[1] == SSL_ALERT_MSG_NO_RENEGOTIATION )
2427 {
2428 SSL_DEBUG_MSG( 2, ( "is a no_renegotiation" ) );
2429 /* Will be handled when trying to parse ServerHello */
2430 ssl->in_left = 0;
2431 return( 0 );
2432 }
2433
2434 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
2435 ssl->endpoint == SSL_IS_SERVER &&
2436 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2437 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
2438 {
2439 SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
2440 /* Will be handled in ssl_parse_certificate() */
2441 ssl->in_left = 0;
2442 return( 0 );
2443 }
2444
2445 /* Silently discard: fetch new message */
2446 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
2448
2449 ssl->in_left = 0;
2450
2451 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2452
2453 return( 0 );
2454}
2455
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002456int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2457{
2458 int ret;
2459
2460 if( ( ret = ssl_send_alert_message( ssl,
2461 SSL_ALERT_LEVEL_FATAL,
2462 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2463 {
2464 return( ret );
2465 }
2466
2467 return( 0 );
2468}
2469
Paul Bakker0a925182012-04-16 06:46:41 +00002470int ssl_send_alert_message( ssl_context *ssl,
2471 unsigned char level,
2472 unsigned char message )
2473{
2474 int ret;
2475
2476 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2477
2478 ssl->out_msgtype = SSL_MSG_ALERT;
2479 ssl->out_msglen = 2;
2480 ssl->out_msg[0] = level;
2481 ssl->out_msg[1] = message;
2482
2483 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2484 {
2485 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2486 return( ret );
2487 }
2488
2489 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2490
2491 return( 0 );
2492}
2493
Paul Bakker5121ce52009-01-03 21:22:43 +00002494/*
2495 * Handshake functions
2496 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002497#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2498 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2499 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2500 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2501 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2502 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2503 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002504int ssl_write_certificate( ssl_context *ssl )
2505{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002506 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
2508 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2509
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002510 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002511 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2512 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002513 {
2514 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2515 ssl->state++;
2516 return( 0 );
2517 }
2518
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002519 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2520 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002521}
2522
2523int ssl_parse_certificate( ssl_context *ssl )
2524{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002525 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2526
2527 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2528
2529 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002530 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2531 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002532 {
2533 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2534 ssl->state++;
2535 return( 0 );
2536 }
2537
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002538 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2539 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002540}
2541#else
2542int ssl_write_certificate( ssl_context *ssl )
2543{
2544 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2545 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002546 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002547 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2548
2549 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2550
2551 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002552 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2553 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002554 {
2555 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2556 ssl->state++;
2557 return( 0 );
2558 }
2559
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002560#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 if( ssl->endpoint == SSL_IS_CLIENT )
2562 {
2563 if( ssl->client_auth == 0 )
2564 {
2565 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2566 ssl->state++;
2567 return( 0 );
2568 }
2569
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002570#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 /*
2572 * If using SSLv3 and got no cert, send an Alert message
2573 * (otherwise an empty Certificate message will be sent).
2574 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002575 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2577 {
2578 ssl->out_msglen = 2;
2579 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002580 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2581 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
2583 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2584 goto write_msg;
2585 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002586#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002588#endif /* POLARSSL_SSL_CLI_C */
2589#if defined(POLARSSL_SSL_SRV_C)
2590 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002592 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 {
2594 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002595 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 }
2597 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002598#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002600 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
2602 /*
2603 * 0 . 0 handshake type
2604 * 1 . 3 handshake length
2605 * 4 . 6 length of all certs
2606 * 7 . 9 length of cert. 1
2607 * 10 . n-1 peer certificate
2608 * n . n+2 length of cert. 2
2609 * n+3 . ... upper level cert, etc.
2610 */
2611 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002612 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002613
Paul Bakker29087132010-03-21 21:03:34 +00002614 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 {
2616 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002617 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 {
2619 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2620 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002621 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 }
2623
2624 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2625 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2626 ssl->out_msg[i + 2] = (unsigned char)( n );
2627
2628 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2629 i += n; crt = crt->next;
2630 }
2631
2632 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2633 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2634 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2635
2636 ssl->out_msglen = i;
2637 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2638 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2639
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002640#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002641write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002642#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
2644 ssl->state++;
2645
2646 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2647 {
2648 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2649 return( ret );
2650 }
2651
2652 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2653
Paul Bakkered27a042013-04-18 22:46:23 +02002654 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655}
2656
2657int ssl_parse_certificate( ssl_context *ssl )
2658{
Paul Bakkered27a042013-04-18 22:46:23 +02002659 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002660 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002661 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
2663 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2664
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002665 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002666 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2667 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002668 {
2669 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2670 ssl->state++;
2671 return( 0 );
2672 }
2673
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002674#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002676 ( ssl->authmode == SSL_VERIFY_NONE ||
2677 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002679 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2681 ssl->state++;
2682 return( 0 );
2683 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002684#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
2686 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2687 {
2688 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2689 return( ret );
2690 }
2691
2692 ssl->state++;
2693
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002694#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002695#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 /*
2697 * Check if the client sent an empty certificate
2698 */
2699 if( ssl->endpoint == SSL_IS_SERVER &&
2700 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2701 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002702 if( ssl->in_msglen == 2 &&
2703 ssl->in_msgtype == SSL_MSG_ALERT &&
2704 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2705 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 {
2707 SSL_DEBUG_MSG( 1, ( "SSLv3 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_OPTIONAL )
2711 return( 0 );
2712 else
Paul Bakker40e46942009-01-03 21:51:57 +00002713 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 }
2715 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002716#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002718#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2719 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 if( ssl->endpoint == SSL_IS_SERVER &&
2721 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2722 {
2723 if( ssl->in_hslen == 7 &&
2724 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2725 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2726 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2727 {
2728 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2729
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002730 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002732 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 else
2734 return( 0 );
2735 }
2736 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002737#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2738 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002739#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
2741 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2742 {
2743 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002744 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 }
2746
2747 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2748 {
2749 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002750 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 }
2752
2753 /*
2754 * Same message structure as in ssl_write_certificate()
2755 */
2756 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2757
2758 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2759 {
2760 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002761 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002762 }
2763
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002764 /* In case we tried to reuse a session but it failed */
2765 if( ssl->session_negotiate->peer_cert != NULL )
2766 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002767 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002768 polarssl_free( ssl->session_negotiate->peer_cert );
2769 }
2770
Mansour Moufidc531b4a2015-02-15 17:35:38 -05002771 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002772 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 {
2774 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002775 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002776 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 }
2778
Paul Bakkerb6b09562013-09-18 14:17:41 +02002779 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
2781 i = 7;
2782
2783 while( i < ssl->in_hslen )
2784 {
2785 if( ssl->in_msg[i] != 0 )
2786 {
2787 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002788 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 }
2790
2791 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2792 | (unsigned int) ssl->in_msg[i + 2];
2793 i += 3;
2794
2795 if( n < 128 || i + n > ssl->in_hslen )
2796 {
2797 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002798 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002799 }
2800
Paul Bakkerddf26b42013-09-18 13:46:23 +02002801 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2802 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 if( ret != 0 )
2804 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002805 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 return( ret );
2807 }
2808
2809 i += n;
2810 }
2811
Paul Bakker48916f92012-09-16 19:57:18 +00002812 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002814 /*
2815 * On client, make sure the server cert doesn't change during renego to
2816 * avoid "triple handshake" attack: https://secure-resumption.com/
2817 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002818#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002819 if( ssl->endpoint == SSL_IS_CLIENT &&
2820 ssl->renegotiation == SSL_RENEGOTIATION )
2821 {
2822 if( ssl->session->peer_cert == NULL )
2823 {
2824 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2825 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2826 }
2827
2828 if( ssl->session->peer_cert->raw.len !=
2829 ssl->session_negotiate->peer_cert->raw.len ||
2830 memcmp( ssl->session->peer_cert->raw.p,
2831 ssl->session_negotiate->peer_cert->raw.p,
2832 ssl->session->peer_cert->raw.len ) != 0 )
2833 {
2834 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2835 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2836 }
2837 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002838#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002839
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 if( ssl->authmode != SSL_VERIFY_NONE )
2841 {
2842 if( ssl->ca_chain == NULL )
2843 {
2844 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002845 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002846 }
2847
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002848 /*
2849 * Main check: verify certificate
2850 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002851 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2852 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2853 &ssl->session_negotiate->verify_result,
2854 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
2856 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002857 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002858 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002859 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002860
2861 /*
2862 * Secondary checks: always done, but change 'ret' only if it was 0
2863 */
2864
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002865#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002866 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002867 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002868
2869 /* If certificate uses an EC key, make sure the curve is OK */
2870 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2871 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2872 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002873 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002874 if( ret == 0 )
2875 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002876 }
2877 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002878#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002879
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002880 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2881 ciphersuite_info,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02002882 ! ssl->endpoint,
2883 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002884 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002885 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002886 if( ret == 0 )
2887 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2888 }
2889
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2891 ret = 0;
2892 }
2893
2894 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2895
2896 return( ret );
2897}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002898#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2899 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2900 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2901 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2902 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2903 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2904 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
2906int ssl_write_change_cipher_spec( ssl_context *ssl )
2907{
2908 int ret;
2909
2910 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2911
2912 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2913 ssl->out_msglen = 1;
2914 ssl->out_msg[0] = 1;
2915
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 ssl->state++;
2917
2918 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2919 {
2920 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2921 return( ret );
2922 }
2923
2924 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2925
2926 return( 0 );
2927}
2928
2929int ssl_parse_change_cipher_spec( ssl_context *ssl )
2930{
2931 int ret;
2932
2933 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2934
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2936 {
2937 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2938 return( ret );
2939 }
2940
2941 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2942 {
2943 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002944 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 }
2946
2947 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2948 {
2949 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002950 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002951 }
2952
2953 ssl->state++;
2954
2955 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2956
2957 return( 0 );
2958}
2959
Paul Bakker41c83d32013-03-20 14:39:14 +01002960void ssl_optimize_checksum( ssl_context *ssl,
2961 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002962{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002963 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002964
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002965#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2966 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002967 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002968 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002969 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002970#endif
2971#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2972#if defined(POLARSSL_SHA512_C)
2973 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2974 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2975 else
2976#endif
2977#if defined(POLARSSL_SHA256_C)
2978 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002979 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002980 else
2981#endif
2982#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002983 {
2984 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002985 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002986 }
Paul Bakker380da532012-04-18 16:10:25 +00002987}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002988
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002989static void ssl_update_checksum_start( ssl_context *ssl,
2990 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002991{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002992#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2993 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002994 md5_update( &ssl->handshake->fin_md5 , buf, len );
2995 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002996#endif
2997#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2998#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002999 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003000#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003001#if defined(POLARSSL_SHA512_C)
3002 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003003#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003004#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003005}
3006
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003007#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3008 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003009static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3010 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003011{
Paul Bakker48916f92012-09-16 19:57:18 +00003012 md5_update( &ssl->handshake->fin_md5 , buf, len );
3013 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003014}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003015#endif
Paul Bakker380da532012-04-18 16:10:25 +00003016
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003017#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3018#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003019static void ssl_update_checksum_sha256( ssl_context *ssl,
3020 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003021{
Paul Bakker9e36f042013-06-30 14:34:05 +02003022 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003023}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003024#endif
Paul Bakker380da532012-04-18 16:10:25 +00003025
Paul Bakker9e36f042013-06-30 14:34:05 +02003026#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003027static void ssl_update_checksum_sha384( ssl_context *ssl,
3028 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003029{
Paul Bakker9e36f042013-06-30 14:34:05 +02003030 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003031}
Paul Bakker769075d2012-11-24 11:26:46 +01003032#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003033#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003034
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003035#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003036static void ssl_calc_finished_ssl(
3037 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003038{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003039 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003040 md5_context md5;
3041 sha1_context sha1;
3042
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 unsigned char padbuf[48];
3044 unsigned char md5sum[16];
3045 unsigned char sha1sum[20];
3046
Paul Bakker48916f92012-09-16 19:57:18 +00003047 ssl_session *session = ssl->session_negotiate;
3048 if( !session )
3049 session = ssl->session;
3050
Paul Bakker1ef83d62012-04-11 12:09:53 +00003051 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3052
Paul Bakker48916f92012-09-16 19:57:18 +00003053 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3054 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
3056 /*
3057 * SSLv3:
3058 * hash =
3059 * MD5( master + pad2 +
3060 * MD5( handshake + sender + master + pad1 ) )
3061 * + SHA1( master + pad2 +
3062 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003063 */
3064
Paul Bakker90995b52013-06-24 19:20:35 +02003065#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003066 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003067 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003068#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003069
Paul Bakker90995b52013-06-24 19:20:35 +02003070#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003071 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003072 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003073#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003074
Paul Bakker3c2122f2013-06-24 19:03:14 +02003075 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3076 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003077
Paul Bakker1ef83d62012-04-11 12:09:53 +00003078 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003079
Paul Bakker3c2122f2013-06-24 19:03:14 +02003080 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003081 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003082 md5_update( &md5, padbuf, 48 );
3083 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
Paul Bakker3c2122f2013-06-24 19:03:14 +02003085 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003086 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003087 sha1_update( &sha1, padbuf, 40 );
3088 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
Paul Bakker1ef83d62012-04-11 12:09:53 +00003090 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
Paul Bakker1ef83d62012-04-11 12:09:53 +00003092 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003093 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003094 md5_update( &md5, padbuf, 48 );
3095 md5_update( &md5, md5sum, 16 );
3096 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003097
Paul Bakker1ef83d62012-04-11 12:09:53 +00003098 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003099 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003100 sha1_update( &sha1, padbuf , 40 );
3101 sha1_update( &sha1, sha1sum, 20 );
3102 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003103
Paul Bakker1ef83d62012-04-11 12:09:53 +00003104 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003105
Paul Bakker5b4af392014-06-26 12:09:34 +02003106 md5_free( &md5 );
3107 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003108
Paul Bakker34617722014-06-13 17:20:13 +02003109 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3110 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3111 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
3113 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3114}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003115#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003116
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003117#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003118static void ssl_calc_finished_tls(
3119 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003120{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003121 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003122 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003123 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003125 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003126
Paul Bakker48916f92012-09-16 19:57:18 +00003127 ssl_session *session = ssl->session_negotiate;
3128 if( !session )
3129 session = ssl->session;
3130
Paul Bakker1ef83d62012-04-11 12:09:53 +00003131 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003132
Paul Bakker48916f92012-09-16 19:57:18 +00003133 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3134 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003135
Paul Bakker1ef83d62012-04-11 12:09:53 +00003136 /*
3137 * TLSv1:
3138 * hash = PRF( master, finished_label,
3139 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3140 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003141
Paul Bakker90995b52013-06-24 19:20:35 +02003142#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003143 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3144 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003145#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003146
Paul Bakker90995b52013-06-24 19:20:35 +02003147#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003148 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3149 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003150#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003151
3152 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003153 ? "client finished"
3154 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155
3156 md5_finish( &md5, padbuf );
3157 sha1_finish( &sha1, padbuf + 16 );
3158
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003159 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003160 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003161
3162 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3163
Paul Bakker5b4af392014-06-26 12:09:34 +02003164 md5_free( &md5 );
3165 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003166
Paul Bakker34617722014-06-13 17:20:13 +02003167 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003168
3169 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3170}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003171#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003172
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003173#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3174#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003175static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003176 ssl_context *ssl, unsigned char *buf, int from )
3177{
3178 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003179 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003180 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003181 unsigned char padbuf[32];
3182
Paul Bakker48916f92012-09-16 19:57:18 +00003183 ssl_session *session = ssl->session_negotiate;
3184 if( !session )
3185 session = ssl->session;
3186
Paul Bakker380da532012-04-18 16:10:25 +00003187 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003188
Paul Bakker9e36f042013-06-30 14:34:05 +02003189 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003190
3191 /*
3192 * TLSv1.2:
3193 * hash = PRF( master, finished_label,
3194 * Hash( handshake ) )[0.11]
3195 */
3196
Paul Bakker9e36f042013-06-30 14:34:05 +02003197#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003198 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003199 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003200#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003201
3202 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003203 ? "client finished"
3204 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003205
Paul Bakker9e36f042013-06-30 14:34:05 +02003206 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003207
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003208 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003209 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003210
3211 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3212
Paul Bakker5b4af392014-06-26 12:09:34 +02003213 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003214
Paul Bakker34617722014-06-13 17:20:13 +02003215 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003216
3217 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3218}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003219#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003220
Paul Bakker9e36f042013-06-30 14:34:05 +02003221#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003222static void ssl_calc_finished_tls_sha384(
3223 ssl_context *ssl, unsigned char *buf, int from )
3224{
3225 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003226 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003227 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003228 unsigned char padbuf[48];
3229
Paul Bakker48916f92012-09-16 19:57:18 +00003230 ssl_session *session = ssl->session_negotiate;
3231 if( !session )
3232 session = ssl->session;
3233
Paul Bakker380da532012-04-18 16:10:25 +00003234 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003235
Paul Bakker9e36f042013-06-30 14:34:05 +02003236 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003237
3238 /*
3239 * TLSv1.2:
3240 * hash = PRF( master, finished_label,
3241 * Hash( handshake ) )[0.11]
3242 */
3243
Paul Bakker9e36f042013-06-30 14:34:05 +02003244#if !defined(POLARSSL_SHA512_ALT)
3245 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3246 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003247#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003248
3249 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003250 ? "client finished"
3251 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003252
Paul Bakker9e36f042013-06-30 14:34:05 +02003253 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003254
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003255 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003256 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003257
3258 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3259
Paul Bakker5b4af392014-06-26 12:09:34 +02003260 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003261
Paul Bakker34617722014-06-13 17:20:13 +02003262 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003263
3264 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3265}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003266#endif /* POLARSSL_SHA512_C */
3267#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003268
Paul Bakker48916f92012-09-16 19:57:18 +00003269void ssl_handshake_wrapup( ssl_context *ssl )
3270{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003271 int resume = ssl->handshake->resume;
3272
Paul Bakker48916f92012-09-16 19:57:18 +00003273 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3274
3275 /*
3276 * Free our handshake params
3277 */
3278 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003279 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003280 ssl->handshake = NULL;
3281
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003282#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003283 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003284 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003285 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003286 ssl->renego_records_seen = 0;
3287 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003288#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003289
Paul Bakker48916f92012-09-16 19:57:18 +00003290 /*
3291 * Switch in our now active transform context
3292 */
3293 if( ssl->transform )
3294 {
3295 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003296 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003297 }
3298 ssl->transform = ssl->transform_negotiate;
3299 ssl->transform_negotiate = NULL;
3300
Paul Bakker0a597072012-09-25 21:55:46 +00003301 if( ssl->session )
3302 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003303#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3304 /* RFC 7366 3.1: keep the EtM state */
3305 ssl->session_negotiate->encrypt_then_mac =
3306 ssl->session->encrypt_then_mac;
3307#endif
3308
Paul Bakker0a597072012-09-25 21:55:46 +00003309 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003310 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003311 }
3312 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003313 ssl->session_negotiate = NULL;
3314
Paul Bakker0a597072012-09-25 21:55:46 +00003315 /*
3316 * Add cache entry
3317 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003318 if( ssl->f_set_cache != NULL &&
3319 ssl->session->length != 0 &&
3320 resume == 0 )
3321 {
Paul Bakker0a597072012-09-25 21:55:46 +00003322 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3323 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003324 }
Paul Bakker0a597072012-09-25 21:55:46 +00003325
Paul Bakker48916f92012-09-16 19:57:18 +00003326 ssl->state++;
3327
3328 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3329}
3330
Paul Bakker1ef83d62012-04-11 12:09:53 +00003331int ssl_write_finished( ssl_context *ssl )
3332{
3333 int ret, hash_len;
3334
3335 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3336
Paul Bakker92be97b2013-01-02 17:30:03 +01003337 /*
3338 * Set the out_msg pointer to the correct location based on IV length
3339 */
3340 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3341 {
3342 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3343 ssl->transform_negotiate->fixed_ivlen;
3344 }
3345 else
3346 ssl->out_msg = ssl->out_iv;
3347
Paul Bakker48916f92012-09-16 19:57:18 +00003348 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003349
3350 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3352
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003353#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003354 ssl->verify_data_len = hash_len;
3355 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003356#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003357
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 ssl->out_msglen = 4 + hash_len;
3359 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3360 ssl->out_msg[0] = SSL_HS_FINISHED;
3361
3362 /*
3363 * In case of session resuming, invert the client and server
3364 * ChangeCipherSpec messages order.
3365 */
Paul Bakker0a597072012-09-25 21:55:46 +00003366 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003367 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003368#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003369 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003370 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003371#endif
3372#if defined(POLARSSL_SSL_SRV_C)
3373 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003375#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 }
3377 else
3378 ssl->state++;
3379
Paul Bakker48916f92012-09-16 19:57:18 +00003380 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003381 * Switch to our negotiated transform and session parameters for outbound
3382 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003383 */
3384 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3385 ssl->transform_out = ssl->transform_negotiate;
3386 ssl->session_out = ssl->session_negotiate;
3387 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003388
Paul Bakker07eb38b2012-12-19 14:42:06 +01003389#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003390 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003391 {
3392 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3393 {
3394 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3395 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3396 }
3397 }
3398#endif
3399
Paul Bakker5121ce52009-01-03 21:22:43 +00003400 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3401 {
3402 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3403 return( ret );
3404 }
3405
3406 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3407
3408 return( 0 );
3409}
3410
3411int ssl_parse_finished( ssl_context *ssl )
3412{
Paul Bakker23986e52011-04-24 08:57:21 +00003413 int ret;
3414 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003415 unsigned char buf[36];
3416
3417 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3418
Paul Bakker48916f92012-09-16 19:57:18 +00003419 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003420
Paul Bakker48916f92012-09-16 19:57:18 +00003421 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003422 * Switch to our negotiated transform and session parameters for inbound
3423 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003424 */
3425 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3426 ssl->transform_in = ssl->transform_negotiate;
3427 ssl->session_in = ssl->session_negotiate;
3428 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003429
Paul Bakker92be97b2013-01-02 17:30:03 +01003430 /*
3431 * Set the in_msg pointer to the correct location based on IV length
3432 */
3433 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3434 {
3435 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3436 ssl->transform_negotiate->fixed_ivlen;
3437 }
3438 else
3439 ssl->in_msg = ssl->in_iv;
3440
Paul Bakker07eb38b2012-12-19 14:42:06 +01003441#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003442 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003443 {
3444 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3445 {
3446 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3447 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3448 }
3449 }
3450#endif
3451
Paul Bakker5121ce52009-01-03 21:22:43 +00003452 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3453 {
3454 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3455 return( ret );
3456 }
3457
3458 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3459 {
3460 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003461 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 }
3463
Paul Bakker1ef83d62012-04-11 12:09:53 +00003464 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3466
3467 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3468 ssl->in_hslen != 4 + hash_len )
3469 {
3470 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003471 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 }
3473
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003474 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 {
3476 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003477 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003478 }
3479
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003480#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003481 ssl->verify_data_len = hash_len;
3482 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003483#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003484
Paul Bakker0a597072012-09-25 21:55:46 +00003485 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003486 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003487#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003488 if( ssl->endpoint == SSL_IS_CLIENT )
3489 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003490#endif
3491#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003492 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003493 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003494#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003495 }
3496 else
3497 ssl->state++;
3498
3499 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3500
3501 return( 0 );
3502}
3503
Paul Bakker968afaa2014-07-09 11:09:24 +02003504static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003505{
3506 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3507
3508#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3509 defined(POLARSSL_SSL_PROTO_TLS1_1)
3510 md5_init( &handshake->fin_md5 );
3511 sha1_init( &handshake->fin_sha1 );
3512 md5_starts( &handshake->fin_md5 );
3513 sha1_starts( &handshake->fin_sha1 );
3514#endif
3515#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3516#if defined(POLARSSL_SHA256_C)
3517 sha256_init( &handshake->fin_sha256 );
3518 sha256_starts( &handshake->fin_sha256, 0 );
3519#endif
3520#if defined(POLARSSL_SHA512_C)
3521 sha512_init( &handshake->fin_sha512 );
3522 sha512_starts( &handshake->fin_sha512, 1 );
3523#endif
3524#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3525
3526 handshake->update_checksum = ssl_update_checksum_start;
3527 handshake->sig_alg = SSL_HASH_SHA1;
3528
3529#if defined(POLARSSL_DHM_C)
3530 dhm_init( &handshake->dhm_ctx );
3531#endif
3532#if defined(POLARSSL_ECDH_C)
3533 ecdh_init( &handshake->ecdh_ctx );
3534#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003535}
3536
3537static void ssl_transform_init( ssl_transform *transform )
3538{
3539 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003540
3541 cipher_init( &transform->cipher_ctx_enc );
3542 cipher_init( &transform->cipher_ctx_dec );
3543
3544 md_init( &transform->md_ctx_enc );
3545 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003546}
3547
3548void ssl_session_init( ssl_session *session )
3549{
3550 memset( session, 0, sizeof(ssl_session) );
3551}
3552
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003553static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003554{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003555 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003556 if( ssl->transform_negotiate )
3557 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003558 if( ssl->session_negotiate )
3559 ssl_session_free( ssl->session_negotiate );
3560 if( ssl->handshake )
3561 ssl_handshake_free( ssl->handshake );
3562
3563 /*
3564 * Either the pointers are now NULL or cleared properly and can be freed.
3565 * Now allocate missing structures.
3566 */
3567 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003568 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003569 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003570 }
Paul Bakker48916f92012-09-16 19:57:18 +00003571
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003572 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003573 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003574 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003575 }
Paul Bakker48916f92012-09-16 19:57:18 +00003576
Paul Bakker82788fb2014-10-20 13:59:19 +02003577 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003578 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003579 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003580 }
Paul Bakker48916f92012-09-16 19:57:18 +00003581
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003582 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003583 if( ssl->handshake == NULL ||
3584 ssl->transform_negotiate == NULL ||
3585 ssl->session_negotiate == NULL )
3586 {
3587 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003588
3589 polarssl_free( ssl->handshake );
3590 polarssl_free( ssl->transform_negotiate );
3591 polarssl_free( ssl->session_negotiate );
3592
3593 ssl->handshake = NULL;
3594 ssl->transform_negotiate = NULL;
3595 ssl->session_negotiate = NULL;
3596
Paul Bakker48916f92012-09-16 19:57:18 +00003597 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3598 }
3599
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003600 /* Initialize structures */
3601 ssl_session_init( ssl->session_negotiate );
3602 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003603 ssl_handshake_params_init( ssl->handshake );
3604
3605#if defined(POLARSSL_X509_CRT_PARSE_C)
3606 ssl->handshake->key_cert = ssl->key_cert;
3607#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003608
Paul Bakker48916f92012-09-16 19:57:18 +00003609 return( 0 );
3610}
3611
Paul Bakker5121ce52009-01-03 21:22:43 +00003612/*
3613 * Initialize an SSL context
3614 */
3615int ssl_init( ssl_context *ssl )
3616{
Paul Bakker48916f92012-09-16 19:57:18 +00003617 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003618 int len = SSL_BUFFER_LEN;
3619
3620 memset( ssl, 0, sizeof( ssl_context ) );
3621
Paul Bakker62f2dee2012-09-28 07:31:51 +00003622 /*
3623 * Sane defaults
3624 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003625 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3626 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3627 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3628 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003629
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003630 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003631
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003632#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003633 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003634 memset( ssl->renego_period, 0xFF, 7 );
3635 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003636#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003637
Paul Bakker62f2dee2012-09-28 07:31:51 +00003638#if defined(POLARSSL_DHM_C)
3639 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003640 POLARSSL_DHM_RFC5114_MODP_2048_P) ) != 0 ||
Paul Bakker62f2dee2012-09-28 07:31:51 +00003641 ( ret = mpi_read_string( &ssl->dhm_G, 16,
Manuel Pégourié-Gonnardf0f399d2015-07-03 17:45:57 +02003642 POLARSSL_DHM_RFC5114_MODP_2048_G) ) != 0 )
Paul Bakker62f2dee2012-09-28 07:31:51 +00003643 {
3644 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3645 return( ret );
3646 }
3647#endif
3648
3649 /*
3650 * Prepare base structures
3651 */
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003652 if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL ||
3653 ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003654 {
3655 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003656 polarssl_free( ssl->in_ctr );
3657 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003658 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003659 }
3660
3661 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3662 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3663
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003664 ssl->in_hdr = ssl->in_ctr + 8;
3665 ssl->in_iv = ssl->in_ctr + 13;
3666 ssl->in_msg = ssl->in_ctr + 13;
3667
3668 ssl->out_hdr = ssl->out_ctr + 8;
3669 ssl->out_iv = ssl->out_ctr + 13;
3670 ssl->out_msg = ssl->out_ctr + 13;
3671
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003672#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3673 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3674#endif
3675
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003676#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3677 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3678#endif
3679
Paul Bakker606b4ba2013-08-14 16:52:14 +02003680#if defined(POLARSSL_SSL_SESSION_TICKETS)
3681 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3682#endif
3683
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003684#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003685 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003686#endif
3687
Paul Bakker48916f92012-09-16 19:57:18 +00003688 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3689 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003690
3691 return( 0 );
3692}
3693
3694/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003695 * Reset an initialized and used SSL context for re-use while retaining
3696 * all application-set variables, function pointers and data.
3697 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003698int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003699{
Paul Bakker48916f92012-09-16 19:57:18 +00003700 int ret;
3701
Paul Bakker7eb013f2011-10-06 12:37:39 +00003702 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003703
3704#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003705 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003706 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003707
3708 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003709 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3710 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003711#endif
3712 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003713
Paul Bakker7eb013f2011-10-06 12:37:39 +00003714 ssl->in_offt = NULL;
3715
Paul Bakker92be97b2013-01-02 17:30:03 +01003716 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003717 ssl->in_msgtype = 0;
3718 ssl->in_msglen = 0;
3719 ssl->in_left = 0;
3720
3721 ssl->in_hslen = 0;
3722 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003723 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003724
Paul Bakker92be97b2013-01-02 17:30:03 +01003725 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003726 ssl->out_msgtype = 0;
3727 ssl->out_msglen = 0;
3728 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003729#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003730 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3731 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003732#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003733
Paul Bakker48916f92012-09-16 19:57:18 +00003734 ssl->transform_in = NULL;
3735 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003736
3737 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3738 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003739
3740#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003741 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003742 {
3743 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003744 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003745 {
3746 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3747 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3748 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003749 }
3750#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003751
Paul Bakker48916f92012-09-16 19:57:18 +00003752 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003753 {
Paul Bakker48916f92012-09-16 19:57:18 +00003754 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003755 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003756 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003757 }
Paul Bakker48916f92012-09-16 19:57:18 +00003758
Paul Bakkerc0463502013-02-14 11:19:38 +01003759 if( ssl->session )
3760 {
3761 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003762 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003763 ssl->session = NULL;
3764 }
3765
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003766#if defined(POLARSSL_SSL_ALPN)
3767 ssl->alpn_chosen = NULL;
3768#endif
3769
Paul Bakker48916f92012-09-16 19:57:18 +00003770 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3771 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003772
3773 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003774}
3775
Paul Bakkera503a632013-08-14 13:48:06 +02003776#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003777static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3778{
3779 aes_free( &tkeys->enc );
3780 aes_free( &tkeys->dec );
3781
3782 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3783}
3784
Paul Bakker7eb013f2011-10-06 12:37:39 +00003785/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003786 * Allocate and initialize ticket keys
3787 */
3788static int ssl_ticket_keys_init( ssl_context *ssl )
3789{
3790 int ret;
3791 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003792 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003793
3794 if( ssl->ticket_keys != NULL )
3795 return( 0 );
3796
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003797 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003798 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003799 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3800
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003801 aes_init( &tkeys->enc );
3802 aes_init( &tkeys->dec );
3803
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003804 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003805 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003806 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003807 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003808 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003809 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003810
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003811 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3812 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3813 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3814 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003815 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003816 polarssl_free( tkeys );
3817 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003818 }
3819
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003820 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003821 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003822 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003823 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003824 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003825 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003826
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003827 ssl->ticket_keys = tkeys;
3828
3829 return( 0 );
3830}
Paul Bakkera503a632013-08-14 13:48:06 +02003831#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003832
3833/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003834 * SSL set accessors
3835 */
3836void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3837{
3838 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003839
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003840#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3841 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003842 if( endpoint == SSL_IS_CLIENT )
3843 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003844#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003845
3846#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3847 if( endpoint == SSL_IS_SERVER )
3848 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3849#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003850}
3851
3852void ssl_set_authmode( ssl_context *ssl, int authmode )
3853{
3854 ssl->authmode = authmode;
3855}
3856
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003857#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003858void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003859 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003860 void *p_vrfy )
3861{
3862 ssl->f_vrfy = f_vrfy;
3863 ssl->p_vrfy = p_vrfy;
3864}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003865#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003866
Paul Bakker5121ce52009-01-03 21:22:43 +00003867void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003868 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003869 void *p_rng )
3870{
3871 ssl->f_rng = f_rng;
3872 ssl->p_rng = p_rng;
3873}
3874
3875void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003876 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003877 void *p_dbg )
3878{
3879 ssl->f_dbg = f_dbg;
3880 ssl->p_dbg = p_dbg;
3881}
3882
3883void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003884 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003885 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003886{
3887 ssl->f_recv = f_recv;
3888 ssl->f_send = f_send;
3889 ssl->p_recv = p_recv;
3890 ssl->p_send = p_send;
3891}
3892
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003893#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003894void ssl_set_session_cache( ssl_context *ssl,
3895 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3896 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003897{
Paul Bakker0a597072012-09-25 21:55:46 +00003898 ssl->f_get_cache = f_get_cache;
3899 ssl->p_get_cache = p_get_cache;
3900 ssl->f_set_cache = f_set_cache;
3901 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003902}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003903#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003904
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003905#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003906int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003907{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003908 int ret;
3909
3910 if( ssl == NULL ||
3911 session == NULL ||
3912 ssl->session_negotiate == NULL ||
3913 ssl->endpoint != SSL_IS_CLIENT )
3914 {
3915 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3916 }
3917
3918 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3919 return( ret );
3920
Paul Bakker0a597072012-09-25 21:55:46 +00003921 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003922
3923 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003924}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003925#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003926
Paul Bakkerb68cad62012-08-23 08:34:18 +00003927void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003928{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003929 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3930 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3931 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3932 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3933}
3934
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003935void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3936 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003937 int major, int minor )
3938{
3939 if( major != SSL_MAJOR_VERSION_3 )
3940 return;
3941
3942 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3943 return;
3944
3945 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003946}
3947
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003948#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003949/* Add a new (empty) key_cert entry an return a pointer to it */
3950static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3951{
3952 ssl_key_cert *key_cert, *last;
3953
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003954 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003955 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003956 return( NULL );
3957
3958 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3959
3960 /* Append the new key_cert to the (possibly empty) current list */
3961 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003962 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003963 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003964 if( ssl->handshake != NULL )
3965 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003966 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003967 else
3968 {
3969 last = ssl->key_cert;
3970 while( last->next != NULL )
3971 last = last->next;
3972 last->next = key_cert;
3973 }
3974
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003975 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003976}
3977
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003978void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003979 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003980{
3981 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003982 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003983 ssl->peer_cn = peer_cn;
3984}
3985
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003986int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003987 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003988{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003989 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3990
3991 if( key_cert == NULL )
3992 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3993
3994 key_cert->cert = own_cert;
3995 key_cert->key = pk_key;
3996
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00003997 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003998}
3999
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004000#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004001#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004002int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004003 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004004{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004005 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004006 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004007
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004008 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004009 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4010
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004011 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004012 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004013 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004014
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004015 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004016
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004017 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004018 if( ret != 0 )
4019 return( ret );
4020
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004021 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004022 return( ret );
4023
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004024 key_cert->cert = own_cert;
4025 key_cert->key_own_alloc = 1;
4026
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004027 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004028}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004029#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004030
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004031int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004032 void *rsa_key,
4033 rsa_decrypt_func rsa_decrypt,
4034 rsa_sign_func rsa_sign,
4035 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004036{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004037 int ret;
4038 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004039
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004040 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004041 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4042
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004043 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004044 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004045 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004046
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004047 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004048
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004049 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4050 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4051 return( ret );
4052
4053 key_cert->cert = own_cert;
4054 key_cert->key_own_alloc = 1;
4055
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004056 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004057}
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004058#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004059#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004060
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004061#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004062int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4063 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004064{
Paul Bakker6db455e2013-09-18 17:29:31 +02004065 if( psk == NULL || psk_identity == NULL )
4066 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4067
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004068 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004069 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4070
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02004071 /* Identity len will be encoded on two bytes */
4072 if( ( psk_identity_len >> 16 ) != 0 ||
4073 psk_identity_len > SSL_MAX_CONTENT_LEN )
4074 {
4075 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4076 }
4077
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004078 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004079 {
4080 polarssl_free( ssl->psk );
4081 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnard9c521762015-10-27 10:54:53 +01004082 ssl->psk = NULL;
4083 ssl->psk_identity = NULL;
Paul Bakker6db455e2013-09-18 17:29:31 +02004084 }
4085
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004086 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
4087 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05004088 {
4089 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004090 polarssl_free( ssl->psk_identity );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004091 ssl->psk = NULL;
Manuel Pégourié-Gonnard5aff0292015-09-28 18:09:45 +02004092 ssl->psk_identity = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05004093 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4094 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004095
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004096 ssl->psk_len = psk_len;
4097 ssl->psk_identity_len = psk_identity_len;
4098
Paul Bakker6db455e2013-09-18 17:29:31 +02004099 memcpy( ssl->psk, psk, ssl->psk_len );
4100 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004101
4102 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004103}
4104
4105void ssl_set_psk_cb( ssl_context *ssl,
4106 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4107 size_t),
4108 void *p_psk )
4109{
4110 ssl->f_psk = f_psk;
4111 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004112}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004113#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004114
Paul Bakker48916f92012-09-16 19:57:18 +00004115#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004116int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004117{
4118 int ret;
4119
Paul Bakker48916f92012-09-16 19:57:18 +00004120 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004121 {
4122 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4123 return( ret );
4124 }
4125
Paul Bakker48916f92012-09-16 19:57:18 +00004126 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004127 {
4128 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4129 return( ret );
4130 }
4131
4132 return( 0 );
4133}
4134
Paul Bakker1b57b062011-01-06 15:48:19 +00004135int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4136{
4137 int ret;
4138
Paul Bakker66d5d072014-06-17 16:39:18 +02004139 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004140 {
4141 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4142 return( ret );
4143 }
4144
Paul Bakker66d5d072014-06-17 16:39:18 +02004145 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004146 {
4147 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4148 return( ret );
4149 }
4150
4151 return( 0 );
4152}
Paul Bakker48916f92012-09-16 19:57:18 +00004153#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004154
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004155#if defined(POLARSSL_SSL_SET_CURVES)
4156/*
4157 * Set the allowed elliptic curves
4158 */
4159void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4160{
4161 ssl->curve_list = curve_list;
4162}
4163#endif
4164
Paul Bakker0be444a2013-08-27 21:55:01 +02004165#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004166int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004167{
4168 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004169 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004170
4171 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004172
4173 if( ssl->hostname_len + 1 == 0 )
4174 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4175
Simon Butcherc988f322015-09-29 23:27:20 +01004176 if( ssl->hostname_len > SSL_MAX_HOST_NAME_LEN )
4177 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4178
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004179 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004180
Paul Bakkerb15b8512012-01-13 13:44:06 +00004181 if( ssl->hostname == NULL )
4182 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4183
Paul Bakker3c2122f2013-06-24 19:03:14 +02004184 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004185 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004186
Paul Bakker40ea7de2009-05-03 10:18:48 +00004187 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004188
4189 return( 0 );
4190}
4191
Paul Bakker5701cdc2012-09-27 21:49:42 +00004192void ssl_set_sni( ssl_context *ssl,
4193 int (*f_sni)(void *, ssl_context *,
4194 const unsigned char *, size_t),
4195 void *p_sni )
4196{
4197 ssl->f_sni = f_sni;
4198 ssl->p_sni = p_sni;
4199}
Paul Bakker0be444a2013-08-27 21:55:01 +02004200#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004201
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004202#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004203int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004204{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004205 size_t cur_len, tot_len;
4206 const char **p;
4207
4208 /*
4209 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4210 * truncated". Check lengths now rather than later.
4211 */
4212 tot_len = 0;
4213 for( p = protos; *p != NULL; p++ )
4214 {
4215 cur_len = strlen( *p );
4216 tot_len += cur_len;
4217
4218 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4219 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4220 }
4221
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004222 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004223
4224 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004225}
4226
4227const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4228{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004229 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004230}
4231#endif /* POLARSSL_SSL_ALPN */
4232
Paul Bakker490ecc82011-10-06 13:04:09 +00004233void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4234{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004235 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4236 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4237 {
4238 ssl->max_major_ver = major;
4239 ssl->max_minor_ver = minor;
4240 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004241}
4242
Paul Bakker1d29fb52012-09-28 13:28:45 +00004243void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4244{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004245 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4246 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4247 {
4248 ssl->min_major_ver = major;
4249 ssl->min_minor_ver = minor;
4250 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004251}
4252
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004253#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4254void ssl_set_fallback( ssl_context *ssl, char fallback )
4255{
4256 ssl->fallback = fallback;
4257}
4258#endif
4259
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004260#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4261void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4262{
4263 ssl->encrypt_then_mac = etm;
4264}
4265#endif
4266
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004267#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4268void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4269{
4270 ssl->extended_ms = ems;
4271}
4272#endif
4273
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004274void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4275{
4276 ssl->arc4_disabled = arc4;
4277}
4278
Paul Bakker05decb22013-08-15 13:33:48 +02004279#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004280int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4281{
Paul Bakker77e257e2013-12-16 15:29:52 +01004282 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004283 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004284 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004285 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004286 }
4287
4288 ssl->mfl_code = mfl_code;
4289
4290 return( 0 );
4291}
Paul Bakker05decb22013-08-15 13:33:48 +02004292#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004293
Paul Bakker1f2bc622013-08-15 13:45:55 +02004294#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004295int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004296{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004297 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004298
4299 return( 0 );
4300}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004301#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004302
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004303#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4304void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4305{
4306 ssl->split_done = split;
4307}
4308#endif
4309
Paul Bakker48916f92012-09-16 19:57:18 +00004310void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4311{
4312 ssl->allow_legacy_renegotiation = allow_legacy;
4313}
4314
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004315#if defined(POLARSSL_SSL_RENEGOTIATION)
4316void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4317{
4318 ssl->disable_renegotiation = renegotiation;
4319}
4320
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004321void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4322{
4323 ssl->renego_max_records = max_records;
4324}
4325
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004326void ssl_set_renegotiation_period( ssl_context *ssl,
4327 const unsigned char period[8] )
4328{
4329 memcpy( ssl->renego_period, period, 8 );
4330}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004331#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004332
Paul Bakkera503a632013-08-14 13:48:06 +02004333#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004334int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4335{
4336 ssl->session_tickets = use_tickets;
4337
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004338#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004339 if( ssl->endpoint == SSL_IS_CLIENT )
4340 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004341#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004342
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004343 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4344 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004345
4346 if( ssl->f_rng == NULL )
4347 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4348
4349 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004350}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004351
4352void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4353{
4354 ssl->ticket_lifetime = lifetime;
4355}
Paul Bakkera503a632013-08-14 13:48:06 +02004356#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004357
Paul Bakker5121ce52009-01-03 21:22:43 +00004358/*
4359 * SSL get accessors
4360 */
4361size_t ssl_get_bytes_avail( const ssl_context *ssl )
4362{
4363 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4364}
4365
Paul Bakkerff60ee62010-03-16 21:09:09 +00004366int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004367{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004368 if( ssl->session != NULL )
4369 return( ssl->session->verify_result );
4370
4371 if( ssl->session_negotiate != NULL )
4372 return( ssl->session_negotiate->verify_result );
4373
4374 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004375}
4376
Paul Bakkere3166ce2011-01-27 17:40:50 +00004377const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004378{
Paul Bakker926c8e42013-03-06 10:23:34 +01004379 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004380 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004381
Paul Bakkere3166ce2011-01-27 17:40:50 +00004382 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004383}
4384
Paul Bakker43ca69c2011-01-15 17:35:19 +00004385const char *ssl_get_version( const ssl_context *ssl )
4386{
4387 switch( ssl->minor_ver )
4388 {
4389 case SSL_MINOR_VERSION_0:
4390 return( "SSLv3.0" );
4391
4392 case SSL_MINOR_VERSION_1:
4393 return( "TLSv1.0" );
4394
4395 case SSL_MINOR_VERSION_2:
4396 return( "TLSv1.1" );
4397
Paul Bakker1ef83d62012-04-11 12:09:53 +00004398 case SSL_MINOR_VERSION_3:
4399 return( "TLSv1.2" );
4400
Paul Bakker43ca69c2011-01-15 17:35:19 +00004401 default:
4402 break;
4403 }
4404 return( "unknown" );
4405}
4406
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004407#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004408const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004409{
4410 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004411 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004412
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004413 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004414}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004415#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004416
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004417#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004418int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4419{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004420 if( ssl == NULL ||
4421 dst == NULL ||
4422 ssl->session == NULL ||
4423 ssl->endpoint != SSL_IS_CLIENT )
4424 {
4425 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4426 }
4427
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004428 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004429}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004430#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004431
Paul Bakker5121ce52009-01-03 21:22:43 +00004432/*
Paul Bakker1961b702013-01-25 14:49:24 +01004433 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004434 */
Paul Bakker1961b702013-01-25 14:49:24 +01004435int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004436{
Paul Bakker40e46942009-01-03 21:51:57 +00004437 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004438
Paul Bakker40e46942009-01-03 21:51:57 +00004439#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004440 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004441 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004442#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004443#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004444 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004445 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004446#endif
4447
Paul Bakker1961b702013-01-25 14:49:24 +01004448 return( ret );
4449}
4450
4451/*
4452 * Perform the SSL handshake
4453 */
4454int ssl_handshake( ssl_context *ssl )
4455{
4456 int ret = 0;
4457
4458 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4459
4460 while( ssl->state != SSL_HANDSHAKE_OVER )
4461 {
4462 ret = ssl_handshake_step( ssl );
4463
4464 if( ret != 0 )
4465 break;
4466 }
4467
Paul Bakker5121ce52009-01-03 21:22:43 +00004468 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4469
4470 return( ret );
4471}
4472
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004473#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004474#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004475/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004476 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004477 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004478static int ssl_write_hello_request( ssl_context *ssl )
4479{
4480 int ret;
4481
4482 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4483
4484 ssl->out_msglen = 4;
4485 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4486 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4487
4488 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4489 {
4490 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4491 return( ret );
4492 }
4493
4494 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4495
4496 return( 0 );
4497}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004498#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004499
4500/*
4501 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004502 * - any side: calling ssl_renegotiate(),
4503 * - client: receiving a HelloRequest during ssl_read(),
4504 * - server: receiving any handshake message on server during ssl_read() after
4505 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004506 * If the handshake doesn't complete due to waiting for I/O, it will continue
4507 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004508 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004509static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004510{
4511 int ret;
4512
4513 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4514
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004515 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4516 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004517
4518 ssl->state = SSL_HELLO_REQUEST;
4519 ssl->renegotiation = SSL_RENEGOTIATION;
4520
Paul Bakker48916f92012-09-16 19:57:18 +00004521 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4522 {
4523 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4524 return( ret );
4525 }
4526
4527 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4528
4529 return( 0 );
4530}
4531
4532/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004533 * Renegotiate current connection on client,
4534 * or request renegotiation on server
4535 */
4536int ssl_renegotiate( ssl_context *ssl )
4537{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004538 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004539
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004540#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004541 /* On server, just send the request */
4542 if( ssl->endpoint == SSL_IS_SERVER )
4543 {
4544 if( ssl->state != SSL_HANDSHAKE_OVER )
4545 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4546
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004547 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4548
4549 /* Did we already try/start sending HelloRequest? */
4550 if( ssl->out_left != 0 )
4551 return( ssl_flush_output( ssl ) );
4552
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004553 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004554 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004555#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004556
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004557#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004558 /*
4559 * On client, either start the renegotiation process or,
4560 * if already in progress, continue the handshake
4561 */
4562 if( ssl->renegotiation != SSL_RENEGOTIATION )
4563 {
4564 if( ssl->state != SSL_HANDSHAKE_OVER )
4565 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4566
4567 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4568 {
4569 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4570 return( ret );
4571 }
4572 }
4573 else
4574 {
4575 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4576 {
4577 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4578 return( ret );
4579 }
4580 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004581#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004582
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004583 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004584}
4585
4586/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004587 * Check record counters and renegotiate if they're above the limit.
4588 */
4589static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4590{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004591 if( ssl->state != SSL_HANDSHAKE_OVER ||
4592 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4593 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4594 {
4595 return( 0 );
4596 }
4597
4598 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004599 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4600 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004601 {
4602 return( 0 );
4603 }
4604
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004605 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004606 return( ssl_renegotiate( ssl ) );
4607}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004608#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004609
4610/*
4611 * Receive application data decrypted from the SSL layer
4612 */
Paul Bakker23986e52011-04-24 08:57:21 +00004613int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004614{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004615 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004616 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004617
4618 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4619
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004620#if defined(POLARSSL_SSL_RENEGOTIATION)
4621 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4622 {
4623 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4624 return( ret );
4625 }
4626#endif
4627
Paul Bakker5121ce52009-01-03 21:22:43 +00004628 if( ssl->state != SSL_HANDSHAKE_OVER )
4629 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004630 ret = ssl_handshake( ssl );
4631 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4632 {
4633 record_read = 1;
4634 }
4635 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 {
4637 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4638 return( ret );
4639 }
4640 }
4641
4642 if( ssl->in_offt == NULL )
4643 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004644 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004645 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004646 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4647 {
4648 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4649 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004650
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004651 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4652 return( ret );
4653 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004654 }
4655
4656 if( ssl->in_msglen == 0 &&
4657 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4658 {
4659 /*
4660 * OpenSSL sends empty messages to randomize the IV
4661 */
4662 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4663 {
Paul Bakker831a7552011-05-18 13:32:51 +00004664 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4665 return( 0 );
4666
Paul Bakker5121ce52009-01-03 21:22:43 +00004667 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4668 return( ret );
4669 }
4670 }
4671
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004672#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004673 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4674 {
4675 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4676
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004677#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004678 if( ssl->endpoint == SSL_IS_CLIENT &&
4679 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4680 ssl->in_hslen != 4 ) )
4681 {
4682 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4683 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4684 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004685#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004686
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004687 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4688 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004689 ssl->allow_legacy_renegotiation ==
4690 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004691 {
4692 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4693
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004694#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004695 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004696 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004697 /*
4698 * SSLv3 does not have a "no_renegotiation" alert
4699 */
4700 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4701 return( ret );
4702 }
4703 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004704#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004705#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4706 defined(POLARSSL_SSL_PROTO_TLS1_2)
4707 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004708 {
4709 if( ( ret = ssl_send_alert_message( ssl,
4710 SSL_ALERT_LEVEL_WARNING,
4711 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4712 {
4713 return( ret );
4714 }
Paul Bakker48916f92012-09-16 19:57:18 +00004715 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004716 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004717#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4718 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004719 {
4720 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004721 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004722 }
Paul Bakker48916f92012-09-16 19:57:18 +00004723 }
4724 else
4725 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004726 ret = ssl_start_renegotiation( ssl );
4727 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4728 {
4729 record_read = 1;
4730 }
4731 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004732 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004733 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004734 return( ret );
4735 }
Paul Bakker48916f92012-09-16 19:57:18 +00004736 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004737
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004738 /* If a non-handshake record was read during renego, fallthrough,
4739 * else tell the user they should call ssl_read() again */
4740 if( ! record_read )
4741 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004742 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004743 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4744 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004745 ssl->renego_records_seen++;
4746
4747 if( ssl->renego_max_records >= 0 &&
4748 ssl->renego_records_seen > ssl->renego_max_records )
4749 {
4750 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4751 "but not honored by client" ) );
4752 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4753 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004754 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004755#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004756
4757 /* Fatal and closure alerts handled by ssl_read_record() */
4758 if( ssl->in_msgtype == SSL_MSG_ALERT )
4759 {
4760 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4761 return( POLARSSL_ERR_NET_WANT_READ );
4762 }
4763
4764 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004765 {
4766 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004767 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004768 }
4769
4770 ssl->in_offt = ssl->in_msg;
4771 }
4772
4773 n = ( len < ssl->in_msglen )
4774 ? len : ssl->in_msglen;
4775
4776 memcpy( buf, ssl->in_offt, n );
4777 ssl->in_msglen -= n;
4778
4779 if( ssl->in_msglen == 0 )
4780 /* all bytes consumed */
4781 ssl->in_offt = NULL;
4782 else
4783 /* more data available */
4784 ssl->in_offt += n;
4785
4786 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4787
Paul Bakker23986e52011-04-24 08:57:21 +00004788 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004789}
4790
4791/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004792 * Send application data to be encrypted by the SSL layer,
4793 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00004794 */
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004795static int ssl_write_real( ssl_context *ssl,
4796 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004797{
Paul Bakker23986e52011-04-24 08:57:21 +00004798 int ret;
4799 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004800 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004801
Paul Bakker05decb22013-08-15 13:33:48 +02004802#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004803 /*
4804 * Assume mfl_code is correct since it was checked when set
4805 */
4806 max_len = mfl_code_to_length[ssl->mfl_code];
4807
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004808 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004809 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004810 */
4811 if( ssl->session_out != NULL &&
4812 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4813 {
4814 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4815 }
Paul Bakker05decb22013-08-15 13:33:48 +02004816#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004817
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004818 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004819
Paul Bakker5121ce52009-01-03 21:22:43 +00004820 if( ssl->out_left != 0 )
4821 {
4822 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4823 {
4824 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4825 return( ret );
4826 }
4827 }
Paul Bakker887bd502011-06-08 13:10:54 +00004828 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004829 {
Paul Bakker887bd502011-06-08 13:10:54 +00004830 ssl->out_msglen = n;
4831 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4832 memcpy( ssl->out_msg, buf, n );
4833
4834 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4835 {
4836 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4837 return( ret );
4838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004839 }
4840
Paul Bakker23986e52011-04-24 08:57:21 +00004841 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004842}
4843
4844/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004845 * Write application data, doing 1/n-1 splitting if necessary.
4846 *
4847 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004848 * then the caller will call us again with the same arguments, so
4849 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004850 */
4851#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004852static int ssl_write_split( ssl_context *ssl,
4853 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004854{
4855 int ret;
4856
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004857 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4858 len <= 1 ||
4859 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004860 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4861 != POLARSSL_MODE_CBC )
4862 {
4863 return( ssl_write_real( ssl, buf, len ) );
4864 }
4865
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004866 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004867 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004868 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004869 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004870 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004871 }
4872
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004873 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4874 return( ret );
4875 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004876
4877 return( ret + 1 );
4878}
4879#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4880
4881/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004882 * Write application data (public-facing wrapper)
4883 */
4884int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4885{
4886 int ret;
4887
4888 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4889
4890#if defined(POLARSSL_SSL_RENEGOTIATION)
4891 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4892 {
4893 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4894 return( ret );
4895 }
4896#endif
4897
4898 if( ssl->state != SSL_HANDSHAKE_OVER )
4899 {
4900 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4901 {
4902 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4903 return( ret );
4904 }
4905 }
4906
4907#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4908 ret = ssl_write_split( ssl, buf, len );
4909#else
4910 ret = ssl_write_real( ssl, buf, len );
4911#endif
4912
4913 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4914
4915 return( ret );
4916}
4917
4918/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004919 * Notify the peer that the connection is being closed
4920 */
4921int ssl_close_notify( ssl_context *ssl )
4922{
4923 int ret;
4924
4925 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4926
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004927 if( ssl->out_left != 0 )
4928 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004929
4930 if( ssl->state == SSL_HANDSHAKE_OVER )
4931 {
Paul Bakker48916f92012-09-16 19:57:18 +00004932 if( ( ret = ssl_send_alert_message( ssl,
4933 SSL_ALERT_LEVEL_WARNING,
4934 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004935 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004936 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004937 return( ret );
4938 }
4939 }
4940
4941 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4942
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004943 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004944}
4945
Paul Bakker48916f92012-09-16 19:57:18 +00004946void ssl_transform_free( ssl_transform *transform )
4947{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004948 if( transform == NULL )
4949 return;
4950
Paul Bakker48916f92012-09-16 19:57:18 +00004951#if defined(POLARSSL_ZLIB_SUPPORT)
4952 deflateEnd( &transform->ctx_deflate );
4953 inflateEnd( &transform->ctx_inflate );
4954#endif
4955
Paul Bakker84bbeb52014-07-01 14:53:22 +02004956 cipher_free( &transform->cipher_ctx_enc );
4957 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004958
Paul Bakker84bbeb52014-07-01 14:53:22 +02004959 md_free( &transform->md_ctx_enc );
4960 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004961
Paul Bakker34617722014-06-13 17:20:13 +02004962 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004963}
4964
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004965#if defined(POLARSSL_X509_CRT_PARSE_C)
4966static void ssl_key_cert_free( ssl_key_cert *key_cert )
4967{
4968 ssl_key_cert *cur = key_cert, *next;
4969
4970 while( cur != NULL )
4971 {
4972 next = cur->next;
4973
4974 if( cur->key_own_alloc )
4975 {
4976 pk_free( cur->key );
4977 polarssl_free( cur->key );
4978 }
4979 polarssl_free( cur );
4980
4981 cur = next;
4982 }
4983}
4984#endif /* POLARSSL_X509_CRT_PARSE_C */
4985
Paul Bakker48916f92012-09-16 19:57:18 +00004986void ssl_handshake_free( ssl_handshake_params *handshake )
4987{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004988 if( handshake == NULL )
4989 return;
4990
Paul Bakker48916f92012-09-16 19:57:18 +00004991#if defined(POLARSSL_DHM_C)
4992 dhm_free( &handshake->dhm_ctx );
4993#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004994#if defined(POLARSSL_ECDH_C)
4995 ecdh_free( &handshake->ecdh_ctx );
4996#endif
4997
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004998#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004999 /* explicit void pointer cast for buggy MS compiler */
5000 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005001#endif
5002
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005003#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5004 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5005 /*
5006 * Free only the linked list wrapper, not the keys themselves
5007 * since the belong to the SNI callback
5008 */
5009 if( handshake->sni_key_cert != NULL )
5010 {
5011 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5012
5013 while( cur != NULL )
5014 {
5015 next = cur->next;
5016 polarssl_free( cur );
5017 cur = next;
5018 }
5019 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005020#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005021
Paul Bakker34617722014-06-13 17:20:13 +02005022 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005023}
5024
5025void ssl_session_free( ssl_session *session )
5026{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005027 if( session == NULL )
5028 return;
5029
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005030#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005031 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005032 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005033 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005034 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005035 }
Paul Bakkered27a042013-04-18 22:46:23 +02005036#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005037
Paul Bakkera503a632013-08-14 13:48:06 +02005038#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005039 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005040#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005041
Paul Bakker34617722014-06-13 17:20:13 +02005042 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005043}
5044
Paul Bakker5121ce52009-01-03 21:22:43 +00005045/*
5046 * Free an SSL context
5047 */
5048void ssl_free( ssl_context *ssl )
5049{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005050 if( ssl == NULL )
5051 return;
5052
Paul Bakker5121ce52009-01-03 21:22:43 +00005053 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5054
Paul Bakker5121ce52009-01-03 21:22:43 +00005055 if( ssl->out_ctr != NULL )
5056 {
Paul Bakker34617722014-06-13 17:20:13 +02005057 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005058 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005059 }
5060
5061 if( ssl->in_ctr != NULL )
5062 {
Paul Bakker34617722014-06-13 17:20:13 +02005063 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005064 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005065 }
5066
Paul Bakker16770332013-10-11 09:59:44 +02005067#if defined(POLARSSL_ZLIB_SUPPORT)
5068 if( ssl->compress_buf != NULL )
5069 {
Paul Bakker34617722014-06-13 17:20:13 +02005070 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005071 polarssl_free( ssl->compress_buf );
5072 }
5073#endif
5074
Paul Bakker40e46942009-01-03 21:51:57 +00005075#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005076 mpi_free( &ssl->dhm_P );
5077 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005078#endif
5079
Paul Bakker48916f92012-09-16 19:57:18 +00005080 if( ssl->transform )
5081 {
5082 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005083 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005084 }
5085
5086 if( ssl->handshake )
5087 {
5088 ssl_handshake_free( ssl->handshake );
5089 ssl_transform_free( ssl->transform_negotiate );
5090 ssl_session_free( ssl->session_negotiate );
5091
Paul Bakker6e339b52013-07-03 13:37:05 +02005092 polarssl_free( ssl->handshake );
5093 polarssl_free( ssl->transform_negotiate );
5094 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005095 }
5096
Paul Bakkerc0463502013-02-14 11:19:38 +01005097 if( ssl->session )
5098 {
5099 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005100 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005101 }
5102
Paul Bakkera503a632013-08-14 13:48:06 +02005103#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005104 if( ssl->ticket_keys )
5105 {
5106 ssl_ticket_keys_free( ssl->ticket_keys );
5107 polarssl_free( ssl->ticket_keys );
5108 }
Paul Bakkera503a632013-08-14 13:48:06 +02005109#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005110
Paul Bakker0be444a2013-08-27 21:55:01 +02005111#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005112 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005113 {
Paul Bakker34617722014-06-13 17:20:13 +02005114 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005115 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005116 ssl->hostname_len = 0;
5117 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005118#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005119
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005120#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005121 if( ssl->psk != NULL )
5122 {
Paul Bakker34617722014-06-13 17:20:13 +02005123 polarssl_zeroize( ssl->psk, ssl->psk_len );
5124 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005125 polarssl_free( ssl->psk );
5126 polarssl_free( ssl->psk_identity );
5127 ssl->psk_len = 0;
5128 ssl->psk_identity_len = 0;
5129 }
5130#endif
5131
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005132#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005133 ssl_key_cert_free( ssl->key_cert );
5134#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005135
Paul Bakker05ef8352012-05-08 09:17:57 +00005136#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5137 if( ssl_hw_record_finish != NULL )
5138 {
5139 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5140 ssl_hw_record_finish( ssl );
5141 }
5142#endif
5143
Paul Bakker5121ce52009-01-03 21:22:43 +00005144 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005145
Paul Bakker86f04f42013-02-14 11:20:09 +01005146 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005147 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005148}
5149
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005150#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005151/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005152 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005153 */
5154unsigned char ssl_sig_from_pk( pk_context *pk )
5155{
5156#if defined(POLARSSL_RSA_C)
5157 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5158 return( SSL_SIG_RSA );
5159#endif
5160#if defined(POLARSSL_ECDSA_C)
5161 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5162 return( SSL_SIG_ECDSA );
5163#endif
5164 return( SSL_SIG_ANON );
5165}
5166
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005167pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5168{
5169 switch( sig )
5170 {
5171#if defined(POLARSSL_RSA_C)
5172 case SSL_SIG_RSA:
5173 return( POLARSSL_PK_RSA );
5174#endif
5175#if defined(POLARSSL_ECDSA_C)
5176 case SSL_SIG_ECDSA:
5177 return( POLARSSL_PK_ECDSA );
5178#endif
5179 default:
5180 return( POLARSSL_PK_NONE );
5181 }
5182}
Paul Bakker9af723c2014-05-01 13:03:14 +02005183#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005184
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005185/*
5186 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5187 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005188md_type_t ssl_md_alg_from_hash( unsigned char hash )
5189{
5190 switch( hash )
5191 {
5192#if defined(POLARSSL_MD5_C)
5193 case SSL_HASH_MD5:
5194 return( POLARSSL_MD_MD5 );
5195#endif
5196#if defined(POLARSSL_SHA1_C)
5197 case SSL_HASH_SHA1:
5198 return( POLARSSL_MD_SHA1 );
5199#endif
5200#if defined(POLARSSL_SHA256_C)
5201 case SSL_HASH_SHA224:
5202 return( POLARSSL_MD_SHA224 );
5203 case SSL_HASH_SHA256:
5204 return( POLARSSL_MD_SHA256 );
5205#endif
5206#if defined(POLARSSL_SHA512_C)
5207 case SSL_HASH_SHA384:
5208 return( POLARSSL_MD_SHA384 );
5209 case SSL_HASH_SHA512:
5210 return( POLARSSL_MD_SHA512 );
5211#endif
5212 default:
5213 return( POLARSSL_MD_NONE );
5214 }
5215}
5216
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005217#if defined(POLARSSL_SSL_SET_CURVES)
5218/*
5219 * Check is a curve proposed by the peer is in our list.
5220 * Return 1 if we're willing to use it, 0 otherwise.
5221 */
5222int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5223{
5224 const ecp_group_id *gid;
5225
5226 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5227 if( *gid == grp_id )
5228 return( 1 );
5229
5230 return( 0 );
5231}
Paul Bakker9af723c2014-05-01 13:03:14 +02005232#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005233
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005234#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005235int ssl_check_cert_usage( const x509_crt *cert,
5236 const ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005237 int cert_endpoint,
5238 int *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005239{
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005240 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005241#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5242 int usage = 0;
5243#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005244#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5245 const char *ext_oid;
5246 size_t ext_len;
5247#endif
5248
5249#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5250 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5251 ((void) cert);
5252 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005253 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005254#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005255
5256#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5257 if( cert_endpoint == SSL_IS_SERVER )
5258 {
5259 /* Server part of the key exchange */
5260 switch( ciphersuite->key_exchange )
5261 {
5262 case POLARSSL_KEY_EXCHANGE_RSA:
5263 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5264 usage = KU_KEY_ENCIPHERMENT;
5265 break;
5266
5267 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5268 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5269 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5270 usage = KU_DIGITAL_SIGNATURE;
5271 break;
5272
5273 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5274 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5275 usage = KU_KEY_AGREEMENT;
5276 break;
5277
5278 /* Don't use default: we want warnings when adding new values */
5279 case POLARSSL_KEY_EXCHANGE_NONE:
5280 case POLARSSL_KEY_EXCHANGE_PSK:
5281 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5282 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5283 usage = 0;
5284 }
5285 }
5286 else
5287 {
5288 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5289 usage = KU_DIGITAL_SIGNATURE;
5290 }
5291
5292 if( x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005293 {
5294 *flags |= BADCERT_KEY_USAGE;
5295 ret = -1;
5296 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005297#else
5298 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005299#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5300
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005301#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5302 if( cert_endpoint == SSL_IS_SERVER )
5303 {
5304 ext_oid = OID_SERVER_AUTH;
5305 ext_len = OID_SIZE( OID_SERVER_AUTH );
5306 }
5307 else
5308 {
5309 ext_oid = OID_CLIENT_AUTH;
5310 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5311 }
5312
5313 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005314 {
5315 *flags |= BADCERT_EXT_KEY_USAGE;
5316 ret = -1;
5317 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005318#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5319
Manuel Pégourié-Gonnarde16b62c2015-04-17 16:55:53 +02005320 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005321}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005322#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005323
5324#endif /* POLARSSL_SSL_TLS_C */